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 10 public class AddressBookWizardController extends WizardController { 11 12 15 public AddressBookWizardController(String sourceTemplate, WizardEditorPane wep) { 16 super(sourceTemplate, wep); 17 } 18 19 23 public void checkStateTransition(String oldState, String newState) throws PropertyValueVetoException { 24 getEditorPane().validateProperty(oldState); 25 if (newState.equals("name")) { 26 PropertyEditorUI addressBookNameEditor = getManager().getPropertyEditor("AddressBook._newValueWizard.name.bookName"); 28 29 String addressBookName = "AddressBook"; 30 setUniqueProperty(addressBookNameEditor, addressBookName, "AddressBook._newValueWizard.name.bookName"); 31 } 32 } 33 34 37 protected void saveProperties() throws PropertyValueVetoException { 38 Properties addressBookProperties = createAddressBookProperties(); 39 41 addAll(addressBookProperties); 42 43 String 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 appendProperty("AddressBook", addressBookName); 50 } 51 } 52 53 56 public void finishWizard() throws PropertyValueVetoException { 57 String 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 ioe) { 69 throw new PropertyValueVetoException("AddressBook._newValueWizard.config.filename", filename, ioe.getMessage(), null); 70 } 71 saveProperties(); 72 getEditorPane().getWizardContainer().closeWizard(); 73 } 74 75 78 public Properties createAddressBookProperties() { 79 Properties returnValue = new Properties(); 80 81 String addressBookName = getManager().getCurrentProperty("AddressBook._newValueWizard.name.bookName", ""); 82 83 String type = getManager().getCurrentProperty("AddressBook._newValueWizard.type.bookType", ""); 84 String 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 96 void addAll(Properties props) { 97 Set<String > names = props.stringPropertyNames(); 98 for (String name: names) { 99 getManager().setProperty(name, props.getProperty(name)); 100 } 101 } 102 103 public void setUniqueProperty(PropertyEditorUI editor, String originalValue, String propertyName) { 104 String 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 } 118 } 119 } 120 121 124 public void appendProperty(String property, String value) { 125 List<String > current = getManager().getPropertyAsList(property, ""); 126 current.add(value); 127 getManager().setProperty(property, VariableBundle.convertToString(current)); 128 } 129 } 130 | Popular Tags |