001: /* ====================================================================
002: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
003: * reserved.
004: *
005: * This file is part of the QueryForm Database Tool.
006: *
007: * The QueryForm Database Tool is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU General Public License as
009: * published by the Free Software Foundation; either version 2 of the
010: * License, or (at your option) any later version.
011: *
012: * The QueryForm Database Tool is distributed in the hope that it will
013: * be useful, but WITHOUT ANY WARRANTY; without even the implied
014: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with the QueryForm Database Tool; if not, write to:
019: *
020: * The Free Software Foundation, Inc.,
021: * 59 Temple Place, Suite 330
022: * Boston, MA 02111-1307 USA
023: *
024: * or visit http://www.gnu.org.
025: *
026: * ====================================================================
027: *
028: * This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/).
030: *
031: * ====================================================================
032: */
033: package org.glasser.qform;
034:
035: import java.awt.*;
036: import java.awt.event.*;
037: import javax.swing.*;
038: import javax.swing.text.JTextComponent;
039: import javax.swing.border.EmptyBorder;
040:
041: import org.glasser.swing.*;
042:
043: public class WhereClauseDialog extends JDialog implements
044: ActionListener {
045:
046: private String sql = null;
047:
048: private String tableName = null;
049:
050: private JTextArea txtInput = new JTextArea() {
051: public boolean isManagingFocus() {
052: return false;
053: }
054: };
055:
056: private JLabel lblHeader = new JLabel();
057:
058: private JButton btnPrev = new JButton();
059:
060: private JButton btnNext = new JButton();
061:
062: private JButton btnSubmit = new JButton("Submit");
063:
064: private JButton btnCancel = new JButton("Cancel");
065:
066: private Object[][] buttonConfig = {
067: { btnPrev, "P", "PREVIOUS",
068: "Edit the previous WHERE clause in the history list." },
069: { btnNext, "N", "NEXT",
070: "Edit the next WHERE clause in the history list." },
071: { btnSubmit, "S", "SUBMIT",
072: "Submit a query with the given WHERE clause." },
073: { btnCancel, "C", "CANCEL", "Cancel the query" }
074:
075: };
076:
077: /**
078: * The index, within the history list, of the WHERE clause that's currently
079: * being edited. It will be -1 if the history list is empty.
080: */
081: private int pointer = -1;
082:
083: /**
084: * When a user closes the dialog with the Submit button, this will hold the
085: * results of the editing, and can be retrieved by the getWhereClause() method.
086: * It will always be non-null if the user clicks Submit; it will be an
087: * empty String ("") if the user clicks Submit without entering anything. It will be
088: * null if the user closes the dialog or clicks Cancel.
089: */
090: private String whereClause = null;
091:
092: private java.util.List historyList = null;
093:
094: public WhereClauseDialog(Frame parent) {
095: super (parent);
096:
097: setTitle("Edit WHERE clause");
098:
099: JPanel panel = new JPanel();
100:
101: panel.setBorder(new EmptyBorder(10, 10, 10, 10));
102:
103: panel.setLayout(new BorderLayout());
104:
105: panel.add(lblHeader, BorderLayout.NORTH);
106:
107: panel.add(txtInput, BorderLayout.CENTER);
108:
109: btnPrev
110: .setIcon(GUIHelper
111: .getImageIconFromClasspath("org/glasser/swing/images/Back16.gif"));
112: btnNext
113: .setIcon(GUIHelper
114: .getImageIconFromClasspath("org/glasser/swing/images/Forward16.gif"));
115:
116: Dimension d = btnSubmit.getPreferredSize();
117: d = (Dimension) d.clone();
118:
119: d.width = d.height;
120:
121: GUIHelper.setAllSizes(btnPrev, d);
122: GUIHelper.setAllSizes(btnNext, d);
123:
124: JPanel buttonPanel = new JPanel();
125:
126: GUIHelper.buildButtonPanel(buttonPanel, buttonConfig, this );
127:
128: txtInput
129: .setBorder(new ThinBevelBorder(ThinBevelBorder.LOWERED));
130: txtInput.setPreferredSize(new Dimension(400, 150));
131: txtInput.enableInputMethods(false);
132: txtInput.setWrapStyleWord(true);
133: txtInput.setLineWrap(true);
134:
135: panel.add(buttonPanel, BorderLayout.SOUTH);
136:
137: // make it so the ESCAPE key closes the dialog.
138: KeyStroke esc = KeyStroke.getKeyStroke("ESCAPE");
139: ButtonClicker closer = new ButtonClicker(btnCancel);
140: panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(esc,
141: "_ESCAPE_");
142: panel
143: .getInputMap(
144: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
145: .put(esc, "_ESCAPE_");
146: panel.getActionMap().put("_ESCAPE_", closer);
147:
148: setContentPane(panel);
149: pack();
150:
151: }
152:
153: public void setVisible(boolean b) {
154: if (b)
155: throw new RuntimeException(
156: "WhereClauseDialog must be opened with the openDialog() method.");
157: }
158:
159: public String getSQL(String tableName) {
160: lblHeader.setText("SELECT * FROM " + tableName + " WHERE ");
161: this .tableName = tableName;
162: sql = null;
163: txtInput.setText(null);
164: setModal(true);
165: txtInput.requestFocus();
166: super .setVisible(true);
167:
168: return sql;
169: }
170:
171: /**
172: * This opens the dialog in a modal state, with the last item
173: * in the historyList displayed for editing. When this method returns,
174: * the results of the user editing can be retrieved with getWhereClause().
175: */
176: public void openDialog(String tableName, java.util.List historyList) {
177: lblHeader.setText("SELECT * FROM " + tableName + " WHERE ");
178: this .tableName = tableName;
179: this .historyList = historyList;
180: pointer = historyList.size() - 1;
181: if (pointer > -1)
182: txtInput.setText((String) historyList.get(pointer));
183: else
184: txtInput.setText(null);
185: setModal(true);
186: txtInput.requestFocus();
187: super .setVisible(true);
188: }
189:
190: /**
191: * When a user closes the dialog, this returns the results of the editing.
192: * The returned String will always be non-null if the user clicks Submit; it will be an
193: * empty String ("") if the user clicks Submit without entering anything. It will be
194: * null if the user closes the dialog or clicks Cancel.
195: */
196: public String getWhereClause() {
197: return whereClause;
198: }
199:
200: public void actionPerformed(ActionEvent e) {
201:
202: String command = e.getActionCommand();
203: if (command.equals("SUBMIT")) {
204: String input = txtInput.getText();
205: if (input != null && (input = input.trim()).length() > 0) {
206: // if the user included "WHERE" remove it.
207: if (input.toUpperCase().indexOf("WHERE ") == 0) {
208: input = input.substring(6);
209: }
210: whereClause = input;
211: sql = "SELECT * FROM " + tableName + " WHERE " + input;
212: } else {
213: whereClause = "";
214: sql = "SELECT * FROM " + tableName;
215: }
216:
217: super .setVisible(false);
218:
219: } else if (command.equals("CANCEL")) {
220: sql = null;
221: whereClause = null;
222: super .setVisible(false);
223: } else if (command.equals("PREVIOUS")) {
224: if (historyList.size() == 0 || pointer == 0) {
225: Toolkit.getDefaultToolkit().beep();
226: return;
227: }
228: pointer--;
229: txtInput.setText((String) historyList.get(pointer));
230: txtInput.requestFocus();
231: } else if (command.equals("NEXT")) {
232: if (historyList.size() == 0
233: || pointer > historyList.size() - 2) {
234: Toolkit.getDefaultToolkit().beep();
235: return;
236: }
237: pointer++;
238: txtInput.setText((String) historyList.get(pointer));
239: txtInput.requestFocus();
240: }
241: }
242:
243: }
|