Search in sources :

Example 36 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class DefaultHyperlinkPresenter method applyTextPresentation.

@Override
public void applyTextPresentation(TextPresentation textPresentation) {
    if (fActiveRegion == null)
        return;
    IRegion region = textPresentation.getExtent();
    if (fActiveRegion.getOffset() + fActiveRegion.getLength() >= region.getOffset() && region.getOffset() + region.getLength() > fActiveRegion.getOffset()) {
        Color color = null;
        if (!fIsUsingNativeLinkColor)
            color = fColor;
        StyleRange styleRange = new StyleRange(fActiveRegion.getOffset(), fActiveRegion.getLength(), color, null);
        styleRange.underlineStyle = SWT.UNDERLINE_LINK;
        styleRange.underline = true;
        textPresentation.mergeStyleRange(styleRange);
    }
}
Also used : Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) IRegion(org.eclipse.jface.text.IRegion)

Example 37 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class HTML2TextReaderTest method verify.

/**
 * @param input input
 * @param expectedOutput expected output
 * @param styleRangeCount count
 * @throws IOException test failure
 * @deprecated pass actual style ranges
 */
@Deprecated
private void verify(String input, String expectedOutput, int styleRangeCount) throws IOException {
    Reader reader = new StringReader(input);
    TextPresentation textPresentation = new TextPresentation();
    String result;
    try (HTML2TextReader htmlReader = new HTML2TextReader(reader, textPresentation)) {
        result = htmlReader.getString();
    }
    if (DEBUG)
        System.out.println("<" + result + "/>");
    assertEquals(expectedOutput, result);
    Iterator<StyleRange> styleRangeIterator = textPresentation.getAllStyleRangeIterator();
    List<StyleRange> ranges = new ArrayList<>();
    while (styleRangeIterator.hasNext()) {
        ranges.add(styleRangeIterator.next());
    }
    assertEquals("Incorrect number of style ranges", styleRangeCount, ranges.size());
    Collections.sort(ranges, (r1, r2) -> r1.start - r2.start);
    for (int i = 0; i < ranges.size() - 1; i++) {
        StyleRange range1 = ranges.get(i);
        StyleRange range2 = ranges.get(i + 1);
        if (range1.start + range1.length > range2.start) {
            assertTrue("StyleRanges overlap", false);
        }
    }
}
Also used : HTML2TextReader(org.eclipse.jface.internal.text.html.HTML2TextReader) StyleRange(org.eclipse.swt.custom.StyleRange) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) HTML2TextReader(org.eclipse.jface.internal.text.html.HTML2TextReader) TextPresentation(org.eclipse.jface.text.TextPresentation)

Example 38 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class HTML2TextReaderTest method testStylesWithPre.

@Test
public void testStylesWithPre() throws IOException {
    String string = "I am <b>bold</b>." + LD + "<p>" + LD + "<pre>One" + LD + LD + "<b>T</b>hree.</pre>" + LD + "<p>" + LD + "<b>Author:</b> me.";
    String expected = "I am bold. " + LD + "One" + LD + LD + "Three. " + LD + "Author: me.";
    StyleRange[] ranges = { new StyleRange(5, 4, null, null, SWT.BOLD), new StyleRange(14 + 3 * LD.length(), 1, null, null, SWT.BOLD), new StyleRange(21 + 4 * LD.length(), 7, null, null, SWT.BOLD) };
    verify(string, expected, ranges);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) Test(org.junit.Test)

Example 39 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class HTML2TextReaderTest method testCombineBoldItalicStriker.

@Test
public void testCombineBoldItalicStriker() throws IOException {
    String string = "<strong>strong</strong>,<em>italic</em>,<s>strike</s>," + "<strong><em>strongItalic</em></strong>,<s><strong>strongStrike</strong></s>," + "<em><s>italicStrike</s></em>," + "<em><strong><s>strongItalicStrike</s></strong></em>," + "none";
    String expected = "strong,italic,strike,strongItalic,strongStrike,italicStrike,strongItalicStrike,none";
    StyleRange[] ranges = { new StyleRange(0, 6, null, null, SWT.BOLD), new StyleRange(7, 6, null, null, SWT.ITALIC), strike(new StyleRange(14, 6, null, null)), new StyleRange(21, 12, null, null, SWT.BOLD | SWT.ITALIC), strike(new StyleRange(34, 12, null, null, SWT.BOLD)), strike(new StyleRange(47, 12, null, null, SWT.ITALIC)), strike(new StyleRange(60, 18, null, null, SWT.BOLD | SWT.ITALIC)) };
    verify(string, expected, ranges);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) Test(org.junit.Test)

Example 40 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class TextPresentationTest method testMergeStyleRange5.

/**
 * Merge range at end of existing default range.
 */
@Test
public void testMergeStyleRange5() {
    StyleRange range = createStyleRange(45, 47, 1, -1, NORMAL);
    fTextPresentation.mergeStyleRange(range);
    StyleRange[] expectedAllRanges = new StyleRange[] { createStyleRange(0, 4, NORMAL), createStyleRange(4, 20, BOLD), createStyleRange(20, 45, NORMAL), createStyleRange(45, 47, 1, -1, NORMAL), createStyleRange(47, 54, BOLD), createStyleRange(54, 96, NORMAL), createStyleRange(96, 102, BOLD), createStyleRange(102, 140, NORMAL) };
    StyleRange[] expectedNonDefaultRanges = new StyleRange[] { createStyleRange(4, 20, BOLD), createStyleRange(45, 47, 1, -1, NORMAL), createStyleRange(47, 54, BOLD), createStyleRange(96, 102, BOLD) };
    checkRegions(expectedAllRanges, expectedNonDefaultRanges);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) Test(org.junit.Test)

Aggregations

StyleRange (org.eclipse.swt.custom.StyleRange)145 Point (org.eclipse.swt.graphics.Point)52 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)25 Color (org.eclipse.swt.graphics.Color)22 StyledText (org.eclipse.swt.custom.StyledText)13 Font (org.eclipse.swt.graphics.Font)10 GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)10 Matcher (java.util.regex.Matcher)9 FontData (org.eclipse.swt.graphics.FontData)8 RGB (org.eclipse.swt.graphics.RGB)8 Rectangle (org.eclipse.swt.graphics.Rectangle)7 IOException (java.io.IOException)5 TextPresentation (org.eclipse.jface.text.TextPresentation)5 Image (org.eclipse.swt.graphics.Image)5 Control (org.eclipse.swt.widgets.Control)5 Position (org.eclipse.jface.text.Position)4 TextStyle (org.eclipse.swt.graphics.TextStyle)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3