| 1 | 4 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 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 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 675 | public class PathGenerator { |
| 31 | |
|
| 32 | |
|
| 33 | |
protected IChildrenResolver childrenResolver; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public IChildrenResolver getChildrenResolver() { |
| 41 | 2 | if (childrenResolver == null) |
| 42 | 1 | setChildrenResolver(new DefaultChildrenResolver()); |
| 43 | 2 | return childrenResolver; |
| 44 | |
} |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public TreePath getPathFromString(String stringPath, Display display) { |
| 54 | 2 | String[] elements = stringPath.split("//"); |
| 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("/"); |
| 65 | |
|
| 66 | 3 | parent = getParent(treePath, parent, i, pathElements); |
| 67 | |
} |
| 68 | 1 | return new TreePath(treePath); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 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 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 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 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public void setChildrenResolver(IChildrenResolver childrenResolver) { |
| 105 | 1 | this.childrenResolver = childrenResolver; |
| 106 | 1 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 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 | |
|
| 135 | |
|
| 136 | |
|
| 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 | |
|
| 148 | |
|
| 149 | |
|
| 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 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 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); |
| 169 | 3 | } |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public String getPathAsString(Widget widget) { |
| 176 | 1 | return getPathAsString(getPath(widget)); |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public TreePath getPath(Widget widget) { |
| 184 | 673 | return new ControlFinder().getPath(widget); |
| 185 | |
} |
| 186 | |
} |