[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] StyledText Extension

I am evaluating SWT for a project. The main goal is to create a "StyledTexteditor" with the ability to "tag" section of the text.
A tag is a combination of a name and a color. When you tag a section of the text, you select some text and click on the "tag" button. Now you get a "TagChooser", select your text and it will be tagged. That is almost the same as the "set background color" action, but with the ability that tags may overlap each other, i.e. a section of text can be tagged with multiple tags.


The SWT StyledText component seems almost perfect for this purpose but I don't see how to implement the tagging.
First I searched for a way to add new styles to the StyledText component, but there seems to be no way, because the styles are hard coded.
Then I tried to use the "background color" and the data field for the tagging purpose. The problem here is that the StyledText component merges the styles (and only the last color "surives", which is reasonable because one can only display one color at a time)


Example:
Text is: 1234567890

Code:
public class StyleTest1 {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		StyledText text = new StyledText (shell, SWT.BORDER);
		text.setText("0123456789");

// make 0123456 have a red font
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 7;
style1.data = "ExampleTag1";
style1.background = display.getSystemColor(SWT.COLOR_BLUE);
text.setStyleRange(style1);
// make 34567890 have a red font
StyleRange style2 = new StyleRange();
style2.start = 3;
style2.length = 7;
style2.data = "ExampleTag2";
style2.background = display.getSystemColor(SWT.COLOR_RED);
text.setStyleRange(style2);

StringBuilder dumpText = new StringBuilder();
StyleRange[] styles = st.getStyleRanges();
if (styles.length > 0) {
for (int i = 0; i < styles.length; i++) {
if (styles[i].data != null) {
dumpText.append("[" + styles[i].toString() + "("+styles[i].data+")]\n");
}
else {
dumpText.append("[" + styles[i].toString() + "]\n");
}
}
}
System.out.println(dumpText);

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}


Result:
[StyleRange {0, 3, fontStyle=normal, background=Color {0, 0, 255}}(ExampleTag1)]
[StyleRange {3, 7, fontStyle=normal, background=Color {255, 0, 0}}(ExampleTag2)]


What I need is something like:
[StyleRange {0, 3, fontStyle=normal, background=Color {0, 0, 255}]
[StyleRange {3, 4, fontStyle=normal, background=Color {128, 128, 128}(ExampleTag1,ExampleTag2)] // multi-tagged regions are displayed in a special color (medium grey here)
[StyleRange {3, 7, fontStyle=normal, background=Color {255, 0, 0}]


or even better:
[StyleRange {0, 3, fontStyle=normal, tag=ExampleTag1]
[StyleRange {3, 4, fontStyle=normal, tag=ExampleTag1,ExampleTag2]
[StyleRange {3, 7, fontStyle=normal, tag=ExampleTag2]

Can anybody help me? Is there a way to do this task with the StyledText component? Is there another/better way to do this task with SWT/JFace?

Thanks in advance!
Dirk.