1 30 package com.genimen.djeneric.tools.modeler.schemagenerator; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.KeyEvent ; 37 import java.io.File ; 38 39 import javax.swing.JButton ; 40 import javax.swing.JComboBox ; 41 import javax.swing.JDialog ; 42 import javax.swing.JFileChooser ; 43 import javax.swing.JLabel ; 44 import javax.swing.JOptionPane ; 45 import javax.swing.JPanel ; 46 import javax.swing.JTextField ; 47 48 import com.genimen.djeneric.language.Messages; 49 import com.genimen.djeneric.tools.modeler.ModelEditor; 50 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 51 import com.genimen.djeneric.ui.Util; 52 import com.genimen.djeneric.util.DjLogger; 53 54 public class SchemaGeneratorDlg extends JDialog 55 { 56 private static final long serialVersionUID = 1L; 57 public final static String ORACLE = "Oracle"; 58 public final static String DB2 = "DB/2"; 59 public final static String HYPERSONIC = "Hypersonic"; 60 public final static String SQLSERVER = "SQL Server"; 61 62 JPanel panel1 = new JPanel (); 63 BorderLayout borderLayout1 = new BorderLayout (); 64 JPanel jPanel1 = new JPanel (); 65 BorderLayout borderLayout2 = new BorderLayout (); 66 JPanel jPanel2 = new JPanel (); 67 JButton _butGenerate = new JButton (); 68 JButton _butCancel = new JButton (); 69 JPanel jPanel3 = new JPanel (); 70 BorderLayout borderLayout3 = new BorderLayout (); 71 JPanel jPanel4 = new JPanel (); 72 JPanel jPanel5 = new JPanel (); 73 JLabel jLabel1 = new JLabel (); 74 JComboBox _cbbDatabaseType = new JComboBox (new String []{ORACLE, DB2, HYPERSONIC, SQLSERVER}); 75 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 76 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 77 JLabel jLabel2 = new JLabel (); 78 JPanel jPanel6 = new JPanel (); 79 BorderLayout borderLayout4 = new BorderLayout (); 80 JTextField _edtOutput = new JTextField (); 81 JButton _butBrowse = new JButton (); 82 83 ModelEditor _modelEditor; 84 85 public SchemaGeneratorDlg(ModelEditor frame) 86 { 87 super(frame, Messages.getString("SchemaGeneratorDlg.GenerateScript"), true); 88 89 _modelEditor = frame; 90 91 try 92 { 93 jbInit(); 94 pack(); 95 96 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 97 Dimension frameSize = getSize(); 98 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 99 100 _edtOutput.setText(_modelEditor.getProperty("generator.outputpath", "")); 101 _cbbDatabaseType.setSelectedItem(_modelEditor.getProperty("generator.database", ORACLE)); 102 103 setVisible(true); 104 } 105 catch (Exception ex) 106 { 107 DjLogger.log(ex); 108 } 109 } 110 111 void jbInit() throws Exception 112 { 113 panel1.setLayout(borderLayout1); 114 jPanel1.setLayout(borderLayout2); 115 _butGenerate.setText(Messages.getString("global.Generate")); 116 _butGenerate.addActionListener(new java.awt.event.ActionListener () 117 { 118 public void actionPerformed(ActionEvent e) 119 { 120 _butGenerate_actionPerformed(e); 121 } 122 }); 123 _butCancel.setText(Messages.getString("global.Cancel")); 124 _butCancel.addActionListener(new java.awt.event.ActionListener () 125 { 126 public void actionPerformed(ActionEvent e) 127 { 128 _butCancel_actionPerformed(e); 129 } 130 }); 131 jPanel3.setLayout(borderLayout3); 132 jLabel1.setText(Messages.getString("SchemaGeneratorDlg.DatabaseType")); 133 jPanel5.setLayout(verticalFlowLayout1); 134 jPanel4.setLayout(verticalFlowLayout2); 135 jLabel2.setText(Messages.getString("SchemaGeneratorDlg.OutputFile")); 136 _cbbDatabaseType.setPreferredSize(new Dimension (130, 21)); 137 jPanel6.setLayout(borderLayout4); 138 _butBrowse.setPreferredSize(new Dimension (21, 21)); 139 _butBrowse.setText("..."); 140 _butBrowse.addActionListener(new java.awt.event.ActionListener () 141 { 142 public void actionPerformed(ActionEvent e) 143 { 144 _butBrowse_actionPerformed(e); 145 } 146 }); 147 jPanel6.setPreferredSize(new Dimension (299, 21)); 148 _edtOutput.addKeyListener(new java.awt.event.KeyAdapter () 149 { 150 public void keyPressed(KeyEvent e) 151 { 152 _edtOutput_keyPressed(e); 153 } 154 }); 155 jPanel2.add(_butCancel, null); 156 getContentPane().add(panel1); 157 panel1.add(jPanel1, BorderLayout.SOUTH); 158 jPanel1.add(jPanel2, BorderLayout.EAST); 159 jPanel2.add(_butGenerate, null); 160 panel1.add(jPanel3, BorderLayout.CENTER); 161 jPanel3.add(jPanel4, BorderLayout.WEST); 162 jPanel4.add(jLabel1, null); 163 jPanel4.add(jLabel2, null); 164 jPanel3.add(jPanel5, BorderLayout.CENTER); 165 jPanel5.add(_cbbDatabaseType, null); 166 jPanel5.add(jPanel6, null); 167 jPanel6.add(_edtOutput, BorderLayout.CENTER); 168 jPanel6.add(_butBrowse, BorderLayout.EAST); 169 Util.sizeButtons(jPanel2); 170 } 171 172 void _butBrowse_actionPerformed(ActionEvent e) 173 { 174 JFileChooser fc = new JFileChooser (); 175 fc.setDialogTitle(Messages.getString("global.GeneratePath")); 176 String path = _edtOutput.getText(); 177 if (path.trim().length() == 0) path = System.getProperties().getProperty("user.dir"); 178 fc.setCurrentDirectory(new File (path)); 179 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 180 181 if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) 182 { 183 String fileName = fc.getSelectedFile().getAbsolutePath(); 184 if (!fileName.toLowerCase().endsWith(".sql")) 185 { 186 fileName += ".sql"; 187 } 188 File chk = new File (fileName); 189 if (chk.exists()) 190 { 191 if (!Util.isOkToOverwrite(_modelEditor, chk)) 192 { 193 return; 194 } 195 } 196 197 _edtOutput.setText(fc.getSelectedFile().getAbsolutePath()); 198 } 199 200 } 201 202 void _butCancel_actionPerformed(ActionEvent e) 203 { 204 setVisible(false); 205 206 } 207 208 void _butGenerate_actionPerformed(ActionEvent e) 209 { 210 try 211 { 212 generate(); 213 } 214 catch (Exception x) 215 { 216 JOptionPane.showMessageDialog(this, x.getMessage(), Messages.getString("SchemaGeneratorDlg.Unable2Generate"), 217 JOptionPane.ERROR_MESSAGE); 218 DjLogger.log(x); 219 } 220 } 221 222 void generate() throws Exception 223 { 224 _modelEditor.setProperty("generator.outputpath", _edtOutput.getText()); 225 String db = _cbbDatabaseType.getSelectedItem().toString(); 226 _modelEditor.setProperty("generator.database", db); 227 228 SchemaGenerator gen = null; 229 if (db.equals(ORACLE)) gen = new OracleSchemaGenerator(_modelEditor.getPersistenceManager()); 230 else if (db.equals(SQLSERVER)) gen = new SqlServerSchemaGenerator(_modelEditor.getPersistenceManager()); 231 else if (db.equals(HYPERSONIC)) gen = new HypersonicSchemaGenerator(_modelEditor.getPersistenceManager()); 232 else if (db.equals(DB2)) gen = new DB2SchemaGenerator(_modelEditor.getPersistenceManager()); 233 else throw new RuntimeException (Messages.getString("SchemaGeneratorDlg.UnsupportedDatabase", db)); 234 235 String fileName = _edtOutput.getText(); 236 if (!fileName.toLowerCase().endsWith(".sql")) 237 { 238 fileName += ".sql"; 239 } 240 241 gen.generate(fileName); 242 _modelEditor.setStatusMessage(Messages.getString("SchemaGeneratorDlg.ScriptGeneratedTo", fileName)); 243 setVisible(false); 244 245 } 246 247 void _edtOutput_keyPressed(KeyEvent e) 248 { 249 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butGenerate_actionPerformed(null); 250 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 251 } 252 } | Popular Tags |