1 56 package org.objectstyle.cayenne.modeler.dialog.db; 57 58 import java.awt.Component ; 59 import java.io.File ; 60 import java.io.FileWriter ; 61 import java.io.IOException ; 62 import java.io.PrintWriter ; 63 import java.util.Iterator ; 64 65 import javax.sql.DataSource ; 66 import javax.swing.JDialog ; 67 import javax.swing.JFileChooser ; 68 import javax.swing.JOptionPane ; 69 import javax.swing.event.ChangeEvent ; 70 import javax.swing.event.ChangeListener ; 71 72 import org.objectstyle.cayenne.access.DbGenerator; 73 import org.objectstyle.cayenne.dba.DbAdapter; 74 import org.objectstyle.cayenne.map.DataMap; 75 import org.objectstyle.cayenne.modeler.Application; 76 import org.objectstyle.cayenne.modeler.ProjectController; 77 import org.objectstyle.cayenne.modeler.dialog.ValidationResultBrowser; 78 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo; 79 import org.objectstyle.cayenne.modeler.pref.DBGeneratorDefaults; 80 import org.objectstyle.cayenne.modeler.util.CayenneController; 81 import org.objectstyle.cayenne.swing.BindingBuilder; 82 import org.objectstyle.cayenne.swing.ObjectBinding; 83 import org.objectstyle.cayenne.validation.ValidationResult; 84 85 88 public class DBGeneratorOptions extends CayenneController { 89 90 protected DBGeneratorOptionsView view; 91 protected ObjectBinding[] optionBindings; 92 protected ObjectBinding sqlBinding; 93 94 protected DBConnectionInfo connectionInfo; 95 protected DataMap dataMap; 96 protected DBGeneratorDefaults generatorDefaults; 97 protected DbGenerator generator; 98 protected String textForSQL; 99 100 protected TableSelector tables; 101 102 public DBGeneratorOptions(ProjectController parent, String title, 103 DBConnectionInfo connectionInfo, DataMap dataMap) { 104 super(parent); 105 106 this.dataMap = dataMap; 107 this.tables = new TableSelector(parent); 108 this.view = new DBGeneratorOptionsView(tables.getView()); 109 this.connectionInfo = connectionInfo; 110 this.generatorDefaults = (DBGeneratorDefaults) parent 111 .getPreferenceDomainForProject() 112 .getDetail("DbGenerator", DBGeneratorDefaults.class, true); 113 114 this.view.setTitle(title); 115 initController(); 116 117 tables.updateTables(dataMap); 118 prepareGenerator(); 119 generatorDefaults.configureGenerator(generator); 120 createSQL(); 121 refreshView(); 122 } 123 124 public Component getView() { 125 return view; 126 } 127 128 public DBGeneratorDefaults getGeneratorDefaults() { 129 return generatorDefaults; 130 } 131 132 public String getTextForSQL() { 133 return textForSQL; 134 } 135 136 protected void initController() { 137 138 BindingBuilder builder = new BindingBuilder( 139 getApplication().getBindingFactory(), 140 this); 141 142 sqlBinding = builder.bindToTextArea(view.getSql(), "textForSQL"); 143 144 optionBindings = new ObjectBinding[5]; 145 optionBindings[0] = builder.bindToStateChangeAndAction(view.getCreateFK(), 146 "generatorDefaults.createFK", 147 "refreshSQLAction()"); 148 optionBindings[1] = builder.bindToStateChangeAndAction(view.getCreatePK(), 149 "generatorDefaults.createPK", 150 "refreshSQLAction()"); 151 optionBindings[2] = builder.bindToStateChangeAndAction(view.getCreateTables(), 152 "generatorDefaults.createTables", 153 "refreshSQLAction()"); 154 optionBindings[3] = builder.bindToStateChangeAndAction(view.getDropPK(), 155 "generatorDefaults.dropPK", 156 "refreshSQLAction()"); 157 optionBindings[4] = builder.bindToStateChangeAndAction(view.getDropTables(), 158 "generatorDefaults.dropTables", 159 "refreshSQLAction()"); 160 161 builder.bindToAction(view.getGenerateButton(), "generateSchemaAction()"); 162 builder.bindToAction(view.getSaveSqlButton(), "storeSQLAction()"); 163 builder.bindToAction(view.getCancelButton(), "closeAction()"); 164 165 view.getTabs().addChangeListener(new ChangeListener () { 167 168 public void stateChanged(ChangeEvent e) { 169 if (view.getTabs().getSelectedIndex() == 0) { 170 refreshGeneratorAction(); 173 } 174 } 175 }); 176 } 177 178 181 protected void prepareGenerator() { 182 try { 183 DbAdapter adapter = connectionInfo.makeAdapter(getApplication() 184 .getClassLoadingService()); 185 this.generator = new DbGenerator(adapter, dataMap, tables.getExcludedTables()); 186 this.generatorDefaults.adjustForAdapter(adapter); 187 } 188 catch (Exception ex) { 189 reportError("Error loading adapter", ex); 190 } 191 } 192 193 196 protected void createSQL() { 197 StringBuffer buf = new StringBuffer (); 199 Iterator it = generator.configuredStatements().iterator(); 200 String batchTerminator = generator.getAdapter().getBatchTerminator(); 201 202 String lineEnd = (batchTerminator != null) 203 ? "\n" + batchTerminator + "\n\n" 204 : "\n\n"; 205 206 while (it.hasNext()) { 207 buf.append(it.next()).append(lineEnd); 208 } 209 210 textForSQL = buf.toString(); 211 } 212 213 protected void refreshView() { 214 215 for (int i = 0; i < optionBindings.length; i++) { 216 optionBindings[i].updateView(); 217 } 218 219 sqlBinding.updateView(); 220 } 221 222 226 229 public void startupAction() { 230 view.pack(); 231 view.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 232 view.setModal(true); 233 makeCloseableOnEscape(); 234 centerView(); 235 view.setVisible(true); 236 } 237 238 public void refreshGeneratorAction() { 239 prepareGenerator(); 240 refreshSQLAction(); 241 } 242 243 246 public void refreshSQLAction() { 247 generatorDefaults.configureGenerator(generator); 249 createSQL(); 250 sqlBinding.updateView(); 251 } 252 253 256 public void generateSchemaAction() { 257 refreshGeneratorAction(); 258 259 if (generator.isEmpty(true)) { 261 JOptionPane.showMessageDialog(getView(), "Nothing to generate."); 262 return; 263 } 264 265 try { 266 DataSource dataSource = connectionInfo.makeDataSource(getApplication() 267 .getClassLoadingService()); 268 generator.runGenerator(dataSource); 269 270 ValidationResult failures = generator.getFailures(); 271 272 if (failures == null || !failures.hasFailures()) { 273 JOptionPane.showMessageDialog(getView(), "Schema Generation Complete."); 274 } 275 else { 276 new ValidationResultBrowser(this) 277 .startupAction("Schema Generation Complete", 278 "Schema generation finished. The following problem(s) were ignored.", 279 failures); 280 } 281 } 282 catch (Throwable th) { 283 reportError("Schema Generation Error", th); 284 } 285 } 286 287 290 public void storeSQLAction() { 291 JFileChooser fc = new JFileChooser (); 292 fc.setDialogType(JFileChooser.SAVE_DIALOG); 293 fc.setDialogTitle("Save SQL Script"); 294 295 File projectDir = Application.getProject().getProjectDirectory(); 296 297 if (projectDir != null) { 298 fc.setCurrentDirectory(projectDir); 299 } 300 301 if (fc.showSaveDialog(getView()) == JFileChooser.APPROVE_OPTION) { 302 refreshGeneratorAction(); 303 304 try { 305 File file = fc.getSelectedFile(); 306 FileWriter fw = new FileWriter (file); 307 PrintWriter pw = new PrintWriter (fw); 308 pw.print(textForSQL); 309 pw.flush(); 310 pw.close(); 311 } 312 catch (IOException ex) { 313 reportError("Error Saving SQL", ex); 314 } 315 } 316 } 317 318 public void closeAction() { 319 view.dispose(); 320 } 321 } | Popular Tags |