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(); }
/** * 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); } }
/** @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; } }