Eclipse插件首选项页面属性在启动时被默认值覆盖

时间:2017-01-26 18:54:23

标签: java eclipse eclipse-plugin

我似乎无法弄清楚是什么原因导致eclipse在启动时覆盖我的插件首选项。 一切似乎工作正常,除非我重新启动eclipse,文件被默认值覆盖。 当我更新首选项时,我可以看到保存到正确文件的值: pluginWorkspace.metadata.plugins \ org.eclipse.core.runtime.settings \ asdfplugin.prefs 另一个奇怪的事实是,当我右键单击plugin.xml文件并作为eclipse应用程序运行时,它似乎工作。 eclipse实例似乎保留了它的价值观。 但是当我编译插件并使用它时,它会在每次eclipse加载时不断替换.prefs文件。 我错过了什么?如何阻止eclipse覆盖prefs文件?

谢谢,

<extension point="org.eclipse.ui.preferencePages">
    <page 
        id="com.wfa.ASDF.ui.preferences.Page1"
        class="com.wfa.ASDF.ui.preferences.ASDFPreferencesPage"
        name="ASDF" >
    </page>

    <page 
        id="LoggingPreferencePage"
        name="Logging"
        class="com.wfa.ASDF.ui.preferences.ASDFLoggingPreferencesPage"
        category="ASDFPreferencesPage">
    </page>
</extension>

public class ASDFPlugin extends AbstractUIPlugin{
        //The shared instance.
        private static ASDFPlugin plugin;

        private PropertiesWFA properties;

        private ASDFPlugin() {
            plugin = this;

            logProperties();
        }

        static public ASDFPlugin getInstance() {
            if(plugin==null){
                plugin = new ASDFPlugin();
            }
            return plugin;
        }

        public PropertiesWFA getPropertiesWFA(){
            if(properties==null){
                refreshProperties();
            }

            return properties;
        }

        public void logProperties(){
            ConsoleUtil.getInstance().postToConsole("ASDFPlugin.logProperties workspace from store ("+getPreferenceStore().getString(ConstantsWFA.STORE_WORKSPACE)+") ");
            ConsoleUtil.getInstance().postToConsole("ASDFPlugin.logProperties zip location from store ("+getPreferenceStore().getString(ConstantsWFA.STORE_ZIP_LOCATION)+") ");
            ConsoleUtil.getInstance().postToConsole("ASDFPlugin.logProperties debug from store ("+getPreferenceStore().getBoolean(ConstantsWFA.STORE_DEBUG)+") ");

            ConsoleUtil.getInstance().postToConsole("ASDFPlugin.logProperties workspace DEFAULT ("+getPreferenceStore().getDefaultString(ConstantsWFA.STORE_WORKSPACE)+") ");
            ConsoleUtil.getInstance().postToConsole("ASDFPlugin.logProperties zip location DEFAULT ("+getPreferenceStore().getDefaultString(ConstantsWFA.STORE_ZIP_LOCATION)+") ");
            ConsoleUtil.getInstance().postToConsole("ASDFPlugin.logProperties debug DEFAULT ("+getPreferenceStore().getDefaultBoolean(ConstantsWFA.STORE_DEBUG)+") ");
        }

        public void refreshProperties(){
            try{
                IPreferenceStore store = getPreferenceStore();
                properties=new PropertiesWFA();
                properties.setWorkspace(store.getString(ConstantsWFA.STORE_WORKSPACE));
                properties.setZipLocation(store.getString(ConstantsWFA.STORE_ZIP_LOCATION));
                properties.setDebug(store.getBoolean(ConstantsWFA.STORE_DEBUG));
                ConsoleUtil.getInstance().postToConsole("ASDFPlugin.refreshProperties  ");
                logProperties();

                if(isEmpty(properties.getWorkspace())){
                    properties.setWorkspace(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString());
                }
                if(isEmpty(properties.getZipLocation() )){
                    String tempDir = System.getProperty("java.io.tmpdir");
                    File dir = new File(tempDir, ConstantsWFA.WORKING_DIR);
                    properties.setZipLocation(dir.getAbsolutePath());
                }
            }catch(Exception e){
                ConsoleUtil.getInstance().error("RefreshProperties failed.", e);
            }
        }

}

public class ASDFPreferencesPage extends ASDFPreferencesBase{
private Text textField_workspace;
private Text textField_ziplocation;
private Button checkBox_debug;

@Override
public void init(IWorkbench arg0) {
    try{
        ASDFPlugin.getInstance().logProperties();
        setPreferenceStore(ASDFPlugin.getInstance().getPreferenceStore());
    }catch(Exception e){
        e.printStackTrace();
    }
}

@Override
protected Control createContents(Composite parent) {
    //composite_textField << parent
    Composite composite_textField = createComposite(parent, 2);
    Label label_textField = createLabel(composite_textField, "Workspace");   
    textField_workspace = createTextField(composite_textField);
    //pushButton_textField = createPushButton(composite_textField, MessageUtil.getString("Change"));
    createDirectoryDialogButton(textField_workspace);

    createLabel(composite_textField, "Zip File(s) Location");    
    textField_ziplocation = createTextField(composite_textField);
    createDirectoryDialogButton(textField_ziplocation);


    Composite composite_checkBox = createComposite(parent, 1);
    checkBox_debug = createCheckBox(composite_checkBox, "Debug");


    initializeValues();

    return new Composite(parent, SWT.NULL);
}

@Override
public void modifyText(ModifyEvent arg0) {
    //System.out.println("modify text");
    // TODO Auto-generated method stub
}

@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
    // TODO Auto-generated method stub
    //System.out.println("widgetDefaultSelected");
}

@Override
public void widgetSelected(SelectionEvent arg0) {
    // TODO Auto-generated method stub
    //System.out.println("widgetSelected");
}

/**
 * Initializes states of the controls from the preference store.
 */
private void initializeValues() {
    IPreferenceStore store = getPreferenceStore();
    PropertiesWFA properties = ASDFPlugin.getInstance().getPropertiesWFA();

    ConsoleUtil.getInstance().debug("InitalizeValues Load workspace as ("+store.getString(ConstantsWFA.STORE_WORKSPACE)+") ");
    ConsoleUtil.getInstance().debug("InitalizeValues Load zip location as ("+store.getString(ConstantsWFA.STORE_ZIP_LOCATION)+") ");
    ConsoleUtil.getInstance().debug("InitalizeValues Load debug as ("+store.getBoolean(ConstantsWFA.STORE_DEBUG)+") ");

    textField_workspace.setText(store.getString(ConstantsWFA.STORE_WORKSPACE));
    if(isFieldEmpty(textField_workspace)){
        textField_workspace.setText(properties.getWorkspace());
    }

    textField_ziplocation.setText(store.getString(ConstantsWFA.STORE_ZIP_LOCATION));
    if(isFieldEmpty(textField_ziplocation)){
        textField_ziplocation.setText(properties.getZipLocation());
    }

    checkBox_debug.setSelection(store.getBoolean(ConstantsWFA.STORE_DEBUG));
}

/* (non-Javadoc)
 * @see org.eclipse.jface.preference.PreferencePage#performOk()
 * You may override performApply if you have additional processing when the user selects Apply. The default implementation is to call performOk.
 */
public boolean performOk() {
    storeValues();
    ASDFPlugin.getInstance().savePluginPreferences();
    ASDFPlugin.getInstance().refreshProperties();
    return super.performOk();
}
private void storeValues() {
    //http://alvinalexander.com/java/jwarehouse/eclipse/org.eclipse.ui.examples.readmetool/Eclipse-UI-Examples-Readme-Tool/org/eclipse/ui/examples/readmetool/ReadmePreferencePage.java.shtml
    ConsoleUtil.getInstance().debug("Save workspace as ("+textField_workspace.getText()+") ");
    ConsoleUtil.getInstance().debug("Save zip location as ("+textField_ziplocation.getText()+") ");
    ConsoleUtil.getInstance().debug("Save debug as ("+checkBox_debug.getSelection()+") ");

    updateStoreValue(ConstantsWFA.STORE_WORKSPACE, textField_workspace.getText());
    updateStoreValue(ConstantsWFA.STORE_ZIP_LOCATION, textField_ziplocation.getText());
    updateStoreValue(ConstantsWFA.STORE_DEBUG, checkBox_debug.getSelection());

    ASDFPlugin.getInstance().logProperties();
}    

private void updateStoreValue(String key, String value){
    IPreferenceStore store = getPreferenceStore();
    store.setValue(key, value);
    //store.setDefault(key, value);
}
private void updateStoreValue(String key, Boolean value){
    IPreferenceStore store = getPreferenceStore();
    store.setValue(key, value);
    store.setDefault(key, value);
}
}

1 个答案:

答案 0 :(得分:0)

默认商店似乎适用于所有工作区。 在我的扩展AbstractUIPlugin的类中,我覆盖了&#34; getPreferenceStore&#34;确保它正在检索实例范围存储的方法。

    public IPreferenceStore getPreferenceStore(){
        IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, ConstantsWFA.PLUGIN_ID+"_store");
        return store;
    }