001: /*
002: * Copyright (C) 2006 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package net.sourceforge.squirrel_sql.plugins.refactoring.gui;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030:
031: import javax.swing.JButton;
032: import javax.swing.JDialog;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.JTextField;
036: import javax.swing.SwingConstants;
037: import javax.swing.border.Border;
038: import javax.swing.border.EmptyBorder;
039:
040: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
041: import net.sourceforge.squirrel_sql.fw.util.StringManager;
042: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
043:
044: /**
045: * A dialog that can be used to get column(s) selected by the user
046: */
047: public abstract class AbstractRefactoringDialog extends JDialog {
048:
049: private JLabel tableNameLabel = null;
050: private JTextField tableNameTextField = null;
051:
052: protected JButton executeButton = null;
053: protected JButton editSQLButton = null;
054: protected JButton showSQLButton = null;
055: protected JButton cancelButton = null;
056:
057: /** The constraint that was used to add the last component */
058: protected GridBagConstraints c = null;
059:
060: protected Dimension mediumField = new Dimension(126, 20);
061:
062: /** the panel in which subclasses may add components */
063: protected JPanel pane = null;
064:
065: protected EmptyBorder emptyBorder = new EmptyBorder(new Insets(5,
066: 5, 5, 5));
067:
068: /** Internationalized strings for this class. */
069: private static final StringManager s_stringMgr = StringManagerFactory
070: .getStringManager(AbstractRefactoringDialog.class);
071:
072: protected interface i18n {
073: //i18n[AbstractRefactoringDialog.cancelButtonLabel=Cancel]
074: String CANCEL_BUTTON_LABEL = s_stringMgr
075: .getString("AbstractRefactoringDialog.cancelButtonLabel");
076:
077: //i18n[AbstractRefactoringDialog.editButtonLabel=Edit SQL]
078: String EDIT_BUTTON_LABEL = s_stringMgr
079: .getString("AbstractRefactoringDialog.editButtonLabel");
080:
081: //i18n[AbstractRefactoringDialog.executeButtonLabel=Execute]
082: String EXECUTE_BUTTON_LABEL = s_stringMgr
083: .getString("AbstractRefactoringDialog.executeButtonLabel");
084:
085: //i18n[AbstractRefactoringDialog.showButtonLabel=Show SQL]
086: String SHOWSQL_BUTTON_LABEL = s_stringMgr
087: .getString("AbstractRefactoringDialog.showButtonLabel");
088:
089: //i18n[AbstractRefactoringDialog.tableNameLabel=Table Name: ]
090: String TABLE_NAME_LABEL = s_stringMgr
091: .getString("AbstractRefactoringDialog.tableNameLabel");
092:
093: }
094:
095: public AbstractRefactoringDialog(boolean addTableNameField) {
096: init(addTableNameField);
097: }
098:
099: public void setTableName(String tableName) {
100: tableNameTextField.setText(tableName);
101: }
102:
103: public String getTableName() {
104: return tableNameTextField.getText();
105: }
106:
107: public void addShowSQLListener(ActionListener listener) {
108: if (listener == null) {
109: throw new IllegalArgumentException(
110: "listener cannot be null");
111: }
112: showSQLButton.addActionListener(listener);
113: }
114:
115: public void addEditSQLListener(ActionListener listener) {
116: if (listener == null) {
117: throw new IllegalArgumentException(
118: "listener cannot be null");
119: }
120: editSQLButton.addActionListener(listener);
121: }
122:
123: public void addExecuteListener(ActionListener listener) {
124: if (listener == null) {
125: throw new IllegalArgumentException(
126: "listener cannot be null");
127: }
128: executeButton.addActionListener(listener);
129: }
130:
131: public void _setVisible(boolean visible) {
132: super .setVisible(visible);
133: }
134:
135: /**
136: * Overridden to make the Execute button have focus when the dialog is
137: * displayed.
138: */
139: public void setVisible(final boolean visible) {
140: GUIUtils.processOnSwingEventThread(new Runnable() {
141: public void run() {
142: AbstractRefactoringDialog.this ._setVisible(visible);
143: }
144: });
145: GUIUtils.processOnSwingEventThread(new Runnable() {
146: public void run() {
147: executeButton.requestFocus();
148: }
149: });
150: }
151:
152: protected GridBagConstraints getLabelConstraints(
153: GridBagConstraints c) {
154: c.gridx = 0;
155: c.gridy++;
156: c.anchor = GridBagConstraints.NORTHEAST;
157: c.fill = GridBagConstraints.NONE;
158: c.weightx = 0;
159: c.weighty = 0;
160: return c;
161: }
162:
163: protected GridBagConstraints getFieldConstraints(
164: GridBagConstraints c) {
165: c.gridx++;
166: c.anchor = GridBagConstraints.NORTHWEST;
167: c.weightx = 0;
168: c.weighty = 0;
169: c.fill = GridBagConstraints.HORIZONTAL;
170: return c;
171: }
172:
173: protected JLabel getBorderedLabel(String text, Border border) {
174: JLabel result = new JLabel(text);
175: result.setBorder(border);
176: result.setPreferredSize(new Dimension(115, 20));
177: result.setHorizontalAlignment(SwingConstants.RIGHT);
178: return result;
179: }
180:
181: /**
182: * Creates the UI for this dialog.
183: */
184: protected void init(boolean addTableNameField) {
185: super .setModal(true);
186: setSize(425, 250);
187:
188: pane = new JPanel();
189: pane.setLayout(new GridBagLayout());
190: pane.setBorder(new EmptyBorder(10, 0, 0, 30));
191:
192: c = new GridBagConstraints();
193: c.gridx = 0;
194: c.gridy = -1;
195:
196: if (addTableNameField) {
197: // Table name
198: tableNameLabel = getBorderedLabel(i18n.TABLE_NAME_LABEL,
199: emptyBorder);
200: pane.add(tableNameLabel, getLabelConstraints(c));
201:
202: tableNameTextField = new JTextField();
203: tableNameTextField.setPreferredSize(mediumField);
204: tableNameTextField.setEditable(false);
205: pane.add(tableNameTextField, getFieldConstraints(c));
206: }
207:
208: Container contentPane = super .getContentPane();
209: contentPane.setLayout(new BorderLayout());
210: contentPane.add(pane, BorderLayout.CENTER);
211:
212: contentPane.add(getButtonPanel(), BorderLayout.SOUTH);
213: }
214:
215: protected JPanel getButtonPanel() {
216: JPanel result = new JPanel();
217: executeButton = new JButton(i18n.EXECUTE_BUTTON_LABEL);
218: result.add(executeButton);
219:
220: editSQLButton = new JButton(i18n.EDIT_BUTTON_LABEL);
221: result.add(editSQLButton);
222: showSQLButton = new JButton(i18n.SHOWSQL_BUTTON_LABEL);
223: result.add(showSQLButton);
224: cancelButton = new JButton(i18n.CANCEL_BUTTON_LABEL);
225: cancelButton.addActionListener(new ActionListener() {
226: public void actionPerformed(ActionEvent e) {
227: setVisible(false);
228: }
229: });
230: result.add(cancelButton);
231: return result;
232: }
233:
234: protected void enable(JButton button) {
235: if (button != null) {
236: button.setEnabled(true);
237: }
238: }
239:
240: protected void disable(JButton button) {
241: if (button != null) {
242: button.setEnabled(false);
243: }
244: }
245:
246: }
|