001: package net.sourceforge.squirrel_sql.client.gui.db;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import static net.sourceforge.squirrel_sql.client.preferences.PreferenceType.DRIVER_DEFINITIONS;
022: import static net.sourceforge.squirrel_sql.client.preferences.PreferenceType.GLOBAL_PREFERENCES;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Container;
027: import java.awt.Dimension;
028: import java.awt.GridBagConstraints;
029: import java.awt.GridBagLayout;
030: import java.awt.Insets;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.KeyEvent;
034: import java.io.File;
035: import java.net.MalformedURLException;
036: import java.net.URL;
037:
038: import javax.swing.AbstractAction;
039: import javax.swing.BorderFactory;
040: import javax.swing.JButton;
041: import javax.swing.JComboBox;
042: import javax.swing.JComponent;
043: import javax.swing.JFileChooser;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046: import javax.swing.JScrollPane;
047: import javax.swing.JSeparator;
048: import javax.swing.JTabbedPane;
049: import javax.swing.JTextField;
050: import javax.swing.KeyStroke;
051: import javax.swing.ListModel;
052: import javax.swing.SwingConstants;
053: import javax.swing.SwingUtilities;
054: import javax.swing.UIManager;
055: import javax.swing.event.ListDataEvent;
056: import javax.swing.event.ListDataListener;
057: import javax.swing.event.ListSelectionEvent;
058: import javax.swing.event.ListSelectionListener;
059:
060: import net.sourceforge.squirrel_sql.client.IApplication;
061: import net.sourceforge.squirrel_sql.client.gui.BaseInternalFrame;
062: import net.sourceforge.squirrel_sql.fw.gui.DefaultFileListBoxModel;
063: import net.sourceforge.squirrel_sql.fw.gui.FileListBox;
064: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
065: import net.sourceforge.squirrel_sql.fw.gui.IFileListBoxModel;
066: import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
067: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
068: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverClassLoader;
069: import net.sourceforge.squirrel_sql.fw.util.FileExtensionFilter;
070: import net.sourceforge.squirrel_sql.fw.util.StringManager;
071: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
072: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
073: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
074:
075: /**
076: * This dialog allows maintenance of a JDBC driver definition.
077: *
078: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
079: */
080: @SuppressWarnings("serial")
081: public class DriverInternalFrame extends BaseInternalFrame {
082: /** Different types of maintenance that can be done. */
083: public interface MaintenanceType {
084: int NEW = 1;
085: int MODIFY = 2;
086: int COPY = 3;
087: }
088:
089: /** Internationalized strings for this class. */
090: private static final StringManager s_stringMgr = StringManagerFactory
091: .getStringManager(DriverInternalFrame.class);
092:
093: /** Logger for this class. */
094: private static final ILogger s_log = LoggerController
095: .createLogger(DriverInternalFrame.class);
096:
097: /** Number of characters to display in D/E fields. */
098: private static final int COLUMN_COUNT = 25;
099:
100: /** Width to make listboxes. */
101: private static final int LIST_WIDTH = 400;
102:
103: /** Application API. */
104: private final IApplication _app;
105:
106: /** JDBC driver being maintained. */
107: private final ISQLDriver _sqlDriver;
108:
109: /** Type of maintenance being done. @see MaintenanceType. */
110: private final int _maintType;
111:
112: /** Frame title. */
113: private final JLabel _titleLbl = new JLabel();
114:
115: /** Control for the <TT>ISQLDriver.IPropertyNames.NAME</TT> property. */
116: private final JTextField _driverName = new JTextField();
117:
118: /** Control for the <TT>ISQLDriver.IPropertyNames.DRIVER_CLASS</TT> property. */
119: private final JComboBox _driverClassCmb = new JComboBox();
120:
121: /** Control for the <TT>ISQLDriver.IPropertyNames.URL</TT> property. */
122: private final JTextField _url = new JTextField();
123:
124: private final JTextField _weburl = new JTextField();
125:
126: /** Listbox containing the Java class path. */
127: private final FileListBox _javaClassPathList = new FileListBox();
128:
129: /** Listbox containing the extra class path. */
130: private final FileListBox _extraClassPathList = new FileListBox(
131: new DefaultFileListBoxModel());
132:
133: /** Button to list drivers in a jar within the Java Class path list. */
134: private ListDriversButton _javaClasspathListDriversBtn;
135:
136: /** Button to list drivers in a jar within the Extra Class path list. */
137: private ListDriversButton _extraClasspathListDriversBtn;
138:
139: /** Button to delete entry from Extra Class path list. */
140: private JButton _extraClasspathDeleteBtn;
141:
142: /** Button to move entry up in Extra Class path list. */
143: private JButton _extraClasspathUpBtn;
144:
145: /** Button to move entry down in Extra Class path list. */
146: private JButton _extraClasspathDownBtn;
147:
148: /**
149: * Ctor.
150: *
151: * @param app Application API.
152: * @param sqlDriver JDBC driver definition to be maintained.
153: * @param maintType Maintenance type. @see MaintenanceType.
154: *
155: * @throws IllegalArgumentException
156: * Thrown if <TT>null</TT> passed for <TT>app</TT> or <TT>sqlDriver</TT> or
157: * an invalid value passed for <TT>maintType</TT>.
158: */
159: DriverInternalFrame(IApplication app, ISQLDriver sqlDriver,
160: int maintType) {
161: super ("", true);
162: if (app == null) {
163: throw new IllegalArgumentException(
164: "Null IApplication passed");
165: }
166: if (sqlDriver == null) {
167: throw new IllegalArgumentException("Null ISQLDriver passed");
168: }
169: if (maintType < MaintenanceType.NEW
170: || maintType > MaintenanceType.COPY) {
171: throw new IllegalArgumentException(
172: // i18n[DriverInternalFrame.error.illegalvalue=Illegal value of {0} passed for Maintenance type]
173: s_stringMgr.getString(
174: "DriverInternalFrame.error.illegalvalue",
175: maintType));
176: }
177:
178: _app = app;
179: _sqlDriver = sqlDriver;
180: _maintType = maintType;
181:
182: createGUI();
183: loadData();
184: pack();
185: }
186:
187: /**
188: * Set title of this frame. Ensure that the title label
189: * matches the frame title.
190: *
191: * @param title New title text.
192: */
193: public void setTitle(String title) {
194: super .setTitle(title);
195: _titleLbl.setText(title);
196: }
197:
198: /**
199: * Return the driver that is being maintained.
200: *
201: * @return the driver that is being maintained.
202: */
203: ISQLDriver getSQLDriver() {
204: return _sqlDriver;
205: }
206:
207: /**
208: * Load data from the JDBC driver definition into the maintenance controls.
209: */
210: private void loadData() {
211: _driverName.setText(_sqlDriver.getName());
212: _driverClassCmb
213: .setSelectedItem(_sqlDriver.getDriverClassName());
214: _url.setText(_sqlDriver.getUrl());
215: _weburl.setText(_sqlDriver.getWebSiteUrl());
216:
217: _extraClassPathList.removeAll();
218: String[] fileNames = _sqlDriver.getJarFileNames();
219: IFileListBoxModel model = _extraClassPathList.getTypedModel();
220: for (int i = 0; i < fileNames.length; ++i) {
221: model.addFile(new File(fileNames[i]));
222: }
223:
224: if (model.getSize() > 0) {
225: _extraClassPathList.setSelectedIndex(0);
226: }
227:
228: }
229:
230: /**
231: * User has requested close so get rid of this maintenance dialog.
232: */
233: private void performClose() {
234: dispose();
235: }
236:
237: /**
238: * OK button pressed. Edit data and if ok save to drivers model
239: * and then close dialog.
240: */
241: private void performOk() {
242: try {
243: applyFromDialog();
244: if (_maintType == MaintenanceType.NEW
245: || _maintType == MaintenanceType.COPY) {
246: _app.getDataCache().addDriver(_sqlDriver,
247: _app.getMessageHandler());
248: } else {
249: _app.getDataCache().refreshDriver(_sqlDriver,
250: _app.getMessageHandler());
251: }
252: _app.savePreferences(DRIVER_DEFINITIONS);
253: _app.savePreferences(GLOBAL_PREFERENCES);
254: dispose();
255: } catch (Throwable th) {
256: displayErrorMessage(th);
257: }
258: }
259:
260: /**
261: * Apply data from the data entry controls to the JDBC driver definition.
262: */
263: private void applyFromDialog() throws ValidationException {
264: _sqlDriver.setName(_driverName.getText().trim());
265: _sqlDriver.setJarFileNames(_extraClassPathList.getTypedModel()
266: .getFileNames());
267:
268: String driverClassName = (String) _driverClassCmb
269: .getSelectedItem();
270: _sqlDriver
271: .setDriverClassName(driverClassName != null ? driverClassName
272: .trim()
273: : null);
274:
275: _sqlDriver.setUrl(_url.getText().trim());
276: _sqlDriver.setWebSiteUrl(_weburl.getText().trim());
277: }
278:
279: /**
280: * Display an error msg in a dialog. Uses
281: * <TT>SwingUtilities.invokeLater()</TT> because this may be called
282: * before the main dialog is displayed.
283: *
284: * @param th The exception containing the error message.
285: */
286: private void displayErrorMessage(final Throwable th) {
287: SwingUtilities.invokeLater(new Runnable() {
288: public void run() {
289: _app.showErrorDialog(th);
290: }
291: });
292: }
293:
294: private void createGUI() {
295: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
296:
297: // This is a tool window.
298: GUIUtils.makeToolWindow(this , true);
299:
300: String winTitle;
301: if (_maintType == MaintenanceType.MODIFY) {
302: winTitle = s_stringMgr.getString(
303: "DriverInternalFrame.changedriver", _sqlDriver
304: .getName());
305: } else {
306: winTitle = s_stringMgr
307: .getString("DriverInternalFrame.adddriver");
308: }
309: setTitle(winTitle);
310:
311: _driverName.setColumns(COLUMN_COUNT);
312: _url.setColumns(COLUMN_COUNT);
313:
314: // Reset the background to the colour that the current Look
315: // and Feel uses for internal frames.
316: Container contentPane = getContentPane();
317: Color color = UIManager.getDefaults().getColor(
318: "Panel.background");
319: if (color != null) {
320: contentPane.setBackground(color);
321: }
322:
323: GridBagConstraints gbc = new GridBagConstraints();
324: contentPane.setLayout(new GridBagLayout());
325:
326: gbc.anchor = GridBagConstraints.NORTHWEST;
327: gbc.gridwidth = GridBagConstraints.REMAINDER;
328: gbc.weightx = 1;
329:
330: // Title label at top.
331: gbc.gridx = 0;
332: gbc.gridy = 0;
333: gbc.insets = new Insets(5, 10, 5, 10);
334: gbc.fill = GridBagConstraints.HORIZONTAL;
335: contentPane.add(_titleLbl, gbc);
336:
337: // Separated by a line.
338: ++gbc.gridy;
339: gbc.insets = new Insets(0, 10, 5, 10);
340: contentPane.add(new JSeparator(), gbc);
341:
342: ++gbc.gridy;
343: contentPane.add(createDriverPanel(), gbc);
344:
345: JTabbedPane tabPnl = new JTabbedPane();
346: tabPnl.addTab(s_stringMgr
347: .getString("DriverInternalFrame.javaclasspath"),
348: createJavaClassPathPanel());
349: tabPnl.addTab(s_stringMgr
350: .getString("DriverInternalFrame.extraclasspath"),
351: createExtraClassPathPanel());
352:
353: ++gbc.gridy;
354: gbc.fill = GridBagConstraints.BOTH;
355: gbc.weighty = 1;
356: contentPane.add(tabPnl, gbc);
357:
358: ++gbc.gridy;
359: gbc.fill = GridBagConstraints.HORIZONTAL;
360: gbc.weighty = 0;
361: contentPane.add(createDriverClassPanel(), gbc);
362:
363: // Separated by a line.
364: ++gbc.gridy;
365: gbc.fill = GridBagConstraints.HORIZONTAL;
366: gbc.insets = new Insets(5, 10, 5, 10);
367: contentPane.add(new JSeparator(), gbc);
368:
369: ++gbc.gridy;
370: contentPane.add(createButtonsPanel(), gbc);
371:
372: AbstractAction closeAction = new AbstractAction() {
373: public void actionPerformed(ActionEvent actionEvent) {
374: performClose();
375: }
376: };
377: KeyStroke escapeStroke = KeyStroke.getKeyStroke(
378: KeyEvent.VK_ESCAPE, 0);
379: getRootPane().getInputMap(
380: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
381: escapeStroke, "CloseAction");
382: getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
383: .put(escapeStroke, "CloseAction");
384: getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(
385: escapeStroke, "CloseAction");
386: getRootPane().getActionMap().put("CloseAction", closeAction);
387:
388: }
389:
390: private JPanel createButtonsPanel() {
391: JPanel pnl = new JPanel();
392:
393: JButton okBtn = new JButton(s_stringMgr
394: .getString("DriverInternalFrame.ok"));
395: okBtn.addActionListener(new ActionListener() {
396: public void actionPerformed(ActionEvent evt) {
397: performOk();
398: }
399: });
400: JButton closeBtn = new JButton(s_stringMgr
401: .getString("DriverInternalFrame.close"));
402: closeBtn.addActionListener(new ActionListener() {
403: public void actionPerformed(ActionEvent evt) {
404: performClose();
405: }
406: });
407:
408: pnl.add(okBtn);
409: pnl.add(closeBtn);
410:
411: GUIUtils
412: .setJButtonSizesTheSame(new JButton[] { okBtn, closeBtn });
413: getRootPane().setDefaultButton(okBtn);
414:
415: return pnl;
416: }
417:
418: private JPanel createDriverPanel() {
419: _driverName.setColumns(25);
420:
421: JPanel pnl = new JPanel(new GridBagLayout());
422: pnl.setBorder(BorderFactory.createTitledBorder(s_stringMgr
423: .getString("DriverInternalFrame.driver")));
424:
425: final GridBagConstraints gbc = new GridBagConstraints();
426: gbc.fill = GridBagConstraints.HORIZONTAL;
427: gbc.insets = new Insets(4, 4, 4, 4);
428:
429: gbc.gridx = 0;
430: gbc.gridy = 0;
431: pnl.add(new JLabel(s_stringMgr
432: .getString("DriverInternalFrame.name"),
433: SwingConstants.RIGHT), gbc);
434:
435: ++gbc.gridy;
436: pnl.add(new JLabel(s_stringMgr
437: .getString("DriverInternalFrame.egurl"),
438: SwingConstants.RIGHT), gbc);
439:
440: ++gbc.gridy;
441: pnl.add(new JLabel(s_stringMgr
442: .getString("DriverInternalFrame.weburl"),
443: SwingConstants.RIGHT), gbc);
444:
445: gbc.weightx = 1.0;
446: gbc.gridy = 0;
447: ++gbc.gridx;
448: pnl.add(_driverName, gbc);
449:
450: ++gbc.gridy;
451: pnl.add(_url, gbc);
452:
453: ++gbc.gridy;
454: pnl.add(_weburl, gbc);
455:
456: return pnl;
457: }
458:
459: private Component createDriverClassPanel() {
460: _driverClassCmb.setEditable(true);
461:
462: JPanel pnl = new JPanel(new GridBagLayout());
463:
464: final GridBagConstraints gbc = new GridBagConstraints();
465: gbc.fill = GridBagConstraints.HORIZONTAL;
466: gbc.insets = new Insets(4, 4, 4, 4);
467:
468: gbc.gridx = 0;
469: gbc.gridy = 0;
470: pnl.add(new JLabel(s_stringMgr
471: .getString("DriverInternalFrame.classname"),
472: SwingConstants.RIGHT), gbc);
473:
474: gbc.weightx = 1.0;
475: ++gbc.gridx;
476: pnl.add(_driverClassCmb, gbc);
477:
478: return pnl;
479: }
480:
481: /**
482: * Create the panel that displays the current class path.
483: *
484: * @return Panel that displays the current class path.
485: */
486: private JPanel createJavaClassPathPanel() {
487: _javaClasspathListDriversBtn = new ListDriversButton(
488: _javaClassPathList);
489: _javaClasspathListDriversBtn.setEnabled(_javaClassPathList
490: .getModel().getSize() > 0);
491: // _javaClassPathList.addListSelectionListener(new JavaClassPathListBoxListener());
492:
493: IFileListBoxModel model = _javaClassPathList.getTypedModel();
494: if (model.getSize() > 0) {
495: _javaClassPathList.setSelectedIndex(0);
496: }
497:
498: JPanel pnl = new JPanel(new GridBagLayout());
499: final GridBagConstraints gbc = new GridBagConstraints();
500: gbc.insets = new Insets(4, 4, 4, 4);
501: gbc.anchor = GridBagConstraints.WEST;
502: gbc.weighty = 1.0;
503:
504: // Scrollbars are "shown always" to stop sheet resizing when they
505: // are shown/hidden.
506: gbc.gridx = 0;
507: gbc.gridy = 0;
508: gbc.gridheight = GridBagConstraints.REMAINDER;
509: gbc.fill = GridBagConstraints.BOTH;
510: gbc.weightx = 1.0;
511: JScrollPane sp = new JScrollPane(_javaClassPathList,
512: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
513: JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
514: final Dimension dm = sp.getPreferredSize();
515: dm.width = LIST_WIDTH; // Required otherwise it gets too wide.
516: sp.setPreferredSize(dm);
517: pnl.add(sp, gbc);
518:
519: ++gbc.gridx;
520: gbc.gridheight = 1;
521: gbc.fill = GridBagConstraints.HORIZONTAL;
522: gbc.anchor = GridBagConstraints.NORTH;
523: gbc.weightx = 0.0;
524: pnl.add(_javaClasspathListDriversBtn, gbc);
525:
526: return pnl;
527: }
528:
529: /**
530: * Create the panel that displays the extra class path.
531: *
532: * @return Panel that displays the extra class path.
533: */
534: private JPanel createExtraClassPathPanel() {
535: _extraClasspathListDriversBtn = new ListDriversButton(
536: _extraClassPathList);
537: _extraClassPathList
538: .addListSelectionListener(new ExtraClassPathListBoxListener());
539: _extraClassPathList.getModel().addListDataListener(
540: new ExtraClassPathListDataListener());
541:
542: _extraClasspathUpBtn = new JButton(s_stringMgr
543: .getString("DriverInternalFrame.up"));
544: _extraClasspathUpBtn.setEnabled(false);
545: _extraClasspathUpBtn.addActionListener(new ActionListener() {
546: public void actionPerformed(ActionEvent evt) {
547: synchronized (_extraClassPathList) {
548: int idx = _extraClassPathList.getSelectedIndex();
549: if (idx > 0) {
550: IFileListBoxModel model = _extraClassPathList
551: .getTypedModel();
552: File file = model.removeFile(idx);
553: --idx;
554: model.insertFileAt(file, idx);
555: _extraClassPathList.setSelectedIndex(idx);
556: }
557: }
558: }
559: });
560:
561: _extraClasspathDownBtn = new JButton(s_stringMgr
562: .getString("DriverInternalFrame.down"));
563: _extraClasspathDownBtn.setEnabled(false);
564: _extraClasspathDownBtn.addActionListener(new ActionListener() {
565: public void actionPerformed(ActionEvent evt) {
566: synchronized (_extraClassPathList) {
567: int idx = _extraClassPathList.getSelectedIndex();
568: IFileListBoxModel model = _extraClassPathList
569: .getTypedModel();
570: if (idx > -1 && idx < (model.getSize() - 1)) {
571: File file = model.removeFile(idx);
572: ++idx;
573: model.insertFileAt(file, idx);
574: _extraClassPathList.setSelectedIndex(idx);
575: }
576: }
577: }
578: });
579:
580: JButton newBtn = new AddListEntryButton();
581:
582: _extraClasspathDeleteBtn = new JButton(s_stringMgr
583: .getString("DriverInternalFrame.delete"));
584: _extraClasspathDeleteBtn.setEnabled(false);
585: _extraClasspathDeleteBtn
586: .addActionListener(new ActionListener() {
587: public void actionPerformed(ActionEvent evt) {
588: int idx = _extraClassPathList
589: .getSelectedIndex();
590: if (idx != -1) {
591: IFileListBoxModel model = _extraClassPathList
592: .getTypedModel();
593: model.removeFile(idx);
594: final int size = model.getSize();
595: if (idx < size) {
596: _extraClassPathList
597: .setSelectedIndex(idx);
598: } else if (size > 0) {
599: _extraClassPathList
600: .setSelectedIndex(size - 1);
601: }
602: }
603: }
604: });
605:
606: JPanel pnl = new JPanel(new GridBagLayout());
607: final GridBagConstraints gbc = new GridBagConstraints();
608: gbc.anchor = GridBagConstraints.WEST;
609: gbc.weightx = 1.0;
610: gbc.weighty = 1.0;
611: gbc.insets = new Insets(4, 4, 4, 4);
612:
613: // Scrollbars are "shown always" to stop sheet resizing when they
614: // are shown/hidden.
615: gbc.gridheight = GridBagConstraints.REMAINDER;
616: gbc.gridx = 0;
617: gbc.gridy = 0;
618: gbc.fill = GridBagConstraints.BOTH;
619: JScrollPane sp = new JScrollPane(_extraClassPathList,
620: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
621: JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
622: final Dimension dm = sp.getPreferredSize();
623: dm.width = LIST_WIDTH; // Required otherwise it gets too wide.
624: sp.setPreferredSize(dm);
625: pnl.add(sp, gbc);
626:
627: gbc.gridheight = 1;
628: gbc.weightx = 0.0;
629: gbc.weighty = 0.0;
630: gbc.fill = GridBagConstraints.HORIZONTAL;
631: ++gbc.gridx;
632: pnl.add(_extraClasspathListDriversBtn, gbc);
633:
634: ++gbc.gridy;
635: gbc.insets = new Insets(5, 5, 5, 5);
636: pnl.add(new JSeparator(), gbc);
637: gbc.insets = new Insets(4, 4, 4, 4);
638:
639: ++gbc.gridy;
640: pnl.add(_extraClasspathUpBtn, gbc);
641:
642: ++gbc.gridy;
643: pnl.add(_extraClasspathDownBtn, gbc);
644:
645: ++gbc.gridy;
646: gbc.insets = new Insets(5, 5, 5, 5);
647: pnl.add(new JSeparator(), gbc);
648: gbc.insets = new Insets(4, 4, 4, 4);
649:
650: ++gbc.gridy;
651: pnl.add(newBtn, gbc);
652:
653: ++gbc.gridy;
654: pnl.add(_extraClasspathDeleteBtn, gbc);
655:
656: return pnl;
657: }
658:
659: /**
660: * Button that allows user to enter new items in the Extra Class Path
661: * list.
662: */
663: private final class AddListEntryButton extends JButton implements
664: ActionListener {
665: private JFileChooser _chooser;
666:
667: AddListEntryButton() {
668: super (s_stringMgr.getString("DriverInternalFrame.add"));
669: addActionListener(this );
670: }
671:
672: public void actionPerformed(ActionEvent evt) {
673: if (_chooser == null) {
674: _chooser = new JFileChooser();
675: _chooser.setMultiSelectionEnabled(true);
676: _chooser
677: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
678: _chooser
679: .addChoosableFileFilter(new FileExtensionFilter(
680: s_stringMgr
681: .getString("DriverInternalFrame.jarfiles"),
682: new String[] { ".jar", ".zip" }));
683: }
684: int returnVal = _chooser.showOpenDialog(getParent());
685: if (returnVal == JFileChooser.APPROVE_OPTION) {
686: File[] selFiles = _chooser.getSelectedFiles();
687: if (selFiles != null) {
688: IFileListBoxModel myModel = _extraClassPathList
689: .getTypedModel();
690: for (int i = 0; i < selFiles.length; ++i) {
691: myModel.addFile(selFiles[i]);
692: }
693: _extraClassPathList.setSelectedIndex(myModel
694: .getSize() - 1);
695: }
696: }
697: }
698: }
699:
700: /**
701: * Button that will list all the drivers in the file current selected
702: * in a listbox.
703: */
704: private final class ListDriversButton extends JButton implements
705: ActionListener {
706: private FileListBox _listBox;
707:
708: ListDriversButton(FileListBox listBox) {
709: super (s_stringMgr
710: .getString("DriverInternalFrame.listdrivers"));
711: setEnabled(false);
712: _listBox = listBox;
713: addActionListener(this );
714: }
715:
716: @SuppressWarnings("unchecked")
717: public void actionPerformed(ActionEvent e) {
718: _driverClassCmb.removeAllItems();
719: // File file = _listBox.getSelectedFile();
720: String[] fileNames = _listBox.getTypedModel()
721: .getFileNames();
722:
723: // if (file != null)
724: if (fileNames.length > 0) {
725: try {
726: final URL[] urls = new URL[fileNames.length];
727: for (int i = 0; i < fileNames.length; ++i) {
728: urls[i] = new File(fileNames[i]).toURL();
729: }
730: // SQLDriverClassLoader cl = new SQLDriverClassLoader(file.toURL());
731: SQLDriverClassLoader cl = new SQLDriverClassLoader(
732: urls);
733: Class[] classes = cl.getDriverClasses(s_log);
734: for (int i = 0; i < classes.length; ++i) {
735: _driverClassCmb.addItem(classes[i].getName());
736: }
737: } catch (MalformedURLException ex) {
738: displayErrorMessage(ex);
739: }
740: }
741: if (_driverClassCmb.getItemCount() > 0) {
742: _driverClassCmb.setSelectedIndex(0);
743: }
744: }
745:
746: }
747:
748: // private class JavaClassPathListBoxListener implements ListSelectionListener
749: // {
750: // public void valueChanged(ListSelectionEvent evt)
751: // {
752: // final int selIdx = _javaClassPathList.getSelectedIndex();
753: // _javaClasspathListDriversBtn.setEnabled(selIdx != -1);
754: // boolean enable = false;
755: // if (selIdx != -1)
756: // {
757: // File file = _javaClassPathList.getSelectedFile();
758: // if (file != null)
759: // {
760: // enable = file.isFile();
761: // }
762: // }
763: // _javaClasspathListDriversBtn.setEnabled(enable);
764: // }
765: // }
766:
767: private class ExtraClassPathListDataListener implements
768: ListDataListener {
769: public void contentsChanged(ListDataEvent evt) {
770: final boolean enable = _extraClassPathList.getModel()
771: .getSize() > 0;
772: _extraClasspathListDriversBtn.setEnabled(enable);
773: }
774:
775: public void intervalAdded(ListDataEvent evt) {
776: final boolean enable = _extraClassPathList.getModel()
777: .getSize() > 0;
778: _extraClasspathListDriversBtn.setEnabled(enable);
779: }
780:
781: public void intervalRemoved(ListDataEvent evt) {
782: final boolean enable = _extraClassPathList.getModel()
783: .getSize() > 0;
784: _extraClasspathListDriversBtn.setEnabled(enable);
785: }
786: }
787:
788: private class ExtraClassPathListBoxListener implements
789: ListSelectionListener {
790: public void valueChanged(ListSelectionEvent evt) {
791: final int selIdx = _extraClassPathList.getSelectedIndex();
792: final ListModel model = _extraClassPathList.getModel();
793:
794: _extraClasspathDeleteBtn.setEnabled(selIdx != -1);
795:
796: _extraClasspathUpBtn.setEnabled(selIdx > 0
797: && model.getSize() > 1);
798: _extraClasspathDownBtn.setEnabled(selIdx > -1
799: && selIdx < (model.getSize() - 1));
800: //
801: // boolean enable = false;
802: // if (selIdx != -1)
803: // {
804: // File file = _extraClassPathList.getSelectedFile();
805: // if (file != null)
806: // {
807: // enable = file.isFile();
808: // }
809: // }
810: // _extraClasspathListDriversBtn.setEnabled(enable);
811: }
812: }
813: }
|