From d73a770b0584ec518a16fb764140080d404d07e9 Mon Sep 17 00:00:00 2001 From: lonnen Date: Fri, 3 Sep 2010 20:19:34 +0000 Subject: [PATCH] Added a preference page so the sketchbook location can be be changed. --- .../Processing Plugin for Eclipse/feature.xml | 2 +- .../core/ProcessingCorePreferences.java | 30 +--- editor/processing.plugin.ui/plugin.xml | 11 ++ .../ui/preferences/CorePreferencePage.java | 159 ++++++++++++++++++ .../ui/preferences/PreferenceConstants.java | 16 ++ .../ui/preferences/PreferenceInitializer.java | 26 +++ 6 files changed, 222 insertions(+), 22 deletions(-) create mode 100644 editor/processing.plugin.ui/src/processing/plugin/ui/preferences/CorePreferencePage.java create mode 100644 editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceConstants.java create mode 100644 editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceInitializer.java diff --git a/editor/Processing Plugin for Eclipse/feature.xml b/editor/Processing Plugin for Eclipse/feature.xml index 9dbff956b..4717979bc 100644 --- a/editor/Processing Plugin for Eclipse/feature.xml +++ b/editor/Processing Plugin for Eclipse/feature.xml @@ -1,7 +1,7 @@ diff --git a/editor/processing.plugin.core/src/processing/plugin/core/ProcessingCorePreferences.java b/editor/processing.plugin.core/src/processing/plugin/core/ProcessingCorePreferences.java index f02ab9797..5394fff45 100644 --- a/editor/processing.plugin.core/src/processing/plugin/core/ProcessingCorePreferences.java +++ b/editor/processing.plugin.core/src/processing/plugin/core/ProcessingCorePreferences.java @@ -11,9 +11,7 @@ package processing.plugin.core; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.preferences.ConfigurationScope; -import org.osgi.service.prefs.BackingStoreException; -import org.osgi.service.prefs.Preferences; +import org.eclipse.core.runtime.Preferences; /** * Container class for controlling access to the Procesing Core @@ -26,18 +24,14 @@ public class ProcessingCorePreferences { private static ProcessingCorePreferences current; // instantiate the plug in - static { - current = new ProcessingCorePreferences(); - } - - // EVERY PREFERENCE SHOULD HAVE A SIMILAR SETUP - + static { current = new ProcessingCorePreferences(); } + /** Name of the sketchbook location preference for lookup. */ protected static final String SKETCHBOOK = ProcessingCore.PLUGIN_ID + ".preferences.skethbook"; /** Returns the stored sketchbook path as a string. */ public String getSketchbookPathString(){ - return this.getStore().get(SKETCHBOOK, ""); + return this.getStore().getString(SKETCHBOOK); } /** Returns the path to the sketchbook or null if there is none. */ @@ -47,13 +41,11 @@ public class ProcessingCorePreferences { /** Saves the path to the sketchbook. */ public void setSketchbookPath(String sketchbookPath){ - this.getStore().put(SKETCHBOOK, sketchbookPath); + this.getStore().setValue(SKETCHBOOK, sketchbookPath); this.save(); } - // an identifier, get string value, and set new string value - // also, directly getting something useful instead of a string (like a path) - + /** Singleton pattern */ private ProcessingCorePreferences(){} @@ -64,16 +56,12 @@ public class ProcessingCorePreferences { /** Save any preference changes */ public void save(){ - try{ - this.getStore().flush(); - } catch (BackingStoreException e){ - ProcessingLog.logError("Preferences could not be saved.", e); - } + ProcessingCore.getProcessingCore().savePluginPreferences(); } /** Get the preferences store. */ - protected Preferences getStore(){ - return new ConfigurationScope().getNode(ProcessingCore.PLUGIN_ID); + public Preferences getStore(){ + return ProcessingCore.getProcessingCore().getPluginPreferences(); } /** Utility method that returns a path from a string or null */ diff --git a/editor/processing.plugin.ui/plugin.xml b/editor/processing.plugin.ui/plugin.xml index b31fdaed6..fb5c326b8 100644 --- a/editor/processing.plugin.ui/plugin.xml +++ b/editor/processing.plugin.ui/plugin.xml @@ -32,5 +32,16 @@ project="true"> + + + + + + diff --git a/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/CorePreferencePage.java b/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/CorePreferencePage.java new file mode 100644 index 000000000..0d58c29f7 --- /dev/null +++ b/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/CorePreferencePage.java @@ -0,0 +1,159 @@ +package processing.plugin.ui.preferences; + +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.preference.PreferencePage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.DirectoryDialog; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; + +import processing.plugin.core.ProcessingCorePreferences; + +/** + * Pref pane class for the Core preferences. + *

+ * Has a single field corresponding to the sketchbook path. Paths are verified, then if they + * are invalid a warning message is displayed. Whenever an empty path or a valid path is set + * it is saved. The OK button is only clickable after a successful save or a legit path. + */ +public class CorePreferencePage extends PreferencePage implements IWorkbenchPreferencePage { + + private Text sketchbookPathField; + + public CorePreferencePage() { + super(); + setTitle("Processing Core"); + noDefaultAndApplyButton(); + } + + + public void init(IWorkbench workbench) {} + + protected Control createContents(Composite parent) { + Composite container = new Composite(parent, SWT.NULL); + container.setLayout(new GridLayout(3, false)); + + Label l = new Label(container, SWT.LEFT); + l.setText("Sketchbook Folder:"); + GridData gd = new GridData(); + gd.horizontalIndent = 20; + l.setLayoutData(gd); + + + sketchbookPathField = new Text(container, SWT.BORDER); + sketchbookPathField.addModifyListener( + new ModifyListener() { + public void modifyText(ModifyEvent e){ + updatePageComplete(); + } + }); + sketchbookPathField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + + Button button_1 = new Button(container, SWT.NONE); + button_1.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e){browseForDestinationFolder();} + }); + button_1.setText("Browse..."); + + initContents(); + + return container; + } + + public void initContents(){ + String sketchbook = ProcessingCorePreferences.current().getSketchbookPathString(); + if(sketchbook != null){ + System.out.println("INIT: " + sketchbook + ";"); + sketchbookPathField.setText(sketchbook.toString()); + } else { + sketchbookPathField.setText(""); + } + } + + /** + * Browse button functionality to find a destination folder + *

+ * Prettifies the file path if it happens to be in the workspace + */ + protected void browseForDestinationFolder() { + IPath path = browse(resolveLocation()); + if (path == null) + return; + IPath rootLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation(); + if (rootLoc.isPrefixOf(path)) + path = path.setDevice(null).removeFirstSegments(rootLoc.segmentCount()); + sketchbookPathField.setText(path.toString()); + } + + /** + * Sets up a dialog box allowing you to select a directory to use for the sketchbook + * + * @param path the path to be investigated + * @return the path chosen in the dialog box + */ + private IPath browse(IPath path){ + DirectoryDialog dialog = new DirectoryDialog(getShell()); + if (path != null){ + if(path.segmentCount() > 1) + dialog.setFilterPath(path.toOSString()); + } + String result = dialog.open(); + if (result == null) + return null; + return new Path(result); + } + + /** + * Verifies that the sketchbook path exists or is empty + */ + private void updatePageComplete() { + // check the sketchbook path first + IPath sketchbookLoc = resolveLocation(); + if (sketchbookLoc == null){ + setErrorMessage(null); + setMessage("Please specify a sketchbook folder."); + } else if (!sketchbookLoc.toFile().exists()){ + // Do not accept invalid paths + setValid(false); + setMessage(null); + setErrorMessage("Must specify a valid folder or leave blank."); + return; + } + // if nothing was caught, enable the finish button + setValid(true); + setMessage(null); + setErrorMessage(null); + ProcessingCorePreferences.current().setSketchbookPath(sketchbookPathField.getText()); + } + + /** + * Tries to resolve the contents of the sketch book path field to an IPath, and returns it. + * If the field contains a relative path it will be resolved relative to the Eclipse workspace folder. + * + * + * @return an absolute IPath handle to the contents of the sketchbookPathField, or null + */ + protected IPath resolveLocation() { + if (sketchbookPathField==null) return null; + String text = sketchbookPathField.getText().trim(); + if (text.length() == 0) return null; + IPath path = new Path(text); + if (!path.isAbsolute()) path = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(path); + return path; + } + + +} \ No newline at end of file diff --git a/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceConstants.java b/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceConstants.java new file mode 100644 index 000000000..6e0f7ea66 --- /dev/null +++ b/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceConstants.java @@ -0,0 +1,16 @@ +package processing.plugin.ui.preferences; + +/** + * Constant definitions for plug-in preferences + */ +public class PreferenceConstants { + + public static final String P_PATH = "pathPreference"; + + public static final String P_BOOLEAN = "booleanPreference"; + + public static final String P_CHOICE = "choicePreference"; + + public static final String P_STRING = "stringPreference"; + +} diff --git a/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceInitializer.java b/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceInitializer.java new file mode 100644 index 000000000..9f907eee0 --- /dev/null +++ b/editor/processing.plugin.ui/src/processing/plugin/ui/preferences/PreferenceInitializer.java @@ -0,0 +1,26 @@ +package processing.plugin.ui.preferences; + +import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +import org.eclipse.jface.preference.IPreferenceStore; + +import processing.plugin.ui.ProcessingPlugin; + +/** + * Class used to initialize default preference values. + */ +public class PreferenceInitializer extends AbstractPreferenceInitializer { + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences() + */ + public void initializeDefaultPreferences() { + IPreferenceStore store = ProcessingPlugin.getDefault().getPreferenceStore(); + store.setDefault(PreferenceConstants.P_BOOLEAN, true); + store.setDefault(PreferenceConstants.P_CHOICE, "choice2"); + store.setDefault(PreferenceConstants.P_STRING, + "Default value"); + } + +}