source: src/main/java/weka/gui/sql/ResultPanel.java @ 17

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

Import di weka.

File size: 10.3 KB
RevLine 
[4]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 * ResultPanel.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.sql;
24
25import weka.gui.JTableHelper;
26import weka.gui.sql.event.QueryExecuteEvent;
27import weka.gui.sql.event.QueryExecuteListener;
28import weka.gui.sql.event.ResultChangedEvent;
29import weka.gui.sql.event.ResultChangedListener;
30
31import java.awt.BorderLayout;
32import java.awt.Dimension;
33import java.awt.event.ActionEvent;
34import java.awt.event.ActionListener;
35import java.util.HashSet;
36import java.util.Iterator;
37
38import javax.swing.JButton;
39import javax.swing.JFrame;
40import javax.swing.JLabel;
41import javax.swing.JPanel;
42import javax.swing.JScrollPane;
43import javax.swing.JTabbedPane;
44import javax.swing.JViewport;
45import javax.swing.event.ChangeEvent;
46import javax.swing.event.ChangeListener;
47
48/**
49 * Represents a panel for displaying the results of a query in table format.
50 *
51 * @author    FracPete (fracpete at waikato dot ac dot nz)
52 * @version   $Revision: 1.3 $
53 */
54public class ResultPanel 
55  extends JPanel
56  implements QueryExecuteListener, ChangeListener {
57
58  /** for serialization */
59  private static final long serialVersionUID = 278654800344034571L;
60 
61  /** the parent of this panel */
62  protected JFrame m_Parent;
63
64  /** the result change listeners */
65  protected HashSet m_Listeners;
66
67  /** the panel where to output the queries */
68  protected QueryPanel m_QueryPanel;
69
70  /** the tabbed pane for the results */
71  protected JTabbedPane m_TabbedPane;
72
73  /** the close button */
74  protected JButton m_ButtonClose = new JButton("Close");
75
76  /** the close all button */
77  protected JButton m_ButtonCloseAll = new JButton("Close all");
78
79  /** the button that copies the query into the QueryPanel */
80  protected JButton m_ButtonCopyQuery = new JButton("Re-use query");
81
82  /** the button for the optimal column width of the current table */
83  protected JButton m_ButtonOptWidth = new JButton("Optimal width");
84
85  /** the counter for the tab names */
86  protected int m_NameCounter;
87 
88  /**
89   * initializes the panel
90   * @param parent        the parent of this panel
91   */
92  public ResultPanel(JFrame parent) {
93    super();
94   
95    m_Parent      = parent;
96    m_QueryPanel  = null;
97    m_NameCounter = 0;
98    m_Listeners   = new HashSet();
99   
100    createPanel();
101  }
102
103  /**
104   * creates the panel with all its components
105   */
106  protected void createPanel() {
107    JPanel          panel;
108    JPanel          panel2;
109    JPanel          panel3;
110    JPanel          panel4;
111
112    setLayout(new BorderLayout());
113    setPreferredSize(new Dimension(0, 200));
114
115    // tabbed pane
116    m_TabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
117    m_TabbedPane.addChangeListener(this);
118    add(m_TabbedPane, BorderLayout.CENTER);
119
120    // buttons
121    panel = new JPanel(new BorderLayout());
122    add(panel, BorderLayout.EAST);
123    panel2 = new JPanel(new BorderLayout());
124    panel.add(panel2, BorderLayout.CENTER);
125    panel3 = new JPanel(new BorderLayout());
126    panel2.add(panel3, BorderLayout.CENTER);
127    panel4 = new JPanel(new BorderLayout());
128    panel3.add(panel4, BorderLayout.CENTER);
129   
130    m_ButtonClose.setMnemonic('l');
131    m_ButtonClose.addActionListener(new ActionListener() {
132        public void actionPerformed(ActionEvent e) {
133          close();
134        }
135      });
136    panel.add(m_ButtonClose, BorderLayout.NORTH);
137   
138    m_ButtonCloseAll.setMnemonic('a');
139    m_ButtonCloseAll.addActionListener(new ActionListener() {
140        public void actionPerformed(ActionEvent e) {
141          closeAll();
142        }
143      });
144    panel2.add(m_ButtonCloseAll, BorderLayout.NORTH);
145   
146    m_ButtonCopyQuery.setMnemonic('Q');
147    m_ButtonCopyQuery.setToolTipText("Copies the query of the currently selected tab into the query field.");
148    m_ButtonCopyQuery.addActionListener(new ActionListener() {
149        public void actionPerformed(ActionEvent e) {
150          copyQuery();
151        }
152      });
153    panel3.add(m_ButtonCopyQuery, BorderLayout.NORTH);
154   
155    m_ButtonOptWidth.setMnemonic('p');
156    m_ButtonOptWidth.setToolTipText("Calculates the optimal column width for the current table.");
157    m_ButtonOptWidth.addActionListener(new ActionListener() {
158        public void actionPerformed(ActionEvent e) {
159          calcOptimalWidth();
160        }
161      });
162    panel4.add(m_ButtonOptWidth, BorderLayout.NORTH);
163
164    // dummy place holder, otherwise is the space too small for the tabbed
165    // pane
166    panel4.add(new JLabel(" "), BorderLayout.CENTER);
167    panel4.add(new JLabel(" "), BorderLayout.SOUTH);
168
169    // set the initial button state
170    setButtons();
171  }
172
173  /**
174   * sets the parameters back to standard
175   */
176  public void clear() {
177    closeAll();
178  }
179
180  /**
181   * sets the focus in a designated control
182   */
183  public void setFocus() {
184    m_TabbedPane.requestFocus();
185  }
186
187  /**
188   * sets the state of the buttons
189   */
190  protected void setButtons() {
191    int         index;
192
193    index = m_TabbedPane.getSelectedIndex();
194
195    m_ButtonClose.setEnabled(index > -1);
196    m_ButtonCloseAll.setEnabled(m_TabbedPane.getTabCount() > 0);
197    m_ButtonCopyQuery.setEnabled(index > -1);
198    m_ButtonOptWidth.setEnabled(index > -1);
199  }
200
201  /**
202   * returns the next name for a tab "QueryXYZ'
203   */
204  protected String getNextTabName() {
205    m_NameCounter++;
206    return "Query" + m_NameCounter;
207  }
208 
209  /**
210   * This method gets called when a query has been executed.
211   */
212  public void queryExecuted(QueryExecuteEvent evt) {
213    ResultSetTable      table;
214   
215    // only displayed successful queries
216    if (evt.failed())
217      return;
218
219    // DDL command like drop etc that don't create ResultSet?
220    if (!evt.hasResult())
221      return;
222
223    try {
224      table = new ResultSetTable(
225                evt.getDbUtils().getDatabaseURL(),
226                evt.getDbUtils().getUsername(),
227                evt.getDbUtils().getPassword(),
228                evt.getQuery(), 
229                new ResultSetTableModel(evt.getResultSet(), evt.getMaxRows()));
230      m_TabbedPane.addTab(getNextTabName(), new JScrollPane(table));
231
232      // set active tab
233      m_TabbedPane.setSelectedIndex(m_TabbedPane.getTabCount() - 1);
234    }
235    catch (Exception e) {
236      e.printStackTrace();
237    }
238
239    // set buttons
240    setButtons();
241  }
242
243  /**
244   * Invoked when the target of the listener has changed its state.
245   */
246  public void stateChanged(ChangeEvent e) {
247    // in case the tabs get clicked
248    setButtons();
249
250    // notify listeners about current query
251    if (getCurrentTable() != null)
252      notifyListeners(getCurrentTable().getURL(), 
253                      getCurrentTable().getUser(),
254                      getCurrentTable().getPassword(),
255                      getCurrentTable().getQuery());
256  }
257
258  /**
259   * returns the currently set QueryPanel, can be NULL
260   * @return        the current QueryPanel, possibly NULL
261   */
262  public QueryPanel getQueryPanel() {
263    return m_QueryPanel;
264  }
265
266  /**
267   * sets the QueryPanel to use for displaying the query
268   * @param panel   the panel used for displaying the query
269   */
270  public void setQueryPanel(QueryPanel panel) {
271    m_QueryPanel = panel;
272  }
273
274  /**
275   * returns the table of the current tab, can be NULL
276   * @return        the currently selected table
277   */
278  protected ResultSetTable getCurrentTable() {
279    ResultSetTable      table;
280    JScrollPane         pane;
281    JViewport           port;
282    int                 index;
283
284    table = null;
285
286    index = m_TabbedPane.getSelectedIndex();
287    if (index > -1) {
288      pane  = (JScrollPane) m_TabbedPane.getComponentAt(index);
289      port  = (JViewport) pane.getComponent(0);
290      table = (ResultSetTable) port.getComponent(0);
291    }
292     
293    return table;
294  }
295
296  /**
297   * closes the current tab
298   */
299  protected void close() {
300    int                 index;
301
302    index = m_TabbedPane.getSelectedIndex();
303
304    if (index > -1) {
305      try {
306        getCurrentTable().finalize();
307      }
308      catch (Throwable t) {
309        System.out.println(t);
310      }
311      m_TabbedPane.removeTabAt(index);
312    }
313
314    // set buttons
315    setButtons();
316  }
317
318  /**
319   * closes all tabs
320   */
321  protected void closeAll() {
322    while (m_TabbedPane.getTabCount() > 0) {
323      m_TabbedPane.setSelectedIndex(0);
324      try {
325        getCurrentTable().finalize();
326      }
327      catch (Throwable t) {
328        System.out.println(t);
329      }
330      m_TabbedPane.removeTabAt(0);
331    }
332
333    // set buttons
334    setButtons();
335  }
336
337  /**
338   * copies the query of the current tab into the QueryPanel
339   */
340  protected void copyQuery() {
341    if ( (getCurrentTable() != null) && (getQueryPanel() != null) )
342      getQueryPanel().setQuery(getCurrentTable().getQuery());
343  }
344
345  /**
346   * calculates the optimal column width for the current table
347   */
348  protected void calcOptimalWidth() {
349    if (getCurrentTable() != null)
350      JTableHelper.setOptimalColumnWidth(getCurrentTable());
351  }
352
353  /**
354   * adds the given listener to the list of listeners
355   * @param l       the listener to add to the list
356   */
357  public void addResultChangedListener(ResultChangedListener l) {
358    m_Listeners.add(l);
359  }
360
361  /**
362   * removes the given listener from the list of listeners
363   * @param l       the listener to remove
364   */
365  public void removeResultChangedListener(ResultChangedListener l) {
366    m_Listeners.remove(l);
367  }
368
369  /**
370   * notifies the listeners of the event
371   * @param url         the database URL
372   * @param user        the user
373   * @param pw          the password
374   * @param query       the query
375   */
376  protected void notifyListeners(String url, String user, 
377                                 String pw, String query) {
378    Iterator                iter;
379    ResultChangedListener   l;
380
381    iter = m_Listeners.iterator();
382    while (iter.hasNext()) {
383      l = (ResultChangedListener) iter.next();
384      l.resultChanged(new ResultChangedEvent(this, url, user, pw, query));
385    }
386  }
387}
388
Note: See TracBrowser for help on using the repository browser.