/**
   * Returns the difference between the baseline of the widget and the baseline as specified by the
   * font for <code>gc</code>. When drawing line numbers, the returned bias should be added to
   * obtain text lined up on the correct base line of the text widget.
   *
   * @param gc the {@code GC} to get the font metrics from
   * @param widgetLine the widget line
   * @return the baseline bias to use when drawing text that is lined up with the text widget.
   */
  private int getBaselineBias(GC gc, int widgetLine) {
    ITextViewer textViewer = getParentRuler().getTextViewer();
    int offset = textViewer.getTextWidget().getOffsetAtLine(widgetLine);
    int widgetBaseline = textViewer.getTextWidget().getBaseline(offset);

    FontMetrics fm = gc.getFontMetrics();
    int fontBaseline = fm.getAscent() + fm.getLeading();
    int baselineBias = widgetBaseline - fontBaseline;
    return Math.max(0, baselineBias);
  }
  /**
   * Draw string at widget offset.
   *
   * @param gc
   * @param offset the widget offset
   * @param s the string to be drawn
   * @param fg the foreground color
   */
  private void draw(GC gc, int offset, String s, Color fg) {
    // Compute baseline delta (see
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=165640)
    int baseline = fTextWidget.getBaseline(offset);
    FontMetrics fontMetrics = gc.getFontMetrics();
    int fontBaseline = fontMetrics.getAscent() + fontMetrics.getLeading();
    int baslineDelta = baseline - fontBaseline;

    Point pos = fTextWidget.getLocationAtOffset(offset);
    gc.setForeground(fg);
    gc.drawString(s, pos.x, pos.y + baslineDelta, true);
  }
Beispiel #3
0
    /*
     * @see org.eclipse.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(org.eclipse.swt.graphics.GC, org.eclipse.swt.custom.StyledText, int, int, org.eclipse.swt.graphics.Color)
     */
    public void draw(
        Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) {
      if (annotation instanceof ProjectionAnnotation) {
        ProjectionAnnotation projectionAnnotation = (ProjectionAnnotation) annotation;
        if (projectionAnnotation.isCollapsed()) {

          if (gc != null) {

            StyledTextContent content = textWidget.getContent();
            int line = content.getLineAtOffset(offset);
            int lineStart = content.getOffsetAtLine(line);
            String text = content.getLine(line);
            int lineLength = text == null ? 0 : text.length();
            int lineEnd = lineStart + lineLength;
            Point p = textWidget.getLocationAtOffset(lineEnd);

            Color c = gc.getForeground();
            gc.setForeground(color);

            FontMetrics metrics = gc.getFontMetrics();

            // baseline: where the dots are drawn
            int baseline = textWidget.getBaseline(offset);
            // descent: number of pixels that the box extends over baseline
            int descent = Math.min(2, textWidget.getLineHeight(offset) - baseline);
            // ascent: so much does the box stand up from baseline
            int ascent = metrics.getAscent();
            // leading: free space from line top to box upper line
            int leading = baseline - ascent;
            // height: height of the box
            int height = ascent + descent;

            int width = metrics.getAverageCharWidth();
            gc.drawRectangle(p.x, p.y + leading, width, height);
            int third = width / 3;
            int dotsVertical = p.y + baseline - 1;
            gc.drawPoint(p.x + third, dotsVertical);
            gc.drawPoint(p.x + width - third, dotsVertical);

            gc.setForeground(c);

          } else {
            textWidget.redrawRange(offset, length, true);
          }
        }
      }
    }
  protected void drawLabel(PaintEvent e) {
    Rectangle bounds = getBounds();

    // label
    // drawn within this composite, ref point is composite
    Rectangle rlabel = new Rectangle(0, 0, bounds.width, bounds.height);
    //
    getItem().setLabeled(true); /*r.height>=fm.getAscent() && r.width>=SWITCH_ITEM_WIDTH*/
    FontMetrics fm = e.gc.getFontMetrics();
    if (getItem().isLabeled()) {
      String s =
          getCell()
              .getCalendar()
              .getModel()
              .getLabel(getItem().getItem(), getCell().getDate().getTime());
      int centery = Math.max(0, (rlabel.height - fm.getAscent()) / 2 - 3);
      e.gc.drawString(s, rlabel.x + 3, rlabel.y + centery);
    }
  }
  @Override
  protected void createContent(final Composite parent) {
    fContentComposite =
        new Composite(parent, SWT.NONE) {
          @Override
          public Point computeSize(final int width, final int height, final boolean changed) {
            return super.computeSize(width, height, changed || width != getSize().x);
          }
        };
    fContentComposite.setBackgroundMode(SWT.INHERIT_FORCE);

    final GridLayout gridLayout = LayoutUtil.applyCompositeDefaults(new GridLayout(), 2);
    gridLayout.horizontalSpacing = (int) ((gridLayout.horizontalSpacing) / 1.5);
    fContentComposite.setLayout(gridLayout);

    final int vIndent = Math.max(1, LayoutUtil.defaultVSpacing() / 4);
    final int hIndent = Math.max(2, LayoutUtil.defaultHSpacing() / 3);

    { // Title image
      fTitleImage = new Label(fContentComposite, SWT.NULL);
      final Image image = SharedUIResources.getImages().get(SharedUIResources.PLACEHOLDER_IMAGE_ID);
      fTitleImage.setImage(image);

      final GridData textGd = new GridData(SWT.FILL, SWT.TOP, false, false);
      fTitleText =
          new StyledText(fContentComposite, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP) {
            @Override
            public Point computeSize(int width, final int height, final boolean changed) {
              if (!fLayoutHint && width <= 0 && fContentComposite.getSize().x > 0) {
                width =
                    fContentComposite.getSize().x
                        - LayoutUtil.defaultHMargin()
                        - fTitleImage.getSize().x
                        - LayoutUtil.defaultHSpacing()
                        - 10;
              }

              final Point size = super.computeSize(width, -1, true);
              //					if (width >= 0) {
              //						size.x = Math.min(size.x, width);
              //					}
              return size;
            }
          };

      fTitleText.setFont(JFaceResources.getDialogFont());
      final GC gc = new GC(fTitleText);
      final FontMetrics fontMetrics = gc.getFontMetrics();
      final GridData imageGd = new GridData(SWT.FILL, SWT.TOP, false, false);
      imageGd.horizontalIndent = hIndent;
      final int textHeight = fontMetrics.getAscent() + fontMetrics.getLeading();
      final int imageHeight = image.getBounds().height;
      final int shift = Math.max(3, (int) ((fontMetrics.getDescent()) / 1.5));
      if (textHeight + shift < imageHeight) {
        imageGd.verticalIndent = vIndent + shift;
        textGd.verticalIndent = vIndent + (imageHeight - textHeight);
      } else {
        imageGd.verticalIndent = vIndent + (textHeight - imageHeight) + shift;
        textGd.verticalIndent = vIndent;
      }
      fTitleImage.setLayoutData(imageGd);
      fTitleText.setLayoutData(textGd);
      fLayoutWorkaround = true;

      gc.dispose();
    }

    fInfoText =
        new StyledText(
            fContentComposite,
            fMode == MODE_FOCUS
                ? (SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL)
                : (SWT.MULTI | SWT.READ_ONLY));
    fInfoText.setIndent(hIndent);
    fInfoText.setFont(JFaceResources.getFont(PREF_DETAIL_PANE_FONT));

    final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
    //		gd.widthHint = LayoutUtil.hintWidth(fInfoText, INFO_FONT, 50);
    fInfoText.setLayoutData(gd);

    setBackgroundColor(getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    setForegroundColor(getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));

    updateInput();
  }