コード例 #1
0
  /**
   * Scrolls the containing editor to the given offset of the contained editor
   *
   * @param editor
   * @param scrollTo text offset in the embedded editor that should be revealed
   */
  public void revealSelection(ContainedEditorManager editor, int scrollTo) {
    StyledText containedStyledText = (StyledText) editor.getAdapter(StyledText.class);

    // this progression determines the location of the offset in the coordinate system
    // of the containing styledText
    Point containedLoc = containedStyledText.getLocationAtOffset(scrollTo);
    Point displayLoc = containedStyledText.toDisplay(containedLoc);
    Point containingLoc = styledText.toControl(displayLoc);

    // next, we determine if this location is in the visible area.
    Point containingSize = styledText.getSize();
    Rectangle containingBounds = new Rectangle(0, 0, containingSize.x, containingSize.y);
    if (!containingBounds.contains(containingLoc)) {
      // pad a little to the left and a little bit down
      containingLoc.x -= 50;
      containingLoc.y += 100;

      // if not, then perform a scroll.
      styledText.setTopPixel(styledText.getTopPixel() + containingLoc.y - containingSize.y);

      // do the same for horizontal
      styledText.setHorizontalPixel(styledText.getHorizontalPixel() + containingLoc.x);
    }
    paint(ContainingEditor.EMBEDDED_REPAINT);
  }