Java Plug-in(九) 首选项

首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。

1. 创建首选项

扩展点【org.eclipse.ui.preferencePages】
在【org.eclipse.ui.preferencePages】中添加【page】,【class】指定首选项的类,必须实现IWorkbenchPreferencePage接口,一般会继承PreferencePage类来简化。


    
    

HomePreferencePage

public class HomePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

    @Override
    protected Control createContents(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        GridLayout containerLayout = new GridLayout();
        container.setLayout(containerLayout);

        Label label = new Label(container, SWT.NONE);
        label.setText("用户配置");

        label = new Label(container, SWT.NONE);
        label.setText("这是用户配置的首页");

        return container;
    }

    @Override
    public void init(IWorkbench workbench) {
        noDefaultAndApplyButton();
    }

}

显示如下
Java Plug-in(九) 首选项_第1张图片

2. FieldEditorPreferencePage类

FieldEditorPreferencePage经常被用来作为首选项的父类来使用,并且定义了多种FieldEditor来方便使用。
定义了新的首选项页SamplePreferencePage,继承FieldEditorPreferencePage,并使用多个FieldEditor

	
    
    

SamplePreferencePage

public class SamplePreferencePage extends FieldEditorPreferencePage
        implements IWorkbenchPreferencePage {
    private final static String[][] RADIO_ARRAY = new String[][] {
        {"radio1", "value1"}, {"radio2", "value2"}, {"radio3", "value3"}};

    public SamplePreferencePage() {
        super(GRID);
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
        setDescription("Field Sample");
    }

    @Override
    public void init(IWorkbench workbench) {
    }

    @Override
    protected void createFieldEditors() {
        Composite parent = getFieldEditorParent();

        addField(new BooleanFieldEditor(PreferenceConstants.P_BOOLEAN,
                "Boolean", parent));

        addField(new RadioGroupFieldEditor(PreferenceConstants.P_RADIO,
                "Radio", 1, RADIO_ARRAY, parent, true));

        addField(new ComboFieldEditor(PreferenceConstants.P_COMBO,
                "Combo", RADIO_ARRAY, parent));

        addField(new StringFieldEditor(PreferenceConstants.P_STRING,
                "String", parent));

        addField(new IntegerFieldEditor(PreferenceConstants.P_INTEGER,
                "Integer", parent));

        addField(new ColorFieldEditor(PreferenceConstants.P_COLOR, 
                "Color", parent));

        addField(new FontFieldEditor(PreferenceConstants.P_FONT, 
                "Font", parent));

        addField(new DirectoryFieldEditor(PreferenceConstants.P_DIR, 
                "Directory", parent));
    }

}

PreferenceConstants

public class PreferenceConstants {
    public static final String P_BOOLEAN 		= "user_info_booleanPreference";
    public static final String P_RADIO 			= "user_info_radioPreference";
    public static final String P_COMBO 			= "user_info_comboPreference";
    public static final String P_STRING 		= "user_info_stringPreference";
    public static final String P_INTEGER 		= "user_info_integerPreference";
    public static final String P_COLOR 			= "user_info_colorPreference";
    public static final String P_FONT 			= "user_info_fontPreference";
    public static final String P_DIR 			= "user_info_dirPreference";
    public static final String P_SHOW_PREVIEW	= "user_info_show_preview";
}

3. PreferenceInitializer类初始化

扩展点【org.eclipse.core.runtime.preferences】
在【org.eclipse.core.runtime.preferences】中添加【initializer】,【class】指定了初始化类,继承AbstractPreferenceInitializer


    
    

PreferenceInitializer

public class PreferenceInitializer extends AbstractPreferenceInitializer {

    public void initializeDefaultPreferences() {
        IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        store.setDefault(PreferenceConstants.P_BOOLEAN, true);
        store.setDefault(PreferenceConstants.P_RADIO, "value2");
        store.setDefault(PreferenceConstants.P_COMBO, "value3");
        store.setDefault(PreferenceConstants.P_STRING, "This is a String");
        store.setDefault(PreferenceConstants.P_INTEGER, 17);
        PreferenceConverter.setDefault(store, PreferenceConstants.P_COLOR,
                Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE).getRGB());
        PreferenceConverter.setDefault(store, PreferenceConstants.P_FONT,
                Display.getCurrent().getSystemFont().getFontData());
        store.setDefault(PreferenceConstants.P_DIR, "");

        store.setDefault(PreferenceConstants.P_SHOW_PREVIEW, true);
    }

}

显示如下
Java Plug-in(九) 首选项_第2张图片

4. 首选项的监听

添加一个新的首选页,用来配置界面的显示。


    
    

UserInfoPreferencePage类,配置在编辑器DemoEditorPart中是否显示预览页。

public class UserInfoPreferencePage extends FieldEditorPreferencePage
        implements IWorkbenchPreferencePage {

    public UserInfoPreferencePage() {
        super(GRID);
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
        setDescription("用户信息配置");
    }

    @Override
    protected void createFieldEditors() {
        Composite parent = getFieldEditorParent();
        addField(new BooleanFieldEditor(PreferenceConstants.P_SHOW_PREVIEW, 
            "显示预览分页", parent));
    }

    @Override
    public void init(IWorkbench workbench) {
    }

}

显示如下
Java Plug-in(九) 首选项_第3张图片
DemoEditorPart添加监听器

private IPreferenceStore mStore;

public DemoEditorPart() {
    mStore = Activator.getDefault().getPreferenceStore();
    mStore.addPropertyChangeListener(new IPropertyChangeListener() {
        @Override
        public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event) {
            if (PreferenceConstants.P_SHOW_PREVIEW.equals(event.getProperty())) {
                boolean show = (Boolean) event.getNewValue();
                try {
                    if (show && getPageCount() == 1) {
                        mPreviewFormPage = new UserPreviewFormPage(DemoEditorPart.this);
                        addPage(mPreviewFormPage);
                    } else if (!show && getPageCount() == 2) {
                        setActivePage(0);
                        removePage(1);
                    }					
                }catch (PartInitException e) {
                }
            }
        }
    });
}

@Override
protected void addPages() {
    mInfoFormPage = new UserInfoFormPage(this);
    mPreviewFormPage = new UserPreviewFormPage(this);

    try {
        addPage(mInfoFormPage);
        if (mStore.getBoolean(PreferenceConstants.P_SHOW_PREVIEW))
            addPage(mPreviewFormPage);
    } catch (PartInitException e) {
    }

    ... ...
}

相关文章
Java Plug-in(一) 创建插件项目
Java Plug-in(二) 菜单
Java Plug-in(三) 视图
Java Plug-in(四) 视图菜单
Java Plug-in(五) 编辑器
Java Plug-in(六) 编辑器菜单
Java Plug-in(七) 透视图
Java Plug-in(八) 向导页
Java Plug-in(九) 首选项
Java Plug-in(十) 自定义扩展点
Java Plug-in(十一) 创建RCP项目

你可能感兴趣的:(Java,插件)