Coverage Report - org.eclipse.swtbot.swt.finder.finders.PathGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
PathGenerator
97%
42/43
87%
14/16
1.786
PathGenerator$1
100%
3/3
N/A
1.786
PathGenerator$2
100%
3/3
N/A
1.786
 
 1  4
 /*******************************************************************************
 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.finders;
 12  
 
 13  
 
 14  
 import org.eclipse.swt.widgets.Display;
 15  
 import org.eclipse.swt.widgets.Shell;
 16  
 import org.eclipse.swt.widgets.Widget;
 17  
 import org.eclipse.swtbot.swt.finder.resolvers.DefaultChildrenResolver;
 18  
 import org.eclipse.swtbot.swt.finder.resolvers.IChildrenResolver;
 19  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 20  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 21  
 import org.eclipse.swtbot.swt.finder.utils.TreePath;
 22  
 
 23  
 /**
 24  
  * This is a path generate used to resolve paths to a component. This is intended for use only internally by the
 25  
  * debugging and logging framework.
 26  
  *
 27  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 28  
  * @version $Id$
 29  
  */
 30  504
 public class PathGenerator {
 31  
 
 32  
         /** The childrenResolver */
 33  
         protected IChildrenResolver        childrenResolver;
 34  
 
 35  
         /**
 36  
          * Gets the current registered child resolver.
 37  
          *
 38  
          * @return the childrenResolver. If it has not been set a default will be used.
 39  
          */
 40  
         public IChildrenResolver getChildrenResolver() {
 41  2
                 if (childrenResolver == null)
 42  1
                         setChildrenResolver(new DefaultChildrenResolver());
 43  2
                 return childrenResolver;
 44  
         }
 45  
 
 46  
         /**
 47  
          * Converts the string to an object representation.
 48  
          *
 49  
          * @param stringPath The string path to find.
 50  
          * @param display The display to use.
 51  
          * @return the path to a component represented by <code>stringPath</code>
 52  
          */
 53  
         public TreePath getPathFromString(String stringPath, Display display) {
 54  2
                 String[] elements = stringPath.split("//"); //$NON-NLS-1$
 55  2
                 if (elements.length <= 1)
 56  1
                         return null;
 57  
 
 58  1
                 Widget[] treePath = new Widget[elements.length - 1];
 59  
 
 60  1
                 Object parent = display;
 61  
 
 62  4
                 for (int i = 1; i < elements.length; i++) {
 63  3
                         String token = elements[i];
 64  3
                         String[] pathElements = token.split("/"); //$NON-NLS-1$
 65  
 
 66  3
                         parent = getParent(treePath, parent, i, pathElements);
 67  
                 }
 68  1
                 return new TreePath(treePath);
 69  
         }
 70  
 
 71  
         /**
 72  
          * Converts the treePath to a string representation.
 73  
          *
 74  
          * @param path The tree path to use.
 75  
          * @return the path in a string format
 76  
          * @see #pathAsString(Widget, StringBuffer)
 77  
          */
 78  
         public String getPathAsString(TreePath path) {
 79  1
                 StringBuffer builder = new StringBuffer();
 80  1
                 _getPathAsString(path, builder);
 81  1
                 return builder.toString();
 82  
         }
 83  
 
 84  
         /**
 85  
          * Gets the widget contained within the parent at the given index.
 86  
          *
 87  
          * @param parent The parent widget to use.
 88  
          * @param index The index of the child widget to get from the parent.
 89  
          * @return the index of the widget in the given <code>parent</code>.
 90  
          */
 91  
         public Widget getWidget(Object parent, int index) {
 92  3
                 if (parent instanceof Display)
 93  1
                         return getShell((Display) parent, index);
 94  2
                 else if (parent instanceof Widget)
 95  2
                         return getWidget((Widget) parent, index);
 96  0
                 return null;
 97  
         }
 98  
 
 99  
         /**
 100  
          * Sets the resolver to use for child resolution.
 101  
          *
 102  
          * @param childrenResolver The child resolver to use.
 103  
          */
 104  
         public void setChildrenResolver(IChildrenResolver childrenResolver) {
 105  1
                 this.childrenResolver = childrenResolver;
 106  1
         }
 107  
 
 108  
         /**
 109  
          * @param treePath
 110  
          * @param parent
 111  
          * @param i
 112  
          * @param pathElements
 113  
          * @return
 114  
          */
 115  
         private Object getParent(Widget[] treePath, Object parent, int i, String[] pathElements) {
 116  3
                 if (pathElements.length > 0) {
 117  
 
 118  3
                         parent = getWidget(parent, Integer.valueOf(pathElements[1]).intValue());
 119  3
                         treePath[i - 1] = (Widget) parent;
 120  
                 }
 121  3
                 return parent;
 122  
         }
 123  
 
 124  
         private void _getPathAsString(TreePath path, StringBuffer result) {
 125  4
                 Widget lastWidget = (Widget) path.getLastSegment();
 126  4
                 TreePath parentPath = path.getParentPath();
 127  4
                 if (parentPath != null)
 128  3
                         _getPathAsString(parentPath, result);
 129  4
                 if (lastWidget != null)
 130  3
                         pathAsString(lastWidget, result);
 131  4
         }
 132  
 
 133  
         /**
 134  
          * @param display
 135  
          * @param index
 136  
          * @return a shell on the display
 137  
          */
 138  
         Widget getShell(final Display display, final int index) {
 139  1
                 return UIThreadRunnable.syncExec(display, new WidgetResult<Shell>() {
 140  
                         public Shell run() {
 141  1
                                 return display.getShells()[index];
 142  
                         }
 143  
                 });
 144  
         }
 145  
 
 146  
         /**
 147  
          * @param parent
 148  
          * @param index
 149  
          * @return the widget at the specified position in the specified parent
 150  
          */
 151  
         Widget getWidget(final Widget parent, final int index) {
 152  2
                 return UIThreadRunnable.syncExec(parent.getDisplay(), new WidgetResult<Widget>() {
 153  
                         public Widget run() {
 154  2
                                 return getChildrenResolver().getChildren(parent).get(index);
 155  
                         }
 156  
                 });
 157  
         }
 158  
 
 159  
         /**
 160  
          * Converts the path to a widget into a String.
 161  
          *
 162  
          * @param w the widget.
 163  
          * @param result the buffer into which the result should be returned.
 164  
          */
 165  
         protected void pathAsString(Widget w, StringBuffer result) {
 166  3
                 String className = w.getClass().getName();
 167  3
                 int widgetIndex = SWTUtils.widgetIndex(w);
 168  3
                 result.append("//" + className.substring(className.lastIndexOf(".") + 1) + "/" + widgetIndex); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 169  3
         }
 170  
 
 171  
         /**
 172  
          * @param widget
 173  
          * @return the path to the control, as a string
 174  
          */
 175  
         public String getPathAsString(Widget widget) {
 176  1
                 return getPathAsString(getPath(widget));
 177  
         }
 178  
 
 179  
         /**
 180  
          * @param widget
 181  
          * @return the path to the widget from the shell that contains it
 182  
          */
 183  
         public TreePath getPath(Widget widget) {
 184  502
                 return new ControlFinder().getPath(widget);
 185  
         }
 186  
 }