|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2010 BREDEX GmbH. |
| 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 |
* BREDEX GmbH - initial API and implementation and/or initial documentation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jubula.rc.rcp.e3.gef.implclasses; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.util.Iterator; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
import org.apache.commons.beanutils.PropertyUtils; |
| 19 |
import org.apache.commons.lang.Validate; |
| 20 |
import org.eclipse.draw2d.Connection; |
| 21 |
import org.eclipse.draw2d.ConnectionAnchor; |
| 22 |
import org.eclipse.draw2d.FigureCanvas; |
| 23 |
import org.eclipse.draw2d.IFigure; |
| 24 |
import org.eclipse.draw2d.geometry.Point; |
| 25 |
import org.eclipse.gef.ConnectionEditPart; |
| 26 |
import org.eclipse.gef.EditDomain; |
| 27 |
import org.eclipse.gef.EditPart; |
| 28 |
import org.eclipse.gef.GraphicalEditPart; |
| 29 |
import org.eclipse.gef.GraphicalViewer; |
| 30 |
import org.eclipse.gef.RootEditPart; |
| 31 |
import org.eclipse.gef.palette.PaletteEntry; |
| 32 |
import org.eclipse.gef.ui.palette.PaletteViewer; |
| 33 |
import org.eclipse.jubula.rc.common.driver.ClickOptions; |
| 34 |
import org.eclipse.jubula.rc.common.driver.IRobot; |
| 35 |
import org.eclipse.jubula.rc.common.driver.IRunnable; |
| 36 |
import org.eclipse.jubula.rc.common.exception.StepExecutionException; |
| 37 |
import org.eclipse.jubula.rc.common.implclasses.MatchUtil; |
| 38 |
import org.eclipse.jubula.rc.common.implclasses.Verifier; |
| 39 |
import org.eclipse.jubula.rc.rcp.e3.accessor.Startup; |
| 40 |
import org.eclipse.jubula.rc.rcp.e3.gef.factory.DefaultEditPartAdapterFactory; |
| 41 |
import org.eclipse.jubula.rc.rcp.e3.gef.identifier.IEditPartIdentifier; |
| 42 |
import org.eclipse.jubula.rc.swt.driver.DragAndDropHelperSwt; |
| 43 |
import org.eclipse.jubula.rc.swt.implclasses.AbstractControlImplClass; |
| 44 |
import org.eclipse.jubula.rc.swt.implclasses.MenuUtil; |
| 45 |
import org.eclipse.jubula.tools.objects.event.EventFactory; |
| 46 |
import org.eclipse.jubula.tools.objects.event.TestErrorEvent; |
| 47 |
import org.eclipse.swt.graphics.Rectangle; |
| 48 |
import org.eclipse.swt.widgets.Composite; |
| 49 |
import org.eclipse.swt.widgets.Control; |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Implementation class for Figure Canvas (Eclipse GEF). |
| 54 |
* |
| 55 |
* @author BREDEX GmbH |
| 56 |
* @created May 13, 2009 |
| 57 |
*/ |
| 58 |
public class FigureCanvasImplClass extends AbstractControlImplClass { |
| 59 |
|
| 60 |
/** |
| 61 |
* the viewer that contains the EditParts corresponding to the FigureCanvas |
| 62 |
*/ |
| 63 |
private GraphicalViewer m_viewer = null; |
| 64 |
|
| 65 |
/** the composite in which the IFigures are displayed */ |
| 66 |
private Composite m_composite = null; |
| 67 |
|
| 68 |
/** |
| 69 |
* {@inheritDoc} |
| 70 |
*/ |
| 71 |
public Control getComponent() { |
| 72 |
return m_composite; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* |
| 77 |
* @return the viewer associated with the canvas to test. |
| 78 |
*/ |
| 79 |
private GraphicalViewer getViewer() { |
| 80 |
return m_viewer; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* |
| 85 |
* @return the control for the canvas to test. |
| 86 |
*/ |
| 87 |
private Control getViewerControl() { |
| 88 |
return getViewer().getControl(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* |
| 93 |
* @return the root edit part of the viewer. |
| 94 |
*/ |
| 95 |
private RootEditPart getRootEditPart() { |
| 96 |
return getViewer().getRootEditPart(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* |
| 101 |
* @return the root of the palette viewer (tool palette). |
| 102 |
*/ |
| 103 |
private RootEditPart getPaletteRoot() { |
| 104 |
return getViewer().getEditDomain().getPaletteViewer().getRootEditPart(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* |
| 109 |
* @param textPath The path to the tool. |
| 110 |
* @param operator The operator used for matching. |
| 111 |
* @return the EditPart found at the end of the given path. Returns |
| 112 |
* <code>null</code> if no EditPart can be found for the given path |
| 113 |
* or if the EditPart found is not a GraphicalEditPart. |
| 114 |
*/ |
| 115 |
private GraphicalEditPart findPaletteEditPart( |
| 116 |
String textPath, String operator) { |
| 117 |
|
| 118 |
final String[] pathItems = MenuUtil.splitPath(textPath); |
| 119 |
boolean isExisting = true; |
| 120 |
|
| 121 |
EditPart currentEditPart = getPaletteRoot().getContents(); |
| 122 |
|
| 123 |
for (int i = 0; i < pathItems.length && currentEditPart != null; i++) { |
| 124 |
List effectiveChildren = currentEditPart.getChildren(); |
| 125 |
|
| 126 |
EditPart [] children = |
| 127 |
(EditPart [])effectiveChildren.toArray( |
| 128 |
new EditPart[effectiveChildren.size()]); |
| 129 |
boolean itemFound = false; |
| 130 |
for (int j = 0; j < children.length && !itemFound; j++) { |
| 131 |
Object model = children[j].getModel(); |
| 132 |
if (model instanceof PaletteEntry) { |
| 133 |
String entryLabel = ((PaletteEntry)model).getLabel(); |
| 134 |
if (entryLabel != null |
| 135 |
&& MatchUtil.getInstance().match( |
| 136 |
entryLabel, pathItems[i], operator)) { |
| 137 |
itemFound = true; |
| 138 |
currentEditPart = children[j]; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
if (!itemFound) { |
| 143 |
isExisting = false; |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
return isExisting && currentEditPart instanceof GraphicalEditPart |
| 150 |
? (GraphicalEditPart)currentEditPart : null; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* {@inheritDoc} |
| 156 |
*/ |
| 157 |
public String[] getTextArrayFromComponent() { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* {@inheritDoc} |
| 163 |
*/ |
| 164 |
public void setComponent(final Object graphicsComponent) { |
| 165 |
getEventThreadQueuer().invokeAndWait(getClass().getName() + ".setComponent", new IRunnable() { //$NON-NLS-1$ |
| 166 |
|
| 167 |
public Object run() throws StepExecutionException { |
| 168 |
FigureCanvas figureCanvas = (FigureCanvas)graphicsComponent; |
| 169 |
Composite parent = figureCanvas; |
| 170 |
while (parent != null |
| 171 |
&& !(parent.getData(Startup.TEST_GEF_VIEWER_DATA_KEY) |
| 172 |
instanceof GraphicalViewer)) { |
| 173 |
parent = parent.getParent(); |
| 174 |
} |
| 175 |
|
| 176 |
if (parent != null) { |
| 177 |
m_composite = parent; |
| 178 |
m_viewer = |
| 179 |
(GraphicalViewer)parent.getData( |
| 180 |
Startup.TEST_GEF_VIEWER_DATA_KEY); |
| 181 |
} |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
}); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Checks whether the figure for the EditPart for the given path exists and |
| 190 |
* is visible. |
| 191 |
* |
| 192 |
* @param textPath The path to the figure. |
| 193 |
* @param operator The operator used for matching. |
| 194 |
* @param exists Whether the figure is expected to exist. |
| 195 |
*/ |
| 196 |
public void gdCheckFigureExists( |
| 197 |
String textPath, String operator, boolean exists) { |
| 198 |
|
| 199 |
boolean isExisting = |
| 200 |
findFigure(findEditPart(textPath, operator)) != null; |
| 201 |
if (!isExisting) { |
| 202 |
// See if there's a connection anchor at the given path |
| 203 |
isExisting = findConnectionAnchor(textPath, operator) != null; |
| 204 |
} |
| 205 |
|
| 206 |
Verifier.equals(exists, isExisting); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Checks the given property of the figure at the given path. |
| 212 |
* |
| 213 |
* @param textPath The path to the figure. |
| 214 |
* @param textPathOperator The operator used for matching the text path. |
| 215 |
* @param propertyName The name of the property |
| 216 |
* @param expectedPropValue The value of the property as a string |
| 217 |
* @param valueOperator The operator used to verify |
| 218 |
*/ |
| 219 |
public void gdVerifyFigureProperty(String textPath, |
| 220 |
String textPathOperator, final String propertyName, |
| 221 |
String expectedPropValue, String valueOperator) { |
| 222 |
|
| 223 |
final IFigure figure = |
| 224 |
findFigure(findEditPart(textPath, textPathOperator)); |
| 225 |
if (figure == null) { |
| 226 |
throw new StepExecutionException( |
| 227 |
"No figure could be found for the given text path.", //$NON-NLS-1$ |
| 228 |
EventFactory.createActionError( |
| 229 |
TestErrorEvent.NOT_FOUND)); |
| 230 |
} |
| 231 |
Object prop = getEventThreadQueuer().invokeAndWait("getProperty", //$NON-NLS-1$ |
| 232 |
new IRunnable() { |
| 233 |
|
| 234 |
public Object run() throws StepExecutionException { |
| 235 |
try { |
| 236 |
return PropertyUtils.getProperty(figure, propertyName); |
| 237 |
} catch (IllegalAccessException e) { |
| 238 |
throw new StepExecutionException( |
| 239 |
e.getMessage(), |
| 240 |
EventFactory.createActionError( |
| 241 |
TestErrorEvent.PROPERTY_NOT_ACCESSABLE)); |
| 242 |
} catch (InvocationTargetException e) { |
| 243 |
throw new StepExecutionException( |
| 244 |
e.getMessage(), |
| 245 |
EventFactory.createActionError( |
| 246 |
TestErrorEvent.PROPERTY_NOT_ACCESSABLE)); |
| 247 |
} catch (NoSuchMethodException e) { |
| 248 |
throw new StepExecutionException( |
| 249 |
e.getMessage(), |
| 250 |
EventFactory.createActionError( |
| 251 |
TestErrorEvent.PROPERTY_NOT_ACCESSABLE)); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
}); |
| 256 |
final String propToStr = String.valueOf(prop); |
| 257 |
Verifier.match(propToStr, expectedPropValue, valueOperator); |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Checks whether the tool for the given path exists and |
| 263 |
* is visible. |
| 264 |
* |
| 265 |
* @param textPath The path to the figure. |
| 266 |
* @param operator The operator used for matching. |
| 267 |
* @param exists Whether the figure is expected to exist. |
| 268 |
*/ |
| 269 |
public void gdCheckToolExists( |
| 270 |
String textPath, String operator, boolean exists) { |
| 271 |
|
| 272 |
boolean isExisting = findPaletteFigure(textPath, operator) != null; |
| 273 |
|
| 274 |
Verifier.equals(exists, isExisting); |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Finds and clicks the figure for the given path. |
| 280 |
* |
| 281 |
* @param textPath The path to the figure. |
| 282 |
* @param operator The operator used for matching. |
| 283 |
* @param count The number of times to click. |
| 284 |
* @param button The mouse button to use for the click. |
| 285 |
*/ |
| 286 |
public void gdClickFigure(String textPath, String operator, |
| 287 |
int count, int button) { |
| 288 |
|
| 289 |
getRobot().click(getViewerControl(), |
| 290 |
getFigureBoundsChecked(textPath, operator), |
| 291 |
ClickOptions.create().setScrollToVisible(false) |
| 292 |
.setClickCount(count).setMouseButton(button)); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Finds and clicks on a connection between a source figure and a |
| 297 |
* target figure. |
| 298 |
* |
| 299 |
* @param sourceTextPath The path to the source figure. |
| 300 |
* @param sourceOperator The operator to use for matching the source |
| 301 |
* figure path. |
| 302 |
* @param targetTextPath The path to the target figure. |
| 303 |
* @param targetOperator The operator to use for matching the target |
| 304 |
* figure path. |
| 305 |
* @param count The number of times to click. |
| 306 |
* @param button The mouse button to use for the click. |
| 307 |
*/ |
| 308 |
public void gdClickConnection(String sourceTextPath, String sourceOperator, |
| 309 |
String targetTextPath, String targetOperator, |
| 310 |
int count, int button) { |
| 311 |
|
| 312 |
GraphicalEditPart sourceEditPart = |
| 313 |
findEditPart(sourceTextPath, sourceOperator); |
| 314 |
GraphicalEditPart targetEditPart = |
| 315 |
findEditPart(targetTextPath, targetOperator); |
| 316 |
|
| 317 |
ConnectionEditPart connectionEditPart = null; |
| 318 |
|
| 319 |
if (sourceEditPart != null) { |
| 320 |
List sourceConnectionList = sourceEditPart.getSourceConnections(); |
| 321 |
ConnectionEditPart [] sourceConnections = |
| 322 |
(ConnectionEditPart [])sourceConnectionList.toArray( |
| 323 |
new ConnectionEditPart[sourceConnectionList.size()]); |
| 324 |
for (int i = 0; i < sourceConnections.length |
| 325 |
&& connectionEditPart == null; i++) { |
| 326 |
if (sourceConnections[i].getTarget() == targetEditPart) { |
| 327 |
connectionEditPart = sourceConnections[i]; |
| 328 |
} |
| 329 |
} |
| 330 |
} else if (targetEditPart != null) { |
| 331 |
List targetConnectionList = targetEditPart.getTargetConnections(); |
| 332 |
ConnectionEditPart [] targetConnections = |
| 333 |
(ConnectionEditPart [])targetConnectionList.toArray( |
| 334 |
new ConnectionEditPart[targetConnectionList.size()]); |
| 335 |
for (int i = 0; i < targetConnections.length |
| 336 |
&& connectionEditPart == null; i++) { |
| 337 |
if (targetConnections[i].getSource() == targetEditPart) { |
| 338 |
connectionEditPart = targetConnections[i]; |
| 339 |
} |
| 340 |
} |
| 341 |
} else { |
| 342 |
throw new StepExecutionException( |
| 343 |
"No figures could be found for the given text paths.", //$NON-NLS-1$ |
| 344 |
EventFactory.createActionError(TestErrorEvent.NOT_FOUND)); |
| 345 |
} |
| 346 |
|
| 347 |
IFigure connectionFigure = findFigure(connectionEditPart); |
| 348 |
if (connectionFigure == null) { |
| 349 |
String missingEnd = sourceEditPart == null ? "source" : "target"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 350 |
throw new StepExecutionException( |
| 351 |
"No connection could be found for the given " + missingEnd + " figure.", //$NON-NLS-1$ //$NON-NLS-2$ |
| 352 |
EventFactory.createActionError(TestErrorEvent.NOT_FOUND)); |
| 353 |
} |
| 354 |
|
| 355 |
// Scrolling |
| 356 |
revealEditPart(connectionEditPart); |
| 357 |
|
| 358 |
if (connectionFigure instanceof Connection) { |
| 359 |
Point midpoint = |
| 360 |
((Connection)connectionFigure).getPoints().getMidpoint(); |
| 361 |
connectionFigure.translateToAbsolute(midpoint); |
| 362 |
getRobot().click(getViewerControl(), null, |
| 363 |
ClickOptions.create().setScrollToVisible(false) |
| 364 |
.setClickCount(count).setMouseButton(button), |
| 365 |
midpoint.x, true, midpoint.y, true); |
| 366 |
} else { |
| 367 |
getRobot().click(getViewerControl(), |
| 368 |
getBounds(connectionFigure), |
| 369 |
ClickOptions.create().setScrollToVisible(false) |
| 370 |
.setClickCount(count).setMouseButton(button)); |
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Clicks the specified position within the given figure. |
| 377 |
* |
| 378 |
* @param textPath The path to the figure. |
| 379 |
* @param operator The operator used for matching. |
| 380 |
* @param count amount of clicks |
| 381 |
* @param button what button should be clicked |
| 382 |
* @param xPos what x position |
| 383 |
* @param xUnits should x position be pixel or percent values |
| 384 |
* @param yPos what y position |
| 385 |
* @param yUnits should y position be pixel or percent values |
| 386 |
* @throws StepExecutionException if step execution fails. |
| 387 |
*/ |
| 388 |
public void gdClickInFigure(String textPath, String operator, |
| 389 |
int count, int button, int xPos, String xUnits, |
| 390 |
int yPos, String yUnits) throws StepExecutionException { |
| 391 |
|
| 392 |
getRobot().click(getViewerControl(), |
| 393 |
getFigureBoundsChecked(textPath, operator), |
| 394 |
ClickOptions.create().setScrollToVisible(false) |
| 395 |
.setClickCount(count).setMouseButton(button), |
| 396 |
xPos, xUnits.equalsIgnoreCase(POS_UNIT_PIXEL), |
| 397 |
yPos, yUnits.equalsIgnoreCase(POS_UNIT_PIXEL)); |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Simulates the beginning of a Drag. Moves to the specified position |
| 402 |
* within the given figure and stores information related to the drag to |
| 403 |
* be used later by a Drop operation. |
| 404 |
* |
| 405 |
* @param textPath The path to the figure. |
| 406 |
* @param operator The operator used for matching. |
| 407 |
* @param mouseButton the mouse button. |
| 408 |
* @param modifier the modifier, e.g. shift, ctrl, etc. |
| 409 |
* @param xPos what x position |
| 410 |
* @param xUnits should x position be pixel or percent values |
| 411 |
* @param yPos what y position |
| 412 |
* @param yUnits should y position be pixel or percent values |
| 413 |
*/ |
| 414 |
public void gdDragFigure(String textPath, String operator, |
| 415 |
int mouseButton, String modifier, int xPos, |
| 416 |
String xUnits, int yPos, String yUnits) { |
| 417 |
// Only store the Drag-Information. Otherwise the GUI-Eventqueue |
| 418 |
// blocks after performed Drag! |
| 419 |
final DragAndDropHelperSwt dndHelper = DragAndDropHelperSwt |
| 420 |
.getInstance(); |
| 421 |
dndHelper.setMouseButton(mouseButton); |
| 422 |
dndHelper.setModifier(modifier); |
| 423 |
dndHelper.setDragComponent(null); |
| 424 |
gdClickInFigure(textPath, operator, 0, mouseButton, |
| 425 |
xPos, xUnits, yPos, yUnits); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Performs a Drop. Moves to the specified location within the given figure |
| 430 |
* and releases the modifier and mouse button pressed by the previous drag |
| 431 |
* operation. |
| 432 |
* |
| 433 |
* @param textPath The path to the figure. |
| 434 |
* @param operator The operator used for matching. |
| 435 |
* @param xPos what x position |
| 436 |
* @param xUnits should x position be pixel or percent values |
| 437 |
* @param yPos what y position |
| 438 |
* @param yUnits should y position be pixel or percent values |
| 439 |
* @param delayBeforeDrop the amount of time (in milliseconds) to wait |
| 440 |
* between moving the mouse to the drop point and |
| 441 |
* releasing the mouse button |
| 442 |
*/ |
| 443 |
public void gdDropOnFigure(final String textPath, final String operator, |
| 444 |
final int xPos, final String xUnits, final int yPos, |
| 445 |
final String yUnits, int delayBeforeDrop) { |
| 446 |
|
| 447 |
final DragAndDropHelperSwt dndHelper = |
| 448 |
DragAndDropHelperSwt.getInstance(); |
| 449 |
final IRobot robot = getRobot(); |
| 450 |
final String modifier = dndHelper.getModifier(); |
| 451 |
final int mouseButton = dndHelper.getMouseButton(); |
| 452 |
// Note: This method performs the drag AND drop action in one runnable |
| 453 |
// in the GUI-Eventqueue because after the mousePress, the eventqueue |
| 454 |
// blocks! |
| 455 |
try { |
| 456 |
pressOrReleaseModifiers(modifier, true); |
| 457 |
|
| 458 |
getEventThreadQueuer().invokeAndWait("gdStartDragFigure", new IRunnable() { //$NON-NLS-1$ |
| 459 |
public Object run() throws StepExecutionException { |
| 460 |
// drag |
| 461 |
robot.mousePress(dndHelper.getDragComponent(), null, |
| 462 |
mouseButton); |
| 463 |
|
| 464 |
shakeMouse(); |
| 465 |
|
| 466 |
// drop |
| 467 |
gdClickInFigure(textPath, operator, 0, |
| 468 |
mouseButton, xPos, xUnits, yPos, yUnits); |
| 469 |
|
| 470 |
return null; |
| 471 |
} |
| 472 |
}); |
| 473 |
|
| 474 |
waitBeforeDrop(delayBeforeDrop); |
| 475 |
} finally { |
| 476 |
getRobot().mouseRelease(null, null, mouseButton); |
| 477 |
pressOrReleaseModifiers(modifier, false); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Returns the bounds for the figure for the given path. If no such |
| 483 |
* figure can be found, a {@link StepExecutionException} will be thrown. |
| 484 |
* |
| 485 |
* @param textPath The path to the figure. |
| 486 |
* @param operator The operator used for matching. |
| 487 |
* @return the bounds of the figure for the given path. |
| 488 |
*/ |
| 489 |
private Rectangle getFigureBoundsChecked(String textPath, String operator) { |
| 490 |
GraphicalEditPart editPart = |
| 491 |
findEditPart(textPath, operator); |
| 492 |
IFigure figure = findFigure(editPart); |
| 493 |
ConnectionAnchor anchor = null; |
| 494 |
|
| 495 |
if (figure == null) { |
| 496 |
// Try to find a connection anchor instead |
| 497 |
anchor = findConnectionAnchor(textPath, operator); |
| 498 |
if (anchor != null) { |
| 499 |
final String[] pathItems = MenuUtil.splitPath(textPath); |
| 500 |
final String[] editPartPathItems = |
| 501 |
new String[pathItems.length - 1]; |
| 502 |
System.arraycopy( |
| 503 |
pathItems, 0, editPartPathItems, 0, |
| 504 |
editPartPathItems.length); |
| 505 |
editPart = findEditPart(operator, editPartPathItems); |
| 506 |
} |
| 507 |
if (anchor == null || findFigure(editPart) == null) { |
| 508 |
throw new StepExecutionException( |
| 509 |
"No figure could be found for the given text path.", //$NON-NLS-1$ |
| 510 |
EventFactory.createActionError( |
| 511 |
TestErrorEvent.NOT_FOUND)); |
| 512 |
} |
| 513 |
|
| 514 |
} |
| 515 |
|
| 516 |
// Scrolling |
| 517 |
revealEditPart(editPart); |
| 518 |
|
| 519 |
return figure != null ? getBounds(figure) : getBounds(anchor); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Clicks the tool found at the given path. |
| 524 |
* |
| 525 |
* @param textPath The path to the tool. |
| 526 |
* @param operator The operator used for matching. |
| 527 |
* @param count The number of times to click. |
| 528 |
*/ |
| 529 |
public void gdSelectTool(String textPath, String operator, int count) { |
| 530 |
Control paletteControl = getPaletteControl(); |
| 531 |
IFigure figure = |
| 532 |
findPaletteFigureChecked(textPath, operator); |
| 533 |
getRobot().click(paletteControl, getBounds(figure), |
| 534 |
ClickOptions.create().setScrollToVisible(false) |
| 535 |
.setClickCount(count)); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* @return the control associated with the palette viewer. |
| 540 |
*/ |
| 541 |
private Control getPaletteControl() { |
| 542 |
EditDomain editDomain = getViewer().getEditDomain(); |
| 543 |
if (editDomain == null) { |
| 544 |
return null; |
| 545 |
} |
| 546 |
|
| 547 |
PaletteViewer paletteViewer = editDomain.getPaletteViewer(); |
| 548 |
if (paletteViewer == null) { |
| 549 |
return null; |
| 550 |
} |
| 551 |
|
| 552 |
return paletteViewer.getControl(); |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* |
| 557 |
* @param figure The figure for which to find the bounds. |
| 558 |
* @return the bounds of the given figure. |
| 559 |
*/ |
| 560 |
private Rectangle getBounds(IFigure figure) { |
| 561 |
org.eclipse.draw2d.geometry.Rectangle figureBounds = |
| 562 |
new org.eclipse.draw2d.geometry.Rectangle(figure.getBounds()); |
| 563 |
|
| 564 |
// Take scrolling and zooming into account |
| 565 |
figure.translateToAbsolute(figureBounds); |
| 566 |
|
| 567 |
return new Rectangle( |
| 568 |
figureBounds.x, figureBounds.y, |
| 569 |
figureBounds.width, figureBounds.height); |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* |
| 574 |
* @param anchor The anchor for which to find the bounds. |
| 575 |
* @return the "bounds" of the given anchor. Since the location of an |
| 576 |
* anchor is defined as a single point, the bounds are a small |
| 577 |
* rectangle with that point at the center. |
| 578 |
*/ |
| 579 |
private Rectangle getBounds(ConnectionAnchor anchor) { |
| 580 |
Validate.notNull(anchor); |
| 581 |
Point refPoint = anchor.getReferencePoint(); |
| 582 |
|
| 583 |
return new Rectangle( |
| 584 |
refPoint.x - 1, refPoint.y - 1, 3, 3); |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* |
| 589 |
* @param textPath The path to the GraphicalEditPart. |
| 590 |
* @param operator The operator used for matching. |
| 591 |
* @return the GraphicalEditPart for the given path. Returns |
| 592 |
* <code>null</code> if no EditPart exists for the given path or if |
| 593 |
* the found EditPart is not a GraphicalEditPart. |
| 594 |
*/ |
| 595 |
private GraphicalEditPart findEditPart(String textPath, String operator) { |
| 596 |
final String[] pathItems = MenuUtil.splitPath(textPath); |
| 597 |
return findEditPart(operator, pathItems); |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* @param operator The operator used for matching. |
| 602 |
* @param pathItems The path to the GraphicalEditPart. Each element in the |
| 603 |
* array represents a single segment of the path. |
| 604 |
* @return the GraphicalEditPart for the given path. Returns |
| 605 |
* <code>null</code> if no EditPart exists for the given path or if |
| 606 |
* the found EditPart is not a GraphicalEditPart. |
| 607 |
*/ |
| 608 |
private GraphicalEditPart findEditPart(String operator, |
| 609 |
final String[] pathItems) { |
| 610 |
boolean isExisting = true; |
| 611 |
EditPart currentEditPart = getRootEditPart().getContents(); |
| 612 |
|
| 613 |
for (int i = 0; i < pathItems.length && currentEditPart != null; i++) { |
| 614 |
List effectiveChildren = currentEditPart.getChildren(); |
| 615 |
EditPart [] children = |
| 616 |
(EditPart [])effectiveChildren.toArray( |
| 617 |
new EditPart[effectiveChildren.size()]); |
| 618 |
boolean itemFound = false; |
| 619 |
for (int j = 0; j < children.length && !itemFound; j++) { |
| 620 |
IEditPartIdentifier childFigureIdentifier = |
| 621 |
DefaultEditPartAdapterFactory.loadFigureIdentifier( |
| 622 |
children[j]); |
| 623 |
if (childFigureIdentifier != null) { |
| 624 |
String figureId = childFigureIdentifier.getIdentifier(); |
| 625 |
if (figureId != null |
| 626 |
&& MatchUtil.getInstance().match( |
| 627 |
figureId, pathItems[i], operator)) { |
| 628 |
itemFound = true; |
| 629 |
currentEditPart = children[j]; |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
if (!itemFound) { |
| 634 |
isExisting = false; |
| 635 |
break; |
| 636 |
} |
| 637 |
|
| 638 |
} |
| 639 |
|
| 640 |
return isExisting && currentEditPart instanceof GraphicalEditPart |
| 641 |
? (GraphicalEditPart)currentEditPart : null; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* |
| 646 |
* @param textPath The path to the figure. |
| 647 |
* @param operator The operator used for matching. |
| 648 |
* @return the figure for the GraphicalEditPart for the given path within |
| 649 |
* the palette. Returns <code>null</code> if no EditPart exists |
| 650 |
* for the given path or if the found EditPart does not have a |
| 651 |
* figure. |
| 652 |
*/ |
| 653 |
private IFigure findPaletteFigure(String textPath, String operator) { |
| 654 |
GraphicalEditPart editPart = findPaletteEditPart(textPath, operator); |
| 655 |
|
| 656 |
// Scrolling |
| 657 |
revealEditPart(editPart); |
| 658 |
|
| 659 |
return findFigure(editPart); |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* Finds and returns the palette figure for the given path. If no such |
| 664 |
* figure can be found, a {@link StepExecutionException} will be thrown. |
| 665 |
* |
| 666 |
* @param textPath The path to the figure. |
| 667 |
* @param operator The operator used for matching. |
| 668 |
* @return the figure for the GraphicalEditPart for the given path within |
| 669 |
* the palette. |
| 670 |
*/ |
| 671 |
private IFigure findPaletteFigureChecked(String textPath, String operator) { |
| 672 |
IFigure figure = findPaletteFigure(textPath, operator); |
| 673 |
if (figure == null) { |
| 674 |
throw new StepExecutionException( |
| 675 |
"No palette figure could be found for the given text path.", //$NON-NLS-1$ |
| 676 |
EventFactory.createActionError(TestErrorEvent.NOT_FOUND)); |
| 677 |
} |
| 678 |
return figure; |
| 679 |
|
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* |
| 684 |
* @param editPart The EditPart for which to find the corresponding figure. |
| 685 |
* @return the (visible) figure corresponding to the given EditPart, or |
| 686 |
* <code>null</code> if no visible figure corresponds to the given |
| 687 |
* EditPart. |
| 688 |
*/ |
| 689 |
private IFigure findFigure(GraphicalEditPart editPart) { |
| 690 |
if (editPart != null) { |
| 691 |
IFigure figure = editPart.getFigure(); |
| 692 |
if (figure.isShowing()) { |
| 693 |
return figure; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
return null; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* Attempts to find a connection anchor at the given textpath. |
| 702 |
* |
| 703 |
* @param textPath The path to the anchor. |
| 704 |
* @param operator The operator used for matching. |
| 705 |
* @return the anchor found at the given text path, or <code>null</code> |
| 706 |
* if no such anchor exists. |
| 707 |
*/ |
| 708 |
private ConnectionAnchor findConnectionAnchor( |
| 709 |
String textPath, String operator) { |
| 710 |
|
| 711 |
final String[] pathItems = MenuUtil.splitPath(textPath); |
| 712 |
final String[] editPartPathItems = new String[pathItems.length - 1]; |
| 713 |
System.arraycopy( |
| 714 |
pathItems, 0, editPartPathItems, 0, editPartPathItems.length); |
| 715 |
GraphicalEditPart editPart = findEditPart(operator, editPartPathItems); |
| 716 |
if (editPart != null) { |
| 717 |
String anchorPathItem = pathItems[pathItems.length - 1]; |
| 718 |
IEditPartIdentifier editPartIdentifier = |
| 719 |
DefaultEditPartAdapterFactory.loadFigureIdentifier(editPart); |
| 720 |
if (editPartIdentifier != null) { |
| 721 |
Map anchorMap = |
| 722 |
editPartIdentifier.getConnectionAnchors(); |
| 723 |
if (anchorMap != null) { |
| 724 |
Iterator anchorMapIter = |
| 725 |
anchorMap.keySet().iterator(); |
| 726 |
while (anchorMapIter.hasNext()) { |
| 727 |
Object anchorMapKey = anchorMapIter.next(); |
| 728 |
Object anchorMapValue = |
| 729 |
anchorMap.get(anchorMapKey); |
| 730 |
if (anchorMapKey instanceof String |
| 731 |
&& anchorMapValue instanceof ConnectionAnchor |
| 732 |
&& MatchUtil.getInstance().match( |
| 733 |
(String)anchorMapKey, anchorPathItem, |
| 734 |
operator)) { |
| 735 |
|
| 736 |
return (ConnectionAnchor)anchorMapValue; |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
return null; |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* Reveals the given {@link EditPart} within its viewer. |
| 748 |
* |
| 749 |
* @param editPart the {@link EditPart} to reveal. |
| 750 |
*/ |
| 751 |
private void revealEditPart(final EditPart editPart) { |
| 752 |
if (editPart != null) { |
| 753 |
getEventThreadQueuer().invokeAndWait(getClass().getName() + ".revealEditPart", new IRunnable() { //$NON-NLS-1$ |
| 754 |
|
| 755 |
public Object run() throws StepExecutionException { |
| 756 |
editPart.getViewer().reveal(editPart); |
| 757 |
return null; |
| 758 |
} |
| 759 |
|
| 760 |
}); |
| 761 |
} |
| 762 |
} |
| 763 |
} |