001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017: package com.finalist.jaggenerator.modules;
018:
019: import com.finalist.jaggenerator.Database;
020: import com.finalist.jaggenerator.DatabaseManager;
021: import com.finalist.jaggenerator.ExtensionsFileFilter;
022: import com.finalist.jaggenerator.JagGenerator;
023:
024: import javax.swing.*;
025: import javax.swing.event.TableModelEvent;
026: import javax.swing.event.TableModelListener;
027: import javax.swing.table.DefaultTableModel;
028: import javax.swing.table.TableColumn;
029: import javax.swing.table.TableModel;
030: import java.io.File;
031: import java.util.*;
032:
033: /**
034: * The JFrame GUI behind the DatabaseManager.
035: *
036: * @author m.oconnor
037: */
038: public class DatabaseManagerFrame extends javax.swing.JFrame {
039: private static DatabaseManagerFrame ourInstance;
040: private static final List newDatabases = new ArrayList();
041: public static final String SELECT = "< select >";
042: private static final String DRIVER_IMPORT_FILECHOOSER = "import driver";
043: private static final String[] ACCEPTABLE_EXTENSIONS = new String[] {
044: "class", "jar", "zip" };
045: private static final Vector COLUMN_NAMES = new Vector(Arrays
046: .asList(new String[] { "Database Type", "Driver File",
047: "Driver Class", "Type Mapping", }));
048:
049: /**
050: * Only one instance of the DatabaseManagerFrame is allowed - this method obtains the one and only instance.
051: * @return
052: */
053: public synchronized static DatabaseManagerFrame getInstance() {
054: if (ourInstance == null) {
055: ourInstance = new DatabaseManagerFrame();
056: }
057: return ourInstance;
058: }
059:
060: /**
061: * Whenever the frame is shown, reinitialise the list.
062: */
063: public void show() {
064: newDatabases.clear();
065: refreshModel();
066: super .show();
067: }
068:
069: private DatabaseManagerFrame() {
070: initComponents();
071: postInit();
072: }
073:
074: private void postInit() {
075: this .setLocationRelativeTo(JagGenerator.jagGenerator);
076: jTable1.setSurrendersFocusOnKeystroke(true);
077: refreshModel();
078: }
079:
080: private void refreshModel() {
081: jTable1
082: .setModel(new DefaultTableModel(getData(), COLUMN_NAMES) {
083: //The middle two columns are not editable
084: public boolean isCellEditable(int row, int column) {
085: if (column == 1 || column == 2) {
086: return false;
087: }
088: return super .isCellEditable(row, column);
089: }
090: });
091:
092: TableColumn typeMappingColumn = jTable1.getColumnModel()
093: .getColumn(3);
094: jTable1.getModel().addTableModelListener(
095: new TableModelListener() {
096: //when the user selects a type mapping and hasn't yet set a DB name,
097: //make the DB name to the same value as the type mapping..
098: public void tableChanged(TableModelEvent e) {
099: int column = e.getColumn();
100: if (column != 3)
101: return; //only interested in user selecting a typemapping
102:
103: int row = e.getFirstRow();
104: TableModel model = (TableModel) e.getSource();
105: String dbName = (String) model.getValueAt(row,
106: 0);
107: if (!Database.ENTER_DB_NAME.equals(dbName))
108: return;
109: String typeMapping = (String) model.getValueAt(
110: row, column);
111: model.setValueAt(typeMapping, row, 0);
112: }
113: });
114:
115: JComboBox comboBox = new JComboBox();
116: comboBox.addItem(SELECT);
117: String[] typeMappings = DatabaseManager.getInstance()
118: .getTypeMappings();
119: for (int i = 0; i < typeMappings.length; i++) {
120: comboBox.addItem(typeMappings[i]);
121: }
122:
123: typeMappingColumn
124: .setCellEditor(new DefaultCellEditor(comboBox));
125: }
126:
127: private Vector getData() {
128: Vector data = new Vector();
129: ArrayList temp = new ArrayList();
130: temp.addAll(Arrays.asList(DatabaseManager.getInstance()
131: .getSupportedDatabases()));
132: temp.addAll(newDatabases);
133: Iterator dbs = temp.iterator();
134: while (dbs.hasNext()) {
135: Database db = (Database) dbs.next();
136: Vector vickie = new Vector();
137: vickie.add(db.getDbName());
138: vickie.add(db.getFilename());
139: vickie.add(db.getDriverClass());
140: vickie.add(db.getTypeMapping());
141: data.add(vickie);
142: }
143:
144: return data;
145: }
146:
147: /** This method is called from within the constructor to
148: * initialize the form.
149: * WARNING: Do NOT modify this code. The content of this method is
150: * always regenerated by the Form Editor.
151: */
152: private void initComponents() {//GEN-BEGIN:initComponents
153: jScrollPane1 = new javax.swing.JScrollPane();
154: jTable1 = new javax.swing.JTable();
155: jLabel1 = new javax.swing.JLabel();
156: jPanel1 = new javax.swing.JPanel();
157: addButtonPanel = new javax.swing.JPanel();
158: addButton = new javax.swing.JButton();
159: cancelButtonPanel = new javax.swing.JPanel();
160: CancelButton = new javax.swing.JButton();
161: saveButtonPanel = new javax.swing.JPanel();
162: OKButton = new javax.swing.JButton();
163:
164: setTitle("JAG Database Driver Manager");
165: addWindowListener(new java.awt.event.WindowAdapter() {
166: public void windowClosing(java.awt.event.WindowEvent evt) {
167: exitForm(evt);
168: }
169: });
170:
171: jScrollPane1.setPreferredSize(new java.awt.Dimension(700, 300));
172: jTable1.setModel(new javax.swing.table.DefaultTableModel(
173: new Object[][] { { null, null, null, null },
174: { null, null, null, null },
175: { null, null, null, null },
176: { null, null, null, null } }, new String[] {
177: "Title 1", "Title 2", "Title 3", "Title 4" }));
178: jScrollPane1.setViewportView(jTable1);
179:
180: getContentPane()
181: .add(jScrollPane1, java.awt.BorderLayout.CENTER);
182:
183: jLabel1
184: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
185: jLabel1.setText("JAG Supported Databases");
186: jLabel1.setMaximumSize(new java.awt.Dimension(149, 32));
187: jLabel1.setMinimumSize(new java.awt.Dimension(149, 32));
188: jLabel1.setPreferredSize(new java.awt.Dimension(149, 32));
189: jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
190: getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);
191:
192: jPanel1.setLayout(new java.awt.BorderLayout());
193:
194: addButton.setText("Add new JDBC driver..");
195: addButton
196: .addActionListener(new java.awt.event.ActionListener() {
197: public void actionPerformed(
198: java.awt.event.ActionEvent evt) {
199: addButtonActionPerformed(evt);
200: }
201: });
202:
203: addButtonPanel.add(addButton);
204:
205: jPanel1.add(addButtonPanel, java.awt.BorderLayout.CENTER);
206:
207: CancelButton.setText("Cancel");
208: CancelButton
209: .addActionListener(new java.awt.event.ActionListener() {
210: public void actionPerformed(
211: java.awt.event.ActionEvent evt) {
212: CancelButtonActionPerformed(evt);
213: }
214: });
215:
216: cancelButtonPanel.add(CancelButton);
217:
218: jPanel1.add(cancelButtonPanel, java.awt.BorderLayout.WEST);
219:
220: OKButton.setText("Save");
221: OKButton.addActionListener(new java.awt.event.ActionListener() {
222: public void actionPerformed(java.awt.event.ActionEvent evt) {
223: OKButtonActionPerformed(evt);
224: }
225: });
226:
227: saveButtonPanel.add(OKButton);
228:
229: jPanel1.add(saveButtonPanel, java.awt.BorderLayout.EAST);
230:
231: getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
232:
233: pack();
234: }//GEN-END:initComponents
235:
236: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
237: JagGenerator
238: .logToConsole("Importing new database driver(s). Please wait...");
239: final JFileChooser fileChooser = new JFileChooser(JagGenerator
240: .getFileChooserStartDir(DRIVER_IMPORT_FILECHOOSER));
241: ExtensionsFileFilter filter = new ExtensionsFileFilter(
242: ACCEPTABLE_EXTENSIONS);
243: fileChooser
244: .setDialogTitle("JDBC Driver Import: Choose a class / archive file..");
245: fileChooser.setFileFilter(filter);
246: int fileChooserStatus = fileChooser.showOpenDialog(null);
247: if (fileChooserStatus == JFileChooser.APPROVE_OPTION) {
248: File driverFile = fileChooser.getSelectedFile();
249: newDatabases.addAll(DatabaseManager.getInstance()
250: .addDrivers(driverFile));
251: JagGenerator.logToConsole("...Driver import complete.");
252: JagGenerator.setFileChooserStartDir(
253: DRIVER_IMPORT_FILECHOOSER, driverFile);
254: refreshModel();
255:
256: } else {
257: JagGenerator.logToConsole("...aborted!");
258: }
259: }//GEN-LAST:event_addButtonActionPerformed
260:
261: private void CancelButtonActionPerformed(
262: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
263: hide();
264: }//GEN-LAST:event_CancelButtonActionPerformed
265:
266: private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
267: //We have to explicitly tell the table's cell editor to accept all 'partial' typed-in values
268: //(i.e. the value contained by the cell that was being edited when the OK button was pressed..
269: if (jTable1.getCellEditor() != null) {
270: jTable1.getCellEditor().stopCellEditing();
271: }
272: ArrayList databases = new ArrayList();
273: for (int row = 0; row < jTable1.getRowCount(); row++) {
274: Database db = new Database();
275: String dbName = (String) jTable1.getModel().getValueAt(row,
276: 0);
277: db.setDbName(dbName);
278: db.setFilename((String) jTable1.getModel().getValueAt(row,
279: 1));
280: db.setDriverClass((String) jTable1.getModel().getValueAt(
281: row, 2));
282:
283: String mapping = (String) jTable1.getModel().getValueAt(
284: row, 3);
285: if (SELECT.equals(mapping)) {
286: JOptionPane
287: .showMessageDialog(
288: this ,
289: "Please select a mapping for the new database driver from the drop-down list.",
290: "Missing Type Mapping!!",
291: javax.swing.JOptionPane.ERROR_MESSAGE);
292: return;
293: }
294:
295: db.setTypeMapping(mapping);
296: databases.add(db);
297: }
298:
299: DatabaseManager.getInstance().setDatabases(databases);
300: hide();
301:
302: }//GEN-LAST:event_OKButtonActionPerformed
303:
304: /** Exit the Application */
305: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
306: this .hide();
307: }//GEN-LAST:event_exitForm
308:
309: // Variables declaration - do not modify//GEN-BEGIN:variables
310: private javax.swing.JButton CancelButton;
311: private javax.swing.JButton OKButton;
312: private javax.swing.JButton addButton;
313: private javax.swing.JPanel addButtonPanel;
314: private javax.swing.JPanel cancelButtonPanel;
315: private javax.swing.JLabel jLabel1;
316: private javax.swing.JPanel jPanel1;
317: private javax.swing.JScrollPane jScrollPane1;
318: private javax.swing.JTable jTable1;
319: private javax.swing.JPanel saveButtonPanel;
320: // End of variables declaration//GEN-END:variables
321:
322: }
|