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

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

Import di weka.

File size: 7.4 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 * SqlViewerDialog.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.sql;
24
25import weka.gui.sql.event.ResultChangedEvent;
26import weka.gui.sql.event.ResultChangedListener;
27
28import java.awt.BorderLayout;
29import java.awt.FlowLayout;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.awt.event.WindowAdapter;
33import java.awt.event.WindowEvent;
34
35import javax.swing.JButton;
36import javax.swing.JCheckBox;
37import javax.swing.JDialog;
38import javax.swing.JFrame;
39import javax.swing.JLabel;
40import javax.swing.JOptionPane;
41import javax.swing.JPanel;
42
43/**
44 * A little dialog containing the SqlViewer.
45 *
46 * @author      FracPete (fracpete at waikato dot ac dot nz)
47 * @version     $Revision: 5279 $
48 */
49public class SqlViewerDialog 
50  extends JDialog
51  implements ResultChangedListener {
52
53  /** for serialization. */
54  private static final long serialVersionUID = -31619864037233099L;
55 
56  /** the parent frame. */
57  protected JFrame m_Parent;
58 
59  /** the SQL panel. */
60  protected SqlViewer m_Viewer;
61
62  /** the panel for the buttons. */
63  protected JPanel m_PanelButtons;
64
65  /** the OK button. */
66  protected JButton m_ButtonOK = new JButton("OK");
67
68  /** the Cancel button. */
69  protected JButton m_ButtonCancel = new JButton("Cancel");
70
71  /** displays the current query. */
72  protected JLabel m_LabelQuery = new JLabel("");
73 
74  /** whether to return sparse instances or not. */
75  protected JCheckBox m_CheckBoxSparseData = new JCheckBox("Generate sparse data");
76
77  /** the return value. */
78  protected int m_ReturnValue = JOptionPane.CANCEL_OPTION;
79
80  /** the connect string with which the query was run. */
81  protected String m_URL;
82
83  /** the user that was used to connect to the DB. */
84  protected String m_User;
85
86  /** the password that was used to connect to the DB. */
87  protected String m_Password;
88
89  /** the currently selected query. */
90  protected String m_Query;
91 
92  /**
93   * initializes the dialog.
94   *
95   * @param parent      the parent frame
96   */
97  public SqlViewerDialog(JFrame parent) {
98    super(parent, "SQL-Viewer", true);
99
100    m_Parent   = parent;
101    m_URL      = "";
102    m_User     = "";
103    m_Password = "";
104    m_Query    = "";
105   
106    createDialog();
107  }
108
109  /**
110   * builds the dialog and all its components.
111   */
112  protected void createDialog() {
113    JPanel                    panel;
114    JPanel                    panel2;
115    final SqlViewerDialog     dialog;
116   
117    dialog = this;
118    setLayout(new BorderLayout());
119
120    // sql panel
121    m_Viewer = new SqlViewer(m_Parent);
122    add(m_Viewer, BorderLayout.CENTER);
123   
124    panel2 = new JPanel(new BorderLayout());
125    add(panel2, BorderLayout.SOUTH);
126   
127    // Buttons
128    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
129    panel2.add(panel, BorderLayout.EAST);
130    m_ButtonOK.setMnemonic('O');
131    panel.add(m_ButtonOK);
132    m_ButtonOK.addActionListener(new ActionListener(){
133        public void actionPerformed(ActionEvent evt){
134          m_ReturnValue = JOptionPane.OK_OPTION;
135          // remove listener, otherwise does the disposal of resultspanel
136          // change the query again!
137          m_Viewer.removeResultChangedListener(dialog);
138          m_Viewer.saveSize();
139          dialog.dispose();
140      }
141    });
142    m_ButtonCancel.setMnemonic('C');
143    panel.add(m_ButtonCancel);
144    m_ButtonCancel.addActionListener(new ActionListener(){
145        public void actionPerformed(ActionEvent evt){
146          m_ReturnValue = JOptionPane.CANCEL_OPTION;
147          // remove listener, otherwise does the disposal of resultspanel
148          // change the query again!
149          m_Viewer.removeResultChangedListener(dialog);
150          m_Viewer.saveSize();
151          dialog.dispose();
152      }
153    });
154   
155    // the checkbox for sparse data
156    panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
157    panel2.add(panel, BorderLayout.WEST);
158    panel.add(m_CheckBoxSparseData);
159    m_CheckBoxSparseData.setMnemonic('s');
160   
161    addWindowListener(new WindowAdapter() {
162      /**
163       * Invoked when a window is in the process of being closed.
164       */
165      public void windowClosing(WindowEvent e) {
166        m_Viewer.saveSize();
167      }
168    });
169   
170    // current Query
171    panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
172    panel2.add(panel, BorderLayout.CENTER);
173    panel.add(m_LabelQuery);
174   
175    pack();
176    getRootPane().setDefaultButton(m_ButtonOK);
177    setResizable(true);
178
179    // listener
180    m_Viewer.addResultChangedListener(this);
181  }
182
183  /**
184   * displays the dialog if TRUE.
185   *
186   * @param b           if true displaying the dialog, hiding otherwise
187   */
188  public void setVisible(boolean b) {
189    if (b)
190      m_ReturnValue = JOptionPane.CANCEL_OPTION;
191
192    super.setVisible(b);
193   
194    // free up memory
195    if (b)
196      m_Viewer.clear();
197  }
198
199  /**
200   * returns whether the user clicked OK (JOptionPane.OK_OPTION) or whether he
201   * cancelled the dialog (JOptionPane.CANCEL_OPTION).
202   * @return            the return value
203   * @see               JOptionPane
204   */
205  public int getReturnValue() {
206    return m_ReturnValue;
207  }
208
209  /**
210   * returns the chosen URL, if any.
211   *
212   * @return            the URL
213   */
214  public String getURL() {
215    return m_URL;
216  }
217
218  /**
219   * returns the chosen user, if any.
220   *
221   * @return            the user
222   */
223  public String getUser() {
224    return m_User;
225  }
226
227  /**
228   * returns the chosen password, if any.
229   *
230   * @return            the password
231   */
232  public String getPassword() {
233    return m_Password;
234  }
235
236  /**
237   * returns the chosen query, if any.
238   *
239   * @return            the query
240   */
241  public String getQuery() {
242    return m_Query;
243  }
244 
245  /**
246   * Returns whether sparse data is generated.
247   *
248   * @return            true if sparse data is to be generated
249   */
250  public boolean getGenerateSparseData() {
251    return m_CheckBoxSparseData.isSelected();
252  }
253
254  /**
255   * This method gets called when a query has been executed.
256   *
257   * @param evt         the event
258   */
259  public void resultChanged(ResultChangedEvent evt) {
260    m_URL      = evt.getURL();
261    m_User     = evt.getUser();
262    m_Password = evt.getPassword();
263    m_Query    = evt.getQuery();
264    m_LabelQuery.setText("Current query: " + m_Query);
265  }
266
267  /**
268   * for testing only.
269   *
270   * @param args        ignored
271   */
272  public static void main(String[] args) {
273    SqlViewerDialog       dialog;
274
275    dialog = new SqlViewerDialog(null);
276    dialog.setDefaultCloseOperation(SqlViewerDialog.DISPOSE_ON_CLOSE);
277    dialog.setVisible(true);
278    System.out.println("ReturnValue = " + dialog.getReturnValue());
279    if (dialog.getReturnValue() == JOptionPane.OK_OPTION) {
280      System.out.println("URL      = " + dialog.getURL());
281      System.out.println("User     = " + dialog.getUser());
282      System.out.println("Password = " + dialog.getPassword().replaceAll(".", "*"));
283      System.out.println("Query    = " + dialog.getQuery());
284    }
285  }
286}
287
Note: See TracBrowser for help on using the repository browser.