Home » Eclipse Projects » Standard Widget Toolkit (SWT) » StyledText Extension
StyledText Extension [message #486337] |
Thu, 17 September 2009 06:38  |
Eclipse User |
|
|
|
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.
|
|
| |
Re: StyledText Extension [message #486736 is a reply to message #486414] |
Fri, 18 September 2009 16:32  |
Eclipse User |
|
|
|
Felipe Heidrich schrieb:
> It is possible, but you have to do the merging os styles by hand.
>
> Before you set a style to a range of text (which you know already will
> override all styles previously set in the range), you need to retrieve
> the styles already set in the range, merge the existing styles with the
> new style you are adding, and then set them on the styledtext.
>
> see org.eclipse.swt.examples.texteditor.TextEditor#setStyle, it
> implements the idea described above. Since you only need background your
> code should be simpler than the TextEditor example.
>
> Regards,
> Felipe
>
Thanks for the hint - works perfectly!
Greetings,
Dirk.
|
|
|
Goto Forum:
Current Time: Sat Jul 05 03:01:18 EDT 2025
Powered by FUDForum. Page generated in 0.03866 seconds
|