View Javadoc
1 package com.bonevich.erj.ui; 2 3 import com.bonevich.erj.ErjConstants; 4 import com.bonevich.erj.app.Project; 5 import com.bonevich.erj.db.GenerationOptionMetaData; 6 import com.bonevich.erj.db.IDdlGenerator; 7 import com.bonevich.erj.db.SupportedDatabase; 8 import com.bonevich.javax.swing.AbstractComboBoxEditor; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import java.awt.*; 14 import java.awt.event.ActionEvent; 15 import java.awt.event.ActionListener; 16 import java.io.File; 17 import java.io.FileWriter; 18 import java.util.*; 19 20 import javax.swing.*; 21 import javax.swing.border.Border; 22 import javax.swing.text.Document; 23 import javax.swing.text.PlainDocument; 24 25 public class GenerateDDLDialog extends JDialog 26 implements ErjConstants 27 { 28 ////////////////////////////////////////////////////////// 29 // Constants 30 public static final Log _logger = LogFactory.getLog(GenerateDDLDialog.class); 31 public static final String GENERATE_STR = "Generate DDL Script From Model"; 32 public static final String OPTIONS_STR = "Options"; 33 public static final String DDL_STR = "Generated Script"; 34 public static final String SCRIPT_STR = "Script Name: "; 35 public static final String NEW_GENERATOR = "unnamed_"; 36 public static int count = 0; 37 38 ////////////////////////////////////////////////////////// 39 // Attributes 40 private Project _project; 41 private Map _options = new HashMap(40); 42 43 private JButton _genBtn = new JButton("Generate"); 44 private JButton _applyBtn = new JButton("Apply"); 45 private JButton _cancelBtn = new JButton("Cancel"); 46 47 private JPanel _optionPane = new JPanel(); 48 private JLabel _scriptLabel = new JLabel(SCRIPT_STR); 49 private JComboBox _scriptList = new JComboBox(); 50 private DefaultComboBoxModel _scriptModel; 51 private JButton _newBtn = new JButton("New"); 52 private JButton _rmBtn = new JButton("Remove"); 53 private JPanel _groupsPane = new JPanel(); 54 private JSeparator _separator1 = new JSeparator(); 55 private JSeparator _separator2 = new JSeparator(); 56 57 private JDialog _genDialog = new JDialog(this, DDL_STR, false); 58 private JButton _saveBtn = new JButton("Save"); 59 private JButton _closeBtn = new JButton("Close"); 60 61 private JPanel _genPane = new JPanel(); 62 private JPanel _ddlPane = new JPanel(); 63 private Border _ddlBrdr = BorderFactory.createLoweredBevelBorder(); 64 private JTextArea _ddlText = new JTextArea(18,30); 65 66 { 67 _optionPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 68 _genPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 69 } 70 71 private JFileChooser _saveChooser = new JFileChooser(); 72 73 ////////////////////////////////////////////////////////// 74 // Constructors 75 public GenerateDDLDialog(Project project) 76 { 77 super(ErjFrame.getInstance(), GENERATE_STR, true); 78 _project = project; 79 setResizable(true); 80 initComponents(); 81 } 82 83 private void initComponents() 84 { 85 initOptionsPane(); 86 initGeneratedScriptDialog(); 87 } 88 89 private void initOptionsPane() 90 { 91 _optionPane.setLayout(new GridBagLayout()); 92 GridBagConstraints constraints = new GridBagConstraints(); 93 94 SupportedDatabase db = _project.getTargetDatabase().getSupportedDatabase(); 95 Vector generators = new Vector(); 96 Iterator itr = db.getGenerators(); 97 while (itr.hasNext()) 98 { 99 generators.add(itr.next()); 100 } 101 _scriptModel = new DefaultComboBoxModel(generators); 102 _scriptList.setModel(_scriptModel); 103 _scriptList.setSelectedItem(db.getDefaultGenerator()); 104 _scriptList.setEditor(new GeneratorComboBoxEditor()); 105 _scriptList.setEditable(true); 106 107 // name label and textbox 108 constraints.gridwidth = 1; 109 constraints.anchor = GridBagConstraints.WEST; 110 constraints.insets = new Insets(0,2,0,2); 111 _optionPane.add(_scriptLabel, constraints); 112 _optionPane.add(Box.createHorizontalStrut(10)); 113 _optionPane.add(_scriptList, constraints); 114 _optionPane.add(Box.createHorizontalStrut(10)); 115 _optionPane.add(_newBtn, constraints); 116 _optionPane.add(Box.createHorizontalStrut(5)); 117 constraints.gridwidth = GridBagConstraints.REMAINDER; 118 constraints.fill = GridBagConstraints.NONE; 119 _optionPane.add(_rmBtn, constraints); 120 121 constraints.anchor = GridBagConstraints.CENTER; 122 constraints.fill = GridBagConstraints.HORIZONTAL; 123 constraints.insets = new Insets(5,0,5,0); 124 _optionPane.add(_separator1, constraints); 125 126 addOptionGroupCheckBoxes(); 127 constraints.gridwidth = GridBagConstraints.REMAINDER; 128 constraints.anchor = GridBagConstraints.WEST; 129 _optionPane.add(_groupsPane, constraints); 130 131 constraints.anchor = GridBagConstraints.CENTER; 132 constraints.fill = GridBagConstraints.HORIZONTAL; 133 constraints.insets = new Insets(5,0,5,0); 134 _optionPane.add(_separator2, constraints); 135 136 // buttons 137 JPanel btnPane = new JPanel(); 138 ((FlowLayout) btnPane.getLayout()).setAlignment(FlowLayout.CENTER); 139 btnPane.add(_genBtn); 140 btnPane.add(_applyBtn); 141 btnPane.add(_cancelBtn); 142 143 constraints.anchor = GridBagConstraints.CENTER; 144 constraints.gridwidth = GridBagConstraints.REMAINDER; 145 constraints.fill = GridBagConstraints.HORIZONTAL; 146 _optionPane.add(btnPane, constraints); 147 getContentPane().add(_optionPane, BorderLayout.CENTER); 148 149 pack(); 150 setLocationRelativeTo(ErjFrame.getInstance()); 151 addActionListeners(); 152 updateOptions(false); 153 } 154 155 private void addOptionGroupCheckBoxes() 156 { 157 java.util.List panes = new ArrayList(30); 158 GenerationOptionMetaData md = _project.getTargetDatabase().getSupportedDatabase().getGenerationOptionMetaData(); 159 Iterator optionGroups = md.getOptionGroupIds(); 160 while (optionGroups.hasNext()) 161 { 162 String groupId = (String) optionGroups.next(); 163 JPanel groupPane = new JPanel(); 164 groupPane.setBorder(BorderFactory.createTitledBorder(md.getOptionGroupDisplayName(groupId))); 165 groupPane.setLayout(new BoxLayout(groupPane, BoxLayout.Y_AXIS)); 166 groupPane.setAlignmentY(Component.TOP_ALIGNMENT); 167 groupPane.setAlignmentX(Component.LEFT_ALIGNMENT); 168 panes.add(groupPane); 169 170 Iterator options = md.getOptionsForGroup(groupId); 171 while(options.hasNext()) 172 { 173 String optionId = (String) options.next(); 174 JCheckBox checkBox = new JCheckBox(md.getOptionDisplayName(groupId, optionId)); 175 checkBox.setAlignmentY(Component.TOP_ALIGNMENT); 176 checkBox.setAlignmentX(Component.LEFT_ALIGNMENT); 177 groupPane.add(checkBox); 178 _options.put(optionId, checkBox); 179 } 180 } 181 182 int numPanes = panes.size(); 183 int columns = 4; 184 int rows = numPanes / columns; 185 if (numPanes % columns > 0) ++rows; 186 187 _groupsPane.setLayout(new GridBagLayout()); 188 GridBagConstraints constraints = new GridBagConstraints(); 189 190 for (int i = 0; i < numPanes; i++) 191 { 192 constraints.gridwidth = ((i+1) % columns == 0) ? GridBagConstraints.REMAINDER : 1; 193 constraints.anchor = GridBagConstraints.NORTH; 194 _groupsPane.add((JPanel)panes.get(i), constraints); 195 } 196 } 197 198 private void initGeneratedScriptDialog() 199 { 200 _genPane.setLayout(new BorderLayout()); 201 202 // generated ddl text area and title border 203 _ddlPane.setLayout(new BorderLayout()); 204 _ddlPane.setAlignmentX(Component.LEFT_ALIGNMENT); 205 _ddlPane.setBorder(_ddlBrdr); 206 _ddlText.setAlignmentX(Component.LEFT_ALIGNMENT); 207 _ddlText.setLineWrap(false); 208 _ddlText.setWrapStyleWord(false); 209 210 JScrollPane scrPane = new JScrollPane(_ddlText); 211 scrPane.setAlignmentX(Component.LEFT_ALIGNMENT); 212 scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 213 scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 214 _ddlPane.add(scrPane, BorderLayout.CENTER); 215 216 _genPane.add(_ddlPane, BorderLayout.CENTER); 217 218 // buttons 219 JPanel btnPane = new JPanel(); 220 ((FlowLayout) btnPane.getLayout()).setAlignment(FlowLayout.CENTER); 221 btnPane.add(_saveBtn); 222 btnPane.add(_closeBtn); 223 224 _genPane.add(btnPane, BorderLayout.SOUTH); 225 226 _genDialog.setResizable(true); 227 _genDialog.getContentPane().add(_genPane, BorderLayout.CENTER); 228 _genDialog.pack(); 229 _genDialog.setLocationRelativeTo(this); 230 } 231 232 private void openGeneratedScriptDialog() 233 { 234 updateOptions(true); 235 updateDdlScriptUI(); 236 if (!_genDialog.isShowing()) 237 { 238 _genDialog.show(); 239 } 240 } 241 242 private void addActionListeners() 243 { 244 _scriptList.addActionListener( 245 new ActionListener() 246 { 247 public void actionPerformed(ActionEvent e) 248 { 249 IDdlGenerator selectedScript = (IDdlGenerator) _scriptList.getSelectedItem(); 250 int index = _scriptList.getSelectedIndex(); 251 if (selectedScript != null) 252 { 253 updateOptions(false); 254 } 255 } 256 } 257 ); 258 259 _newBtn.addActionListener( 260 new ActionListener() 261 { 262 public void actionPerformed(ActionEvent e) 263 { 264 SupportedDatabase db = _project.getTargetDatabase().getSupportedDatabase(); 265 String generatorName = NEW_GENERATOR + (++count); 266 IDdlGenerator generator = db.addGenerator(generatorName); 267 _scriptList.addItem(generator); 268 _scriptList.setSelectedItem(generator); 269 } 270 } 271 ); 272 273 _rmBtn.addActionListener( 274 new ActionListener() 275 { 276 public void actionPerformed(ActionEvent e) 277 { 278 IDdlGenerator generator = (IDdlGenerator) _scriptList.getSelectedItem(); 279 if (generator != null && 280 !generator.getName().equals(SupportedDatabase.DEFAULT_GENERATOR_NAME)) 281 { 282 int index = _scriptList.getSelectedIndex(); 283 SupportedDatabase db = _project.getTargetDatabase().getSupportedDatabase(); 284 db.removeGenerator(generator.getName()); 285 _scriptList.removeItem(generator); 286 _scriptList.setSelectedIndex(index - 1); 287 } 288 } 289 } 290 ); 291 292 _genBtn.addActionListener( 293 new ActionListener() 294 { 295 public void actionPerformed(ActionEvent e) 296 { 297 // save the option settings and goto the generated script tab 298 openGeneratedScriptDialog(); 299 } 300 } 301 ); 302 303 _applyBtn.addActionListener( 304 new ActionListener() 305 { 306 public void actionPerformed(ActionEvent e) 307 { 308 // save the option settings 309 updateOptions(true); 310 } 311 } 312 ); 313 314 _cancelBtn.addActionListener( 315 new ActionListener() 316 { 317 public void actionPerformed(ActionEvent e) 318 { 319 _genDialog.dispose(); 320 dispose(); 321 } 322 } 323 ); 324 325 _saveBtn.addActionListener( 326 new ActionListener() 327 { 328 public void actionPerformed(ActionEvent e) 329 { 330 _saveChooser.addChoosableFileFilter(ErjFileFilter.SQL); 331 _saveChooser.setFileFilter(ErjFileFilter.SQL); 332 int choice = _saveChooser.showSaveDialog(GenerateDDLDialog.this); 333 if (choice == JFileChooser.APPROVE_OPTION) 334 { 335 File sqlFile = _saveChooser.getSelectedFile(); 336 FileWriter writer = null; 337 try 338 { 339 writer = new FileWriter(sqlFile); 340 writer.write(_ddlText.getText()); 341 } 342 catch (Exception ioe) 343 { 344 // popup error message window! 345 String error = "Could not save DDL Script to file " + sqlFile.getAbsolutePath(); 346 JOptionPane.showMessageDialog( 347 _saveChooser, error, "", JOptionPane.ERROR_MESSAGE 348 ); 349 } 350 finally 351 { 352 try { writer.close(); } catch (Exception ex) {} 353 } 354 } 355 } 356 } 357 ); 358 359 _closeBtn.addActionListener( 360 new ActionListener() 361 { 362 public void actionPerformed(ActionEvent e) 363 { 364 _genDialog.dispose(); 365 } 366 } 367 ); 368 } 369 370 private void updateDdlScriptUI() 371 { 372 IDdlGenerator generator = (IDdlGenerator) _scriptList.getSelectedItem(); 373 if (generator != null) 374 { 375 String script = generator.generate(_project.getModel()); 376 _ddlText.setText(script); 377 } 378 } 379 380 private void updateOptions(boolean updateGenerator) 381 { 382 IDdlGenerator generator = (IDdlGenerator) _scriptList.getSelectedItem(); 383 if (generator != null) 384 { 385 Iterator optionIds = _options.keySet().iterator(); 386 while (optionIds.hasNext()) 387 { 388 String optionId = (String) optionIds.next(); 389 JCheckBox optionBox = (JCheckBox) _options.get(optionId); 390 if (updateGenerator) 391 { 392 generator.setOption(optionId, optionBox.isSelected()); 393 } 394 optionBox.setSelected(generator.isOptionSet(optionId)); 395 } 396 397 if (updateGenerator) 398 { 399 _project.getParent().savePreferences(); 400 } 401 } 402 } 403 404 /*** 405 * Over-written to control the absolute minimum size of the dialog: 406 * min = preferred size. 407 * @see java.awt.Component#paint(Graphics) 408 */ 409 public void paint(Graphics g) 410 { 411 Dimension dim = getPreferredSize(); 412 Rectangle bounds = getBounds(); 413 if (bounds.width < dim.width || bounds.height < dim.height) 414 { 415 bounds.width = (bounds.width > dim.width) ? bounds.width : dim.width; 416 bounds.height = (bounds.height > dim.height) ? bounds.height : dim.height; 417 setBounds(bounds); 418 pack(); 419 } 420 super.paint(g); 421 } 422 423 424 /*** 425 * ClassDescription 426 * 427 * @author jbonevic 428 * @version $Id: GenerateDDLDialog.html,v 1.1 2009/03/07 17:55:33 jbonevic Exp $ 429 */ 430 private final class GeneratorComboBoxEditor extends AbstractComboBoxEditor 431 { 432 IDdlGenerator _item; 433 JTextField _editor = new JTextField(15); 434 { 435 _editor.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 436 } 437 438 /*** 439 * Constructor for GeneratorComboBoxEditor. 440 */ 441 public GeneratorComboBoxEditor() 442 { 443 super(); 444 this.addListeners(); 445 } 446 /*** 447 * @see javax.swing.ComboBoxEditor#getEditorComponent() 448 */ 449 public Component getEditorComponent() 450 { 451 return _editor; 452 } 453 /*** 454 * @see javax.swing.ComboBoxEditor#setItem(Object) 455 */ 456 public void setItem(Object item) 457 { 458 if (item instanceof IDdlGenerator) 459 { 460 _item = (IDdlGenerator) item; 461 _editor.setText(_item.getName()); 462 } 463 } 464 /*** 465 * @see javax.swing.ComboBoxEditor#getItem() 466 */ 467 public Object getItem() 468 { 469 return _item; 470 } 471 472 private void addListeners() 473 { 474 _editor.addActionListener( 475 new ActionListener() 476 { 477 public void actionPerformed(ActionEvent e) 478 { 479 String originalName = _item.getName(); 480 if (!originalName.equals(SupportedDatabase.DEFAULT_GENERATOR_NAME)) 481 { 482 String newName = _editor.getText(); 483 //_logger.debug("updateOptions changing generator name from " + originalName + " to " + newName); 484 _item.setName(newName); 485 SupportedDatabase db = _project.getTargetDatabase().getSupportedDatabase(); 486 db.removeGenerator(originalName); 487 db.addGenerator(_item); 488 _project.getParent().savePreferences(); 489 } 490 } 491 } 492 ); 493 } 494 495 } /* end inner class GeneratorComboBoxEditor */ 496 497 } /* end class GenerateDDLDialog */

This page was automatically generated by Maven