private String trim(final StringBuffer buffer, final TextPresentation presentation) {

    int length = buffer.length();

    int end = length - 1;
    while (end >= 0 && Character.isWhitespace(buffer.charAt(end))) {
      --end;
    }

    if (end == -1) {
      return ""; //$NON-NLS-1$
    }

    if (end < length - 1) {
      buffer.delete(end + 1, length);
    } else {
      end = length;
    }

    int start = 0;
    while (start < end && Character.isWhitespace(buffer.charAt(start))) {
      ++start;
    }

    buffer.delete(0, start);
    presentation.setResultWindow(new Region(start, buffer.length()));
    return buffer.toString();
  }
 /*
  * @see AbstractInfoView#setInput(Object)
  */
 protected void setInput(Object input) {
   String javadocHtml = (String) input;
   if (fIsUsingBrowserWidget) {
     if (javadocHtml != null && javadocHtml.length() > 0) {
       boolean RTL = (getSite().getShell().getStyle() & SWT.RIGHT_TO_LEFT) != 0;
       if (RTL) {
         StringBuffer buffer = new StringBuffer(javadocHtml);
         HTMLPrinter.insertStyles(buffer, new String[] {"direction:rtl"}); // $NON-NLS-1$
         javadocHtml = buffer.toString();
       }
     }
     fBrowser.setText(javadocHtml);
   } else {
     fPresentation.clear();
     Rectangle size = fText.getClientArea();
     try {
       javadocHtml =
           ((DefaultInformationControl.IInformationPresenterExtension) fPresenter)
               .updatePresentation(
                   getSite().getShell(), javadocHtml, fPresentation, size.width, size.height);
     } catch (IllegalArgumentException ex) {
       // the javadoc might no longer be valid
       return;
     }
     fText.setText(javadocHtml);
     TextPresentation.applyTextPresentation(fPresentation, fText);
   }
 }
  /**
   * Extended to remove any style ranges that overlap with an embedded editor.
   *
   * <p>We don't want to change style ranges over the region where there is a contained editor,
   * because thet will remove the GlyphMetrics that we created earlier
   */
  public void applyTextPresentation(TextPresentation textPresentation) {
    // need to check if any of the ranges of the textPresentation overlaps
    // with an embedded editor region.

    // for now, we can assume that there won't be many store positions, so we can
    // just go through them sequentially.
    // if this turns out to be expensive, we can be more intelligent later
    Collection<Position> values = editorPositionMap.values();
    for (Iterator<StyleRange> rangeIter = textPresentation.getAllStyleRangeIterator();
        rangeIter.hasNext(); ) {
      StyleRange range = rangeIter.next();
      Position overlapPosition = null;
      for (final Position editorPosition : values) {
        if (editorPosition.overlapsWith(range.start, range.length)) {
          overlapPosition = editorPosition;
          break;
        }
      }

      if (overlapPosition != null) {
        textPresentation.replaceStyleRanges(
            createStyleRange(getEditor(overlapPosition), overlapPosition));
      }
    }
  }
  boolean generateControls(int start, int length) {

    // if null then the control manager is not initialized
    if (annotationModel == null) return false;

    // set up a scan for the entire document.
    scanner.setPartialRange(doc, start, length, IDocument.DEFAULT_CONTENT_TYPE, start);

    boolean controlCreated = false;

    // create the controls,
    // determine their ranges,
    // add them to the StyledText
    IToken tok;
    while (!(tok = scanner.nextToken()).isEOF()) {
      if (tok == ContainingEditorScanner.EDITOR_TOKEN) {
        StyleRange[] ranges =
            createAndAddControl(scanner.getTokenOffset(), scanner.getTokenLength());
        TextPresentation singlePres = new TextPresentation();
        singlePres.addStyleRange(ranges[0]);
        singlePres.addStyleRange(ranges[1]);
        this.containingEditor.internalGetSourceViewer().changeTextPresentation(singlePres, true);

        controlCreated = true;
      }
    }

    return controlCreated;
  }
 public static StyleRange[] getStyles(final TextPresentation presentation) {
   final StyleRange[] ranges = new StyleRange[presentation.getDenumerableRanges()];
   final Iterator<?> e = presentation.getAllStyleRangeIterator();
   for (int i = 0; e.hasNext(); i++) {
     ranges[i] = (StyleRange) e.next();
   }
   return ranges;
 }
  protected void adaptTextPresentation(
      final TextPresentation presentation, final int offset, final int insertLength) {

    int yoursStart = offset;
    int yoursEnd = offset + insertLength - 1;
    yoursEnd = Math.max(yoursStart, yoursEnd);

    @SuppressWarnings("rawtypes")
    Iterator e = presentation.getAllStyleRangeIterator();
    while (e.hasNext()) {

      StyleRange range = (StyleRange) e.next();

      int myStart = range.start;
      int myEnd = range.start + range.length - 1;
      myEnd = Math.max(myStart, myEnd);

      if (myEnd < yoursStart) {
        continue;
      }

      if (myStart < yoursStart) {
        range.length += insertLength;
      } else {
        range.start += insertLength;
      }
    }
  }
  public static void applyStyles(
      final TextPresentation presentation, final Match[] matches, final Match selected) {

    final StyleRange[] ranges =
        createStyleRanges(
            presentation.getExtent(), matches, ColorManager.getInstance().getBackgroundColor());

    presentation.mergeStyleRanges(ranges);

    if (selected != null) {
      final StyleRange[] selectedRanges =
          createStyleRanges(
              presentation.getExtent(),
              new Match[] {selected},
              ColorManager.getInstance().getSelectedBackgroundColor());
      presentation.mergeStyleRanges(selectedRanges);
    }
  }
 protected void stopBold() {
   --fBold;
   if (fBold == 0) {
     if (fTextPresentation != null) {
       fTextPresentation.addStyleRange(
           new StyleRange(fStartOffset, fCounter - fStartOffset, null, null, SWT.BOLD));
     }
     fStartOffset = -1;
   }
 }
 /**
  * Adds style information to the given text presentation.
  *
  * @param presentation the text presentation to be extended
  * @param offset the offset of the range to be styled
  * @param length the length of the range to be styled
  * @param attr the attribute describing the style of the range to be styled
  */
 protected void addRange(
     final TextPresentation presentation,
     final int offset,
     final int length,
     final TextAttribute attr) {
   if (attr != null) {
     presentation.addStyleRange(
         new StyleRange(
             offset, length, attr.getForeground(), attr.getBackground(), attr.getStyle()));
   }
 }
 /**
  * Adds style information to the given text presentation.
  *
  * @param presentation the text presentation to be extended
  * @param offset the offset of the range to be styled
  * @param length the length of the range to be styled
  * @param attr the attribute describing the style of the range to be styled
  */
 protected void addRange(
     TextPresentation presentation, int offset, int length, TextAttribute attr) {
   if (attr != null) {
     int style = attr.getStyle();
     int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
     StyleRange styleRange =
         new StyleRange(offset, length, attr.getForeground(), attr.getBackground(), fontStyle);
     styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
     styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;
     styleRange.font = attr.getFont();
     presentation.addStyleRange(styleRange);
   }
 }
Exemple #11
0
 @Override
 public void createPresentation(TextPresentation presentation, ITypedRegion region) {
   // Use tokens provided by the lexer to highlight keywords, etc...
   // Seems fast enough to skip Eclipse partitioning. Infact, the Eclipse
   // partitioner seems to slow everything down...
   TSLKGrammarLexer lexer = new TSLKGrammarLexer(new ANTLRInputStream(document.get()));
   Token t = null;
   while ((t = lexer.nextToken()).getType() != Token.EOF) {
     if (t.getStartIndex() > region.getOffset() + region.getLength()) break;
     int start = t.getStartIndex();
     int end = t.getStopIndex();
     RGB foreground = null;
     RGB background = null;
     int style = SWT.NORMAL;
     switch (t.getType()) { // TODO: Make keywords customisable
       case TSLKGrammarLexer.WHILE:
       case TSLKGrammarLexer.FOR:
       case TSLKGrammarLexer.FUNC:
       case TSLKGrammarLexer.IF:
       case TSLKGrammarLexer.THEN:
       case TSLKGrammarLexer.DO:
       case TSLKGrammarLexer.END:
         foreground = ColorManager.KEYWORD;
         style = SWT.BOLD;
         break;
       case TSLKGrammarLexer.STRING:
         foreground = ColorManager.STRING;
         break;
       case TSLKGrammarLexer.SLCOMMENT:
         foreground = ColorManager.SINGLE_LINE_COMMENT;
         break;
       case TSLKGrammarLexer.MLCOMMENT:
         foreground = ColorManager.MULTI_LINE_COMMENT;
         break;
       default:
         foreground = ColorManager.DEFAULT;
         break;
     }
     presentation.addStyleRange(
         new StyleRange(
             start,
             end - start + 1,
             colorManager.getColor(foreground),
             colorManager.getColor(background),
             style));
   }
 }
 /*
  * @see org.eclipse.cdt.dsf.debug.internal.ui.disassembly.presentation.ISourcePresentationCreator#getPresentation(org.eclipse.jface.text.IRegion, org.eclipse.jface.text.IDocument)
  */
 public TextPresentation getPresentation(IRegion region, IDocument document) {
   assert fViewer != null;
   if (fViewer == null) {
     return null;
   }
   if (fPresentation == null) {
     setDocumentToDamagers(document);
     setDocumentToRepairers(document);
     int docLength = document.getLength();
     if (docLength <= 128 * 1024) {
       IRegion all = new Region(0, docLength);
       fPresentation = createPresentation(all, document);
     } else {
       return createPresentation(region, document);
     }
   }
   fPresentation.setResultWindow(region);
   return fPresentation;
 }
Exemple #13
0
  @SuppressWarnings("unchecked")
  private void updateImage(String imgSrc, ImageData imageData) {
    if (display.isDisposed() || viewer.getTextWidget().isDisposed()) {
      return;
    }
    Image image =
        imageData == null
            ? imageCache.getMissingImage()
            : ImageDescriptor.createFromImageData(imageData).createImage();
    imageCache.putImage(imgSrc, image);

    Set<ImageAnnotation> modifiedAnnotations = new HashSet<>();

    AnnotationModel annotationModel = (AnnotationModel) viewer.getAnnotationModel();
    Object annotationLockObject = annotationModel.getLockObject();
    if (annotationLockObject == null) {
      annotationLockObject = annotationModel;
    }
    synchronized (annotationLockObject) {
      Iterator<Annotation> iterator = annotationModel.getAnnotationIterator();
      while (iterator.hasNext()) {
        Annotation annotation = iterator.next();
        if (annotation instanceof ImageAnnotation) {
          ImageAnnotation imageAnnotation = (ImageAnnotation) annotation;
          if (imgSrc.equals(imageAnnotation.getUrl())) {
            imageAnnotation.setImage(image);
            modifiedAnnotations.add(imageAnnotation);
          }
        }
      }
    }

    if (!modifiedAnnotations.isEmpty()) {
      computingChanges = true;
      try {
        boolean rangesAdjusted = false;
        List<StyleRange> ranges = new ArrayList<>();

        Iterator<?> allStyleRangeIterator = viewer.getTextPresentation().getAllStyleRangeIterator();
        while (allStyleRangeIterator.hasNext()) {
          StyleRange range = (StyleRange) allStyleRangeIterator.next();
          ranges.add((StyleRange) range.clone());
        }

        GC gc = new GC(viewer.getTextWidget());
        try {
          viewer.getTextWidget().setRedraw(false);
          TextPresentation textPresentation = viewer.getTextPresentation();
          //			textPresentation.
          for (ImageAnnotation annotation : modifiedAnnotations) {
            int height = annotation.getImage().getBounds().height;
            Position position = annotationModel.getPosition(annotation);
            String widgetText = viewer.getTextWidget().getText();
            Font font = null;
            if (widgetText.length() > 0 && widgetText.length() > position.offset) {
              StyleRange styleRange = viewer.getTextWidget().getStyleRangeAtOffset(position.offset);
              if (styleRange != null) {
                font = styleRange.font;
              }
            }
            if (font == null) {
              font = viewer.getTextWidget().getFont();
            }
            gc.setFont(font);
            Point extent = gc.textExtent("\n"); // $NON-NLS-1$
            if (extent.y > 0) {
              int numNewlines = (int) Math.ceil(((double) height) / ((double) extent.y));
              final int originalNewlines = numNewlines;
              IDocument document = viewer.getDocument();
              try {
                for (int x = position.offset; x < document.getLength(); ++x) {
                  if (document.getChar(x) == '\n') {
                    if (x != position.offset
                        && Util.annotationsIncludeOffset(viewer.getAnnotationModel(), x)) {
                      break;
                    }
                    --numNewlines;
                  } else {
                    break;
                  }
                }
                if (numNewlines > 0) {
                  String newlines = ""; // $NON-NLS-1$
                  for (int x = 0; x < numNewlines; ++x) {
                    newlines += "\n"; // $NON-NLS-1$
                  }
                  document.replace(position.offset + 1, 0, newlines);
                } else if (numNewlines < 0) {
                  document.replace(position.offset, -numNewlines, ""); // $NON-NLS-1$
                }
                if (numNewlines != 0) {
                  // no need to fixup other annotation positions, since the annotation model is
                  // hooked into the document.

                  // fix up styles
                  for (StyleRange range : ranges) {
                    if (range.start > position.offset) {
                      range.start += numNewlines;
                      rangesAdjusted = true;
                    } else if (range.start + range.length > position.offset) {
                      range.length += numNewlines;
                      rangesAdjusted = true;
                    }
                  }
                }

                // bug# 248643: update the annotation size to reflect the full size of the image
                //              so that it gets repainted when some portion of the image is exposed
                //              as a result of scrolling
                if (position.getLength() != originalNewlines) {
                  annotationModel.modifyAnnotationPosition(
                      annotation, new Position(position.offset, originalNewlines));
                }
              } catch (BadLocationException e) {
                // ignore
              }
            }
          }
          if (rangesAdjusted) {
            TextPresentation presentation = new TextPresentation();
            if (textPresentation.getDefaultStyleRange() != null) {
              StyleRange defaultStyleRange =
                  (StyleRange) textPresentation.getDefaultStyleRange().clone();
              if (viewer.getDocument() != null) {
                if (defaultStyleRange.length < viewer.getDocument().getLength()) {
                  defaultStyleRange.length = viewer.getDocument().getLength();
                }
              }
              presentation.setDefaultStyleRange(defaultStyleRange);
            }
            for (StyleRange range : ranges) {
              presentation.addStyleRange(range);
            }
            viewer.setTextPresentation(presentation);
            viewer.invalidateTextPresentation();
          }
        } finally {
          viewer.getTextWidget().setRedraw(true);
          gc.dispose();
        }
        viewer.getTextWidget().redraw();
      } finally {
        computingChanges = false;
      }
    }
  }