/** * Clicks on the {@link MenuItem}. * * @param menuItem * the {@link MenuItem} to click on */ private static void click(final MenuItem menuItem) { final Event event = new Event(); event.time = (int) System.currentTimeMillis(); event.widget = menuItem; event.display = menuItem.getDisplay(); event.type = SWT.Selection; UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() { @Override public void run() { if (SWTUtils.hasStyle(menuItem, SWT.CHECK) || SWTUtils.hasStyle(menuItem, SWT.RADIO)) { menuItem.setSelection(!menuItem.getSelection()); } menuItem.notifyListeners(SWT.Selection, event); } }); }
/** * Clicks the context menu matching the given labels. * When a context menu 'Compile' exists with the sub context menu 'All Invalids', * then the context menu 'All Invalids' can be clicked by giving the labels 'Compile' and 'All Invalids'. * * @param bot * the {@link AbstractSWTBot} on which to infer the context menu * @param labels * the labels on the context menus * @throw {@link WidgetNotFoundException} if the context menu could not be found */ public static void clickContextMenu(final AbstractSWTBot<? extends Control> bot, final String... labels) { MenuItem menuItem = getContextMenuItem(bot, labels); if (menuItem == null) { long endTime = System.currentTimeMillis() + SWTBotPreferences.TIMEOUT; while (menuItem == null && System.currentTimeMillis() < endTime) { SWTUtils.sleep(SWTBotPreferences.DEFAULT_POLL_DELAY); menuItem = getContextMenuItem(bot, labels); } if (menuItem == null) { throw new WidgetNotFoundException("Could not find menu: " + Arrays.asList(labels)); } } click(menuItem); }
/** * {@inheritDoc} A customized version with less notifications, since the button could be disabled after the {@link SWT.Selection} event. */ @Override public SwtBotButton click() { log.debug(MessageFormat.format("Clicking on {0}", SWTUtils.getText(widget))); //$NON-NLS-1$ waitForEnabled(); notify(SWT.MouseEnter); notify(SWT.MouseMove); notify(SWT.Activate); notify(SWT.FocusIn); notify(SWT.MouseDown); notify(SWT.MouseUp); notify(SWT.Selection); log.debug(MessageFormat.format("Clicked on {0}", SWTUtils.getText(widget))); //$NON-NLS-1$ return this; }
/** * Performs the drag and drop from source {@link Point} to destination {@link Point}. * * @param source * the source {@link Point} to start dragging * @param dest * the destination {@link Point} for dropping */ private void doDragAndDrop(final Point source, final Point dest) { // log.debug(MessageFormat.format("Drag-and-dropping from ({0},{1}) to ({2},{3})", // source.x, source.y, dest.x, dest.y)); try { final Robot awtRobot = new Robot(); // the x+10 motion is needed to let native functions register a drag // detect. It did not work under Windows // otherwise and has been reported to be required for linux, too. // But I could not test that. syncExec(new VoidResult() { @Override public void run() { awtRobot.mouseMove(source.x, source.y); awtRobot.mousePress(InputEvent.BUTTON1_MASK); awtRobot.mouseMove(source.x + DRAG_THRESHOLD, source.y); } }); /* drag delay */ SWTUtils.sleep(DRAG_DELAY); syncExec(new VoidResult() { @Override public void run() { awtRobot.mouseMove(dest.x + DRAG_THRESHOLD, dest.y); awtRobot.mouseMove(dest.x, dest.y); } }); /* drop delay */ SWTUtils.sleep(DRAG_DELAY); syncExec(new VoidResult() { @Override public void run() { awtRobot.mouseRelease(InputEvent.BUTTON1_MASK); } }); } catch (final AWTException e) { // log.error(e.getMessage(), e); throw new IllegalStateException(e); } }