SWT/JFace的工具类

自己写的一个SWT/JFace的工具类,有了它,构建Label,Button,Composite,Group等等的基础组件,就变得很容易了。

 

/**
 * 布局均为GridLayout,GridData
 * 
 * @author TigerChen
 * 
 */
public class UIFactory {

    /**
     * 防止Text被撑大,而给它设置的一个默认的widthHint
     */
    public static final int SIZING_TEXT_FIELD_WIDTH = 80;

    public static Label createLabel(Composite parent, int style, String text, GridData data) {
        Label label = new Label(parent, style);
        label.setText(text);
        label.setLayoutData(data);
        return label;
    }

    public static Label createLabel(Composite parent, int style, String text, int horizontalSpan) {
        Label label = new Label(parent, style);
        label.setText(text);
        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        gd.horizontalSpan = horizontalSpan;
        label.setLayoutData(gd);
        return label;
    }

    public static Label createLabel(Composite parent, String text) {
        return createLabel(parent, text, 1, GridData.HORIZONTAL_ALIGN_BEGINNING);
    }

    /**
     * 创建特定字体的标签
     * 
     * @param parent
     * @param text
     * @param font 字体
     * @return
     */
    public static Label createLabel(Composite parent, String text, Font font) {
        Label label = createLabel(parent, text);
        label.setFont(font);

        return label;
    }

    public static Label createLabel(Composite parent, String text, int horizontalSpan) {
        return createLabel(parent, text, horizontalSpan, GridData.HORIZONTAL_ALIGN_BEGINNING);
    }

    /**
     * 创建特定字体的标签
     * 
     * @param parent
     * @param text
     * @param horizontalSpan
     * @param font 字体
     * @return
     */
    public static Label createLabel(Composite parent, String text, int horizontalSpan, Font font) {
        Label label = createLabel(parent, text, horizontalSpan);
        label.setFont(font);

        return label;
    }

    public static Label createLabel(Composite parent, String text, int horizontalSpan, int gridData) {
        Label label = new Label(parent, SWT.NONE);
        label.setText(text);
        GridData gd = new GridData(gridData);
        gd.horizontalSpan = horizontalSpan;
        label.setLayoutData(gd);
        return label;
    }

    public static Label createLabel(Composite parent, String text, GridData gd) {
        Label label = new Label(parent, SWT.NONE);
        label.setText(text);
        label.setLayoutData(gd);
        return label;
    }

    public static Label createLabel(Composite parent, int style, String text) {
        Label label = new Label(parent, style);
        label.setText(text);
        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        label.setLayoutData(gd);
        return label;
    }

    public static Label createTopLabel(Composite parent, String text) {
        Label topLabel = UIFactory.createLabel(parent, SWT.TOP, text);
        GridData desGd = (GridData) topLabel.getLayoutData();
        desGd.verticalAlignment = SWT.TOP;
        topLabel.setLayoutData(desGd);
        return topLabel;
    }

    public static Label createSeparatorLabel(Composite parent, int horizontalSpan) {
        Label sepLabel = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        sepLabel.setLayoutData(data);
        data.horizontalSpan = horizontalSpan;
        return sepLabel;
    }

    public static Label createSeparatorLabel(Composite parent) {
        return createSeparatorLabel(parent, 1);
    }

    public static Text createText(Composite parent, int style, GridData data) {
        Text txt = new Text(parent, style);
        txt.setLayoutData(data);
        return txt;
    }

    public static Text createText(Composite parent, int style, int horizontalSpan) {
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.widthHint = SIZING_TEXT_FIELD_WIDTH;
        gd.horizontalSpan = horizontalSpan;
        return createText(parent, style, gd);
    }

    public static Text createText(Composite parent) {
        return createText(parent, SWT.BORDER);
    }

    public static Text createText(Composite parent, int style) {
        Text text = new Text(parent, style);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.widthHint = SIZING_TEXT_FIELD_WIDTH;
        text.setLayoutData(gd);
        return text;
    }

    public static Group createGroup(Composite parent, int style, String text, int colnum, GridData data) {
        Group group = new Group(parent, style);
        group.setText(text);
        group.setLayout(new GridLayout(colnum, false));
        group.setLayoutData(data);
        return group;

    }

    public static Group createGroup(Composite parent, int sytle, String text, int colnum) {
        return createGroup(parent, sytle, text, colnum, 1);
    }

    public static Group createGroup(Composite parent, int sytle, String text, int colnum, int horizontalSpan) {
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalSpan = horizontalSpan;
        return createGroup(parent, sytle, text, colnum, gd);
    }

    public static Group createGroup(Composite parent,
                                    int sytle,
                                    String text,
                                    int colnum,
                                    int horizontalSpan,
                                    int gridData) {
        GridData gd = new GridData(gridData);
        gd.horizontalSpan = horizontalSpan;
        return createGroup(parent, sytle, text, colnum, gd);
    }

    public static Button createButton(Composite parent, int style, String text, GridData data) {
        Button btn = new Button(parent, style);
        btn.setText(text);
        btn.setLayoutData(data);
        return btn;
    }

    public static Button createButton(Composite parent, int style, String text, int horizontalSpan) {
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = horizontalSpan;
        return createButton(parent, style, text, gd);
    }

    public static Button createButton(Composite parent, String text) {
        return createButton(parent, SWT.PUSH, text);
    }

    public static Button createButton(Composite parent, String text, Image image) {
        Button button = createButton(parent, SWT.PUSH, text);
        button.setImage(image);
        return button;
    }

    public static Button createButton(Composite parent, Image image) {
        Button button = new Button(parent, SWT.PUSH);
        button.setImage(image);
        return button;
    }

    public static Button createButton(Composite parent, int style, String text) {
        Button btn = new Button(parent, style);
        btn.setText(text);
        return btn;
    }

    public static Composite createComposite(Composite parent, int style, int colnum, GridData data) {
        Composite comp = new Composite(parent, style);
        comp.setLayout(new GridLayout(colnum, false));
        comp.setLayoutData(data);
        return comp;
    }

    public static Composite createComposite(Composite parent, int style, int colnum, int horizontalSpan) {
        return createComposite(parent, style, colnum, horizontalSpan, GridData.FILL_BOTH);
    }

    public static Composite createComposite(Composite parent, int style, int colnum, int horizontalSpan, int gridData) {
        GridData gd = new GridData(gridData);
        gd.horizontalSpan = horizontalSpan;
        return createComposite(parent, style, colnum, gd);
    }

    public static Composite createComposite(Composite parent, int style, int colnum) {
        Composite comp = new Composite(parent, style);
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalSpan = 1;
        comp.setLayout(new GridLayout(colnum, false));
        comp.setLayoutData(gd);
        return comp;
    }

    public static Composite createCompositeMini(Composite parent, int style, int colnum, GridData data) {
        Composite comp = new Composite(parent, style);
        GridLayout gl = new GridLayout(colnum, false);
        gl.marginWidth = 0;
        gl.marginHeight = 0;
        comp.setLayout(gl);
        comp.setLayoutData(data);
        return comp;
    }

    public static Composite createCompositeMini(Composite parent, int style, int colnum, int horizontalSpan) {
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalSpan = horizontalSpan;
        return createCompositeMini(parent, style, colnum, gd);
    }

    public static Composite createCompositeMini(Composite parent,
                                                int style,
                                                int colnum,
                                                int horizontalSpan,
                                                int gridData) {
        GridData gd = new GridData(gridData);
        gd.horizontalSpan = horizontalSpan;
        return createCompositeMini(parent, style, colnum, gd);
    }

    public static Composite createCompositeMini(Composite parent, int style, int colnum) {
        return createCompositeMini(parent, style, colnum, 1);
    }

    public static Combo createCombo(Composite parent, int style, int horizontalSpan) {
        Combo combo = new Combo(parent, style);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = horizontalSpan;
        combo.setLayoutData(gd);
        return combo;
    }

    public static Combo createCombo(Composite parent, int style) {
        Combo combo = new Combo(parent, style);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        combo.setLayoutData(gd);
        return combo;
    }


    public static Text createBigText(Composite container, int horizontalSpan) {
        Text descTxt = UIFactory.createText(container, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL, 2);
        GridData desGd = (GridData) descTxt.getLayoutData();
        int heightHint = 5 * descTxt.getLineHeight();
        desGd = new GridData(GridData.FILL_BOTH);
        desGd.widthHint = SIZING_TEXT_FIELD_WIDTH;
        desGd.heightHint = heightHint;
        desGd.horizontalSpan = horizontalSpan;
        descTxt.setLayoutData(desGd);
        return descTxt;
    }

    /**
     * 创建指定初始宽度和高度的输入框
     * 
     * @param container 父容器
     * @param horizontalSpan 占位
     * @param width 指定的宽度
     * @param height 指定的高度
     * @return 返回输入框的引用
     */
    public static Text createBigText(Composite container, int horizontalSpan, int width, int height) {
        Text descTxt = UIFactory.createText(container, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL, 2);
        GridData desGd = (GridData) descTxt.getLayoutData();
        desGd = new GridData(GridData.FILL_BOTH);
        desGd.widthHint = width;
        desGd.heightHint = height;
        desGd.horizontalSpan = horizontalSpan;
        descTxt.setLayoutData(desGd);
        return descTxt;
    }

    /**
     * 弹出警告信息,非主线程也提供了支持。
     * 
     * @param message 警告信息内容
     */
    public static void openWarning(String message) {
        open(MessageDialog.WARNING, "Warning", message);
    }

    public static void openWarning(String title, String message) {
        open(MessageDialog.WARNING, title, message);
    }

    public static void openMessage(String title, String message) {
        open(MessageDialog.INFORMATION, title, message);
    }

    public static void openError(String title, String message) {
        open(MessageDialog.ERROR, title, message);
    }

    /**
     * 弹出信息,非主线程也提供了支持。
     * 
     * @param kind the kind of dialog to open, one of {@link #ERROR}, {@link #MessageDialog.INFORMATION}, {@link #MessageDialog.QUESTION},
     *            {@link #MessageDialog.WARNING}, {@link #MessageDialog.CONFIRM}, or {@link #MessageDialog.QUESTION_WITH_CANCEL}.
     * @param title
     * @param message
     */
    public static void open(final int kind, final String title, final String message) {
        if (message == null || "".equals(message)) {
            return;
        }

        if (Display.getCurrent() != null) {
            Display display = Display.getCurrent();
            Shell shell = display.getActiveShell();
            MessageDialog.open(kind, shell, title, message, SWT.NONE);
        } else {
            Display.getDefault().syncExec(new Runnable() {

                public void run() {
                    Display display = Display.getCurrent();
                    Shell shell = display.getActiveShell();
                    MessageDialog.open(kind, shell, title, message, SWT.NONE);
                }

            });
        }
    }

    public static ControlDecoration createDecoration(Control control, int style) {

        ControlDecoration decoration = new ControlDecoration(control, style);
        // decoration.setImage(ESBAdminImages.getImage(ESBAdminImages.IMG_LIGHT_TIP));
        decoration.hide();

        return decoration;
    }

    public static ControlDecoration createDecoration(Control control) {

        return createDecoration(control, SWT.LEFT | SWT.TOP);
    }
    
    public static Control createEmptyLine(Composite parent) {
        Label label = new Label(parent, SWT.LEFT);
        GridData gd = new GridData();
        gd.horizontalAlignment = GridData.BEGINNING;
        gd.grabExcessHorizontalSpace = false;
        gd.horizontalSpan = ((GridLayout) parent.getLayout()).numColumns;
        gd.horizontalIndent = 0;

        label.setLayoutData(gd);
        return label;
    }

}

你可能感兴趣的:(swt jface)