Permalink
Browse files

Implemented setValue() for page components to complement getValue().

Can now load/set the WizardSettings of a WizardContainer.
  • Loading branch information...
1 parent 5edd69f commit 9eeb6e8b42ad7242f6ef855be2cd113e518e7610 dpdearing committed Jul 31, 2008
@@ -31,6 +31,13 @@
*/
public Object getValue();
+ /**
+ * Sets the value of the component.
+ *
+ * @param o The value object.
+ */
+ public void setValue(Object o);
+
/**
* Gets the name of this component.
* (this is provided by the Swing component)
@@ -15,15 +15,28 @@
*/
package org.ciscavate.cjwizard;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.util.HashMap;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
/**
* @author ddearing
*/
public class FlatWizardSettings extends HashMap<String, Object>
implements WizardSettings
{
+ /**
+ * Commons logging log instance
+ */
+ private static Log log = LogFactory.getLog(FlatWizardSettings.class);
+
/* (non-Javadoc)
* @see org.ciscavate.cjwizard.WizardSettings#newPage(java.lang.String)
*/
@@ -41,4 +54,76 @@ public void rollBack()
{
// no-op
}
+
+ /**
+ * Serialize the current instance of {@link FlatWizardSettings} to the given
+ * filename.
+ *
+ * @param filename
+ * The filename.
+ */
+ public void serialize(String filename) {
+ ObjectOutputStream out = null;
+ FileOutputStream fout = null;
+ try {
+ fout = new FileOutputStream(filename);
+ out = new ObjectOutputStream(fout);
+ out.writeObject(this);
+ } catch (IOException ioe) {
+ log.error("Error writing settings", ioe);
+ } finally {
+ if (null != out) {
+ try {
+ out.close();
+ } catch (IOException ioe) {
+ log.error("Error closing output stream", ioe);
+ }
+ }
+ if (null != fout) {
+ try {
+ fout.close();
+ } catch (IOException ioe) {
+ log.error("Error closing file", ioe);
+ }
+ }
+ }
+ }
+
+ /**
+ * Deserialize a {@link FlatWizardSettings} object from the specified file.
+ *
+ * @param filename
+ * The filename.
+ * @return The deserialized {@link FlatWizardSettings}.
+ */
+ static public FlatWizardSettings deserialize(String filename) {
+ ObjectInputStream in = null;
+ FileInputStream fin = null;
+ try {
+ fin = new FileInputStream(filename);
+ in = new ObjectInputStream(fin);
+ return (FlatWizardSettings)in.readObject();
+ } catch (IOException ioe) {
+ log.error("Error reading settings", ioe);
+ } catch (ClassNotFoundException cnfe) {
+ log.error("Couldn't instantiate seralized class", cnfe);
+ } finally {
+ if (null != in) {
+ try {
+ in.close();
+ } catch (IOException ioe) {
+ log.error("Error closing inputstream", ioe);
+ }
+ }
+ if (null != fin) {
+ try {
+ fin.close();
+ } catch (IOException ioe) {
+ log.error("Error closing file", ioe);
+ }
+ }
+ }
+ return null;
+ }
+
}
@@ -50,7 +50,7 @@
/**
* Storage for all the collected information.
*/
- private final WizardSettings _settings;
+ private WizardSettings _settings;
/**
* The path from the start of the dialog to the current location.
@@ -215,9 +215,8 @@ public void next() {
if (0 != _path.size()){
// get the settings from the page that is going away:
- WizardPage lastPage = _path.get(_path.size()-1);
- getSettings().newPage(lastPage.getId());
- lastPage.updateSettings(getSettings());
+ getSettings().newPage(lastPage().getId());
+ lastPage().updateSettings(getSettings());
}
WizardPage curPage = _factory.createPage(getPath(), getSettings());
@@ -320,15 +319,39 @@ public void removeWizardListener(WizardListener listener){
* @see org.ciscavate.cjwizard.WizardController#getSettings()
*/
public WizardSettings getSettings(){
+ if (null != lastPage()) {
+ lastPage().updateSettings(_settings);
+ }
return _settings;
}
+ /**
+ * Set/load the specified settings map nad re-render the current page.
+ *
+ * @param settings
+ * The settings to load.
+ */
+ public void setSettings(WizardSettings settings)
+ {
+ _settings = settings;
+ lastPage().rendering(_path, _settings);
+ }
+
/* (non-Javadoc)
* @see org.ciscavate.cjwizard.WizardController#getPath()
*/
public List<WizardPage> getPath() {
return _path;
}
+
+ /**
+ * @return The last page of the current {@link #_path} or null if the path is
+ * empty.
+ */
+ public WizardPage lastPage() {
+ int lastIdx = _path.size() - 1;
+ return (lastIdx < 0) ? null : _path.get(lastIdx);
+ }
/* (non-Javadoc)
* @see org.ciscavate.cjwizard.WizardController#setNextEnabled(boolean)
@@ -350,4 +373,5 @@ public void setPrevEnabled(boolean enabled) {
public void setFinishEnabled(boolean enabled) {
_finishAction.setEnabled(enabled);
}
+
}
@@ -19,6 +19,7 @@
import java.awt.Container;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -132,6 +133,7 @@ public void updateSettings(WizardSettings settings){
}
}
+
/**
* Gets the value from a component.
*
@@ -157,15 +159,58 @@ private Object getValue(Component c) {
return val;
}
+
+ /**
+ * Sets the value of a component.
+ *
+ * @param c The component.
+ * @param o The value.
+ */
+ private void setValue(Component c, Object o) {
+
+ if (null == o) {
+ // don't set null values
+ return;
+ }
+ if (c instanceof CustomWizardComponent) {
+ ((CustomWizardComponent) c).setValue(o);
+ } else if (c instanceof JTextComponent) {
+ String text = (String)o;
+ if (!text.isEmpty()) {
+ ((JTextComponent) c).setText((String)o);
+ }
+ } else if (c instanceof AbstractButton){
+ ((AbstractButton) c).setSelected((Boolean)o);
+ } else if (c instanceof JComboBox){
+ ((JComboBox) c).setSelectedItem(o);
+ } else if (c instanceof JList){
+ List<Object> items = Arrays.asList((Object[])o);
+ JList list = (JList)c;
+ int[] indices = new int[items.size()];
+ int i = 0;
+ for (int j = 0; j < list.getModel().getSize(); j++) {
+ Object e = list.getModel().getElementAt(j);
+ if (items.contains(e)) {
+ indices[i++] = j;
+ }
+ }
+ list.setSelectedIndices(indices);
+ } else {
+ log.warn("Unknown component: "+c);
+ }
+
+ }
/**
* Invoked immediately prior to rendering the wizard page on screen.
*
* This provides an opportunity to adjust the next/finish buttons and
* customize the ui based on feedback.
*/
- public void rendering(List<WizardPage> path, WizardSettings settings){
- // intentionally empty. (default implementation)
+ public void rendering(List<WizardPage> path, WizardSettings settings) {
+ for (Component c : _namedComponents){
+ setValue(c, settings.get(c.getName()));
+ }
}
/**
@@ -212,6 +257,7 @@ protected void setFinishEnabled(boolean enabled){
/**
* Returns a string reperesntation of this wizard page.
*/
+ @Override
public String toString(){
return getId() + ": " +getTitle();
}
@@ -151,6 +151,7 @@ public void updateSettings(WizardSettings settings) {
* displayed:
*/
public void rendering(List<WizardPage> path, WizardSettings settings) {
+ super.rendering(path, settings);
setFinishEnabled(true);
setNextEnabled(false);
}
@@ -31,6 +31,14 @@ public Object getValue() {
return "sample value";
}
+ /* (non-Javadoc)
+ * @see org.ciscavate.cjwizard.CustomWizardComponent#setValue(java.lang.Object)
+ */
+ @Override
+ public void setValue(Object o)
+ {
+ // no-op
+ }
}
private class TestWizardPage extends WizardPage{
@@ -89,11 +97,11 @@ public void testUpdateSettings_keysExist() {
WizardSettings settings = new StackWizardSettings();
page.updateSettings(settings);
- Assert.assertTrue("Key set did not contain needed key.",
+ Assert.assertTrue("Key set did not contain needed key.",
settings.keySet().contains("test box"));
- Assert.assertTrue("Key set did not contain needed key.",
+ Assert.assertTrue("Key set did not contain needed key.",
settings.keySet().contains("test field"));
- Assert.assertTrue("Key set did not contain needed key.",
+ Assert.assertTrue("Key set did not contain needed key.",
settings.keySet().contains("custom comp"));
}

0 comments on commit 9eeb6e8

Please sign in to comment.