KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > WizardEditorPane


1 package net.suberic.util.gui.propedit;
2 import java.awt.CardLayout JavaDoc;
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.util.*;
5 import javax.swing.*;
6
7 /**
8  * A SwingEditorPane that implements Wizard functionality.
9  */

10 public class WizardEditorPane extends CompositeSwingPropertyEditor {
11
12   Map<String JavaDoc, SwingPropertyEditor> layoutMap = new HashMap<String JavaDoc, SwingPropertyEditor>();
13   CardLayout JavaDoc layout;
14   WizardPropertyEditor wizardContainer = null;
15   WizardController controller = null;
16
17   /**
18    * Configures the editor.
19    */

20   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
21     configureBasic(propertyName, template, propertyBaseName, newManager);
22
23     editors = new ArrayList<SwingPropertyEditor>();
24
25     layout = new CardLayout JavaDoc();
26     this.setLayout(layout);
27
28     // see about loading the WizardController
29
String JavaDoc controllerClassString = manager.getProperty(template + ".controllerClass", "");
30     if (controllerClassString.length() > 0) {
31       try {
32         Class JavaDoc controllerClass = Class.forName(controllerClassString);
33         Constructor JavaDoc constructor = controllerClass.getConstructor(Class.forName("java.lang.String"), Class.forName("net.suberic.util.gui.propedit.WizardEditorPane"));
34         controller = (WizardController) constructor.newInstance(template, this);
35       } catch (Exception JavaDoc e) {
36         getLogger().log(java.util.logging.Level.SEVERE, "Error loading controller class " + controllerClassString, e);
37         controller = new WizardController(template, this);
38       }
39     } else {
40       controller = new WizardController(template, this);
41     }
42     controller.initialize();
43   }
44
45   /**
46    * Creates the editors for each state.
47    */

48   public void createEditors(List<String JavaDoc> stateList) {
49     for (String JavaDoc stateString: stateList) {
50       String JavaDoc subProperty = createSubProperty(manager.getProperty(editorTemplate + "._states." + stateString + ".editor", ""));
51       String JavaDoc subTemplate = createSubTemplate(manager.getProperty(editorTemplate + "._states." + stateString + ".editor", ""));
52       SwingPropertyEditor newEditor = (SwingPropertyEditor) manager.getFactory().createEditor(subProperty, subTemplate, subTemplate, manager);
53       layoutMap.put(stateString, newEditor);
54       this.add(stateString, newEditor);
55       editors.add(newEditor);
56     }
57   }
58
59   /**
60    * Commits the value for the given state.
61    */

62   public void setValue(String JavaDoc state) throws PropertyValueVetoException {
63     SwingPropertyEditor editor = layoutMap.get(state);
64     editor.setValue();
65   }
66
67   /**
68    * Validates the given state.
69    */

70   public void validateProperty(String JavaDoc state) throws PropertyValueVetoException {
71     SwingPropertyEditor editor = layoutMap.get(state);
72     editor.validateProperty();
73   }
74
75   /**
76    * Loads the current state.
77    */

78   public void loadState(String JavaDoc state) {
79     layout.show(this, state);
80     loadContainerState();
81     SwingUtilities.invokeLater(new Runnable JavaDoc() {
82         public void run() {
83           acceptDefaultFocus();
84         }
85       });
86   }
87
88   /**
89    * Loads the state info into the container.
90    */

91   public void loadContainerState() {
92     if (getWizardContainer() != null) {
93       getWizardContainer().setBeginningState(inBeginningState());
94       getWizardContainer().setEndState(inEndState());
95     }
96   }
97
98   /**
99    * Returns the controller.
100    */

101   public WizardController getController() {
102     return controller;
103   }
104
105
106   /**
107    * Returns the current Wizard state.
108    */

109   public String JavaDoc getState() {
110     return controller.getState();
111   }
112
113   /**
114    * Returns if this is the beginning state.
115    */

116   public boolean inBeginningState() {
117     return controller.inBeginningState();
118   }
119
120   /**
121    * Returns if this is in a valid end state.
122    */

123   public boolean inEndState() {
124     return controller.inEndState();
125   }
126
127   /**
128    * Goes back a state.
129    */

130   public void back() {
131     controller.back();
132   }
133
134   /**
135    * Goes forward a state.
136    */

137   public void next() throws PropertyValueVetoException {
138     controller.next();
139   }
140
141   /**
142    * Sets the WizardPropertyEditor container.
143    */

144   public void setWizardContainer(WizardPropertyEditor wpe) {
145     wizardContainer = wpe;
146   }
147
148   /**
149    * Gets the WizardPropertyEditor container.
150    */

151   public WizardPropertyEditor getWizardContainer() {
152     return wizardContainer;
153   }
154
155   /**
156    * Accepts or rejects the initial focus for this component.
157    */

158   public boolean acceptDefaultFocus() {
159     SwingPropertyEditor currentEditor = layoutMap.get(getState());
160     return currentEditor.acceptDefaultFocus();
161   }
162
163 }
164
Popular Tags