KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > AddressBookWizardController


1 package net.suberic.pooka.gui.propedit;
2 import java.io.*;
3 import java.util.*;
4 import net.suberic.util.gui.propedit.*;
5 import net.suberic.util.VariableBundle;
6
7 /**
8  * The controller class for the AddressBookWizard.
9  */

10 public class AddressBookWizardController extends WizardController {
11
12   /**
13    * Creates a AddressBookWizardController.
14    */

15   public AddressBookWizardController(String JavaDoc sourceTemplate, WizardEditorPane wep) {
16     super(sourceTemplate, wep);
17   }
18
19   /**
20    * Checks the state transition to make sure that we can move from
21    * state to state.
22    */

23   public void checkStateTransition(String JavaDoc oldState, String JavaDoc newState) throws PropertyValueVetoException {
24     getEditorPane().validateProperty(oldState);
25     if (newState.equals("name")) {
26       // set the address book name
27
PropertyEditorUI addressBookNameEditor = getManager().getPropertyEditor("AddressBook._newValueWizard.name.bookName");
28
29       String JavaDoc addressBookName = "AddressBook";
30       setUniqueProperty(addressBookNameEditor, addressBookName, "AddressBook._newValueWizard.name.bookName");
31     }
32   }
33
34   /**
35    * Saves all of the properties for this wizard.
36    */

37   protected void saveProperties() throws PropertyValueVetoException {
38     Properties addressBookProperties = createAddressBookProperties();
39     //getManager().clearValues();
40

41     addAll(addressBookProperties);
42
43     String JavaDoc addressBookName = getManager().getCurrentProperty("AddressBook._newValueWizard.name.bookName", "");
44     MultiEditorPane mep = (MultiEditorPane) getManager().getPropertyEditor("AddressBook");
45     if (mep != null) {
46       mep.addNewValue(addressBookName);
47     } else {
48       // if there's no editor, then set the value itself.
49
appendProperty("AddressBook", addressBookName);
50     }
51   }
52
53   /**
54    * Finsihes the wizard.
55    */

56   public void finishWizard() throws PropertyValueVetoException {
57     // check to make sure that the new file is valid.
58
String JavaDoc filename = getManager().getCurrentProperty("AddressBook._newValueWizard.config.filename", "");
59
60     File file = new File(filename);
61     try {
62       if (! file.exists()) {
63         file.createNewFile();
64       }
65       if (! file.canRead()) {
66         throw new PropertyValueVetoException("AddressBook._newValueWizard.config.filename", filename, getManager().getProperty("error.cannotReadFile", "Can not read file."), null);
67       }
68     } catch (java.io.IOException JavaDoc ioe) {
69       throw new PropertyValueVetoException("AddressBook._newValueWizard.config.filename", filename, ioe.getMessage(), null);
70     }
71     saveProperties();
72     getEditorPane().getWizardContainer().closeWizard();
73   }
74
75   /**
76    * Creates the addressBookProperties from the wizard values.
77    */

78   public Properties createAddressBookProperties() {
79     Properties returnValue = new Properties();
80
81     String JavaDoc addressBookName = getManager().getCurrentProperty("AddressBook._newValueWizard.name.bookName", "");
82
83     String JavaDoc type = getManager().getCurrentProperty("AddressBook._newValueWizard.type.bookType", "");
84     String JavaDoc filename = getManager().getCurrentProperty("AddressBook._newValueWizard.config.filename", "");
85
86     returnValue.setProperty("AddressBook." + addressBookName + ".type", type);
87     returnValue.setProperty("AddressBook." + addressBookName + ".filename", filename);
88
89     return returnValue;
90   }
91
92   /**
93    * Adds all of the values from the given Properties to the
94    * PropertyEditorManager.
95    */

96   void addAll(Properties props) {
97     Set<String JavaDoc> names = props.stringPropertyNames();
98     for (String JavaDoc name: names) {
99       getManager().setProperty(name, props.getProperty(name));
100     }
101   }
102
103   public void setUniqueProperty(PropertyEditorUI editor, String JavaDoc originalValue, String JavaDoc propertyName) {
104     String JavaDoc value = originalValue;
105     boolean success = false;
106     for (int i = 0 ; ! success && i < 10; i++) {
107       if (i != 0) {
108         value = originalValue + "_" + i;
109       }
110       try {
111         editor.setOriginalValue(value);
112         editor.resetDefaultValue();
113         getManager().setTemporaryProperty(propertyName, value);
114         success = true;
115       } catch (PropertyValueVetoException pvve) {
116         // on an exception, just start over.
117
}
118     }
119   }
120
121   /**
122    * Appends the given value to the property.
123    */

124   public void appendProperty(String JavaDoc property, String JavaDoc value) {
125     List<String JavaDoc> current = getManager().getPropertyAsList(property, "");
126     current.add(value);
127     getManager().setProperty(property, VariableBundle.convertToString(current));
128   }
129 }
130
Popular Tags