source: src/main/java/weka/gui/sql/ConnectionPanel.java @ 24

Last change on this file since 24 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 11.7 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * ConnectionPanel.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.gui.sql;
25
26import weka.gui.DatabaseConnectionDialog;
27import weka.gui.ListSelectorDialog;
28import weka.gui.sql.event.ConnectionEvent;
29import weka.gui.sql.event.ConnectionListener;
30import weka.gui.sql.event.HistoryChangedEvent;
31import weka.gui.sql.event.HistoryChangedListener;
32
33import java.awt.BorderLayout;
34import java.awt.FlowLayout;
35import java.awt.event.ActionEvent;
36import java.awt.event.ActionListener;
37import java.util.HashSet;
38import java.util.Iterator;
39
40import javax.swing.DefaultListModel;
41import javax.swing.JButton;
42import javax.swing.JFrame;
43import javax.swing.JLabel;
44import javax.swing.JList;
45import javax.swing.JOptionPane;
46import javax.swing.JPanel;
47import javax.swing.JTextField;
48import javax.swing.event.CaretListener;
49import javax.swing.event.CaretEvent;
50
51/**
52 * Enables the user to insert a database URL, plus user/password to connect
53 * to this database.
54 *
55 * @author      FracPete (fracpete at waikato dot ac dot nz)
56 * @version     $Revision: 1.3 $
57 */
58public class ConnectionPanel 
59  extends JPanel
60  implements CaretListener {
61
62  /** for serialization. */
63  static final long serialVersionUID = 3499317023969723490L;
64 
65  /** the name of the history. */
66  public final static String HISTORY_NAME = "connection";
67 
68  /** the parent frame. */
69  protected JFrame m_Parent = null;
70 
71  /** the databae connection dialog. */
72  protected DatabaseConnectionDialog m_DbDialog;
73
74  /** the URL to use. */
75  protected String m_URL = "";
76
77  /** the user to use for connecting to the DB. */
78  protected String m_User = "";
79
80  /** the password to use for connecting to the DB. */
81  protected String m_Password = "";
82
83  /** the label for the URL. */
84  protected JLabel m_LabelURL = new JLabel("URL ");
85
86  /** the textfield for the URL. */
87  protected JTextField m_TextURL = new JTextField(40);
88
89  /** the button for the DB-Dialog. */
90  protected JButton m_ButtonDatabase = new JButton("User...");
91
92  /** the button for connecting to the database. */
93  protected JButton m_ButtonConnect = new JButton("Connect");
94
95  /** the button for the history. */
96  protected JButton m_ButtonHistory = new JButton("History...");
97
98  /** the connection listeners. */
99  protected HashSet m_ConnectionListeners;
100
101  /** the history listeners. */
102  protected HashSet m_HistoryChangedListeners;
103
104  /** for connecting to the database. */
105  protected DbUtils m_DbUtils;
106
107  /** the history of connections. */
108  protected DefaultListModel m_History = new DefaultListModel();
109
110  /**
111   * initializes the panel.
112   *
113   * @param parent      the parent of this panel
114   */
115  public ConnectionPanel(JFrame parent) {
116    super();
117   
118    m_Parent                  = parent;
119    m_ConnectionListeners     = new HashSet();
120    m_HistoryChangedListeners = new HashSet();
121   
122    try {
123      m_DbUtils   = new DbUtils();
124      m_URL       = m_DbUtils.getDatabaseURL();
125      m_User      = m_DbUtils.getUsername();
126      m_Password  = m_DbUtils.getPassword();
127    }
128    catch (Exception e) {
129      e.printStackTrace();
130      m_URL      = "";
131      m_User     = "";
132      m_Password = "";
133    }
134   
135    createPanel();
136  }
137
138  /**
139   * builds the panel with all its components.
140   */
141  protected void createPanel() {
142    JPanel        panel;
143    JPanel        panel2;
144   
145    setLayout(new BorderLayout());
146    panel2 = new JPanel(new FlowLayout());
147    add(panel2, BorderLayout.WEST);
148
149    // label
150    m_LabelURL.setLabelFor(m_ButtonDatabase);
151    m_LabelURL.setDisplayedMnemonic('U');
152    panel2.add(m_LabelURL);
153
154    // editfield
155    m_TextURL.setText(m_URL);
156    m_TextURL.addCaretListener(this);
157    panel2.add(m_TextURL);
158   
159    // buttons
160    panel = new JPanel(new FlowLayout());
161    panel2.add(panel);
162   
163    m_ButtonDatabase.setMnemonic('s');
164    m_ButtonDatabase.addActionListener(new ActionListener() {
165        public void actionPerformed(ActionEvent e) {
166          showDialog();
167        }
168      });
169    panel.add(m_ButtonDatabase);
170   
171    m_ButtonConnect.setMnemonic('n');
172    m_ButtonConnect.addActionListener(new ActionListener() {
173        public void actionPerformed(ActionEvent e) {
174          connect();
175        }
176      });
177    panel.add(m_ButtonConnect);
178
179    m_ButtonHistory.addActionListener(new ActionListener() {
180        public void actionPerformed(ActionEvent e) {
181          showHistory();
182        }
183      });
184    panel.add(m_ButtonHistory);
185
186    setButtons();
187  }
188
189  /**
190   * sets the buttons according to the connected-state.
191   */
192  protected void setButtons() {
193    boolean isEmpty;
194   
195    isEmpty = m_TextURL.getText().equals("");
196   
197    m_ButtonConnect.setEnabled(!isEmpty);
198    m_ButtonDatabase.setEnabled(!isEmpty);
199    m_ButtonHistory.setEnabled(m_History.size() > 0);
200  }
201
202  /**
203   * sets the parameters back to standard.
204   */
205  public void clear() {
206    setURL(m_DbUtils.getDatabaseURL());
207    setUser(m_DbUtils.getUsername());
208    setPassword(m_DbUtils.getPassword());
209  }
210 
211  /**
212   * sets the focus in a designated control.
213   */
214  public void setFocus() {
215    m_TextURL.requestFocus();
216  }
217
218  /**
219   * sets the URL.
220   *
221   * @param url       the new value of the URL
222   */
223  public void setURL(String url) {
224    m_URL = url;
225    m_TextURL.setText(url);
226  }
227
228  /**
229   * returns the current URL.
230   *
231   * @return the current URL
232   */
233  public String getURL() {
234    m_URL = m_TextURL.getText();
235    return m_URL;
236  }
237
238  /**
239   * sets the User.
240   *
241   * @param user       the new value of the User
242   */
243  public void setUser(String user) {
244    m_User = user;
245  }
246
247  /**
248   * returns the current User.
249   *
250   * @return the current user
251   */
252  public String getUser() {
253    return m_User;
254  }
255
256  /**
257   * sets the Password.
258   *
259   * @param pw       the new value of the Password
260   */
261  public void setPassword(String pw) {
262    m_Password = pw;
263  }
264
265  /**
266   * returns the current Password.
267   *
268   * @return the current password
269   */
270  public String getPassword() {
271    return m_Password;
272  }
273
274  /**
275   * adds the given string to the history (removes duplicates).
276   *
277   * @param s           the string to add
278   */
279  protected void addHistory(String s) {
280    if (s.equals(""))
281      return;
282   
283    // no duplicates!
284    if (m_History.contains(s))
285      m_History.removeElement(s);
286
287    m_History.add(0, s);
288   
289    // send notification
290    notifyHistoryChangedListeners();
291  }
292
293  /**
294   * sets the local history to the given one.
295   *
296   * @param history     the history to use
297   */
298  public void setHistory(DefaultListModel history) {
299    int           i;
300   
301    m_History.clear();
302    for (i = 0; i < history.size(); i++)
303      m_History.addElement(history.get(i));
304
305    setButtons();
306  }
307
308  /**
309   * returns the history.
310   *
311   * @return        the current history
312   */
313  public DefaultListModel getHistory() {
314    return m_History;
315  }
316
317  /**
318   * displays the database dialog.
319   */
320  protected void showDialog() {
321    m_DbDialog = new DatabaseConnectionDialog(m_Parent, getURL(), getUser(), false);
322    m_DbDialog.setVisible(true);
323    if (m_DbDialog.getReturnValue() == JOptionPane.OK_OPTION) {
324      setURL(m_DbDialog.getURL());
325      setUser(m_DbDialog.getUsername());
326      setPassword(m_DbDialog.getPassword());
327    }
328
329    setButtons();
330  }
331
332  /**
333   * connects to the database, notifies the listeners.
334   */
335  protected void connect() {
336    // disconnect if still connected
337    if (m_DbUtils.isConnected()) {
338      try {
339        m_DbUtils.disconnectFromDatabase();
340        notifyConnectionListeners(ConnectionEvent.DISCONNECT);
341      }
342      catch (Exception e) {
343        e.printStackTrace();
344        notifyConnectionListeners(ConnectionEvent.DISCONNECT, e);
345      }
346    }
347
348    // connect
349    try {
350      m_DbUtils.setDatabaseURL(getURL());
351      m_DbUtils.setUsername(getUser());
352      m_DbUtils.setPassword(getPassword());
353      m_DbUtils.connectToDatabase();
354      notifyConnectionListeners(ConnectionEvent.CONNECT);
355      // add to history
356      addHistory(getUser() + "@" + getURL());
357    }
358    catch (Exception e) {
359      e.printStackTrace();
360      notifyConnectionListeners(ConnectionEvent.CONNECT, e);
361    }
362
363    setButtons();
364  }
365
366  /**
367   * displays the query history.
368   */
369  public void showHistory() {
370    JList                 list;
371    ListSelectorDialog    dialog;
372    String                tmpStr;
373
374    list   = new JList(m_History);
375    dialog = new ListSelectorDialog(m_Parent, list);
376   
377    if (dialog.showDialog() == ListSelectorDialog.APPROVE_OPTION) {
378      if (list.getSelectedValue() != null) {
379        tmpStr = list.getSelectedValue().toString();
380        if (tmpStr.indexOf("@") > -1) {
381          setUser(tmpStr.substring(0, tmpStr.indexOf("@")));
382          setURL(tmpStr.substring(tmpStr.indexOf("@") + 1));
383          showDialog();
384        }
385        else {
386          setUser("");
387          setURL(tmpStr);
388        }
389      }
390    }
391
392    setButtons();
393  }
394
395  /**
396   * adds the given listener to the list of listeners.
397   *
398   * @param l       the listener to add to the list
399   */
400  public void addConnectionListener(ConnectionListener l) {
401    m_ConnectionListeners.add(l);
402  }
403
404  /**
405   * removes the given listener from the list of listeners.
406   *
407   * @param l       the listener to remove
408   */
409  public void removeConnectionListener(ConnectionListener l) {
410    m_ConnectionListeners.remove(l);
411  }
412
413  /**
414   * notifies the connection listeners of the event.
415   *
416   * @param type      the type of the action, CONNECT or DISCONNECT
417   */
418  protected void notifyConnectionListeners(int type) {
419    notifyConnectionListeners(type, null);
420  }
421
422  /**
423   * notifies the connection listeners of the event.
424   *
425   * @param type      the type of the action, CONNECT or DISCONNECT
426   * @param ex        an optional exception that happened (indicates failure!)
427   */
428  protected void notifyConnectionListeners(int type, Exception ex) {
429    Iterator              iter;
430    ConnectionListener    l;
431
432    iter = m_ConnectionListeners.iterator();
433    while (iter.hasNext()) {
434      l = (ConnectionListener) iter.next();
435      l.connectionChange(
436          new ConnectionEvent(this, type, m_DbUtils, ex));
437    }
438  }
439
440  /**
441   * adds the given listener to the list of listeners.
442   *
443   * @param l       the listener to add to the list
444   */
445  public void addHistoryChangedListener(HistoryChangedListener l) {
446    m_HistoryChangedListeners.add(l);
447  }
448
449  /**
450   * removes the given listener from the list of listeners.
451   *
452   * @param l       the listener to remove
453   */
454  public void removeHistoryChangedListener(HistoryChangedListener l) {
455    m_HistoryChangedListeners.remove(l);
456  }
457
458  /**
459   * notifies the history listeners of the event.
460   */
461  protected void notifyHistoryChangedListeners() {
462    Iterator                iter;
463    HistoryChangedListener  l;
464
465    iter = m_HistoryChangedListeners.iterator();
466    while (iter.hasNext()) {
467      l = (HistoryChangedListener) iter.next();
468      l.historyChanged(
469          new HistoryChangedEvent(this, HISTORY_NAME, getHistory()));
470    }
471  }
472
473  /**
474   * Called when the caret position is updated.
475   *
476   * @param event the event to process
477   */
478  public void caretUpdate(CaretEvent event) {
479    setButtons();
480  }
481}
Note: See TracBrowser for help on using the repository browser.