开发者

eclipse rcp : help to create a customized StyledText widget

I want a customized StyledText widget which开发者_StackOverflow社区 can accept a keyword list,then highlight these keywords!

I found it's very hard to implement it by myself.


God damned! after spent hours searching the web, I finally found some sample code from the book The Definitive Guide to SWT and JFace, it's quite simple :

Main class :

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 ggfan@amarsoft
 *
 */
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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜