001: /*
002: * ValidatingDialog.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Component;
016: import java.awt.Dialog;
017: import java.awt.EventQueue;
018: import java.awt.FlowLayout;
019: import java.awt.Frame;
020: import java.awt.Window;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.WindowEvent;
024: import java.awt.event.WindowListener;
025: import javax.swing.ActionMap;
026: import javax.swing.BorderFactory;
027: import javax.swing.InputMap;
028: import javax.swing.JButton;
029: import javax.swing.JComponent;
030: import javax.swing.JDialog;
031: import javax.swing.JPanel;
032: import javax.swing.JRootPane;
033: import javax.swing.border.Border;
034: import workbench.WbManager;
035: import workbench.gui.WbSwingUtilities;
036: import workbench.gui.actions.EscAction;
037: import workbench.interfaces.ValidatingComponent;
038: import workbench.resource.ResourceMgr;
039:
040: /**
041: * @author support@sql-workbench.net
042: */
043: public class ValidatingDialog extends JDialog implements
044: WindowListener, ActionListener {
045: protected ValidatingComponent validator = null;
046: protected JComponent editorComponent;
047: private JButton[] optionButtons;
048:
049: private JButton cancelButton;
050: private boolean isCancelled = true;
051: private int selectedOption = -1;
052: private EscAction esc;
053: private JPanel buttonPanel;
054:
055: public ValidatingDialog(Dialog owner, String title,
056: JComponent editor) {
057: super (owner, title, true);
058: init(owner, editor, new String[] { ResourceMgr
059: .getString("LblOK") }, true);
060: }
061:
062: public ValidatingDialog(Frame owner, String title, JComponent editor) {
063: this (owner, title, editor, true);
064: }
065:
066: public ValidatingDialog(Frame owner, String title,
067: JComponent editor, boolean addCancelButton) {
068: super (owner, title, true);
069: init(owner, editor, new String[] { ResourceMgr
070: .getString("LblOK") }, addCancelButton);
071: }
072:
073: public ValidatingDialog(Dialog owner, String title,
074: JComponent editor, String[] options) {
075: this (owner, title, editor, options, true);
076: }
077:
078: public ValidatingDialog(Dialog owner, String title,
079: JComponent editor, String[] options, boolean addCancelButton) {
080: super (owner, title, true);
081: init(owner, editor, options, addCancelButton);
082: }
083:
084: public void setDefaultButton(int index) {
085: JRootPane root = this .getRootPane();
086: if (index >= optionButtons.length && cancelButton != null) {
087: root.setDefaultButton(cancelButton);
088: } else {
089: root.setDefaultButton(optionButtons[index]);
090: }
091: }
092:
093: private void init(Window owner, JComponent editor,
094: String[] options, boolean addCancelButton) {
095: if (editor instanceof ValidatingComponent) {
096: this .validator = (ValidatingComponent) editor;
097: }
098: this .editorComponent = editor;
099: this .optionButtons = new JButton[options.length];
100: for (int i = 0; i < options.length; i++) {
101: this .optionButtons[i] = new WbButton(options[i]);
102: this .optionButtons[i].addActionListener(this );
103: }
104:
105: if (addCancelButton) {
106: this .cancelButton = new WbButton(ResourceMgr
107: .getString("LblCancel"));
108: this .cancelButton.addActionListener(this );
109: }
110:
111: JRootPane root = this .getRootPane();
112: root.setDefaultButton(optionButtons[0]);
113:
114: if (addCancelButton) {
115: InputMap im = root
116: .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
117: ActionMap am = root.getActionMap();
118: this .esc = new EscAction(this );
119: im.put(esc.getAccelerator(), esc.getActionName());
120: am.put(esc.getActionName(), esc);
121:
122: im = editor.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
123: am = editor.getActionMap();
124: im.put(esc.getAccelerator(), esc.getActionName());
125: am.put(esc.getActionName(), esc);
126: }
127:
128: JPanel content = new JPanel();
129: content.setLayout(new BorderLayout());
130: Border b = BorderFactory.createEmptyBorder(10, 10, 10, 10);
131: content.setBorder(b);
132: content.add(editor, BorderLayout.CENTER);
133: buttonPanel = new JPanel();
134: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
135: for (int i = 0; i < optionButtons.length; i++) {
136: buttonPanel.add(optionButtons[i]);
137: }
138: if (cancelButton != null)
139: buttonPanel.add(cancelButton);
140: b = BorderFactory.createEmptyBorder(2, 0, 0, 0);
141: buttonPanel.setBorder(b);
142: content.add(buttonPanel, BorderLayout.SOUTH);
143: this .getContentPane().add(content);
144: this .doLayout();
145: this .pack();
146: this .addWindowListener(this );
147: }
148:
149: public int getSelectedOption() {
150: return this .selectedOption;
151: }
152:
153: public boolean isCancelled() {
154: return this .isCancelled;
155: }
156:
157: public static boolean showConfirmDialog(Window parent,
158: JComponent editor, String title) {
159: return showConfirmDialog(parent, editor, title, null, 0, false);
160: }
161:
162: public static boolean showConfirmDialog(Window parent,
163: JComponent editor, String title, int defaultButton) {
164: return showConfirmDialog(parent, editor, title, null,
165: defaultButton, false);
166: }
167:
168: public static boolean showConfirmDialog(Window parent,
169: JComponent editor, String title, Component reference,
170: int defaultButton, boolean centeredButtons) {
171: ValidatingDialog dialog = null;
172: if (parent == null) {
173: dialog = new ValidatingDialog(WbManager.getInstance()
174: .getCurrentWindow(), title, editor);
175: } else {
176: if (parent instanceof Frame)
177: dialog = new ValidatingDialog((Frame) parent, title,
178: editor);
179: else if (parent instanceof Dialog)
180: dialog = new ValidatingDialog((Dialog) parent, title,
181: editor);
182: else
183: throw new IllegalArgumentException(
184: "Parent component must be Dialog or Frame");
185: }
186: if (reference != null) {
187: WbSwingUtilities.center(dialog, reference);
188: } else {
189: WbSwingUtilities.center(dialog, parent);
190: }
191:
192: dialog.setDefaultButton(defaultButton);
193: if (centeredButtons) {
194: dialog.buttonPanel.setLayout(new FlowLayout(
195: FlowLayout.CENTER));
196: }
197: dialog.setVisible(true);
198:
199: return !dialog.isCancelled();
200: }
201:
202: private void close() {
203: this .setVisible(false);
204: this .dispose();
205: }
206:
207: public void windowActivated(WindowEvent e) {
208: }
209:
210: public void windowClosed(WindowEvent e) {
211: }
212:
213: public void windowClosing(WindowEvent e) {
214: this .close();
215: }
216:
217: public void windowDeactivated(WindowEvent e) {
218: }
219:
220: public void windowDeiconified(WindowEvent e) {
221: }
222:
223: public void windowIconified(WindowEvent e) {
224: }
225:
226: public void windowOpened(WindowEvent e) {
227: EventQueue.invokeLater(new Runnable() {
228: public void run() {
229: editorComponent.grabFocus();
230: if (validator != null)
231: validator.componentDisplayed();
232: }
233: });
234: }
235:
236: public void actionPerformed(ActionEvent e) {
237: if (e.getSource() == this .cancelButton
238: || e.getSource() == this .esc) {
239: this .selectedOption = -1;
240: this .isCancelled = true;
241: this .close();
242: } else {
243: for (int i = 0; i < optionButtons.length; i++) {
244: if (e.getSource() == optionButtons[i]) {
245: this .selectedOption = i;
246: break;
247: }
248: }
249: if (validator == null || this .validator.validateInput()) {
250: this .isCancelled = false;
251: this.close();
252: }
253: }
254: }
255:
256: }
|