Coverage Report - org.eclipse.swtbot.swt.finder.utils.internal.SiblingFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
SiblingFinder
100%
16/16
75%
6/8
2
 
 1  17
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.utils.internal;
 12  
 
 13  
 
 14  
 import org.eclipse.swt.widgets.Composite;
 15  
 import org.eclipse.swt.widgets.Control;
 16  
 import org.eclipse.swt.widgets.TabItem;
 17  
 import org.eclipse.swt.widgets.Widget;
 18  
 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
 19  
 
 20  
 /**
 21  
  * Finds the siblings of a widget.
 22  
  *
 23  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 24  
  */
 25  
 public final class SiblingFinder implements ArrayResult<Widget> {
 26  
         /**
 27  
          * The widget to use.
 28  
          */
 29  
         private final Widget        w;
 30  
 
 31  
         /**
 32  
          * Constructs the sibling finder with the given widget.
 33  
          *
 34  
          * @param w the widget
 35  
          */
 36  3076
         public SiblingFinder(Widget w) {
 37  3076
                 this.w = w;
 38  3076
         }
 39  
 
 40  
         /**
 41  
          * Runs the process of finding the siblings.
 42  
          *
 43  
          * @see org.eclipse.swtbot.swt.finder.results.ArrayResult#run()
 44  
          * @return The object found.
 45  
          */
 46  
         public Widget[] run() {
 47  3076
                 Widget[] siblings = new Widget[] {};
 48  3076
                 if (isControl(w))
 49  3075
                         siblings = children(((Control) w).getParent());
 50  1
                 else if (isTabItem(w))
 51  1
                         siblings = ((TabItem) w).getParent().getItems();
 52  3076
                 return siblings;
 53  
         }
 54  
 
 55  
         /**
 56  
          * Gets the children widgets starting with the given composite.
 57  
          *
 58  
          * @param parent The parent composite.
 59  
          * @return The list of child widgets or an empty list if none.
 60  
          */
 61  
         private Widget[] children(Composite parent) {
 62  3075
                 if (parent == null)
 63  4
                         return new Widget[] {};
 64  3071
                 Control[] children = parent.getChildren();
 65  3071
                 return (children == null) ? new Widget[] {} : children;
 66  
         }
 67  
 
 68  
         /**
 69  
          * Gets if this passed in widget is a control.
 70  
          *
 71  
          * @param w The widget.
 72  
          * @return <code>true</code> if it is a control. Otherwise <code>false</code>.
 73  
          */
 74  
         private boolean isControl(Widget w) {
 75  3076
                 return w instanceof Control;
 76  
         }
 77  
 
 78  
         /**
 79  
          * Gets if this is a tab item widget.
 80  
          *
 81  
          * @param w The widget.
 82  
          * @return <code>true</code> if it is a tab item. Otherwise <code>false</code>.
 83  
          */
 84  
         private boolean isTabItem(Widget w) {
 85  1
                 return w instanceof TabItem;
 86  
         }
 87  
 }