示例#1
1
 /**
  * Event.detail line start offset (input) Event.text line text (input) LineStyleEvent.styles
  * Enumeration of StyleRanges, need to be in order. (output) LineStyleEvent.background line
  * background color (output)
  */
 public void lineGetStyle(LineStyleEvent event) {
   Vector styles = new Vector();
   int token;
   StyleRange lastStyle;
   // If the line is part of a block comment, create one style for the entire line.
   if (inBlockComment(event.lineOffset, event.lineOffset + event.lineText.length())) {
     styles.addElement(
         new StyleRange(event.lineOffset, event.lineText.length(), getColor(COMMENT), null));
     event.styles = new StyleRange[styles.size()];
     styles.copyInto(event.styles);
     return;
   }
   Color defaultFgColor = ((Control) event.widget).getForeground();
   scanner.setRange(event.lineText);
   token = scanner.nextToken();
   while (token != EOF) {
     if (token == OTHER) {
       // do nothing for non-colored tokens
     } else if (token != WHITE) {
       Color color = getColor(token);
       // Only create a style if the token color is different than the
       // widget's default foreground color and the token's style is not
       // bold.  Keywords are bolded.
       if ((!color.equals(defaultFgColor)) || (token == KEY)) {
         StyleRange style =
             new StyleRange(
                 scanner.getStartOffset() + event.lineOffset, scanner.getLength(), color, null);
         if (token == KEY) {
           style.fontStyle = SWT.BOLD;
         }
         if (styles.isEmpty()) {
           styles.addElement(style);
         } else {
           // Merge similar styles.  Doing so will improve performance.
           lastStyle = (StyleRange) styles.lastElement();
           if (lastStyle.similarTo(style) && (lastStyle.start + lastStyle.length == style.start)) {
             lastStyle.length += style.length;
           } else {
             styles.addElement(style);
           }
         }
       }
     } else if ((!styles.isEmpty())
         && ((lastStyle = (StyleRange) styles.lastElement()).fontStyle == SWT.BOLD)) {
       int start = scanner.getStartOffset() + event.lineOffset;
       lastStyle = (StyleRange) styles.lastElement();
       // A font style of SWT.BOLD implies that the last style
       // represents a java keyword.
       if (lastStyle.start + lastStyle.length == start) {
         // Have the white space take on the style before it to
         // minimize the number of style ranges created and the
         // number of font style changes during rendering.
         lastStyle.length += scanner.getLength();
       }
     }
     token = scanner.nextToken();
   }
   event.styles = new StyleRange[styles.size()];
   styles.copyInto(event.styles);
 }
示例#2
1
  /**
   * Event.detail line start offset (input) Event.text line text (input) LineStyleEvent.styles
   * Enumeration of StyleRanges, need to be in order. (output) LineStyleEvent.background line
   * background color (output)
   */
  public void lineGetStyle(LineStyleEvent event) {
    Vector<StyleRange> styles = new Vector<StyleRange>();
    int token;
    StyleRange lastStyle;

    if (inBlockComment(event.lineOffset, event.lineOffset + event.lineText.length())) {
      styles.addElement(
          new StyleRange(event.lineOffset, event.lineText.length() + 4, colors[2], null));
      event.styles = new StyleRange[styles.size()];
      styles.copyInto(event.styles);
      return;
    }
    scanner.setRange(event.lineText);
    String xs = ((StyledText) event.widget).getText();
    if (xs != null) parseBlockComments(xs);
    token = scanner.nextToken();
    while (token != EOF) {
      if (token == OTHER) {
        // do nothing
      } else if ((token == WHITE) && (!styles.isEmpty())) {
        int start = scanner.getStartOffset() + event.lineOffset;
        lastStyle = (StyleRange) styles.lastElement();
        if (lastStyle.fontStyle != SWT.NORMAL) {
          if (lastStyle.start + lastStyle.length == start) {
            // have the white space take on the style before it to minimize font style
            // changes
            lastStyle.length += scanner.getLength();
          }
        }
      } else {
        Color color = getColor(token);
        if (color != colors[0]) { // hardcoded default foreground color, black
          StyleRange style =
              new StyleRange(
                  scanner.getStartOffset() + event.lineOffset, scanner.getLength(), color, null);
          if (token == KEY) {
            style.fontStyle = SWT.BOLD;
          }
          if (styles.isEmpty()) {
            styles.addElement(style);
          } else {
            lastStyle = (StyleRange) styles.lastElement();
            if (lastStyle.similarTo(style) && (lastStyle.start + lastStyle.length == style.start)) {
              lastStyle.length += style.length;
            } else {
              styles.addElement(style);
            }
          }
        }
      }
      token = scanner.nextToken();
    }
    event.styles = new StyleRange[styles.size()];
    styles.copyInto(event.styles);
  }
示例#3
0
  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;
      }
    }
  }
示例#4
0
 public static StyleRange copy(final StyleRange range) {
   final StyleRange result = new StyleRange(range);
   result.start = range.start;
   result.length = range.length;
   result.fontStyle = range.fontStyle;
   return result;
 }
 private StyleRange createStyleRange(int start, int end, Styler style) {
   StyleRange styleRange = new StyleRange();
   styleRange.start = start;
   styleRange.length = end - start;
   style.applyStyles(styleRange);
   return styleRange;
 }
示例#6
0
 /**
  * Use hyperlink detectors to find a text viewer's hyperlinks and return the style ranges to
  * render them.
  *
  * @param textViewer
  * @param hyperlinkDetectors
  * @return the style ranges to render the detected hyperlinks
  */
 public static StyleRange[] getHyperlinkDetectorStyleRanges(
     ITextViewer textViewer, IHyperlinkDetector[] hyperlinkDetectors) {
   List<StyleRange> styleRangeList = new ArrayList<StyleRange>();
   if (hyperlinkDetectors != null && hyperlinkDetectors.length > 0) {
     for (int i = 0; i < textViewer.getTextWidget().getText().length(); i++) {
       IRegion region = new Region(i, 0);
       for (IHyperlinkDetector hyperLinkDetector : hyperlinkDetectors) {
         IHyperlink[] hyperlinks = hyperLinkDetector.detectHyperlinks(textViewer, region, true);
         if (hyperlinks != null) {
           for (IHyperlink hyperlink : hyperlinks) {
             StyleRange hyperlinkStyleRange =
                 new StyleRange(
                     hyperlink.getHyperlinkRegion().getOffset(),
                     hyperlink.getHyperlinkRegion().getLength(),
                     Display.getDefault().getSystemColor(SWT.COLOR_BLUE),
                     Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
             hyperlinkStyleRange.underline = true;
             styleRangeList.add(hyperlinkStyleRange);
           }
         }
       }
     }
   }
   StyleRange[] styleRangeArray = new StyleRange[styleRangeList.size()];
   styleRangeList.toArray(styleRangeArray);
   return styleRangeArray;
 }
  private void formatIpAddress(int start, int end) {
    StyleRange style = new StyleRange();
    style.start = start;
    style.length = end - start;
    style.font = new Font(sShell.getDisplay(), "Courier New", 9, SWT.BOLD);
    style.foreground = new Color(sShell.getDisplay(), 255, 127, 0); // Orange	

    activeHostsFileStyledText.setStyleRange(style);
  }
  private void formatRemark(int start, int end) {
    StyleRange style = new StyleRange();
    style.start = start;
    style.length = end - start;
    style.font = new Font(sShell.getDisplay(), "Courier New", 9, SWT.NORMAL);
    style.foreground = new Color(sShell.getDisplay(), 0, 128, 0); // Green	

    activeHostsFileStyledText.setStyleRange(style);
  }
  private void formatHostName(int start, int end) {
    StyleRange style = new StyleRange();
    style.start = start;
    style.length = end - start;
    style.font = new Font(sShell.getDisplay(), "Courier New", 9, SWT.BOLD);
    style.foreground = sShell.getDisplay().getSystemColor(SWT.COLOR_BLUE);

    activeHostsFileStyledText.setStyleRange(style);
  }
 /**
  * {@inheritDoc}
  *
  * <p>Performance optimization: since we know at this place that none of the clients expects the
  * given range to be untouched we reuse the given range as return value.
  */
 @Override
 protected StyleRange modelStyleRange2WidgetStyleRange(StyleRange range) {
   IRegion region = modelRange2WidgetRange(new Region(range.start, range.length));
   if (region != null) {
     // don't clone the style range, but simply reuse it.
     range.start = region.getOffset();
     range.length = region.getLength();
     return range;
   }
   return null;
 }
示例#11
0
 private void updateInput() {
   if (this.infoText == null || !this.inputChanged) {
     return;
   }
   if (this.labelProvider == null) {
     this.labelProvider =
         new RLabelProvider(
             RLabelProvider.LONG | RLabelProvider.HEADER | RLabelProvider.NAMESPACE);
   }
   if (this.input != null) {
     final Image image = this.labelProvider.getImage(this.input.getElement());
     this.titleImage.setImage(
         (image != null)
             ? image
             : SharedUIResources.getImages().get(SharedUIResources.PLACEHOLDER_IMAGE_ID));
     final StyledString styleString =
         this.labelProvider.getStyledText(
             this.input.getElement(), this.input.getElementName(), this.input.getElementAttr());
     if (this.input.isElementOfActiveBinding()) {
       styleString.append(" (active binding)", StyledString.QUALIFIER_STYLER);
     }
     this.titleText.setText(styleString.getString());
     this.titleText.setStyleRanges(styleString.getStyleRanges());
     if (this.input.hasDetail()) {
       this.infoText.setText(
           this.input.getDetailTitle()
               + '\n'
               + ((this.input.getDetailInfo() != null)
                   ? this.input.getDetailInfo()
                   : "")); //$NON-NLS-1$
       final StyleRange title =
           new StyleRange(0, this.input.getDetailTitle().length(), null, null);
       title.underline = true;
       this.infoText.setStyleRange(title);
     } else {
       this.infoText.setText(""); // $NON-NLS-1$
     }
   } else {
     this.titleImage.setImage(
         SharedUIResources.getImages().get(SharedUIResources.PLACEHOLDER_IMAGE_ID));
     this.titleText.setText(""); // $NON-NLS-1$
     this.infoText.setText(""); // $NON-NLS-1$
   }
   if ((this.mode & MODE_FOCUS) != 0) {
     getToolBarManager().update(true);
   } else {
     setStatusText(
         (this.input.getControl() != null && this.input.getControl().isFocusControl())
             ? InformationDispatchHandler.getTooltipAffordanceString()
             : ""); //$NON-NLS-1$
   }
   this.inputChanged = false;
 }
示例#12
0
  private void drawCurrentLine(LineBackgroundEvent event, final IRegion lineRegion) {
    final StyledText textWidget = fViewer.getTextWidget();
    final int offset = event.lineOffset;
    final RGBa lineHighlight = getCurrentTheme().getLineHighlight();
    event.lineBackground = getColorManager().getColor(lineHighlight.toRGB());

    // In this case, we should be overriding the bg of the style ranges for the line too!
    if (textWidget.isDisposed()) {
      return;
    }
    // FIXME Only change bg colors of visible ranges!
    int replaceLength = 160;
    if (lineRegion != null) {
      replaceLength = Math.min(replaceLength, lineRegion.getLength());
    }

    // be safe about offsets
    int charCount = textWidget.getCharCount();
    if (offset + replaceLength > charCount) {
      replaceLength = charCount - offset;
      if (replaceLength < 0) {
        // Just playing safe here
        replaceLength = 0;
      }
    }
    final StyleRange[] ranges = textWidget.getStyleRanges(offset, replaceLength, true);
    if (ranges == null || ranges.length == 0) {
      return;
    }
    Color background = textWidget.getBackground();
    final int[] positions = new int[ranges.length << 1];
    int x = 0;
    boolean apply = false;
    for (StyleRange range : ranges) {
      if (range.background != null) {
        if (!range.background.equals(background)) {
          positions[x] = range.start;
          positions[x + 1] = range.length;
          x += 2;
          continue;
        }
        apply = true;
      }
      range.background = null;
      positions[x] = range.start;
      positions[x + 1] = range.length;
      x += 2;
    }

    if (apply) {
      textWidget.setStyleRanges(offset, replaceLength, positions, ranges);
    }
  }
 /**
  * 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);
   }
 }
示例#14
0
 public static void lightenStyledTextColors(StyledText st, double pct) {
   StyleRange[] srs = st.getStyleRanges();
   Color defaultFGColor = CCWPlugin.getColor(RGBUtil.lighten(st.getForeground().getRGB(), pct));
   for (int i = 0; i < srs.length; i++) {
     StyleRange oldSR = srs[i];
     StyleRange newSR = newStyleRange(oldSR);
     Color lightForeground =
         (oldSR.foreground == null)
             ? defaultFGColor
             : CCWPlugin.getColor(RGBUtil.lighten(oldSR.foreground.getRGB(), pct));
     newSR.foreground = lightForeground;
     st.setStyleRange(newSR);
   }
   st.setForeground(defaultFGColor);
 }
示例#15
0
 private org.eclipse.swt.custom.StyleRange getStyleRangeAtPosition(
     org.eclipse.jface.text.Position position) {
   org.eclipse.swt.custom.StyleRange styleRange = null;
   try {
     styleRange = textWidget.getStyleRangeAtOffset(position.offset);
   } catch (IllegalArgumentException iae) {
   }
   if (styleRange == null) {
     styleRange =
         new org.eclipse.swt.custom.StyleRange(position.offset, position.length, black, null);
   } else {
     styleRange.length = position.length;
   }
   return styleRange;
 }
 public void run() {
   style = editor.getStyleRange();
   editor.fontData.setHeight(10);
   style.font = new Font(editor.getShell().getDisplay(), editor.fontData);
   editor.setStyleRange(style);
   editor.getStyledText().setFont(style.font);
 }
 private boolean similarRange(final StyleRange inStyleRange) {
   if (styleRange == null) {
     if (inStyleRange == null) return true;
     return false;
   }
   return styleRange.similarTo(inStyleRange);
 }
示例#18
0
  /**
   * Create the composite.
   *
   * @param parent
   * @param style
   */
  public FeatureOffset(Composite parent, int style) {
    super(parent, style);
    setLayout(new GridLayout(3, true));

    stylerange = new StyleRange();
    stylerange.background = AndrospyMain.shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);

    txtSrch = new Text(this, SWT.BORDER);
    txtSrch.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    Button btnSearch = new Button(this, SWT.NONE);
    btnSearch.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent arg0) {
            String srch = txtSrch.getText();
            String fulltext = text.getText();
            if (srch != "" && fulltext != "") {
              find(srch, fulltext);
            }
          }
        });
    GridData gd_btnSearch = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
    gd_btnSearch.widthHint = 104;
    btnSearch.setLayoutData(gd_btnSearch);
    btnSearch.setText("lk.score.androphsy.main.Search");

    text = new StyledText(this, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1);
    gd_text.heightHint = 230;
    text.setLayoutData(gd_text);

    lblOffset = new Label(this, SWT.NONE);
    lblOffset.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblOffset.setText("Offset:");

    txtOffset = new Text(this, SWT.BORDER);
    txtOffset.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    btnHexView = new Button(this, SWT.NONE);
    btnHexView.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent arg0) {
            if (txtOffset.getText().equals("")) {
              MessageBox msbox = new MessageBox(getShell(), SWT.ICON_ERROR);
              msbox.setMessage("ERROR - Hex View");
              msbox.setText("Offset value cannot be null");
              msbox.open();
              return;
            }
            hv.getHexdump(txtOffset.getText());
          }
        });
    btnHexView.setText("Hex View");

    hv = new HexViewer(this, SWT.NONE);
    hv.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
  }
 @Override
 public String toString() {
   final StringBuilder out = new StringBuilder();
   out.append(styleRange == null ? "Style: null" : styleRange.toString()); // $NON-NLS-1$
   out.append("; Bullet: "); // $NON-NLS-1$
   out.append(bullet == null ? "null" : bullet.toString()); // $NON-NLS-1$
   return out.toString();
 }
 private void updateInput() {
   if (fInfoText == null || !fInputChanged) {
     return;
   }
   if (fLabelProvider == null) {
     fLabelProvider =
         new RLabelProvider(
             RLabelProvider.LONG | RLabelProvider.HEADER | RLabelProvider.NAMESPACE);
   }
   if (fInput != null) {
     final Image image = fLabelProvider.getImage(fInput.element);
     fTitleImage.setImage(
         (image != null)
             ? image
             : SharedUIResources.getImages().get(SharedUIResources.PLACEHOLDER_IMAGE_ID));
     final StyledString styleString =
         fLabelProvider.getStyledText(
             fInput.element, fInput.element.getElementName(), fInput.elementAttr);
     fTitleText.setText(styleString.getString());
     fTitleText.setStyleRanges(styleString.getStyleRanges());
     if (fInput.detailTitle != null) {
       fInfoText.setText(
           fInput.detailTitle
               + '\n'
               + ((fInput.detailInfo != null) ? fInput.detailInfo : "")); // $NON-NLS-1$
       final StyleRange title = new StyleRange(0, fInput.detailTitle.length(), null, null);
       title.underline = true;
       fInfoText.setStyleRange(title);
     } else {
       fInfoText.setText(""); // $NON-NLS-1$
     }
   } else {
     fTitleImage.setImage(
         SharedUIResources.getImages().get(SharedUIResources.PLACEHOLDER_IMAGE_ID));
     fTitleText.setText(""); // $NON-NLS-1$
     fInfoText.setText(""); // $NON-NLS-1$
   }
   if (fMode == MODE_SIMPLE) {
     setStatusText(
         (fInput.control != null && fInput.control.isFocusControl())
             ? InformationDispatchHandler.getTooltipAffordanceString()
             : ""); //$NON-NLS-1$
   }
   fInputChanged = false;
 }
示例#21
0
  public IToken nextToken1() {
    int current = lastOffset + lastLength;
    if (current >= offset + length) {
      return Token.EOF;
    }

    int next = offset + length;
    Hyperlink h = getHyperlinkAt(current);
    if (h != null) {
      if (current >= h.start) {
        next = Math.min(next, h.start + h.length);
      } else {
        next = Math.min(next, h.start);
      }
    }

    StyleRange style = getTextStyleAt(current);
    if (style != null) {
      if (current >= style.start) {
        next = Math.min(next, style.start + style.length);
      } else {
        next = Math.min(next, style.start);
      }
    }

    this.lastOffset = current;
    this.lastLength = next - current;
    if (lastLength <= 0) {
      return Token.EOF;
    }

    if (!(style != null && current >= style.start && next <= style.start + style.length)) {
      style = null;
    }

    if (h != null && current >= h.start && next <= h.start + h.length) {
      if (style == null) style = RichTextUtils.DEFAULT_STYLE;
      style = (StyleRange) style.clone();
      if (style.foreground == null) {
        style.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
      }
      style.underline = true;
    }
    return new Token(style);
  }
示例#22
0
 private void removeHighlightingCategory(
     org.eclipse.jface.text.IDocument document, String category) {
   org.eclipse.jface.text.Position[] positions = positionHelper.getPositions(document, category);
   if (category.equals(
       bento.language.bentocomp.resource.bento.ui.BentoPositionCategory.BRACKET.toString())) {
     org.eclipse.swt.custom.StyleRange styleRange;
     for (org.eclipse.jface.text.Position position : positions) {
       org.eclipse.jface.text.Position tmpPosition = convertToWidgetPosition(position);
       if (tmpPosition != null) {
         styleRange = getStyleRangeAtPosition(tmpPosition);
         styleRange.borderStyle = org.eclipse.swt.SWT.NONE;
         styleRange.borderColor = null;
         styleRange.background = null;
         textWidget.setStyleRange(styleRange);
       }
     }
   }
   positionHelper.removePositions(document, category);
 }
    /** @return Returns a corresponding style range. */
    public StyleRange createStyleRange() {
      int len = 0;
      if (fStyle.isEnabled()) len = getLength();

      TextAttribute textAttribute = fStyle.getTextAttribute();
      int style = textAttribute.getStyle();
      int fontStyle = style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
      StyleRange styleRange =
          new StyleRange(
              getOffset(),
              len,
              textAttribute.getForeground(),
              textAttribute.getBackground(),
              fontStyle);
      styleRange.strikeout = (style & TextAttribute.STRIKETHROUGH) != 0;
      styleRange.underline = (style & TextAttribute.UNDERLINE) != 0;

      return styleRange;
    }
示例#24
0
  private void setBracketHighlighting(org.eclipse.jface.text.IDocument document) {
    org.eclipse.swt.custom.StyleRange styleRange = null;
    org.eclipse.jface.text.Position[] positions =
        positionHelper.getPositions(
            document,
            bento.language.bentocomp.resource.bento.ui.BentoPositionCategory.BRACKET.toString());

    for (org.eclipse.jface.text.Position position : positions) {
      org.eclipse.jface.text.Position tmpPosition = convertToWidgetPosition(position);
      if (tmpPosition != null) {
        styleRange = getStyleRangeAtPosition(tmpPosition);
        styleRange.borderStyle = org.eclipse.swt.SWT.BORDER_SOLID;
        styleRange.borderColor = bracketColor;
        if (styleRange.foreground == null) {
          styleRange.foreground = black;
        }
        textWidget.setStyleRange(styleRange);
      }
    }
  }
示例#25
0
  /**
   * Inputs in the view the selected PO whose pretty code is recorded in the Goal class
   *
   * @throws IOException
   */
  public void inputPO() throws IOException {

    // we want to see a context, not the goal
    if (!POMode) {
      return;
    }

    if (ttext > 0) // if we've a title text
    setPartName(IConstants.PO_VIEW_TITLE + " : po " + ttext); // put it in the view
    else setPartName(IConstants.PO_VIEW_TITLE); // else, put the view title only

    // gets the PO pretty text
    String container = "";
    container = Goal.getGoal();

    // if the Goal is empty, we can return
    if (container.equals("")) {
      text.setText(container);
      return;
    }

    // get the style ranges
    Vector<StyleRange> range = Goal.getStyleRanges();
    int w = range.size() - 1;
    // keep the ranges which can be applied in the text area
    while (((StyleRange) range.get(w)).start >= container.length()) {
      w--;
    }

    // put the bar between the goal and the arrow result excerpted from this same goal
    container += "\n_______________________________________________\n\n";
    container += Goal.getResult();
    text.setText(container);

    // apply style ranges to the first part of the goal (before the bar)
    for (int e = 0; e <= w; e++) {
      text.setStyleRange((StyleRange) range.get(e));
    }

    // puts a style range to color the bar in black
    StyleRange srg = new StyleRange();
    srg.start += Goal.getGoal().length();
    srg.length = 50;
    srg.foreground = new Color(null, 0, 0, 0);
    text.setStyleRange(srg);

    // puts a style range to color the end of the goal in blue
    srg = new StyleRange();
    srg.start += Goal.getGoal().length() + 50;
    srg.length = Goal.getResult().length();
    srg.foreground = new Color(null, 0, 0, 255);
    text.setStyleRange(srg);

    // sets the cursor at the end of the code
    text.setSelection(text.getText().length(), text.getText().length());
  }
示例#26
0
    @Override
    public void run() {
      try {
        String query = queryString.getSelectionText();
        Point queryRange = queryString.getSelectionRange();

        if ("".equals(query)) // $NON-NLS-1$
        {
          query = queryString.getText();
          queryRange = new Point(0, queryString.getCharCount());
        }

        try {
          // force parsing of OQL query
          SnapshotFactory.createQuery(query);
          new OQLJob(OQLPane.this, query, state).schedule();
        } catch (final OQLParseException e) {
          int start = findInText(query, e.getLine(), e.getColumn());

          StyleRange style2 = new StyleRange();
          style2.start = start + queryRange.x;
          style2.length = queryRange.y - start;
          style2.foreground = JFaceResources.getColorRegistry().get(JFacePreferences.ERROR_COLOR);
          style2.underline = true;
          style2.underlineStyle = SWT.UNDERLINE_SQUIGGLE;
          queryString.replaceStyleRanges(0, queryString.getCharCount(), new StyleRange[] {style2});

          createExceptionPane(e, query);
        } catch (Exception e) {
          createExceptionPane(e, query);
        }
      } catch (PartInitException e1) {
        ErrorHelper.logThrowableAndShowMessage(e1, Messages.OQLPane_ErrorExecutingQuery);
      }
    }
  private void updateStyle(ITextViewer viewer) {
    StyledText text = viewer.getTextWidget();
    int widgetOffset = getWidgetOffset(viewer, fRememberedStyleRange.start);
    StyleRange range = new StyleRange(fRememberedStyleRange);
    range.start = widgetOffset;
    range.length = fRememberedStyleRange.length;
    StyleRange currentRange = text.getStyleRangeAtOffset(widgetOffset);
    if (currentRange != null) {
      range.strikeout = currentRange.strikeout;
      range.underline = currentRange.underline;
      range.fontStyle = currentRange.fontStyle;
    }

    // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754
    try {
      text.setStyleRange(range);
    } catch (IllegalArgumentException x) {
      // catching exception as offset + length might be outside of the text widget
      fRememberedStyleRange = null;
    }
  }
 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setText("StyledText with underline and strike through");
   shell.setLayout(new FillLayout());
   StyledText text = new StyledText(shell, SWT.BORDER);
   text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
   // make 0123456789 appear underlined
   StyleRange style1 = new StyleRange();
   style1.start = 0;
   style1.length = 10;
   style1.underline = true;
   text.setStyleRange(style1);
   // make ABCDEFGHIJKLM have a strike through
   StyleRange style2 = new StyleRange();
   style2.start = 11;
   style2.length = 13;
   style2.strikeout = true;
   text.setStyleRange(style2);
   // make NOPQRSTUVWXYZ appear underlined and have a strike through
   StyleRange style3 = new StyleRange();
   style3.start = 25;
   style3.length = 13;
   style3.underline = true;
   style3.strikeout = true;
   text.setStyleRange(style3);
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) display.sleep();
   }
   display.dispose();
 }
示例#29
0
  /**
   * creates the style range of the StyledText for the range of the contained editor
   *
   * <p>XXX problem will occur if there is a newline in the position. Working on this! code folding.
   */
  private StyleRange[] createStyleRange(ContainedEditorManager c, Position p) {
    int offset = p.offset;
    int length = p.length;

    Rectangle rect = c.getControl().getBounds();
    int ascent = rect.height - 4;
    int descent = 4;

    // use two style ranges

    // first style range covers the entire size of the contained editor
    StyleRange first = new StyleRange();
    first.start = offset;
    first.length = Math.min(1, length);
    first.background = this.containingEditor.colorManager.getColor(new RGB(255, 255, 255));
    first.metrics =
        new GlyphMetrics(
            ascent + ContainingEditor.MARGIN,
            descent + ContainingEditor.MARGIN,
            rect.width + 2 * ContainingEditor.MARGIN);

    // this style range is hidden.  the height and width are 0
    StyleRange second = new StyleRange();
    second.start = offset + 1;
    second.length = length - 1;
    second.background = this.containingEditor.colorManager.getColor(new RGB(255, 255, 255));
    second.metrics = new GlyphMetrics(0, 0, 0);
    second.font = TINY_FONT;

    return new StyleRange[] {first, second};
  }
  private boolean prepareTwigRegions(
      Collection<StyleRange> holdResults,
      ITwigScriptRegion region,
      int regionStart,
      int partitionStartOffset,
      int partitionLength) {

    assert (region.getType() == TwigRegionContext.TWIG_CONTENT
        || region.getType() == TwigRegionContext.TWIG_COMMENT);

    StyleRange styleRange = null;
    TextAttribute attr;
    TextAttribute previousAttr = null;

    ITextRegion[] twigTokens = null;
    try {

      int from;
      int length;
      if (partitionStartOffset < regionStart) {
        from = 0;
        length = partitionLength - (regionStart - partitionStartOffset);
      } else {
        from = partitionStartOffset - regionStart;
        length = partitionLength;
      }
      twigTokens = region.getTwigTokens(from, Math.min(length, region.getLength()));
      ITextRegion prevElement = null;
      for (int i = 0; i < twigTokens.length; i++) {

        ITextRegion element = twigTokens[i];
        attr = getAttributeFor(element);
        // Check that the elements are different - otherwise the
        // coloring is not valid
        if (prevElement == element || attr == null) {
          continue;
        }
        if ((styleRange != null)
            && (previousAttr != null)
            && (previousAttr.equals(attr))
            && prevElement != null
            && prevElement.getLength() == prevElement.getLength()) {
          // extends the prev styleRange with the current element
          // length
          styleRange.length += element.getLength();
          if (styleRange.start + styleRange.length > partitionStartOffset + partitionLength) {
            styleRange.length -=
                (styleRange.start + styleRange.length) - (partitionStartOffset + partitionLength);
          }
        } else {
          // create new styleRange
          int styleStart = regionStart + element.getStart();
          int styleLength = element.getLength();
          if (styleStart + styleLength < partitionStartOffset) { // if
            // the
            // range
            // ends
            // before
            // the
            // requested
            // starting
            // position
            // -
            // ignoring
            // it
            continue;
          }
          if (styleStart < partitionStartOffset) { // if the region
            // starts before
            // the requested
            // starting
            // position -
            // adjusting the
            // style start
            // position
            styleLength -= (partitionStartOffset - styleStart);
            styleStart = partitionStartOffset;
          }
          if (styleStart > partitionStartOffset + partitionLength) {
            // if the region ends after the requested end position -
            // making it shorter
            styleLength -= styleStart - (partitionStartOffset + partitionLength);
          }
          if (attr.getBackground() != null && element.getTextEnd() != element.getEnd()) { // in
            // case
            // of
            // background
            // color
            // make
            // sure
            // the
            // highlighting
            // will
            // not
            // paint
            // the
            // whitespaces
            // applying style to the region w/o the whitespace
            styleRange =
                new StyleRange(
                    styleStart,
                    styleLength - (element.getEnd() - element.getTextEnd()),
                    attr.getForeground(),
                    attr.getBackground(),
                    attr.getStyle());
            if ((attr.getStyle() & TextAttribute.UNDERLINE) != 0) {
              styleRange.underline = true;
              styleRange.fontStyle &= ~TextAttribute.UNDERLINE;
            }
            if ((attr.getStyle() & TextAttribute.STRIKETHROUGH) != 0) {
              styleRange.strikeout = true;
              styleRange.fontStyle &= ~TextAttribute.STRIKETHROUGH;
            }
            holdResults.add(styleRange);
            // applying style to the whitespace (important for the
            // refresh of the specific range
            styleRange =
                new StyleRange(
                    regionStart + element.getTextEnd(),
                    element.getEnd() - element.getTextEnd(),
                    attr.getForeground(),
                    null,
                    attr.getStyle());
            holdResults.add(styleRange);
            previousAttr = null;
          } else {
            styleRange =
                new StyleRange(
                    styleStart,
                    styleLength,
                    attr.getForeground(),
                    attr.getBackground(),
                    attr.getStyle());
            if ((attr.getStyle() & TextAttribute.UNDERLINE) != 0) {
              styleRange.underline = true;
              styleRange.fontStyle &= ~TextAttribute.UNDERLINE;
            }
            if ((attr.getStyle() & TextAttribute.STRIKETHROUGH) != 0) {
              styleRange.strikeout = true;
              styleRange.fontStyle &= ~TextAttribute.STRIKETHROUGH;
            }
            holdResults.add(styleRange);
            // technically speaking, we don't need to update
            // previousAttr
            // in the other case, because the other case is when
            // it hasn't changed
            previousAttr = attr;
          }
        }
        prevElement = element;
      }
      return true;
    } catch (BadLocationException e) {
      Logger.logException(e);
      return false;
    }
  }