001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program 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: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.ideTools;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/ideTools/DatabaseDialog.java $
024: //$Author: Dan $
025: //$Revision: 9 $
026: //$Modtime: 6/11/03 4:27p $
027: /////////////////////////
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.io.File;
031: import java.util.*;
032:
033: import javax.swing.*;
034:
035: public class DatabaseDialog extends JDialog implements ActionListener,
036: WindowListener {
037:
038: boolean _cancelClicked = true;
039: JButton _ok;
040: JButton _cancel;
041: JFrame _owner;
042: int _fieldIndex = 0;
043: int _noFields = 10;
044: Field _fields[];
045: Box _mainGrid;
046: Hashtable _dbsettings;
047: File[] _selectedFiles;
048:
049: public static final int TYPE_TEXT = 0;
050: public static final int TYPE_PW = 1;
051: public static final int TYPE_FILE = 2;
052: public static final int TYPE_DIR = 3;
053: public static final int TYPE_DIR_FILE = 4;
054: public static final int TYPE_CHECKBOX = 5;
055:
056: private class Field {
057: public JLabel caption;
058: public JComponent field;
059: public String property;
060: public JComponent lookup;
061: public int type;
062: }
063:
064: public DatabaseDialog(JFrame owner, Hashtable dbsettings) {
065: super (owner, "Specify Database Settings", true);
066: _owner = owner;
067: _dbsettings = dbsettings;
068:
069: int width = 600;
070: int height = 150 + (30 * 2);
071: Dimension frameBounds = Toolkit.getDefaultToolkit()
072: .getScreenSize();
073: int x = (frameBounds.width - width) / 2;
074: int y = (frameBounds.height - height) / 2;
075: setBounds(x, y, width, height);
076:
077: _fields = new Field[_noFields];
078: _mainGrid = Box.createVerticalBox();
079:
080: addFieldToGrid("JDBCJarFile", "JDBC Jar File", TYPE_DIR_FILE);
081: addFieldToGrid("JDBCDriver", "JDBC Driver Class", TYPE_TEXT);
082: addFieldToGrid("JDBCConnectUrl", "JDBC Connect Url", TYPE_TEXT);
083: addFieldToGrid("JDBCUser", "JDBC Username", TYPE_TEXT);
084: addFieldToGrid("JDBCPassword", "JDBC Password", TYPE_TEXT);
085: if (_dbsettings != null) {
086: ((JTextField) _fields[0].field)
087: .setText((String) _dbsettings.get("JDBCJarFile"));
088: ((JTextField) _fields[1].field)
089: .setText((String) _dbsettings.get("JDBCDriver"));
090: ((JTextField) _fields[2].field)
091: .setText((String) _dbsettings.get("JDBCConnectUrl"));
092: ((JTextField) _fields[3].field)
093: .setText((String) _dbsettings.get("JDBCUser"));
094: ((JTextField) _fields[4].field)
095: .setText((String) _dbsettings.get("JDBCPassword"));
096: }
097:
098: _ok = new JButton("OK");
099: _ok.addActionListener(this );
100: _cancel = new JButton("Cancel");
101: _cancel.addActionListener(this );
102: JPanel buttonBar = new JPanel(new FlowLayout());
103: buttonBar.add(_ok);
104: buttonBar.add(_cancel);
105: buttonBar.setAlignmentX(Component.CENTER_ALIGNMENT);
106: Box box = Box.createVerticalBox();
107:
108: box.add(Box.createRigidArea(new Dimension(100, 10)));
109: box.add(_mainGrid);
110: box.add(Box.createRigidArea(new Dimension(100, 5)));
111: box.add(buttonBar);
112:
113: getContentPane().add(box);
114:
115: addWindowListener(this );
116: setVisible(true);
117: }
118:
119: public void actionPerformed(ActionEvent e) {
120: if (e.getSource() == _cancel) {
121: _cancelClicked = true;
122: setVisible(false);
123: } else if (e.getSource() == _ok) {
124: Field fJDBCJarFile = _fields[0];
125: String sJDBCJarFile = ((JTextField) fJDBCJarFile.field)
126: .getText();
127: if (sJDBCJarFile == null || sJDBCJarFile.trim().equals("")) {
128: displayError("JDBC Jar File is required.");
129: return;
130: } else {
131: StringTokenizer stFiles = new StringTokenizer(
132: sJDBCJarFile, ";");
133: Vector vFiles = new Vector();
134: while (stFiles.hasMoreTokens()) {
135: String sJarFile = stFiles.nextToken();
136: File fJarFile = new File(sJarFile);
137: if (!fJarFile.exists()) {
138: displayError("JDBC Jar File "
139: + fJarFile.getAbsolutePath()
140: + " does not exist.");
141: return;
142: }
143: vFiles.addElement(fJarFile);
144: }
145: if (vFiles.size() > 0) {
146: _selectedFiles = new File[vFiles.size()];
147: for (int i = 0; i < vFiles.size(); i++)
148: _selectedFiles[i] = (File) vFiles.elementAt(i);
149: } else
150: _selectedFiles = null;
151: }
152: Field fJDBCDriver = _fields[1];
153: String sJDBCDriver = ((JTextField) fJDBCDriver.field)
154: .getText();
155: if (sJDBCDriver == null || sJDBCDriver.trim().equals("")) {
156: displayError("JDBC Driver is required.");
157: return;
158: }
159: Field fJDBCConnectUrl = _fields[2];
160: String sJDBCConnectUrl = ((JTextField) fJDBCConnectUrl.field)
161: .getText();
162: if (sJDBCConnectUrl == null
163: || sJDBCConnectUrl.trim().equals("")) {
164: displayError("JDBC Connect Url is required.");
165: return;
166: } else {
167: if (!sJDBCConnectUrl.startsWith("jdbc:")) {
168: displayError("JDBC Connect Url must start with jdbc:.");
169: return;
170: }
171: }
172: Field fJDBCUser = _fields[3];
173: String sJDBCUser = ((JTextField) fJDBCUser.field).getText();
174: Field fJDBCPassword = _fields[4];
175: String sJDBCPassword = ((JTextField) fJDBCPassword.field)
176: .getText();
177: _dbsettings = new Hashtable();
178: _dbsettings.put(fJDBCJarFile.property, _selectedFiles);
179: _dbsettings.put(fJDBCDriver.property, sJDBCDriver);
180: _dbsettings.put(fJDBCConnectUrl.property, sJDBCConnectUrl);
181: _dbsettings.put(fJDBCUser.property, sJDBCUser);
182: _dbsettings.put(fJDBCPassword.property, sJDBCPassword);
183:
184: _cancelClicked = false;
185: setVisible(false);
186: } else {
187: Field f = null;
188: for (int i = 0; i < _noFields; i++) {
189: if (_fields[i].lookup != null
190: && _fields[i].lookup == e.getSource()) {
191: f = _fields[i];
192: break;
193: }
194: }
195: if (f != null) {
196: String fileName = ((JTextField) f.field).getText();
197: JFileChooser c = new JFileChooser();
198: c.setMultiSelectionEnabled(true);
199: if (f.type == TYPE_FILE)
200: c.setFileSelectionMode(JFileChooser.FILES_ONLY);
201: else if (f.type == TYPE_DIR)
202: c
203: .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
204: else
205: c
206: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
207: c.setCurrentDirectory(new File(fileName));
208: if (c.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
209: _selectedFiles = c.getSelectedFiles();
210: if (_selectedFiles != null
211: && _selectedFiles.length != 0) {
212: StringBuffer sbFiles = new StringBuffer();
213: for (int i = 0; i < _selectedFiles.length; i++) {
214: sbFiles.append(_selectedFiles[i]
215: .getAbsolutePath());
216: if (i != _selectedFiles.length - 1)
217: sbFiles.append(";");
218: }
219: ((JTextField) f.field).setText(sbFiles
220: .toString());
221: } else {
222: if (c.getSelectedFile().exists()) {
223: ((JTextField) f.field).setText(c
224: .getSelectedFile()
225: .getAbsolutePath());
226: _selectedFiles = new File[1];
227: _selectedFiles[0] = c.getSelectedFile();
228: } else {
229: String sFile = c.getSelectedFile()
230: .getAbsolutePath();
231: int iSlashIndex = sFile
232: .lastIndexOf(File.separator);
233: sFile = sFile.substring(0, iSlashIndex);
234: ((JTextField) f.field).setText(sFile);
235: _selectedFiles = new File[1];
236: _selectedFiles[0] = new File(sFile);
237: }
238: }
239: /* if (c.getSelectedFile().exists())
240: ((JTextField) f.field).setText(c.getSelectedFile().getAbsolutePath());
241: else {
242: String sFile=c.getSelectedFile().getAbsolutePath();
243: int iSlashIndex=sFile.lastIndexOf(File.separator);
244: sFile=sFile.substring(0,iSlashIndex);
245: ((JTextField) f.field).setText(sFile);
246: }
247: */
248: }
249: }
250: }
251: }
252:
253: private JComponent addFieldToGrid(String property, String label,
254: int type) {
255: Field f = new Field();
256: f.property = property;
257: Dimension capSize = new Dimension(180, 25);
258: Dimension fieldSize = new Dimension(350, 25);
259: Dimension buttonSize = new Dimension(30, 25);
260: Dimension lookupSize = new Dimension(320, 25);
261:
262: f.type = type;
263: if (type != TYPE_CHECKBOX)
264: f.caption = DialogComponentFactory.makeLabel(label + ":");
265: else
266: f.caption = DialogComponentFactory.makeLabel("");
267: f.caption.setMaximumSize(capSize);
268: f.caption.setMinimumSize(capSize);
269: Box b = Box.createHorizontalBox();
270: b.add(f.caption);
271:
272: if (type == TYPE_TEXT) {
273: f.field = new JTextField("");
274: f.field.setMaximumSize(fieldSize);
275: f.field.setMinimumSize(fieldSize);
276: b.add(f.field);
277: _mainGrid.add(b);
278: _mainGrid.add(Box.createRigidArea(new Dimension(255, 3)));
279: } else if (type == TYPE_PW) {
280: f.field = new JPasswordField("");
281: f.field.setMaximumSize(fieldSize);
282: f.field.setMinimumSize(fieldSize);
283: b.add(f.field);
284: _mainGrid.add(b);
285: _mainGrid.add(Box.createRigidArea(new Dimension(255, 3)));
286: } else if (type == TYPE_FILE || type == TYPE_DIR
287: || type == TYPE_DIR_FILE) {
288: f.field = new JTextField("");
289: f.field.setMaximumSize(lookupSize);
290: f.field.setMinimumSize(lookupSize);
291:
292: f.lookup = new JButton("..");
293: ((JButton) f.lookup).addActionListener(this );
294: ((JButton) f.lookup).setMaximumSize(buttonSize);
295: ((JButton) f.lookup).setMinimumSize(buttonSize);
296: b.add(f.field);
297: b.add(f.lookup);
298: _mainGrid.add(b);
299: } else if (type == TYPE_CHECKBOX) {
300: f.field = new JCheckBox(label);
301: f.field.setMaximumSize(lookupSize);
302: f.field.setMinimumSize(lookupSize);
303: b.add(f.field);
304: _mainGrid.add(b);
305: }
306:
307: _fields[_fieldIndex++] = f;
308: return f.field;
309: }
310:
311: private static void displayError(String error) {
312: System.out.println("Error Display:" + error);
313: JFrame f = new JFrame();
314: JOptionPane.showMessageDialog(f, error, "Error",
315: JOptionPane.ERROR_MESSAGE);
316: f.dispose();
317: }
318:
319: /**
320: * Returns true if the user clicked the cancel button to exit the dialog
321: */
322: public boolean getCancel() {
323: return _cancelClicked;
324: }
325:
326: /**
327: * Returns a Hashtable of items changed by the user
328: */
329: public Hashtable getDBSettings() {
330: return _dbsettings;
331: }
332:
333: public void windowActivated(WindowEvent e) {
334:
335: }
336:
337: public void windowClosed(WindowEvent e) {
338: }
339:
340: public void windowClosing(WindowEvent e) {
341: }
342:
343: public void windowDeactivated(WindowEvent e) {
344: }
345:
346: public void windowDeiconified(WindowEvent e) {
347: }
348:
349: public void windowIconified(WindowEvent e) {
350: }
351:
352: public void windowOpened(WindowEvent e) {
353: }
354: }
|