public static void recursiveMouseTrackListener(
     Control c, MouseTrackListener listener, boolean includeListened) {
   if (includeListened
       || (c.getListeners(SWT.MouseEnter).length == 0
           || c.getListeners(SWT.MouseExit).length == 0
           || c.getListeners(SWT.MouseHover).length == 0)) c.addMouseTrackListener(listener);
   if (c instanceof Composite)
     for (Control child : ((Composite) c).getChildren())
       recursiveMouseTrackListener(child, listener, includeListened);
 }
  /*
   * @see org.eclipse.jface.text.source.IVerticalRulerColumn#createControl(org.eclipse.jface.text.source.CompositeRuler, org.eclipse.swt.widgets.Composite)
   */
  public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
    Control control = super.createControl(parentRuler, parentControl);

    // set background
    Color background = getCachedTextViewer().getTextWidget().getBackground();
    control.setBackground(background);

    // install hover listener
    control.addMouseTrackListener(
        new MouseTrackAdapter() {
          public void mouseExit(MouseEvent e) {
            if (clearCurrentAnnotation()) redraw();
          }
        });

    // install mouse move listener
    control.addMouseMoveListener(
        new MouseMoveListener() {
          public void mouseMove(MouseEvent e) {
            boolean redraw = false;
            ProjectionAnnotation annotation = findAnnotation(toDocumentLineNumber(e.y), false);
            if (annotation != fCurrentAnnotation) {
              if (fCurrentAnnotation != null) {
                fCurrentAnnotation.setRangeIndication(false);
                redraw = true;
              }
              fCurrentAnnotation = annotation;
              if (fCurrentAnnotation != null && !fCurrentAnnotation.isCollapsed()) {
                fCurrentAnnotation.setRangeIndication(true);
                redraw = true;
              }
            }
            if (redraw) redraw();
          }
        });
    return control;
  }
Beispiel #3
0
    public void activateHoverHelp(final org.eclipse.swt.widgets.Control control) {
      control.addMouseListener(
          new org.eclipse.swt.events.MouseAdapter() {
            public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
              if (tipShell.isVisible()) {
                tipShell.setVisible(false);
              }
            }
          });
      control.addMouseTrackListener(
          new org.eclipse.swt.events.MouseTrackAdapter() {
            public void mouseExit(org.eclipse.swt.events.MouseEvent e) {
              if (tipShell.isVisible()) {
                tipShell.setVisible(false);
              }
              tipWidget = null;
            }

            public void mouseHover(org.eclipse.swt.events.MouseEvent event) {
              org.eclipse.swt.graphics.Point pt =
                  new org.eclipse.swt.graphics.Point(event.x, event.y);
              org.eclipse.swt.widgets.Widget widget = event.widget;
              if (widget instanceof org.eclipse.swt.widgets.ToolBar) {
                org.eclipse.swt.widgets.ToolBar w = (org.eclipse.swt.widgets.ToolBar) widget;
                widget = w.getItem(pt);
              }
              if (widget instanceof org.eclipse.swt.widgets.Table) {
                org.eclipse.swt.widgets.Table w = (org.eclipse.swt.widgets.Table) widget;
                widget = w.getItem(pt);
              }
              if (widget instanceof org.eclipse.swt.widgets.Tree) {
                org.eclipse.swt.widgets.Tree w = (org.eclipse.swt.widgets.Tree) widget;
                widget = w.getItem(pt);
              }
              if (widget == null) {
                tipShell.setVisible(false);
                tipWidget = null;
                return;
              }
              if (widget == tipWidget) {
                return;
              }
              tipWidget = widget;
              tipPosition = control.toDisplay(pt);
              java.lang.String text = (java.lang.String) widget.getData("TIP_TEXT");
              org.eclipse.swt.graphics.Image image =
                  (org.eclipse.swt.graphics.Image) widget.getData("TIP_IMAGE");
              tipLabelText.setText(text != null ? text : "");
              tipLabelImage.setImage(image);
              tipShell.pack();
              setHoverLocation(tipShell, tipPosition);
              tipShell.setVisible(true);
            }
          });
      control.addHelpListener(
          new org.eclipse.swt.events.HelpListener() {
            public void helpRequested(org.eclipse.swt.events.HelpEvent event) {
              if (tipWidget == null) {
                return;
              }
              org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler handler =
                  (org.eclipse.swt.examples.hoverhelp.HoverHelp.ToolTipHelpTextHandler)
                      tipWidget.getData("TIP_HELPTEXTHANDLER");
              if (handler == null) {
                return;
              }
              java.lang.String text = handler.getHelpText(tipWidget);
              if (text == null) {
                return;
              }
              if (tipShell.isVisible()) {
                tipShell.setVisible(false);
                org.eclipse.swt.widgets.Shell helpShell =
                    new org.eclipse.swt.widgets.Shell(parentShell, SWT.SHELL_TRIM);
                helpShell.setLayout(new org.eclipse.swt.layout.FillLayout());
                org.eclipse.swt.widgets.Label label =
                    new org.eclipse.swt.widgets.Label(helpShell, SWT.NONE);
                label.setText(text);
                helpShell.pack();
                setHoverLocation(helpShell, tipPosition);
                helpShell.open();
              }
            }
          });
    }
  /**
   * Enables customized hover help for a specified control
   *
   * @control the control on which to enable hoverhelp
   */
  public void install(final Control control) {

    informationPresenterManager.install(control);

    /*
     * Get out of the way if we attempt to activate the control underneath the tooltip
     */
    control.addMouseListener(
        new MouseAdapter() {
          public void mouseDown(MouseEvent e) {
            disposeOfCurrentTipShell();
          }
        });

    /*
     * Trap hover events to pop-up tooltip
     */
    control.addMouseTrackListener(
        new MouseTrackAdapter() {
          public void mouseExit(MouseEvent e) {
            disposeOfCurrentTipShell();
          }

          public void mouseHover(MouseEvent event) {
            Point pt = new Point(event.x, event.y);
            Widget widget = event.widget;
            if (widget instanceof ToolBar) {
              ToolBar w = (ToolBar) widget;
              widget = w.getItem(pt);
            }
            if (widget instanceof Table) {
              Table w = (Table) widget;
              widget = w.getItem(pt);
            }
            if (widget instanceof Tree) {
              Tree w = (Tree) widget;
              widget = w.getItem(pt);
            }
            if (widget == null) {
              disposeOfCurrentTipShell();
              return;
            }
            if (widget == tipWidget) {
              return;
            }

            tipWidget = widget;
            Object data = widget.getData(TIP_DATA);
            if (data == null) {
              return;
            }
            final String text;
            if (data instanceof String) {
              text = (String) data;
            } else {
              text = data.toString();
            }
            if (text == null) {
              return;
            }

            // It must be set before showing the tooltip, as we'll loose the focus to the tooltip
            // and the
            // currently active bindings will become inactive.
            KeySequence activateEditorBinding =
                KeyBindingHelper.getCommandKeyBinding("org.eclipse.ui.window.activateEditor");
            informationPresenterManager.setActivateEditorBinding(activateEditorBinding);
            Shell activeShell = UIUtils.getActiveShell();
            informationPresenterManager.setInitiallyActiveShell(activeShell);

            createControls();

            final Point pos = new Point(pt.x + 10, pt.y);
            ITooltipInformationProvider provider =
                new ITooltipInformationProvider() {

                  public Object getInformation(Control fControl) {
                    return text;
                  }

                  public Point getPosition(Control fControl) {
                    return pos;
                  }
                };

            if (presenter instanceof IInformationPresenterAsTooltip) {
              IInformationPresenterAsTooltip iInformationPresenterAsTooltip =
                  (IInformationPresenterAsTooltip) presenter;
              iInformationPresenterAsTooltip.setData(data);
            }
            informationPresenterManager.setInformationProvider(provider);
            informationPresenterManager.showInformation();
          }
        });
  }