1 56 package org.objectstyle.cayenne.modeler.dialog.pref; 57 58 import java.awt.Component ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.util.HashMap ; 62 import java.util.Map ; 63 64 import javax.swing.JDialog ; 65 import javax.swing.JList ; 66 import javax.swing.event.ListSelectionEvent ; 67 import javax.swing.event.ListSelectionListener ; 68 69 import org.objectstyle.cayenne.modeler.pref.ComponentGeometry; 70 import org.objectstyle.cayenne.modeler.util.CayenneController; 71 import org.objectstyle.cayenne.pref.Domain; 72 import org.objectstyle.cayenne.pref.PreferenceEditor; 73 import org.objectstyle.cayenne.pref.PreferenceException; 74 75 80 public class PreferenceDialog extends CayenneController { 81 82 public static final String GENERAL_KEY = "General"; 83 public static final String DATA_SOURCES_KEY = "Local DataSources"; 84 public static final String CLASS_PATH_KEY = "ClassPath"; 85 86 private static final String [] preferenceMenus = new String [] { 87 GENERAL_KEY, DATA_SOURCES_KEY, CLASS_PATH_KEY 88 }; 89 90 protected PreferenceDialogView view; 91 protected Map detailControllers; 92 protected PreferenceEditor editor; 93 94 public PreferenceDialog(CayenneController parent) { 95 super(parent); 96 this.view = new PreferenceDialogView(); 97 this.detailControllers = new HashMap (); 98 99 this.editor = new CayenneModelerPreferenceEditor(application); 101 102 initBindings(); 103 } 104 105 protected void initBindings() { 106 final JList list = view.getList(); 107 list.setListData(preferenceMenus); 108 list.addListSelectionListener(new ListSelectionListener () { 109 110 public void valueChanged(ListSelectionEvent e) { 111 Object selection = list.getSelectedValue(); 112 if (selection != null) { 113 showDetailViewAction(selection.toString()); 114 } 115 } 116 }); 117 118 view.getCancelButton().addActionListener(new ActionListener () { 119 120 public void actionPerformed(ActionEvent e) { 121 cancelAction(); 122 } 123 }); 124 125 view.getSaveButton().addActionListener(new ActionListener () { 126 127 public void actionPerformed(ActionEvent e) { 128 savePreferencesAction(); 129 } 130 }); 131 } 132 133 public void cancelAction() { 134 editor.revert(); 135 view.dispose(); 136 } 137 138 public void savePreferencesAction() { 139 editor.save(); 140 141 143 view.dispose(); 144 } 145 146 150 public void showDataSourceEditorAction(Object dataSourceKey) { 151 showDetailViewAction(DATA_SOURCES_KEY); 153 154 DataSourcePreferences controller = (DataSourcePreferences) detailControllers 155 .get(DATA_SOURCES_KEY); 156 controller.editDataSourceAction(dataSourceKey); 157 } 158 159 162 public void showDetailViewAction(String name) { 163 164 if (!detailControllers.containsKey(name)) { 165 CayenneController c; 166 167 if (GENERAL_KEY.equals(name)) { 168 c = new GeneralPreferences(this); 169 } 170 else if (DATA_SOURCES_KEY.equals(name)) { 171 c = new DataSourcePreferences(this); 172 } 173 else if (CLASS_PATH_KEY.equals(name)) { 174 c = new ClasspathPreferences(this); 175 } 176 else { 177 throw new PreferenceException("Unknown detail key: " + name); 178 } 179 180 detailControllers.put(name, c); 181 view.getDetailPanel().add(c.getView(), name); 182 183 view.getDetailPanel().getParent().validate(); 185 } 186 187 view.getDetailLayout().show(view.getDetailPanel(), name); 188 } 189 190 public void startupAction() { 191 192 Domain prefDomain = application.getPreferenceDomain().getSubdomain( 194 view.getClass()); 195 ComponentGeometry geometry = ComponentGeometry.getPreference(prefDomain); 196 geometry.bind(view, 650, 350); 197 geometry.bindIntProperty(view.getSplit(), "dividerLocation", 220); 198 199 centerView(); 201 makeCloseableOnEscape(); 202 203 view.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 204 view.setModal(true); 205 view.setVisible(true); 206 207 } 208 209 public Component getView() { 210 return view; 211 } 212 213 public PreferenceEditor getEditor() { 214 return editor; 215 } 216 } | Popular Tags |