I am trying to highlight Java syntax using StyleRange
s in SWT StyledText
boxes. Here is the relevant code.
String code = text.getText();
int fromIndex = 0;
String keyword = "public";
if(code.toLowerCase().contains(keyword.toLowerCase())){
System.out.println("Match found");
Shell shell = text.getShell();
Display display = shell.getDisplay();
System.out.println("Got shell and display...");
Color orange = new Color(display, 255, 127, 0);
int index = code.indexOf(keyword, fromIndex);
int length = keyword.length();
StyleRange styleRange = new StyleRange(0, 22, orange, null,SWT.BOLD);
text.setStyleRange(styleRange);
System.out.println("colored...");
fromIndex = index;
}
but the StyleRange
s do nothing? Can someone help me out with this?
Edit: If i use this new code `private void Color_Code(StyledText text) {
Shell shell = this.getShell();
Display display = shell.getDisplay();
String[] lines = text.getText().split("\\n");
String keyWord = "public";
Color red = display.getSystemColor(SWT.COLOR_RED);
int offset = 0;
for (String line : lines)
{
int index = line.indexOf(keyWord);
if (index != -1)
{
StyleRange range = new StyleRange(index + offset, keyWord.length(), red, null, SWT.BOLD);
text.setStyleRange(range);
}
offset += line.length() + 1; // +1 for the newline character
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}`
Tree population code: private void Populate_Method_Tree(Tree tree) {
// TODO Auto-generated method stub
for (int i = 0; i < SampleHandler.f.MCCCLONES.size(); i++) {
int id = SampleHandler.f.MCCCLONES.get(i).getMCCID();
TreeItem temp = new TreeItem(tree, SWT.V_SCROLL);
temp.setText("MCCID: " + id);
Populate_Drop_Down(id);
}
}
Dropdown Population code:
protected void Populate_Drop_Down(int id) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
for (int i = 0; i < SampleHandler.f.MCCCLONES.size(); i++) {
if (id == SampleHandler.f.MCCCLONES.get(i).getMCCID()) {
ArrayList<String> Method_Names = new ArrayList<>();
for (int j = 0; j < SampleHandler.f.MCCCLONES.get(i)
.getMethod_Clones().size(); j++) {
String name = SampleHandler.f.MCCCLONES.get(i)
.getMethod_Clones().get(j).getMethod()
.getFileName();
String[] parts = name.split("[\\\\ .]");
Method_Names.add(parts[parts.length - 2]
+ " "
+ Integer
.toString(SampleHandler.f.MCCCLONES
.get(i).getMethod_Clones()
.get(j).getMethod()
.getMethodID()));
}
String[] Methds = new String[Method_Names.size()];
Methds = Method_Names.toArray(Methds);
combo.setItems(Methds);
combo.setText(Methds[0]);
String[] parts = Methds[0].split("[\\s+]");
int MID = Integer.parseInt(parts[1]);
Fill_Code(MID);
}
}
}
Textbox filling code:
private void Fill_Code(int MID) {
// TODO Auto-generated method stub
for (int i = 0; i < SampleHandler.f.METHODS.size(); i++) {
if (SampleHandler.f.METHODS.get(i).getMethodID() == MID) {
text.setText(SampleHandler.f.METHODS.get(i).getCode());
//Color_Code(text);
}
}
}
it highlight the word public in my first instance but stops populating my dropdown menu and tree.
There has to be something wrong with your other code. Everything works just fine here.
Here is an example:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
StyledText text = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP);
text.setText("public class Text\n{\n public static void main(String[] args)\n {\n System.out.println(\"Text\");\n }\n}");
String[] lines = text.getText().split("\\n");
String keyWord = "public";
Color red = display.getSystemColor(SWT.COLOR_RED);
int offset = 0;
for (String line : lines)
{
int index = line.indexOf(keyWord);
if (index != -1)
{
StyleRange range = new StyleRange(index + offset, keyWord.length(), red, null, SWT.BOLD);
text.setStyleRange(range);
}
offset += line.length() + 1; // +1 for the newline character
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this: