1 56 package org.objectstyle.cayenne.modeler.dialog.db; 57 58 import java.awt.Component ; 59 import java.sql.Connection ; 60 import java.sql.SQLException ; 61 import java.util.Arrays ; 62 import java.util.Map ; 63 64 import javax.swing.DefaultComboBoxModel ; 65 import javax.swing.JDialog ; 66 67 import org.objectstyle.cayenne.modeler.ClassLoadingService; 68 import org.objectstyle.cayenne.modeler.dialog.pref.PreferenceDialog; 69 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo; 70 import org.objectstyle.cayenne.modeler.util.CayenneController; 71 import org.objectstyle.cayenne.swing.BindingBuilder; 72 import org.objectstyle.cayenne.swing.ObjectBinding; 73 74 80 public class DataSourceWizard extends CayenneController { 81 82 protected DataSourceWizardView view; 83 84 protected DBConnectionInfo altDataSource; 85 protected String altDataSourceKey; 86 protected ObjectBinding dataSourceBinding; 87 protected Map dataSources; 88 89 protected String dataSourceKey; 90 91 protected DBConnectionInfo connectionInfo; 94 95 protected boolean canceled; 96 97 public DataSourceWizard(CayenneController parent, String title, 98 String altDataSourceKey, DBConnectionInfo altDataSource) { 99 super(parent); 100 101 this.view = new DataSourceWizardView(this); 102 this.view.setTitle(title); 103 this.altDataSource = altDataSource; 104 this.altDataSourceKey = altDataSourceKey; 105 this.connectionInfo = new DBConnectionInfo(); 106 107 initBindings(); 108 } 109 110 protected void initBindings() { 111 BindingBuilder builder = new BindingBuilder( 112 getApplication().getBindingFactory(), 113 this); 114 115 dataSourceBinding = builder.bindToComboSelection( 116 view.getDataSources(), 117 "dataSourceKey"); 118 119 builder.bindToAction(view.getCancelButton(), "cancelAction()"); 120 builder.bindToAction(view.getOkButton(), "okAction()"); 121 builder.bindToAction(view.getConfigButton(), "dataSourceConfigAction()"); 122 } 123 124 public String getDataSourceKey() { 125 return dataSourceKey; 126 } 127 128 public void setDataSourceKey(String dataSourceKey) { 129 this.dataSourceKey = dataSourceKey; 130 131 DBConnectionInfo currentInfo = (DBConnectionInfo) dataSources.get(dataSourceKey); 133 if (currentInfo != null) { 134 currentInfo.copyTo(connectionInfo); 135 } 136 else { 137 connectionInfo = new DBConnectionInfo(); 138 } 139 140 view.getConnectionInfo().setConnectionInfo(connectionInfo); 141 } 142 143 147 public boolean startupAction() { 148 this.canceled = true; 149 150 refreshDataSources(); 151 152 view.pack(); 153 view.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 154 view.setModal(true); 155 makeCloseableOnEscape(); 156 centerView(); 157 view.setVisible(true); 158 159 return !canceled; 160 } 161 162 public DBConnectionInfo getConnectionInfo() { 163 return connectionInfo; 164 } 165 166 170 public void okAction() { 171 DBConnectionInfo info = getConnectionInfo(); 172 ClassLoadingService classLoader = getApplication().getClassLoadingService(); 173 174 try { 176 info.makeAdapter(classLoader); 177 } 178 catch (Throwable th) { 179 reportError("DbAdapter Error", th); 180 return; 181 } 182 183 try { 186 Connection connection = info.makeDataSource(classLoader).getConnection(); 187 try { 188 connection.close(); 189 } 190 catch (SQLException ex) { 191 } 193 } 194 catch (Throwable th) { 195 reportError("Connection Error", th); 196 return; 197 } 198 199 canceled = false; 201 view.dispose(); 202 } 203 204 public void cancelAction() { 205 canceled = true; 206 view.dispose(); 207 } 208 209 212 public void dataSourceConfigAction() { 213 PreferenceDialog prefs = new PreferenceDialog(this); 214 prefs.showDataSourceEditorAction(dataSourceKey); 215 prefs.startupAction(); 216 217 refreshDataSources(); 218 } 219 220 public Component getView() { 221 return view; 222 } 223 224 protected void refreshDataSources() { 225 this.dataSources = getApplication().getPreferenceDomain().getDetailsMap( 226 DBConnectionInfo.class); 227 228 if (altDataSourceKey != null 229 && !dataSources.containsKey(altDataSourceKey) 230 && altDataSource != null) { 231 dataSources.put(altDataSourceKey, altDataSource); 232 } 233 234 Object [] keys = dataSources.keySet().toArray(); 235 Arrays.sort(keys); 236 view.getDataSources().setModel(new DefaultComboBoxModel (keys)); 237 238 if (getDataSourceKey() == null) { 239 String key = null; 240 241 if (altDataSourceKey != null) { 242 key = altDataSourceKey; 243 } 244 else if (keys.length > 0) { 245 key = keys[0].toString(); 246 } 247 248 setDataSourceKey(key); 249 dataSourceBinding.updateView(); 250 } 251 } 252 } | Popular Tags |