KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > schemagenerator > SchemaGeneratorDlg


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.schemagenerator;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.KeyEvent JavaDoc;
37 import java.io.File JavaDoc;
38
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JComboBox JavaDoc;
41 import javax.swing.JDialog JavaDoc;
42 import javax.swing.JFileChooser JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JOptionPane JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JTextField JavaDoc;
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 JavaDoc
55 {
56   private static final long serialVersionUID = 1L;
57   public final static String JavaDoc ORACLE = "Oracle";
58   public final static String JavaDoc DB2 = "DB/2";
59   public final static String JavaDoc HYPERSONIC = "Hypersonic";
60   public final static String JavaDoc SQLSERVER = "SQL Server";
61
62   JPanel JavaDoc panel1 = new JPanel JavaDoc();
63   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
64   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
65   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
66   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
67   JButton JavaDoc _butGenerate = new JButton JavaDoc();
68   JButton JavaDoc _butCancel = new JButton JavaDoc();
69   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
70   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
71   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
72   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
73   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
74   JComboBox JavaDoc _cbbDatabaseType = new JComboBox JavaDoc(new String JavaDoc[]{ORACLE, DB2, HYPERSONIC, SQLSERVER});
75   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
76   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
77   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
78   JPanel JavaDoc jPanel6 = new JPanel JavaDoc();
79   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
80   JTextField JavaDoc _edtOutput = new JTextField JavaDoc();
81   JButton JavaDoc _butBrowse = new JButton JavaDoc();
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 JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
97       Dimension JavaDoc 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 JavaDoc ex)
106     {
107       DjLogger.log(ex);
108     }
109   }
110
111   void jbInit() throws Exception JavaDoc
112   {
113     panel1.setLayout(borderLayout1);
114     jPanel1.setLayout(borderLayout2);
115     _butGenerate.setText(Messages.getString("global.Generate"));
116     _butGenerate.addActionListener(new java.awt.event.ActionListener JavaDoc()
117     {
118       public void actionPerformed(ActionEvent JavaDoc e)
119       {
120         _butGenerate_actionPerformed(e);
121       }
122     });
123     _butCancel.setText(Messages.getString("global.Cancel"));
124     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
125     {
126       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc(130, 21));
137     jPanel6.setLayout(borderLayout4);
138     _butBrowse.setPreferredSize(new Dimension JavaDoc(21, 21));
139     _butBrowse.setText("...");
140     _butBrowse.addActionListener(new java.awt.event.ActionListener JavaDoc()
141     {
142       public void actionPerformed(ActionEvent JavaDoc e)
143       {
144         _butBrowse_actionPerformed(e);
145       }
146     });
147     jPanel6.setPreferredSize(new Dimension JavaDoc(299, 21));
148     _edtOutput.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
149     {
150       public void keyPressed(KeyEvent JavaDoc 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 JavaDoc e)
173   {
174     JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
175     fc.setDialogTitle(Messages.getString("global.GeneratePath"));
176     String JavaDoc path = _edtOutput.getText();
177     if (path.trim().length() == 0) path = System.getProperties().getProperty("user.dir");
178     fc.setCurrentDirectory(new File JavaDoc(path));
179     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
180
181     if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
182     {
183       String JavaDoc fileName = fc.getSelectedFile().getAbsolutePath();
184       if (!fileName.toLowerCase().endsWith(".sql"))
185       {
186         fileName += ".sql";
187       }
188       File JavaDoc chk = new File JavaDoc(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 JavaDoc e)
203   {
204     setVisible(false);
205
206   }
207
208   void _butGenerate_actionPerformed(ActionEvent JavaDoc e)
209   {
210     try
211     {
212       generate();
213     }
214     catch (Exception JavaDoc 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 JavaDoc
223   {
224     _modelEditor.setProperty("generator.outputpath", _edtOutput.getText());
225     String JavaDoc 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 JavaDoc(Messages.getString("SchemaGeneratorDlg.UnsupportedDatabase", db));
234
235     String JavaDoc 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 JavaDoc 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