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.JOptionPane ; 62 63 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo; 64 import org.objectstyle.cayenne.modeler.util.CayenneController; 65 import org.objectstyle.cayenne.pref.Domain; 66 import org.objectstyle.cayenne.pref.PreferenceEditor; 67 import org.objectstyle.cayenne.swing.BindingBuilder; 68 69 72 public class DataSourceDuplicator extends CayenneController { 73 74 protected DataSourceDuplicatorView view; 75 protected PreferenceEditor editor; 76 protected Domain domain; 77 protected boolean canceled; 78 protected Map dataSources; 79 protected String prototypeKey; 80 81 public DataSourceDuplicator(DataSourcePreferences parent, String prototypeKey) { 82 super(parent); 83 this.view = new DataSourceDuplicatorView("Create a copy of \"" 84 + prototypeKey 85 + "\""); 86 this.editor = parent.getEditor(); 87 this.domain = parent.getDataSourceDomain(); 88 this.dataSources = parent.getDataSources(); 89 this.prototypeKey = prototypeKey; 90 91 String suggestion = prototypeKey + "0"; 92 for (int i = 1; i <= dataSources.size(); i++) { 93 suggestion = prototypeKey + i; 94 if (!dataSources.containsKey(suggestion)) { 95 break; 96 } 97 } 98 99 this.view.getDataSourceName().setText(suggestion); 100 initBindings(); 101 } 102 103 public Component getView() { 104 return view; 105 } 106 107 protected void initBindings() { 108 BindingBuilder builder = new BindingBuilder( 109 getApplication().getBindingFactory(), 110 this); 111 builder.bindToAction(view.getCancelButton(), "cancelAction()"); 112 builder.bindToAction(view.getOkButton(), "okAction()"); 113 } 114 115 public void okAction() { 116 if (getName() == null) { 117 JOptionPane.showMessageDialog( 118 view, 119 "Enter DataSource Name", 120 null, 121 JOptionPane.WARNING_MESSAGE); 122 } 123 else if (dataSources.containsKey(getName())) { 124 JOptionPane.showMessageDialog( 125 view, 126 "'" + getName() + "' is already in use, enter a different name", 127 null, 128 JOptionPane.WARNING_MESSAGE); 129 } 130 else { 131 canceled = false; 132 view.dispose(); 133 } 134 } 135 136 public void cancelAction() { 137 canceled = true; 138 view.dispose(); 139 } 140 141 144 public DBConnectionInfo startupAction() { 145 canceled = true; 147 148 view.setModal(true); 149 view.pack(); 150 view.setResizable(false); 151 makeCloseableOnEscape(); 152 centerView(); 153 154 view.setVisible(true); 155 return createDataSource(); 156 } 157 158 public String getName() { 159 String name = view.getDataSourceName().getText(); 160 return (name.length() > 0) ? name : null; 161 } 162 163 protected DBConnectionInfo createDataSource() { 164 if (canceled) { 165 return null; 166 } 167 168 DBConnectionInfo prototype = (DBConnectionInfo) dataSources.get(prototypeKey); 169 DBConnectionInfo dataSource = (DBConnectionInfo) editor.createDetail( 170 domain, 171 getName(), 172 DBConnectionInfo.class); 173 174 prototype.copyTo(dataSource); 175 return dataSource; 176 } 177 178 } | Popular Tags |