예제 #1
0
파일: SWTEvents.java 프로젝트: xored/rcptt
  public synchronized void sendFocus(Widget widget) {
    if (widget.isDisposed()) {
      return;
    }
    if (!(widget instanceof Control)) {
      return;
    }
    Control ctrl = (Control) widget;

    boolean sendUnfocus = TeslaCellEditorManager.getInstance().getEditors().length > 0;

    if (sendUnfocus) {
      if (ctrl != lastFocusControl) {
        // new focus control
        // send focusOut for real last focused control
        if (ctrl == lastFocusControl) {
          return;
        }
        if (lastFocusControl != null && !lastFocusControl.isDisposed()) {
          if (lastFocusControl.isFocusControl()) {
            sendEvent(lastFocusControl, SWT.FocusOut);
            if (!lastFocusControl.isDisposed()) {
              sendEvent(lastFocusControl, SWT.Deactivate);
            }
            if (!lastFocusControl.isDisposed()) {
              sendEvent(lastFocusControl, SWT.MouseExit);
            }
          }
        }
        lastFocusControl = ctrl;
        if (!ctrl.isFocusControl()) {
          ctrl.setFocus();
          sendEvent(lastFocusControl, SWT.MouseEnter);
          sendEvent(lastFocusControl, SWT.MouseMove);
          sendEvent(lastFocusControl, SWT.Activate);
          sendEvent(lastFocusControl, SWT.FocusIn);
        }
      }
    } else {
      // ctrl.getShell().forceActive();
      if (!ctrl.isFocusControl()) {
        try {
          ShellUtilsProvider.getShellUtils().forceActive(ctrl.getShell());
        } catch (CoreException e) {
          throw new RuntimeException(e);
        }
        ctrl.setFocus();
        ctrl.forceFocus();
        sendEvent(ctrl, SWT.MouseEnter);
        sendEvent(ctrl, SWT.MouseMove);
        sendEvent(ctrl, SWT.Activate);
        sendEvent(ctrl, SWT.FocusIn);
      }
      lastFocusControl = ctrl;
    }
  }
  /* a different method in ConsistencyUtility is invoked depending on what method
   * equals
   * ConsistencyUtility.MOUSE_CLICK:
   * 			paramA is the x coordinate offset from control
   * 			paramB is the y coordinate offset from control
   * 			paramC is the mouse button to click.
   * 			paramD if it equals ConsistencyUtility.ESCAPE_MENU, then another click
   * 			will be made on the border of the shell, to escape the menu that came up
   * 			(ie right clicking in a text widget)
   * 			invokes ConsistencyUtility.postClick(Display, Point, int)
   * ConsistencyUtility.MOUSE_DOUBLECLICK
   * 			paramA is the x coordinate offset from control
   * 			paramB is the y coordinate offset from control
   * 			paramC is the mouse button to click.
   * 			invokes ConsistencyUtility.postDoubleClick(Display, Point, int)
   * ConsistencyUtility.MOUSE_DRAG:
   * 			paramA is the x coordinate offset from control of the origin of drag
   * 			paramB is the y coordinate offset from control of the origin of drag
   * 			paramC is the x coordinate offset from control of the destination of drag
   * 			paramD is the y coordinate offset from control of the destination of drag
   * 			invokes ConsistencyUtility.postDrag(Display, Point, Point)
   * ConsistencyUtility.KEY_PRESS:
   * 		 	paramA is the character to press
   * 			paramB is the keyCode
   * 			invokes ConsistencyUtility.postKeyPress(Display, int, int)
   * ConsistencyUtility.DOUBLE_KEY_PRESS:
   * 		 	paramA is the character to press and hold
   * 			paramB is the keyCode
   * 			paramC is the second key to press while the first one is held (ie ctrl-a)
   * 			paramD is the second keycode
   * 			invokes ConsistencyUtility.postDoubleKeyPress(Display, int, int, int, int)
   * ConsistencyUtility.SELECTION:
   * 			paramA is the x coordinate offset from control of the first click
   * 			paramB is the y coordinate offset from control of the first click
   * 			paramC is the x coordinate offset from control of the second click
   * 			paramD is the y coordinate offset from control of the second click
   * 			invokes ConsistencyUtility.postSelection(Display, Point, Point)
   * ConsistencyUtility.SHELL_ICONIFY:
   * 			paramA is the button to click with
   * 			invokes ConsistencyUtility.postShellIconify(Display, Point, int)
   */
  protected void consistencyEvent(
      final int paramA,
      final int paramB,
      final int paramC,
      final int paramD,
      final int method,
      List<String> events,
      boolean focus) {
    if (SwtTestUtil.fTestConsistency) {
      final Display display = shell.getDisplay();
      if (events == null) events = new ArrayList<String>();
      final String test = getTestName();

      shell.setLayout(new org.eclipse.swt.layout.FillLayout());
      shell.setText("Parent");

      shell.pack();
      shell.open();
      if (control instanceof Shell) {
        ((Shell) control).pack();
        ((Shell) control).open();
      }
      final Point[] pt = determineLocations(paramA, paramB, paramC, paramD, method);
      if (focus && !control.setFocus()) control.forceFocus();
      String[] expectedEvents = hookExpectedEvents(test, events);
      new Thread() {
        @Override
        public void run() {
          display.wake();
          switch (method) {
            case ConsistencyUtility.MOUSE_CLICK:
              Assert.assertTrue(test, ConsistencyUtility.postClick(display, pt[0], paramC));
              if (paramD == ConsistencyUtility.ESCAPE_MENU) {
                Assert.assertTrue(test, ConsistencyUtility.postClick(display, pt[1], 1));
              }
              break;
            case ConsistencyUtility.MOUSE_DOUBLECLICK:
              Assert.assertTrue(test, ConsistencyUtility.postDoubleClick(display, pt[0], paramC));
              break;
            case ConsistencyUtility.KEY_PRESS:
              Assert.assertTrue(test, ConsistencyUtility.postKeyPress(display, paramA, paramB));
              break;
            case ConsistencyUtility.DOUBLE_KEY_PRESS:
              Assert.assertTrue(
                  test,
                  ConsistencyUtility.postDoubleKeyPress(display, paramA, paramB, paramC, paramD));
              break;
            case ConsistencyUtility.MOUSE_DRAG:
              Assert.assertTrue(test, ConsistencyUtility.postDrag(display, pt[0], pt[1]));
              break;
            case ConsistencyUtility.SELECTION:
              Assert.assertTrue(test, ConsistencyUtility.postSelection(display, pt[0], pt[1]));
              break;
            case ConsistencyUtility.SHELL_ICONIFY:
              Assert.assertTrue(test, ConsistencyUtility.postShellIconify(display, pt[1], paramA));
              if (control instanceof Shell) {
                display.syncExec(
                    new Thread() {
                      @Override
                      public void run() {
                        ((Shell) control).setMinimized(false);
                      }
                    });
              } else fail("Iconifying a non shell control");
              break;
          }
          display.asyncExec(
              new Thread() {
                @Override
                public void run() {
                  shell.dispose();
                }
              });
        }
      }.start();

      while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
      }
      setUp();
      String[] results = new String[events.size()];
      results = events.toArray(results);
      assertArrayEquals(test + " event ordering", expectedEvents, results);
    }
  }