Example #1
0
  private void setRuler(RulerProvider provider, int orientation) {
    Object ruler = null;
    if (isRulerVisible && provider != null)
      // provider.getRuler() might return null (at least the API does not prevent that)
      ruler = provider.getRuler();

    if (ruler == null) {
      // Ruler is not visible or is not present
      setRulerContainer(null, orientation);
      // Layout right-away to prevent an empty control from showing
      layout(true);
      return;
    }

    GraphicalViewer container = getRulerContainer(orientation);
    if (container == null) {
      container = createRulerContainer(orientation);
      setRulerContainer(container, orientation);
    }
    if (container.getContents() != ruler) {
      container.setContents(ruler);
      needToLayout = true;
      Display.getCurrent().asyncExec(runnable);
    }
  }
Example #2
0
  /**
   * Creates rulers for the given graphical viewer.
   *
   * <p>The primaryViewer or its Control cannot be <code>null</code>. The primaryViewer's Control
   * should be a FigureCanvas and a child of this Composite. This method should only be invoked
   * once.
   *
   * <p>To create ruler(s), simply add the RulerProvider(s) (with the right key:
   * RulerProvider.PROPERTY_HORIZONTAL_RULER or RulerProvider.PROPERTY_VERTICAL_RULER) as a property
   * on the given viewer. It can be done after this method is invoked.
   * RulerProvider.PROPERTY_RULER_VISIBILITY can be used to show/hide the rulers.
   *
   * @param primaryViewer The graphical viewer for which the rulers have to be created
   */
  public void setGraphicalViewer(ScrollingGraphicalViewer primaryViewer) {
    // pre-conditions
    Assert.isNotNull(primaryViewer);
    Assert.isNotNull(primaryViewer.getControl());
    Assert.isTrue(diagramViewer == null);

    diagramViewer = primaryViewer;
    editor = (FigureCanvas) diagramViewer.getControl();

    // layout whenever the scrollbars are shown or hidden, and whenever the RulerComposite
    // is resized
    layoutListener =
        new Listener() {
          public void handleEvent(Event event) {
            // @TODO:Pratik  If you use Display.asyncExec(runnable) here, some flashing
            // occurs.  You can see it when the palette is in the editor, and you hit
            // the button to show/hide it.
            layout(true);
          }
        };
    addListener(SWT.Resize, layoutListener);
    editor.getHorizontalBar().addListener(SWT.Show, layoutListener);
    editor.getHorizontalBar().addListener(SWT.Hide, layoutListener);
    editor.getVerticalBar().addListener(SWT.Show, layoutListener);
    editor.getVerticalBar().addListener(SWT.Hide, layoutListener);

    propertyListener =
        new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            String property = evt.getPropertyName();
            if (RulerProvider.PROPERTY_HORIZONTAL_RULER.equals(property)) {
              setRuler(
                  (RulerProvider)
                      diagramViewer.getProperty(RulerProvider.PROPERTY_HORIZONTAL_RULER),
                  PositionConstants.NORTH);
            } else if (RulerProvider.PROPERTY_VERTICAL_RULER.equals(property)) {
              setRuler(
                  (RulerProvider) diagramViewer.getProperty(RulerProvider.PROPERTY_VERTICAL_RULER),
                  PositionConstants.WEST);
            } else if (RulerProvider.PROPERTY_RULER_VISIBILITY.equals(property))
              setRulerVisibility(
                  ((Boolean) diagramViewer.getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY))
                      .booleanValue());
          }
        };
    diagramViewer.addPropertyChangeListener(propertyListener);
    Boolean rulerVisibility =
        (Boolean) diagramViewer.getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY);
    if (rulerVisibility != null) setRulerVisibility(rulerVisibility.booleanValue());
    setRuler(
        (RulerProvider) diagramViewer.getProperty(RulerProvider.PROPERTY_HORIZONTAL_RULER),
        PositionConstants.NORTH);
    setRuler(
        (RulerProvider) diagramViewer.getProperty(RulerProvider.PROPERTY_VERTICAL_RULER),
        PositionConstants.WEST);
  }
Example #3
0
 private void setRulerVisibility(boolean isVisible) {
   if (isRulerVisible != isVisible) {
     isRulerVisible = isVisible;
     if (diagramViewer != null) {
       setRuler(
           (RulerProvider) diagramViewer.getProperty(RulerProvider.PROPERTY_HORIZONTAL_RULER),
           PositionConstants.NORTH);
       setRuler(
           (RulerProvider) diagramViewer.getProperty(RulerProvider.PROPERTY_VERTICAL_RULER),
           PositionConstants.WEST);
     }
   }
 }
Example #4
0
 private void disposeRulerViewer(GraphicalViewer viewer) {
   if (viewer == null) return;
   /*
    * There's a tie from the editor's range model to the RulerViewport (via a listener)
    * to the RulerRootEditPart to the RulerViewer.  Break this tie so that the viewer
    * doesn't leak and can be garbage collected.
    */
   RangeModel rModel = new DefaultRangeModel();
   Viewport port = ((FigureCanvas) viewer.getControl()).getViewport();
   port.setHorizontalRangeModel(rModel);
   port.setVerticalRangeModel(rModel);
   rulerEditDomain.removeViewer(viewer);
   viewer.getControl().dispose();
 }
Example #5
0
  private void doLayout() {
    if (left == null && top == null) {
      Rectangle area = getClientArea();
      if (!editor.getBounds().equals(area)) editor.setBounds(area);
      return;
    }

    int leftWidth = 0, topHeight = 0;
    Rectangle leftTrim = null, topTrim = null;
    if (left != null) {
      leftTrim = calculateRulerTrim((Canvas) left.getControl());
      // Adding the trim width here because FigureCanvas#computeSize() does not
      leftWidth = left.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).x + leftTrim.width;
    }
    if (top != null) {
      topTrim = calculateRulerTrim((Canvas) top.getControl());
      topHeight = top.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y + topTrim.height;
    }

    Rectangle editorSize = getClientArea();
    editorSize.x = leftWidth;
    editorSize.y = topHeight;
    editorSize.width -= leftWidth;
    editorSize.height -= topHeight;
    editor.setBounds(editorSize);

    /*
     * Fix for Bug# 67554
     * Take trim into account.  Some platforms (such as MacOS and Motif) leave some
     * trimming around some canvasses.
     */
    Rectangle trim = calculateEditorTrim(editor);
    if (left != null) {
      // The - 1 and + 1 are to compensate for the RulerBorder
      left.getControl()
          .setBounds(
              0,
              topHeight - trim.x + leftTrim.x - 1,
              leftWidth,
              editorSize.height - trim.height + leftTrim.height + 1);
    }
    if (top != null) {
      top.getControl()
          .setBounds(
              leftWidth - trim.y + topTrim.y - 1,
              0,
              editorSize.width - trim.width + topTrim.width + 1,
              topHeight);
    }
  }
Example #6
0
  private GraphicalViewer createRulerContainer(int orientation) {
    ScrollingGraphicalViewer viewer = new RulerViewer();
    final boolean isHorizontal =
        orientation == PositionConstants.NORTH || orientation == PositionConstants.SOUTH;

    // Finish initializing the viewer
    viewer.setRootEditPart(new RulerRootEditPart(isHorizontal));
    viewer.setEditPartFactory(new RulerEditPartFactory(diagramViewer));
    viewer.createControl(this);
    ((GraphicalEditPart) viewer.getRootEditPart())
        .getFigure()
        .setBorder(new RulerBorder(isHorizontal));
    viewer.setProperty(GraphicalViewer.class.toString(), diagramViewer);

    // Configure the viewer's control
    FigureCanvas canvas = (FigureCanvas) viewer.getControl();
    canvas.setScrollBarVisibility(FigureCanvas.NEVER);
    if (font == null) {
      FontData[] data = canvas.getFont().getFontData();
      for (int i = 0; i < data.length; i++) {
        data[i].setHeight(data[i].getHeight() - 1);
      }
      font = new Font(Display.getCurrent(), data);
    }
    canvas.setFont(font);
    if (isHorizontal) {
      canvas.getViewport().setHorizontalRangeModel(editor.getViewport().getHorizontalRangeModel());
    } else {
      canvas.getViewport().setVerticalRangeModel(editor.getViewport().getVerticalRangeModel());
    }

    // Add the viewer to the rulerEditDomain
    if (rulerEditDomain == null) {
      rulerEditDomain = new EditDomain();
      rulerEditDomain.setCommandStack(diagramViewer.getEditDomain().getCommandStack());
    }
    rulerEditDomain.addViewer(viewer);

    return viewer;
  }
Example #7
0
 private void disposeResources() {
   if (diagramViewer != null) diagramViewer.removePropertyChangeListener(propertyListener);
   if (font != null) font.dispose();
   // layoutListener is not being removed from the scroll bars because they are already
   // disposed at this point.
 }