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(); }
/** * 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; }
@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 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; }
/** * 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); } }
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; }
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); }
/** @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; }
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; } }
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; } }
public IToken nextToken() { int current = lastOffset + lastLength; if (current >= offset + length) { return Token.EOF; } int next = offset + length; Hyperlink h = getHyperlinkAt(current); AutoHyperlink a = getAutoHyperlinkAt(current); int hyperNext = 0; if (h != null) { if (current >= h.start) { hyperNext = Math.min(next, h.start + h.length); } else { hyperNext = Math.min(next, h.start); } } int autoNext = 0; if (a != null) { if (current >= a.start) autoNext = Math.min(next, a.start + a.length); else autoNext = Math.min(next, a.start); } int tempStart = 0, tempLength = 0; if (h != null && a != null) { if (h.start < a.start) { next = hyperNext; tempStart = h.start; tempLength = h.length; } else { next = autoNext; tempStart = a.start; tempLength = a.length; } } else if (h != null && a == null) { next = hyperNext; tempStart = h.start; tempLength = h.length; } else if (a != null && h == null) { next = autoNext; tempStart = a.start; tempLength = a.length; } 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 (a != null && current >= a.start && next <= a.start + a.len) { if (h != null || a != null) { if (current >= tempStart && next <= tempStart + tempLength) { 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); }