001: package org.jsqltool.conn.gui;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.event.*;
007: import java.io.*;
008: import java.util.*;
009:
010: import org.jsqltool.conn.*;
011: import org.jsqltool.gui.*;
012: import org.jsqltool.utils.ImageLoader;
013: import org.jsqltool.utils.Options;
014: import org.jsqltool.gui.tableplugins.datatable.filter.*;
015:
016: /**
017: * <p>Title: JSqlTool Project</p>
018: * <p>Description: Window used to select a database connection.
019: * This window allows to create/edit/delete connections.
020: * </p>
021: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
022: *
023: * <p> This file is part of JSqlTool project.
024: * This library is free software; you can redistribute it and/or
025: * modify it under the terms of the (LGPL) Lesser General Public
026: * License as published by the Free Software Foundation;
027: *
028: * GNU LESSER GENERAL PUBLIC LICENSE
029: * Version 2.1, February 1999
030: *
031: * This library is distributed in the hope that it will be useful,
032: * but WITHOUT ANY WARRANTY; without even the implied warranty of
033: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
034: * Library General Public License for more details.
035: *
036: * You should have received a copy of the GNU Library General Public
037: * License along with this library; if not, write to the Free
038: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
039: *
040: * The author may be contacted at:
041: * maurocarniel@tin.it</p>
042: *
043: * @author Mauro Carniel
044: * @version 1.0
045: */
046: public class ConnectionFrame extends JInternalFrame {
047:
048: JScrollPane scrollPane = new JScrollPane();
049: Vector connNames = new Vector();
050: JList connList = new JList(connNames);
051: JPanel buttonsPanel = new JPanel();
052: JButton delButton = new JButton();
053: JButton newButton = new JButton();
054: JButton editButton = new JButton();
055: GridBagLayout gridBagLayout1 = new GridBagLayout();
056: GridBagLayout gridBagLayout2 = new GridBagLayout();
057: JButton okButton = new JButton();
058: JButton cancelButton = new JButton();
059: JLabel connLabel = new JLabel();
060: private ArrayList conns = new ArrayList();
061: private MainFrame parent = null;
062: JButton copyButton = new JButton();
063:
064: public ConnectionFrame(MainFrame parent) {
065: this .parent = parent;
066: try {
067: this .setFrameIcon(ImageLoader.getInstance().getIcon(
068: "logo.gif"));
069: this .setSize(350, 300);
070: jbInit();
071: init();
072: toFront();
073: setSelected(true);
074: } catch (Exception e) {
075: e.printStackTrace();
076: }
077: }
078:
079: /**
080: * Constructor called by Replication Dialog to fetch connections list.
081: */
082: public ConnectionFrame() {
083: loadProfile();
084: }
085:
086: /**
087: * @return list of DbConnection objects
088: */
089: public final ArrayList getConnections() {
090: return conns;
091: }
092:
093: private void init() {
094: loadProfile();
095: if (connList.getModel().getSize() > 0) {
096: connList.setSelectedIndex(0);
097: }
098: }
099:
100: public final void updateList(DbConnection c, boolean isEdit) {
101: if (!isEdit) {
102: conns.add(c);
103: connNames.add(c.getName());
104: } else {
105: connNames.setElementAt(c.getName(), connList
106: .getSelectedIndex());
107: }
108: new DbConnectionUtil(parent, c).saveProfile(isEdit);
109:
110: scrollPane.getViewport().removeAll();
111: DefaultListModel model = new DefaultListModel();
112: for (int i = 0; i < connNames.size(); i++)
113: model.addElement(connNames.get(i));
114: connList.setModel(model);
115: connList.revalidate();
116: connList.repaint();
117: scrollPane.getViewport().add(connList, null);
118:
119: }
120:
121: /**
122: * Load all connection profile files (files profile/*.ini)
123: */
124: private void loadProfile() {
125: try {
126: conns.clear();
127: connNames.clear();
128:
129: // retrieve .ini file list...
130: File dir = new File("profile");
131: dir.mkdir();
132: File[] files = dir.listFiles(new FileFilter() {
133: public boolean accept(File pathname) {
134: return pathname.getName().endsWith(".ini");
135: }
136: });
137:
138: // load all .ini files...
139: ConnectionProfile cProfile = new ConnectionProfile();
140: for (int i = 0; i < files.length; i++) {
141: cProfile.loadProfile(this , files[i], conns, connNames);
142: }
143:
144: connList.revalidate();
145: } catch (Exception ex) {
146: ex.printStackTrace();
147: JOptionPane
148: .showMessageDialog(
149: this ,
150: Options
151: .getInstance()
152: .getResource(
153: "error on loading connections profile files.")
154: + ":\n" + ex.getMessage(),
155: Options.getInstance().getResource("error"),
156: JOptionPane.ERROR_MESSAGE);
157: }
158: }
159:
160: private void jbInit() throws Exception {
161: this .setIcon(false);
162: this .setTitle(Options.getInstance().getResource(
163: "JSqlTool login"));
164: this .setBorder(BorderFactory.createRaisedBevelBorder());
165: this .setDebugGraphicsOptions(0);
166: this .getContentPane().setLayout(gridBagLayout1);
167: delButton.setMnemonic(Options.getInstance().getResource(
168: "deleteconn.mnemonic").charAt(0));
169: delButton.setText(Options.getInstance().getResource(
170: "deleteconn.text"));
171: delButton
172: .addActionListener(new ConnectionFrame_delButton_actionAdapter(
173: this ));
174: newButton.setMnemonic(Options.getInstance().getResource(
175: "newconn.mnemonic").charAt(0));
176: newButton.setText(Options.getInstance().getResource(
177: "newconn.text"));
178: newButton
179: .addActionListener(new ConnectionFrame_newButton_actionAdapter(
180: this ));
181: editButton.setMnemonic(Options.getInstance().getResource(
182: "editconn.mnemonic").charAt(0));
183: editButton.setText(Options.getInstance().getResource(
184: "editconn.text"));
185: editButton
186: .addActionListener(new ConnectionFrame_editButton_actionAdapter(
187: this ));
188: buttonsPanel.setLayout(gridBagLayout2);
189: okButton.setMnemonic(Options.getInstance().getResource(
190: "okbutton.mnemonic").charAt(0));
191: okButton.setText(Options.getInstance().getResource(
192: "okbutton.text"));
193: okButton
194: .addActionListener(new ConnectionFrame_okButton_actionAdapter(
195: this ));
196: cancelButton.setMnemonic(Options.getInstance().getResource(
197: "cancelbutton.mnemonic").charAt(0));
198: cancelButton.setText(Options.getInstance().getResource(
199: "cancelbutton.text"));
200: cancelButton
201: .addActionListener(new ConnectionFrame_cancelButton_actionAdapter(
202: this ));
203: connLabel.setText(Options.getInstance().getResource(
204: "connections"));
205: copyButton.setMnemonic(Options.getInstance().getResource(
206: "copyconn.mnemonic").charAt(0));
207: copyButton.setText(Options.getInstance().getResource(
208: "copyconn.text"));
209: copyButton
210: .addActionListener(new ConnectionFrame_copyButton_actionAdapter(
211: this ));
212: buttonsPanel.setBorder(BorderFactory.createEtchedBorder());
213: connList
214: .addMouseListener(new ConnectionFrame_connList_mouseAdapter(
215: this ));
216: connList
217: .addKeyListener(new ConnectionFrame_connList_keyAdapter(
218: this ));
219: scrollPane.getViewport().add(connList, null);
220: this .getContentPane().add(
221: buttonsPanel,
222: new GridBagConstraints(1, 0, 1, 2, 1.0, 1.0,
223: GridBagConstraints.EAST,
224: GridBagConstraints.BOTH,
225: new Insets(5, 5, 5, 5), 0, 0));
226: buttonsPanel.add(okButton, new GridBagConstraints(0, 0, 1, 1,
227: 0.0, 0.0, GridBagConstraints.CENTER,
228: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
229: 0, 0));
230: buttonsPanel.add(delButton, new GridBagConstraints(0, 5, 1, 1,
231: 0.0, 0.0, GridBagConstraints.NORTH,
232: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
233: 0, 0));
234: buttonsPanel.add(newButton, new GridBagConstraints(0, 2, 1, 1,
235: 0.0, 0.0, GridBagConstraints.CENTER,
236: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
237: 0, 0));
238: buttonsPanel.add(editButton, new GridBagConstraints(0, 3, 1, 1,
239: 0.0, 0.0, GridBagConstraints.CENTER,
240: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
241: 0, 0));
242: this .getContentPane().add(
243: scrollPane,
244: new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
245: GridBagConstraints.CENTER,
246: GridBagConstraints.BOTH,
247: new Insets(0, 5, 5, 5), 0, 0));
248: this .getContentPane().add(
249: connLabel,
250: new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
251: GridBagConstraints.WEST,
252: GridBagConstraints.NONE,
253: new Insets(5, 5, 5, 5), 0, 0));
254: buttonsPanel.add(cancelButton, new GridBagConstraints(0, 1, 1,
255: 1, 0.0, 0.0, GridBagConstraints.CENTER,
256: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
257: 0, 0));
258: buttonsPanel.add(copyButton, new GridBagConstraints(0, 4, 1, 1,
259: 0.0, 0.0, GridBagConstraints.CENTER,
260: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
261: 0, 0));
262: }
263:
264: void editButton_actionPerformed(ActionEvent e) {
265: if (connList.getSelectedIndex() == -1)
266: return;
267: new ConnectionDialog(parent, this , (DbConnection) conns
268: .get(connList.getSelectedIndex()),
269: ConnectionDialog.EDIT);
270: }
271:
272: void newButton_actionPerformed(ActionEvent e) {
273: new ConnectionDialog(parent, this , new DbConnection(0, "", "",
274: "", "", "", false, 0, false, "", new Hashtable(),
275: new ArrayList(), false), ConnectionDialog.INSERT);
276: }
277:
278: void delButton_actionPerformed(ActionEvent e) {
279: if (connList.getSelectedIndex() == -1)
280: return;
281: DbConnection c = (DbConnection) conns.remove(connList
282: .getSelectedIndex());
283: connNames.remove(connList.getSelectedIndex());
284:
285: scrollPane.getViewport().removeAll();
286: connList = new JList(connNames);
287: scrollPane.getViewport().add(connList, null);
288:
289: new File("profile/" + c.getName().replace(' ', '_') + ".ini")
290: .delete();
291: }
292:
293: void okButton_actionPerformed(ActionEvent e) {
294: if (connList.getSelectedIndex() == -1)
295: return;
296: new Thread() {
297: public void run() {
298: setVisible(false);
299: DbConnection c = (DbConnection) conns.get(connList
300: .getSelectedIndex());
301: try {
302: parent.createTableListFrame(new DbConnectionUtil(
303: parent, c));
304: dispose();
305: } catch (Throwable ex) {
306: }
307: }
308: }.start();
309: }
310:
311: void cancelButton_actionPerformed(ActionEvent e) {
312: this .setVisible(false);
313: this .dispose();
314: }
315:
316: void copyButton_actionPerformed(ActionEvent e) {
317: if (connList.getSelectedIndex() == -1)
318: return;
319: DbConnection c = (DbConnection) conns.get(connList
320: .getSelectedIndex());
321: new ConnectionDialog(parent, this , new DbConnection(c
322: .getDbType(), "", c.getClassName(), c.getUrl(), c
323: .getUsername(), c.getPassword(), c.isAutoCommit(), c
324: .getIsolationLevel(), c.isReadOnly(), c.getCatalog(),
325: new Hashtable(), new ArrayList(), c.isQuotes()),
326: ConnectionDialog.COPY);
327: }
328:
329: void connList_mouseClicked(MouseEvent e) {
330: if (connList.getSelectedIndex() != -1 && e.getClickCount() == 2
331: && SwingUtilities.isLeftMouseButton(e))
332: okButton_actionPerformed(null);
333: }
334:
335: void connList_keyTyped(KeyEvent e) {
336: if (e.getKeyChar() == '\n' && connList.getSelectedIndex() != -1)
337: okButton_actionPerformed(null);
338: }
339:
340: public void requestFocus() {
341: connList.requestFocus();
342: }
343:
344: }
345:
346: class ConnectionFrame_editButton_actionAdapter implements
347: java.awt.event.ActionListener {
348: ConnectionFrame adaptee;
349:
350: ConnectionFrame_editButton_actionAdapter(ConnectionFrame adaptee) {
351: this .adaptee = adaptee;
352: }
353:
354: public void actionPerformed(ActionEvent e) {
355: adaptee.editButton_actionPerformed(e);
356: }
357: }
358:
359: class ConnectionFrame_newButton_actionAdapter implements
360: java.awt.event.ActionListener {
361: ConnectionFrame adaptee;
362:
363: ConnectionFrame_newButton_actionAdapter(ConnectionFrame adaptee) {
364: this .adaptee = adaptee;
365: }
366:
367: public void actionPerformed(ActionEvent e) {
368: adaptee.newButton_actionPerformed(e);
369: }
370: }
371:
372: class ConnectionFrame_delButton_actionAdapter implements
373: java.awt.event.ActionListener {
374: ConnectionFrame adaptee;
375:
376: ConnectionFrame_delButton_actionAdapter(ConnectionFrame adaptee) {
377: this .adaptee = adaptee;
378: }
379:
380: public void actionPerformed(ActionEvent e) {
381: adaptee.delButton_actionPerformed(e);
382: }
383: }
384:
385: class ConnectionFrame_okButton_actionAdapter implements
386: java.awt.event.ActionListener {
387: ConnectionFrame adaptee;
388:
389: ConnectionFrame_okButton_actionAdapter(ConnectionFrame adaptee) {
390: this .adaptee = adaptee;
391: }
392:
393: public void actionPerformed(ActionEvent e) {
394: adaptee.okButton_actionPerformed(e);
395: }
396: }
397:
398: class ConnectionFrame_cancelButton_actionAdapter implements
399: java.awt.event.ActionListener {
400: ConnectionFrame adaptee;
401:
402: ConnectionFrame_cancelButton_actionAdapter(ConnectionFrame adaptee) {
403: this .adaptee = adaptee;
404: }
405:
406: public void actionPerformed(ActionEvent e) {
407: adaptee.cancelButton_actionPerformed(e);
408: }
409: }
410:
411: class ConnectionFrame_copyButton_actionAdapter implements
412: java.awt.event.ActionListener {
413: ConnectionFrame adaptee;
414:
415: ConnectionFrame_copyButton_actionAdapter(ConnectionFrame adaptee) {
416: this .adaptee = adaptee;
417: }
418:
419: public void actionPerformed(ActionEvent e) {
420: adaptee.copyButton_actionPerformed(e);
421: }
422: }
423:
424: class ConnectionFrame_connList_mouseAdapter extends
425: java.awt.event.MouseAdapter {
426: ConnectionFrame adaptee;
427:
428: ConnectionFrame_connList_mouseAdapter(ConnectionFrame adaptee) {
429: this .adaptee = adaptee;
430: }
431:
432: public void mouseClicked(MouseEvent e) {
433: adaptee.connList_mouseClicked(e);
434: }
435: }
436:
437: class ConnectionFrame_connList_keyAdapter extends
438: java.awt.event.KeyAdapter {
439: ConnectionFrame adaptee;
440:
441: ConnectionFrame_connList_keyAdapter(ConnectionFrame adaptee) {
442: this .adaptee = adaptee;
443: }
444:
445: public void keyTyped(KeyEvent e) {
446: adaptee.connList_keyTyped(e);
447: }
448: }
|