use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_custom_StyledText method rtfCopy.
/**
* Regression test for bug 19985
*/
protected void rtfCopy() {
String lines = "Line0\nLine1\nLine2\nLine3\nLine4\nLine5";
final int[] linesCalled = new int[] { 0 };
LineStyleListener listener = event -> {
Display display = Display.getDefault();
Color red = display.getSystemColor(SWT.COLOR_RED);
StyledText styledText = (StyledText) event.widget;
int lineIndex = styledText.getLineAtOffset(event.lineOffset);
int lineStart = event.lineOffset;
int lineEnd = lineStart + event.lineText.length();
StyleRange goodRange = new StyleRange(0, 1, red, red);
event.styles = new StyleRange[2];
switch(lineIndex % 6) {
case 0:
event.styles[0] = goodRange;
event.styles[1] = new StyleRange(lineEnd, 1, red, red);
linesCalled[0]++;
break;
case 1:
event.styles[0] = goodRange;
event.styles[1] = new StyleRange(lineEnd, -1, red, red);
linesCalled[0]++;
break;
case 2:
event.styles[0] = goodRange;
event.styles[1] = new StyleRange(lineEnd - 1, -1, red, red);
linesCalled[0]++;
break;
case 3:
event.styles[0] = goodRange;
event.styles[1] = new StyleRange(lineStart, -1, red, red);
linesCalled[0]++;
break;
case 4:
event.styles[0] = new StyleRange(lineStart, 1, red, red);
event.styles[1] = new StyleRange(lineStart, -1, red, red);
linesCalled[0]++;
break;
case 5:
event.styles[0] = new StyleRange(lineEnd / 2, 1, red, red);
event.styles[1] = new StyleRange(lineEnd / 2, -1, red, red);
linesCalled[0]++;
break;
}
};
text.setText(lines);
// cause StyledText to call the listener.
text.setSelection(0, text.getCharCount());
text.addLineStyleListener(listener);
linesCalled[0] = 0;
text.copy();
assertTrue("not all lines tested for RTF copy", linesCalled[0] == text.getLineCount());
Clipboard clipboard = new Clipboard(text.getDisplay());
RTFTransfer rtfTranfer = RTFTransfer.getInstance();
String clipboardText = (String) clipboard.getContents(rtfTranfer);
assertTrue("RTF copy failed", clipboardText.length() > 0);
clipboard.dispose();
text.removeLineStyleListener(listener);
}
use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_custom_StyledText method test_getRanges.
@Test
public void test_getRanges() {
StyleRange style0 = new StyleRange();
style0.rise = 10;
StyleRange style1 = new StyleRange();
style1.rise = 5;
StyleRange style2 = new StyleRange();
style2.rise = 30;
StyleRange[] expectedStyles;
int[] expectedRanges;
// tests using the new API
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
expectedStyles = new StyleRange[] { style0 };
expectedRanges = new int[] { 0, 10 };
testStyles("Test 1", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
expectedStyles = new StyleRange[] { style1 };
expectedRanges = new int[] { 3, 4 };
testStyles("Test 2", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
expectedStyles = new StyleRange[] { style0, style1, style0 };
expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
testStyles("Test 3", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 1, 4 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style2, style1, style0 };
expectedRanges = new int[] { 0, 1, 1, 4, 5, 2, 7, 3 };
testStyles("Test 4", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 1, 8 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style2, style0 };
expectedRanges = new int[] { 0, 1, 1, 8, 9, 1 };
testStyles("Test 5", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 0, 5 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style2, style1, style0 };
expectedRanges = new int[] { 0, 5, 5, 2, 7, 3 };
testStyles("Test 6", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 1, 6 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style2, style0 };
expectedRanges = new int[] { 0, 1, 1, 6, 7, 3 };
testStyles("Test 7", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style2, style0 };
expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
testStyles("Test 8", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 0, 3 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style2, style1, style0 };
expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
testStyles("Test 9", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 7, 3 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style1, style2 };
expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
testStyles("Test 10", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 4, 2 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style1, style2, style1, style0 };
expectedRanges = new int[] { 0, 3, 3, 1, 4, 2, 6, 1, 7, 3 };
testStyles("Test 11", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 2, 6 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style2, style0 };
expectedRanges = new int[] { 0, 2, 2, 6, 8, 2 };
testStyles("Test 12", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style1 });
expectedStyles = new StyleRange[] { style1 };
expectedRanges = new int[] { 0, 10 };
testStyles("Test 13", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789AB");
text.setStyleRanges(0, 0, new int[] { 1, 3 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 5, 1 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 7, 1 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 9, 2 }, new StyleRange[] { style1 });
expectedStyles = new StyleRange[] { style0, style1, style0, style1 };
expectedRanges = new int[] { 1, 3, 5, 1, 7, 1, 9, 2 };
testStyles("Test 14", text.getRanges(0, 12), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789AB");
text.setStyleRanges(0, 0, new int[] { 1, 3 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 5, 1 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 7, 1 }, new StyleRange[] { style0 });
text.setStyleRanges(0, 0, new int[] { 9, 2 }, new StyleRange[] { style1 });
text.setStyleRanges(0, 0, new int[] { 2, 8 }, new StyleRange[] { style2 });
expectedStyles = new StyleRange[] { style0, style2, style1 };
expectedRanges = new int[] { 1, 1, 2, 8, 10, 1 };
testStyles("Test 15", text.getRanges(0, 12), expectedRanges, text.getStyleRanges(false), expectedStyles);
// tests mixing the old API and the new API
text.setText("");
text.setText("0123456789");
text.setStyleRanges(new StyleRange[] { new StyleRange(3, 4, null, null, SWT.BOLD) });
expectedStyles = new StyleRange[] { new StyleRange(3, 4, null, null, SWT.BOLD) };
expectedRanges = new int[] { 3, 4 };
testStyles("Test 16", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
// test the merging code
text.setText("");
text.setText("0123456789");
text.setStyleRanges(new int[] { 1, 3, 6, 3 }, new StyleRange[] { style0, style0 });
text.setStyleRanges(0, 0, new int[] { 4, 2 }, new StyleRange[] { style0 });
expectedStyles = new StyleRange[] { style0 };
expectedRanges = new int[] { 1, 8 };
testStyles("Test 17", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(new int[] { 1, 3, 6, 3 }, new StyleRange[] { style0, style0 });
text.setStyleRanges(0, 0, new int[] { 2, 6 }, new StyleRange[] { style0 });
expectedStyles = new StyleRange[] { style0 };
expectedRanges = new int[] { 1, 8 };
testStyles("Test 18", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
text.setText("");
text.setText("0123456789");
text.setStyleRanges(new int[] { 1, 3, 6, 3 }, new StyleRange[] { style0, style0 });
text.setStyleRanges(3, 4, new int[] { 0, 1, 9, 1 }, new StyleRange[] { style0, style0 });
expectedStyles = new StyleRange[] { style0, style0 };
expectedRanges = new int[] { 0, 3, 7, 3 };
testStyles("Test 19", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
// tests mixing the old API and the new API
text.setText("");
text.setText("0123456789");
text.setStyleRanges(new StyleRange[] { new StyleRange(3, 4, null, null, SWT.BOLD) });
expectedStyles = new StyleRange[] {};
expectedRanges = new int[] {};
testStyles("Test 20", text.getRanges(3, 0), expectedRanges, text.getStyleRanges(3, 0, false), expectedStyles);
// bug 250859 (getRanges)
text.setText("");
text.setText("The Eclipse Foundation is currently going through the exercise");
text.setStyleRanges(new int[] { 12, 10, 36, 5 }, new StyleRange[] { style0, style1 });
expectedStyles = new StyleRange[] { style0 };
expectedRanges = new int[] { 12, 10 };
testStyles("Test 21 (bug 250859)", text.getRanges(12, 10), expectedRanges, text.getStyleRanges(12, 10, false), expectedStyles);
// bug 250193
text.setText("");
text.setText("The Eclipse Foundation is currently going through the exercise");
StyleRange sr1 = new StyleRange();
sr1.underline = true;
StyleRange sr2 = new StyleRange();
sr2.strikeout = true;
sr1.start = 12;
sr1.length = 10;
sr2.start = 36;
sr2.length = 5;
text.setStyleRange(sr1);
text.setStyleRange(sr2);
text.replaceTextRange(12, 10, "");
expectedStyles = new StyleRange[] { sr2 };
expectedRanges = new int[] { 26, 5 };
testStyles("Test 22 (bug 250193)", text.getRanges(26, 5), expectedRanges, text.getStyleRanges(26, 5, false), expectedStyles);
// bug 212851 (getStyleRanges)
text.setText("");
text.setText("line0\nline1\nline2");
text.setStyleRanges(new int[] { 0, 2, 2, 2, 4, 4, 13, 3 }, new StyleRange[] { style0, style1, style2, style0 });
expectedRanges = new int[] { 6, 2 };
expectedStyles = new StyleRange[] { style2 };
testStyles("Test 23 (bug 212851 - getRanges)", text.getRanges(6, 6), expectedRanges, text.getStyleRanges(6, 6, false), expectedStyles);
StyleRange[] styles = text.getStyleRanges(6, 6, true);
int[] ranges = new int[styles.length * 2];
for (int i = 0; i < ranges.length; i += 2) {
ranges[i] = styles[i / 2].start;
ranges[i + 1] = styles[i / 2].length;
}
testStyles("Test 24 (bug 212851 - getStyleRanges)", ranges, expectedRanges, text.getStyleRanges(6, 6, false), expectedStyles);
expectedRanges = new int[] { 6, 2, 13, 1 };
expectedStyles = new StyleRange[] { style2, style0 };
testStyles("Test 25 ", text.getRanges(6, 8), expectedRanges, text.getStyleRanges(6, 8, false), expectedStyles);
styles = text.getStyleRanges(6, 8, true);
ranges = new int[styles.length * 2];
for (int i = 0; i < ranges.length; i += 2) {
ranges[i] = styles[i / 2].start;
ranges[i + 1] = styles[i / 2].length;
}
testStyles("Test 26 ", ranges, expectedRanges, text.getStyleRanges(6, 8, false), expectedStyles);
}
use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight method testFont.
@Test
public void testFont() {
styledText.setText("a\nb\nc\nd");
// Create little, big font
Font initialFont = styledText.getFont();
FontData[] fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(24);
}
Font bigFont = new Font(styledText.getDisplay(), fontData);
fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(fontData[i].getHeight() - 1);
}
Font littleFont = new Font(styledText.getDisplay(), fontData);
int length = styledText.getCharCount();
// No style
assertVariableLineHeightEquals(0, 0);
// style with big font
StyleRange style = new StyleRange();
style.start = 0;
style.length = length;
style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
style.font = bigFont;
styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
assertVariableLineHeightEquals(0, 0, bigFont);
// style with no font
style = new StyleRange();
style.start = 0;
style.length = length;
style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
style.font = null;
styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
assertVariableLineHeightEquals(0, 0);
// style with big font
style = new StyleRange();
style.start = 0;
style.length = length;
style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
style.font = bigFont;
styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
assertVariableLineHeightEquals(0, 0, bigFont);
// style with little font
style = new StyleRange();
style.start = 0;
style.length = length;
style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
style.font = littleFont;
styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
assertVariableLineHeightEquals(0, 0);
// style with no font
style = new StyleRange();
style.start = 0;
style.length = length;
style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
style.font = null;
styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
assertVariableLineHeightEquals(0, 0);
// style with little font
style = new StyleRange();
style.start = 0;
style.length = length;
style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
style.font = littleFont;
styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
assertVariableLineHeightEquals(0, 0);
bigFont.dispose();
littleFont.dispose();
}
use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight method testBigFontSeveralRanges.
@Test
public void testBigFontSeveralRanges() {
styledText.setText("abc\nd");
// Create little, big font
Font initialFont = styledText.getFont();
FontData[] fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(fontData[i].getHeight() * 2);
}
Font bigFont = new Font(styledText.getDisplay(), fontData);
fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(fontData[i].getHeight() / 2);
}
Font littleFont = new Font(styledText.getDisplay(), fontData);
StyleRange[] ranges = new StyleRange[2];
ranges[0] = new StyleRange();
ranges[0].start = 0;
ranges[0].length = 1;
ranges[0].font = bigFont;
ranges[1] = new StyleRange();
ranges[1].start = 1;
ranges[1].length = 1;
ranges[1].font = littleFont;
styledText.replaceStyleRanges(0, 3, ranges);
// line height is the height of the big font
assertVariableLineHeightEquals(0, 0, bigFont);
bigFont.dispose();
littleFont.dispose();
}
use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight method testLittleFontSeveralRanges.
@Test
public void testLittleFontSeveralRanges() {
styledText.setText("abc\nd");
// Create little, big font
Font initialFont = styledText.getFont();
FontData[] fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(fontData[i].getHeight() / 2);
}
Font littleFont1 = new Font(styledText.getDisplay(), fontData);
fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(fontData[i].getHeight() / 4);
}
Font littleFont2 = new Font(styledText.getDisplay(), fontData);
StyleRange[] ranges = new StyleRange[2];
ranges[0] = new StyleRange();
ranges[0].start = 0;
ranges[0].length = 1;
ranges[0].font = littleFont1;
ranges[1] = new StyleRange();
ranges[1].start = 1;
ranges[1].length = 1;
ranges[1].font = littleFont2;
styledText.replaceStyleRanges(0, 3, ranges);
// 2 little fonts, line height is the height of the default font of StyledText
assertVariableLineHeightEquals(0, 0);
littleFont1.dispose();
littleFont2.dispose();
}
Aggregations