Search code examples
eclipseeclipse-plugineclipse-pde

How can I use values from Eclipse Preference Pages to initialize external tool parameters?


I've created Eclipse Plugin from default template with Preference Pages. There are some preference parameters, two of them are String. I also created custom External Tool launch configuration in this Plugin and I use one of String parameters (let it be P_PATH) mentioned before to initialize one of tool's fields by default (using SetDefaults method). The problem is that it doesn't work as I suggested. When I launch this plugin (in another Eclipse instance) I go to Window->Preferences->Sample Preferences where I can edit and save field values. But after I'm done (when i enter something to field connected with P_PATH value there) I create new external tool of selected type and String parameter of selected field (the one tied to P_PATH, let it be "File Path") is inialized by the value that was specified in Plugin, not the one I entered into Preference Pages form. So, I want String value I enter in Preference Pages to be passed to External Tool as one of its parameters and when I create new External Tool of selected type it should be there (as default). How can I do that? I tied one External Tool field and one Preference Pages field to the same string parameter but looks like it's not properly passed to External Tool after all.

Added some code, there are three classes for Preference Pages and the forth one is for Launch Configuration Tabs. Here is only relevant code:

public class PreferenceConstants {
    public static final String P_PATH = "pathPreference";
}

public class PreferenceInitializer extends AbstractPreferenceInitializer {org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()

    public void initializeDefaultPreferences() {        
        store.setDefault(PreferenceConstants.P_PATH,"Default value");
    }
}

public class PreferencePage
    extends FieldEditorPreferencePage
    implements IWorkbenchPreferencePage {

    public PreferencePage() {
        super(GRID);
        setPreferenceStore(Activator.getDefault().getPreferenceStore());
        setDescription("A demonstration of a preference page implementation");
    }


    public void createFieldEditors() {
        addField(new FileFieldEditor(PreferenceConstants.P_PATH, 
                "&Console compiler path:", getFieldEditorParent()));

    }

    public void init(IWorkbench workbench) {
    }   
}

  public class LaunchConfigurationTabs extends AbstractLaunchConfigurationTabGroup {    
        @Override   
        public void setDefaults(ILaunchConfigurationWorkingCopy configuration){     
            configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", PreferenceConstants.P_PATH);       
        }   
    }

Solution

  • The line:

    configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", PreferenceConstants.P_PATH);       
    

    Just sets the attribute value to 'pathPreference' - this does not do anything to look up the value in the preferences.

    You can look up the preference value at that point:

    IPreferenceStore prefStore = Activator.getDefault().getPreferenceStore();
    
    String value = prefStore.getString(PreferenceConstants.P_PATH);
    
    configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", value);  
    

    I don't think you can do anything that will make the attribute value automatically update if the preference changes.