2011-09-22 5 views

답변

2

신의 가증! 웹 검색 시간을 보냈다 후, 나는 마침내 SWT와 JFace는에 책 확실한 가이드에서 일부 샘플 코드를 발견, 그것은 아주 간단합니다

Main 클래스 : 대답에 대한

package amarsoft.rcp.base.widgets.test; 

import org.eclipse.jface.window.ApplicationWindow; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

import amarsoft.rcp.base.widgets.SQLSegmentEditor; 

public class PageDemo extends ApplicationWindow { 

    public PageDemo(Shell parentShell) { 
     super(parentShell); 
     final Composite topComp = new Composite(parentShell, SWT.BORDER); 
     FillLayout fl = new FillLayout(); 
     fl.marginWidth = 100; 
     topComp.setLayout(fl); 
     new SQLSegmentEditor(topComp); 

    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 

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

package amarsoft.rcp.base.widgets; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.StyledText; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Composite; 

/** 
* SQL语句/SQL语句片段编辑器,除了内容编辑之外,提供一个额外的功能——常用SQL语句关键字高亮显示。 
* @author [email protected] 
* 
*/ 
public class SQLSegmentEditor extends Composite{ 

    private StyledText st; 

    public SQLSegmentEditor(Composite parent) { 
     super(parent, SWT.NONE); 
     this.setLayout(new FillLayout()); 
     st = new StyledText(this, SWT.WRAP); 
     st.addLineStyleListener(new SQLSegmentLineStyleListener()); 
    } 

} 
package amarsoft.rcp.base.widgets; 

import java.util.ArrayList; 
import java.util.List; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.LineStyleEvent; 
import org.eclipse.swt.custom.LineStyleListener; 
import org.eclipse.swt.custom.StyleRange; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.wb.swt.SWTResourceManager; 

public class SQLSegmentLineStyleListener implements LineStyleListener { 

    private static final Color KEYWORD_COLOR = SWTResourceManager 
      .getColor(SWT.COLOR_BLUE); 

    private List<String> keywords = new ArrayList<String>(); 

    public SQLSegmentLineStyleListener() { 
     super(); 
     keywords.add("select"); 
     keywords.add("from"); 
     keywords.add("where"); 
    } 

    @Override 
    public void lineGetStyle(LineStyleEvent event) { 
     List<StyleRange> styles = new ArrayList<StyleRange>(); 
     int start = 0; 
     int length = event.lineText.length(); 
     System.out.println("current line length:" + event.lineText.length()); 
     while (start < length) { 
      System.out.println("while lopp"); 
      if (Character.isLetter(event.lineText.charAt(start))) { 
       StringBuffer buf = new StringBuffer(); 
       int i = start; 
       for (; i < length 
         && Character.isLetter(event.lineText.charAt(i)); i++) { 
        buf.append(event.lineText.charAt(i)); 
       } 
       if (keywords.contains(buf.toString())) { 
        styles.add(new StyleRange(event.lineOffset + start, i - start, KEYWORD_COLOR, null, SWT.BOLD)); 
       } 
       start = i; 
      } 
      else{ 
       start ++; 
      } 
     } 
     event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]); 
    } 

} 
+0

이 답변으로 문제가 해결되면 받아들입니다. 이것은 새로운 SO 회원들을 도울 것입니다. – Favonius