1 56 package org.objectstyle.cayenne.modeler.dialog.pref; 57 58 import java.awt.Component ; 59 import java.sql.Connection ; 60 import java.sql.Driver ; 61 import java.sql.SQLException ; 62 import java.util.Arrays ; 63 import java.util.Map ; 64 65 import javax.swing.DefaultComboBoxModel ; 66 import javax.swing.JOptionPane ; 67 68 import org.objectstyle.cayenne.conn.DriverDataSource; 69 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo; 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.swing.BindingBuilder; 74 import org.objectstyle.cayenne.util.Util; 75 76 81 public class DataSourcePreferences extends CayenneController { 82 83 protected DataSourcePreferencesView view; 84 protected PreferenceEditor editor; 85 protected String dataSourceKey; 86 protected Map dataSources; 87 88 public DataSourcePreferences(PreferenceDialog parentController) { 89 super(parentController); 90 91 this.view = new DataSourcePreferencesView(this); 92 this.editor = parentController.getEditor(); 93 94 this.dataSources = getDataSourceDomain().getDetailsMap(DBConnectionInfo.class); 96 97 Object [] keys = dataSources.keySet().toArray(); 98 Arrays.sort(keys); 99 DefaultComboBoxModel dataSourceModel = new DefaultComboBoxModel (keys); 100 view.getDataSources().setModel(dataSourceModel); 101 102 initBindings(); 103 104 if (keys.length > 0) { 106 view.getDataSources().setSelectedIndex(0); 107 editDataSourceAction(); 108 } 109 } 110 111 public Component getView() { 112 return view; 113 } 114 115 protected void initBindings() { 116 BindingBuilder builder = new BindingBuilder( 117 getApplication().getBindingFactory(), 118 this); 119 builder.bindToAction(view.getAddDataSource(), "newDataSourceAction()"); 120 builder 121 .bindToAction( 122 view.getDuplicateDataSource(), 123 "duplicateDataSourceAction()"); 124 builder.bindToAction(view.getRemoveDataSource(), "removeDataSourceAction()"); 125 builder.bindToAction(view.getTestDataSource(), "testDataSourceAction()"); 126 127 builder.bindToComboSelection(view.getDataSources(), "dataSourceKey"); 128 } 129 130 public Domain getDataSourceDomain() { 131 return editor.editableInstance(getApplication().getPreferenceDomain()); 132 } 133 134 public PreferenceEditor getEditor() { 135 return editor; 136 } 137 138 public Map getDataSources() { 139 return dataSources; 140 } 141 142 public String getDataSourceKey() { 143 return dataSourceKey; 144 } 145 146 public void setDataSourceKey(String dataSourceKey) { 147 this.dataSourceKey = dataSourceKey; 148 editDataSourceAction(); 149 } 150 151 public DBConnectionInfo getConnectionInfo() { 152 return (DBConnectionInfo) dataSources.get(dataSourceKey); 153 } 154 155 158 public void newDataSourceAction() { 159 160 DataSourceCreator creatorWizard = new DataSourceCreator(this); 161 DBConnectionInfo dataSource = creatorWizard.startupAction(); 162 163 if (dataSource != null) { 164 dataSources.put(creatorWizard.getName(), dataSource); 165 166 Object [] keys = dataSources.keySet().toArray(); 167 Arrays.sort(keys); 168 view.getDataSources().setModel(new DefaultComboBoxModel (keys)); 169 view.getDataSources().setSelectedItem(creatorWizard.getName()); 170 editDataSourceAction(); 171 } 172 } 173 174 177 public void duplicateDataSourceAction() { 178 Object selected = view.getDataSources().getSelectedItem(); 179 if (selected != null) { 180 DataSourceDuplicator wizard = new DataSourceDuplicator(this, selected 181 .toString()); 182 DBConnectionInfo dataSource = wizard.startupAction(); 183 184 if (dataSource != null) { 185 dataSources.put(wizard.getName(), dataSource); 186 187 Object [] keys = dataSources.keySet().toArray(); 188 Arrays.sort(keys); 189 view.getDataSources().setModel(new DefaultComboBoxModel (keys)); 190 view.getDataSources().setSelectedItem(wizard.getName()); 191 editDataSourceAction(); 192 } 193 } 194 } 195 196 199 public void removeDataSourceAction() { 200 String key = getDataSourceKey(); 201 if (key != null) { 202 editor.deleteDetail(getDataSourceDomain(), key); 203 dataSources.remove(key); 204 205 Object [] keys = dataSources.keySet().toArray(); 206 Arrays.sort(keys); 207 view.getDataSources().setModel(new DefaultComboBoxModel (keys)); 208 editDataSourceAction(keys.length > 0 ? keys[0] : null); 209 } 210 } 211 212 215 public void editDataSourceAction(Object dataSourceKey) { 216 view.getDataSources().setSelectedItem(dataSourceKey); 217 editDataSourceAction(); 218 } 219 220 223 public void editDataSourceAction() { 224 this.view.getDataSourceEditor().setConnectionInfo(getConnectionInfo()); 225 } 226 227 230 public void testDataSourceAction() { 231 DBConnectionInfo currentDataSource = getConnectionInfo(); 232 if (currentDataSource == null) { 233 return; 234 } 235 236 if (currentDataSource.getJdbcDriver() == null) { 237 JOptionPane.showMessageDialog( 238 null, 239 "No JDBC Driver specified", 240 "Warning", 241 JOptionPane.WARNING_MESSAGE); 242 return; 243 } 244 245 if (currentDataSource.getUrl() == null) { 246 JOptionPane.showMessageDialog( 247 null, 248 "No Database URL specified", 249 "Warning", 250 JOptionPane.WARNING_MESSAGE); 251 return; 252 } 253 254 try { 255 Class driverClass = getApplication().getClassLoadingService().loadClass( 256 currentDataSource.getJdbcDriver()); 257 Driver driver = (Driver ) driverClass.newInstance(); 258 259 Connection c = new DriverDataSource( 261 driver, 262 currentDataSource.getUrl(), 263 currentDataSource.getUserName(), 264 currentDataSource.getPassword()).getConnection(); 265 try { 266 c.close(); 267 } 268 catch (SQLException e) { 269 } 271 272 JOptionPane.showMessageDialog( 273 null, 274 "Connected Successfully", 275 "Success", 276 JOptionPane.INFORMATION_MESSAGE); 277 } 278 catch (Throwable th) { 279 th = Util.unwindException(th); 280 JOptionPane.showMessageDialog(null, "Error connecting to DB: " 281 + th.getLocalizedMessage(), "Warning", JOptionPane.WARNING_MESSAGE); 282 return; 283 } 284 } 285 286 } | Popular Tags |