2014-07-24 5 views
-1

SWT StyledText 상자에 StyleRange을 사용하여 Java 구문을 강조하려고합니다. 다음은 관련 코드입니다.Java SWT StyleRange

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; 
} 

그러나 StyleRange은 아무 것도하지 않습니까? 누군가가 나를 도와 줄 수 있습니까?

편집 : 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); } }

드롭 다운 인구 코드 :

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); } } }

나는이 새로운 코드 `개인 무효의 Color_Code (StyledText 텍스트) {

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(); 
}` 

트리 인구 코드를 사용하는 경우

텍스트 채우기 코드 :

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); } } } 내 첫 번째 인스턴스에서 public이라는 단어를 강조 표시하지만 내 드롭 다운 메뉴와 트리를 채우지 않습니다.

+1

그냥 참고 : 모든 자원 당신은, 예를 들어, 자신을 만들 수') ('처분하는 것을 잊지 마십시오 여기

은 예입니다 '색깔'. – Baz

+0

텍스트의 처음 22 글자를 굵게 표시하거나 오렌지색으로 설정하는 것은 원하는 것입니까? –

+0

지금은 그냥 뭔가가 강조되고 굵게 표시되는 것을보고 싶습니다. – Irtza

답변

2

다른 코드에 문제가있을 수 있습니다. 모든 것이 잘 작동합니다.

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(); 
} 

는 다음과 같습니다 :

enter image description here

+0

위의 제안 된 솔루션으로 작업을 시도했습니다. 하지만 내 출력이 [IMG] http://i62.tinypic.com/swrepc.png [/ IMG]에서 [IMG] http://i62.tinypic.com/fvlq37.png [/ IMG]로 변경되었습니다. – Irtza

+0

@Irtza 정확히 작동하지 않는다면 무엇을 정교하게해야할까요? 나는 당신의 코드를 모른다. 그래서 어떻게 코드를 수정해야 제대로 작동하는지 말할 수 없다. – Baz

+0

구문을 강조하는 코드를 추가 할 때. 내 트리와 드롭 다운 메뉴를 모두 채우지 않고 쉘의 제목과 설명을 제거하여 부팅합니다. – Irtza