001: package net.sourceforge.squirrel_sql.plugins.sqlparam.gui;
002:
003: /*
004: * Copyright (C) 2007 Thorsten Mürell
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020: import java.awt.BorderLayout;
021: import java.awt.Component;
022:
023: import javax.swing.JCheckBox;
024: import javax.swing.JPanel;
025: import javax.swing.JTextField;
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.event.DocumentListener;
028:
029: import net.sourceforge.squirrel_sql.client.gui.BaseInternalFrame;
030: import net.sourceforge.squirrel_sql.client.gui.IOkClosePanelListener;
031: import net.sourceforge.squirrel_sql.client.gui.OkClosePanel;
032: import net.sourceforge.squirrel_sql.client.gui.OkClosePanelEvent;
033: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
034: import net.sourceforge.squirrel_sql.fw.util.StringManager;
035: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
036:
037: import com.jgoodies.forms.builder.PanelBuilder;
038: import com.jgoodies.forms.layout.CellConstraints;
039: import com.jgoodies.forms.layout.FormLayout;
040:
041: /**
042: * The dialog to ask the user for a value.
043: *
044: * @author Thorsten Mürell
045: */
046: public class AskParamValueDialog extends BaseInternalFrame {
047: private static final long serialVersionUID = 3470927611018381204L;
048:
049: private static final StringManager stringMgr = StringManagerFactory
050: .getStringManager(AskParamValueDialog.class);
051:
052: private OkClosePanel btnsPnl = new OkClosePanel();
053: private JTextField value = new JTextField();
054: private JCheckBox quote = new JCheckBox();
055: private String parameter = null;
056: private String oldValue = null;
057:
058: private boolean done = false;
059: private boolean cancelled = false;
060:
061: /**
062: * Creates the dialog.
063: *
064: * @param parameter The name of the parameter to replace.
065: * @param oldValue The old value of the parameter to provide as a default.
066: */
067: public AskParamValueDialog(String parameter, String oldValue) {
068: //i18n[sqlparam.inputParameterValues=Please input the parameter values]
069: super (stringMgr.getString("sqlparam.inputParameterValues"),
070: true);
071: this .parameter = parameter;
072: this .oldValue = oldValue;
073:
074: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
075: GUIUtils.makeToolWindow(this , true);
076:
077: final JPanel content = new JPanel(new BorderLayout());
078: content.add(createMainPanel(), BorderLayout.CENTER);
079: setContentPane(content);
080: btnsPnl.makeOKButtonDefault();
081: btnsPnl.getRootPane().setDefaultButton(btnsPnl.getOKButton());
082: pack();
083: }
084:
085: /**
086: * @return <code>true</code> if the dialog is done
087: */
088: public boolean isDone() {
089: return done;
090: }
091:
092: /**
093: * If the user doesn't want to input a value, he hits the close
094: * button. The this method returns true.
095: *
096: * @return <code>true</code> if the dialog was cancelled by the user.
097: */
098: public boolean isCancelled() {
099: return cancelled;
100: }
101:
102: /**
103: * Sets the value of the dialog.
104: *
105: * @param defaultValue the value to set as a default in this dialog.
106: */
107: public void setValue(String defaultValue) {
108: value.setText(defaultValue);
109: }
110:
111: /**
112: * Gets the value of the input field in this dialog.
113: *
114: * @return The value for the parameter.
115: */
116: public String getValue() {
117: return value.getText();
118: }
119:
120: /**
121: * Returns if quotes around the value are needed.
122: *
123: * @return <code>true</code> if quoting is needed.
124: */
125: public boolean isQuotingNeeded() {
126: return quote.isSelected();
127: }
128:
129: private void updateCheckbox() {
130: boolean isNumber = false;
131:
132: try {
133: Float.parseFloat(value.getText());
134: isNumber = true;
135: } catch (NumberFormatException nfe) {
136: isNumber = false;
137: }
138:
139: if (isNumber) {
140: quote.setSelected(false);
141: quote.setEnabled(true);
142: } else {
143: quote.setSelected(true);
144: quote.setEnabled(false);
145: }
146: }
147:
148: private Component createMainPanel() {
149: value.setColumns(20);
150: value.setText(oldValue);
151: value.getDocument().addDocumentListener(new DocumentListener() {
152: public void changedUpdate(DocumentEvent e) {
153: AskParamValueDialog.this .updateCheckbox();
154: }
155:
156: public void insertUpdate(DocumentEvent e) {
157: AskParamValueDialog.this .updateCheckbox();
158: }
159:
160: public void removeUpdate(DocumentEvent e) {
161: AskParamValueDialog.this .updateCheckbox();
162: }
163: });
164: updateCheckbox();
165: btnsPnl.addListener(new MyOkClosePanelListener());
166:
167: final FormLayout layout = new FormLayout(
168: // Columns
169: "right:pref, 8dlu, left:min(100dlu;pref):grow",
170: // Rows
171: "pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 6dlu, pref, 3dlu, pref, 3dlu, pref");
172:
173: PanelBuilder builder = new PanelBuilder(layout);
174: CellConstraints cc = new CellConstraints();
175: builder.setDefaultDialogBorder();
176:
177: int y = 1;
178: builder.addSeparator(title, cc.xywh(1, y, 3, 1));
179:
180: y += 2;
181: //i18n[sqlparam.valueFor=Value for {0}]
182: builder.addLabel(stringMgr.getString("sqlparam.valueFor",
183: parameter), cc.xy(1, y));
184: builder.add(value, cc.xywh(3, y, 1, 1));
185:
186: y += 2;
187: //i18n[sqlparam.quoteValues=Quote Values]
188: builder.addLabel(stringMgr.getString("sqlparam.quoteValues"),
189: cc.xy(1, y));
190: builder.add(quote, cc.xywh(3, y, 1, 1));
191:
192: y += 2;
193: builder.addSeparator("", cc.xywh(1, y, 3, 1));
194:
195: y += 2;
196: builder.add(btnsPnl, cc.xywh(1, y, 3, 1));
197:
198: return builder.getPanel();
199: }
200:
201: /**
202: * Cancels the dialog.
203: */
204: public void cancel() {
205: done = true;
206: cancelled = true;
207: dispose();
208: }
209:
210: /**
211: * Confirms the dialog.
212: */
213: public void ok() {
214: done = true;
215: dispose();
216: }
217:
218: private final class MyOkClosePanelListener implements
219: IOkClosePanelListener {
220: /**
221: * Callback for the ok key.
222: *
223: * @param evt the event
224: */
225: public void okPressed(OkClosePanelEvent evt) {
226: AskParamValueDialog.this .ok();
227: }
228:
229: /**
230: * Callback for the close key.
231: *
232: * @param evt the event
233: */
234: public void closePressed(OkClosePanelEvent evt) {
235: AskParamValueDialog.this .cancel();
236: }
237:
238: /**
239: * Callback for the cancel key.
240: *
241: * @param evt the event
242: */
243: public void cancelPressed(OkClosePanelEvent evt) {
244: AskParamValueDialog.this.cancel();
245: }
246: }
247:
248: }
|