001: package org.jsqltool.gui;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.*;
006: import java.util.*;
007: import java.math.BigDecimal;
008: import org.jsqltool.utils.Options;
009: import org.jsqltool.utils.ImageLoader;
010: import org.jsqltool.gui.graphics.CalendarCombo;
011:
012: /**
013: * <p>Title: JSqlTool Project</p>
014: * <p>Description: Dialog used by SQLFrame to find out sql parameters and prompt user to set them.
015: * </p>
016: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
017: *
018: * <p> This file is part of JSqlTool project.
019: * This library is free software; you can redistribute it and/or
020: * modify it under the terms of the (LGPL) Lesser General Public
021: * License as published by the Free Software Foundation;
022: *
023: * GNU LESSER GENERAL PUBLIC LICENSE
024: * Version 2.1, February 1999
025: *
026: * This library is distributed in the hope that it will be useful,
027: * but WITHOUT ANY WARRANTY; without even the implied warranty of
028: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029: * Library General Public License for more details.
030: *
031: * You should have received a copy of the GNU Library General Public
032: * License along with this library; if not, write to the Free
033: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
034: *
035: * The author may be contacted at:
036: * maurocarniel@tin.it</p>
037: *
038: * @author Mauro Carniel
039: * @version 1.0
040: */
041: public class ParametersDialog extends JDialog {
042:
043: JPanel mainPanel = new JPanel();
044: JPanel buttonsPanel = new JPanel();
045: BorderLayout borderLayout1 = new BorderLayout();
046: JButton okButton = new JButton();
047: JLabel typeLabel = new JLabel();
048: JComboBox typeComboBox = new JComboBox();
049: JLabel valueLabel = new JLabel();
050: GridBagLayout gridBagLayout1 = new GridBagLayout();
051:
052: private static final String TEXT = Options.getInstance()
053: .getResource("text");
054: private static final String NUM = Options.getInstance()
055: .getResource("numeric");
056: private static final String DATE = Options.getInstance()
057: .getResource("date");
058:
059: private CalendarCombo date = new CalendarCombo();
060: private JTextField text = new JTextField();
061: private JTextField num = new JTextField();
062:
063: /** current input control */
064: private Component currentControl = null;
065:
066: /** current value */
067: private Object value = null;
068:
069: /** position of parameter */
070: private int pos = -1;
071:
072: /** values used by query */
073: private Vector values = null;
074:
075: /** sql to analyze */
076: private String sql = null;
077:
078: /** parent frame */
079: private SQLFrame frame = null;
080:
081: /** MDI frame */
082: private JFrame parent = null;
083:
084: /**
085: * Constructor called by SQLFrame.
086: */
087: public ParametersDialog(JFrame parent, SQLFrame frame, String sql,
088: Vector values) {
089: super (parent, Options.getInstance().getResource("variable"),
090: false);
091: this .parent = parent;
092: this .frame = frame;
093: this .sql = sql;
094: this .values = values;
095: try {
096: init();
097: jbInit();
098: setSize(300, 170);
099: setLocation(parent.getWidth() / 2 - 200,
100: parent.getHeight() / 2 - 70);
101:
102: // find thr first occurrence of ?
103: findNextVar();
104: } catch (Exception e) {
105: e.printStackTrace();
106: }
107: }
108:
109: /**
110: * Find the next occurrence of ? in sql.
111: * If no other ? are found, then execute the sql, i.e. execute the method executeSQLWithValues.
112: */
113: private void findNextVar() {
114: if (currentControl != null)
115: mainPanel.remove(currentControl);
116: currentControl = null;
117: typeComboBox.setSelectedIndex(-1);
118: while ((pos = sql.indexOf("?", pos + 1)) != -1) {
119: if (isParameter(sql, pos)) {
120: break;
121: }
122: }
123: if (pos != -1)
124: setVisible(true);
125: else
126: frame.executeSQLWithValues(sql, values);
127: }
128:
129: public ParametersDialog() {
130: try {
131: jbInit();
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: }
136:
137: /**
138: * @param sql sql which contains patterns of type "=?" or ">?" or "<?"
139: * @param pos position inside sql of ? character
140: * @return <code>true</code> if no space character at the left of pos is "=" or "<" or ">"
141: */
142: private boolean isParameter(String sql, int pos) {
143: if (pos == 0)
144: return false;
145: for (int i = pos - 1; i >= 0; i--)
146: if (sql.charAt(i) == '=' || sql.charAt(i) == '<'
147: || sql.charAt(i) == '>')
148: return true;
149: else if (sql.charAt(i) != ' ')
150: return false;
151: return false;
152: }
153:
154: /**
155: * Initialize type combo.
156: */
157: private void init() {
158: typeComboBox.addItem(TEXT);
159: typeComboBox.addItem(NUM);
160: typeComboBox.addItem(DATE);
161: typeComboBox.setSelectedIndex(-1);
162:
163: text.addKeyListener(new KeyAdapter() {
164: public void keyReleased(KeyEvent e) {
165: if (e.getKeyChar() == '\n')
166: okButton_actionPerformed(null);
167: }
168: });
169: }
170:
171: private void jbInit() throws Exception {
172: this .getContentPane().setLayout(borderLayout1);
173: mainPanel.setBorder(BorderFactory.createEtchedBorder());
174: mainPanel.setLayout(gridBagLayout1);
175: buttonsPanel.setBorder(BorderFactory.createEtchedBorder());
176: okButton.setText(Options.getInstance().getResource(
177: "okbutton.text"));
178: okButton.setMnemonic(Options.getInstance().getResource(
179: "okbutton.mnemonic").charAt(0));
180: okButton
181: .addActionListener(new ParametersDialog_okButton_actionAdapter(
182: this ));
183: typeLabel.setText(Options.getInstance().getResource("type"));
184: typeComboBox
185: .addItemListener(new ParametersDialog_typeComboBox_itemAdapter(
186: this ));
187: valueLabel.setText(Options.getInstance().getResource("value"));
188: this .getContentPane().add(mainPanel, BorderLayout.CENTER);
189: this .getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
190: buttonsPanel.add(okButton, null);
191: mainPanel.add(typeLabel, new GridBagConstraints(0, 0, 1, 1,
192: 0.0, 0.0, GridBagConstraints.NORTHWEST,
193: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
194: mainPanel.add(typeComboBox, new GridBagConstraints(1, 0, 1, 1,
195: 1.0, 0.0, GridBagConstraints.NORTHWEST,
196: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
197: 0, 0));
198: mainPanel.add(valueLabel, new GridBagConstraints(0, 1, 1, 1,
199: 0.0, 1.0, GridBagConstraints.NORTHWEST,
200: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
201: }
202:
203: void okButton_actionPerformed(ActionEvent e) {
204: value = null;
205: if (TEXT.equals(typeComboBox.getSelectedItem())) {
206: value = text.getText();
207: if (value.equals(""))
208: value = null;
209: } else if (NUM.equals(typeComboBox.getSelectedItem())) {
210: value = new BigDecimal(num.getText());
211: } else if (DATE.equals(typeComboBox.getSelectedItem())) {
212: value = date.getDate();
213: if (value != null && value instanceof java.util.Date)
214: value = new java.sql.Date(((java.util.Date) value)
215: .getTime());
216: }
217: if (value == null) {
218: JOptionPane.showMessageDialog(parent, Options.getInstance()
219: .getResource("value is required"), Options
220: .getInstance().getResource("attention"),
221: JOptionPane.WARNING_MESSAGE);
222: return;
223: } else {
224: values.add(value);
225: text.setText(null);
226: num.setText(null);
227: date.setDate(null);
228: setVisible(false);
229: typeComboBox.setSelectedIndex(-1);
230: findNextVar();
231: }
232: }
233:
234: void typeComboBox_itemStateChanged(ItemEvent e) {
235: if (e.getStateChange() == e.SELECTED) {
236: if (currentControl != null)
237: mainPanel.remove(currentControl);
238:
239: if (typeComboBox.getSelectedItem().equals(TEXT)) {
240: currentControl = text;
241: } else if (typeComboBox.getSelectedItem().equals(NUM)) {
242: currentControl = num;
243: } else if (typeComboBox.getSelectedItem().equals(DATE)) {
244: if (currentControl != null)
245: mainPanel.remove(currentControl);
246: currentControl = date;
247: }
248:
249: if (currentControl != null) {
250: mainPanel.add(currentControl, new GridBagConstraints(1,
251: 1, 1, 1, 1.0, 0.0,
252: GridBagConstraints.NORTHWEST,
253: GridBagConstraints.HORIZONTAL, new Insets(5, 0,
254: 5, 5), 0, 0));
255: currentControl.requestFocus();
256: }
257: mainPanel.revalidate();
258: mainPanel.repaint();
259: }
260: }
261:
262: }
263:
264: class ParametersDialog_okButton_actionAdapter implements
265: java.awt.event.ActionListener {
266: ParametersDialog adaptee;
267:
268: ParametersDialog_okButton_actionAdapter(ParametersDialog adaptee) {
269: this .adaptee = adaptee;
270: }
271:
272: public void actionPerformed(ActionEvent e) {
273: adaptee.okButton_actionPerformed(e);
274: }
275: }
276:
277: class ParametersDialog_typeComboBox_itemAdapter implements
278: java.awt.event.ItemListener {
279: ParametersDialog adaptee;
280:
281: ParametersDialog_typeComboBox_itemAdapter(ParametersDialog adaptee) {
282: this .adaptee = adaptee;
283: }
284:
285: public void itemStateChanged(ItemEvent e) {
286: adaptee.typeComboBox_itemStateChanged(e);
287: }
288: }
|