Added a preference page so the sketchbook location can be be changed.

This commit is contained in:
lonnen
2010-09-03 20:19:34 +00:00
parent 63f80a5138
commit d73a770b05
6 changed files with 222 additions and 22 deletions
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<feature
id="processing.plugin"
label="Processing Plugin for Eclipse"
label="Processing Plug-In for Eclipse"
version="1.0.0.qualifier"
provider-name="Processing.org">
@@ -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 */
+11
View File
@@ -32,5 +32,16 @@
project="true">
</wizard>
</extension>
<extension
point="org.eclipse.ui.preferencePages">
<page
class="processing.plugin.ui.preferences.CorePreferencePage"
id="processing.plugin.ui.preferences.CorePreferencePage"
name="Processing">
</page>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
</extension>
</plugin>
@@ -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.
* <p>
* 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
* <p>
* 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;
}
}
@@ -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";
}
@@ -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");
}
}