1 56 package org.objectstyle.cayenne.modeler.dialog.pref; 57 58 import java.awt.Component ; 59 import java.util.Map ; 60 61 import javax.swing.DefaultComboBoxModel ; 62 import javax.swing.JOptionPane ; 63 64 import org.objectstyle.cayenne.modeler.AdapterMapping; 65 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo; 66 import org.objectstyle.cayenne.modeler.util.CayenneController; 67 import org.objectstyle.cayenne.modeler.util.DbAdapterInfo; 68 import org.objectstyle.cayenne.pref.Domain; 69 import org.objectstyle.cayenne.pref.PreferenceEditor; 70 import org.objectstyle.cayenne.swing.BindingBuilder; 71 72 75 public class DataSourceCreator extends CayenneController { 76 77 private static final String NO_ADAPTER = "Custom / Undefined"; 78 79 protected DataSourceCreatorView view; 80 protected PreferenceEditor editor; 81 protected Domain domain; 82 protected boolean canceled; 83 protected Map dataSources; 84 85 public DataSourceCreator(DataSourcePreferences parent) { 86 super(parent); 87 this.view = new DataSourceCreatorView(); 88 this.editor = parent.getEditor(); 89 this.domain = parent.getDataSourceDomain(); 90 this.dataSources = parent.getDataSources(); 91 92 DefaultComboBoxModel model = new DefaultComboBoxModel (DbAdapterInfo 93 .getStandardAdapters()); 94 model.insertElementAt(NO_ADAPTER, 0); 95 this.view.getAdapters().setModel(model); 96 this.view.getAdapters().setSelectedIndex(0); 97 98 String suggestion = "DataSource0"; 99 for (int i = 1; i <= dataSources.size(); i++) { 100 suggestion = "DataSource" + i; 101 if (!dataSources.containsKey(suggestion)) { 102 break; 103 } 104 } 105 106 this.view.getDataSourceName().setText(suggestion); 107 initBindings(); 108 } 109 110 public Component getView() { 111 return view; 112 } 113 114 protected void initBindings() { 115 BindingBuilder builder = new BindingBuilder( 116 getApplication().getBindingFactory(), 117 this); 118 builder.bindToAction(view.getCancelButton(), "cancelAction()"); 119 builder.bindToAction(view.getOkButton(), "okAction()"); 120 } 121 122 public void okAction() { 123 if (getName() == null) { 124 JOptionPane.showMessageDialog( 125 view, 126 "Enter DataSource Name", 127 null, 128 JOptionPane.WARNING_MESSAGE); 129 } 130 else if (dataSources.containsKey(getName())) { 131 JOptionPane.showMessageDialog( 132 view, 133 "'" + getName() + "' is already in use, enter a different name", 134 null, 135 JOptionPane.WARNING_MESSAGE); 136 } 137 else { 138 canceled = false; 139 view.dispose(); 140 } 141 } 142 143 public void cancelAction() { 144 canceled = true; 145 view.dispose(); 146 } 147 148 151 public DBConnectionInfo startupAction() { 152 canceled = true; 154 155 view.setModal(true); 156 view.pack(); 157 view.setResizable(false); 158 makeCloseableOnEscape(); 159 centerView(); 160 161 view.setVisible(true); 162 return createDataSource(); 163 } 164 165 public String getName() { 166 String name = view.getDataSourceName().getText(); 167 return (name.length() > 0) ? name : null; 168 } 169 170 protected DBConnectionInfo createDataSource() { 171 if (canceled) { 172 return null; 173 } 174 175 DBConnectionInfo dataSource = (DBConnectionInfo) editor.createDetail( 176 domain, 177 getName(), 178 DBConnectionInfo.class); 179 180 Object adapter = view.getAdapters().getSelectedItem(); 181 if (NO_ADAPTER.equals(adapter)) { 182 adapter = null; 183 } 184 185 if (adapter != null) { 186 String adapterString = adapter.toString(); 187 dataSource.setDbAdapter(adapterString); 188 189 AdapterMapping defaultMap = getApplication().getAdapterMapping(); 191 dataSource.setJdbcDriver(defaultMap.jdbcDriverForAdapter(adapterString)); 192 dataSource.setUrl(defaultMap.jdbcURLForAdapter(adapterString)); 193 } 194 195 return dataSource; 196 } 197 } | Popular Tags |