001: /*
002: * Copyright 2002 by Mark A. Kobold
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is Markus A. Kobold.
015: *
016: * Portions created by Mark A. Kobold are
017: * Copyright (C) Copyright (C) 2000 Mark A. Kobold. All Rights Reserved.
018: * Contributor(s): Mark A. Kobold <mkobold@sprintpcs.com>.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * If you didn't download this code from the following link, you should check if
024: * you aren't using an obsolete version:
025: * http://isql.sourceforge.net/
026: */
027: package org.isqlviewer.ui;
028:
029: import java.awt.BorderLayout;
030: import java.awt.Component;
031: import java.awt.Container;
032: import java.awt.Frame;
033: import java.awt.GridBagConstraints;
034: import java.awt.GridBagLayout;
035: import java.awt.Insets;
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038: import java.awt.event.WindowEvent;
039: import java.util.prefs.BackingStoreException;
040: import java.util.prefs.Preferences;
041:
042: import javax.swing.Box;
043: import javax.swing.DefaultComboBoxModel;
044: import javax.swing.JButton;
045: import javax.swing.JComboBox;
046: import javax.swing.JComponent;
047: import javax.swing.JDialog;
048: import javax.swing.JFrame;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JTextField;
052:
053: import org.isqlviewer.util.LocalMessages;
054:
055: /**
056: * Modal dialog to prompt the user for variable substitution.
057: * <p>
058: *
059: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
060: * @version 1.0
061: */
062: public class VariableSubstitutor extends JDialog implements
063: ActionListener {
064:
065: private static final long serialVersionUID = 8720304755017125400L;
066: private static final Insets INSETS = new Insets(2, 2, 2, 2);
067: private static final GridBagConstraints UI_CONSTRAINT = new GridBagConstraints(
068: 0, 0, 0, 0, 0, 0, 0, 0, INSETS, 0, 0);
069:
070: private static final String BUNDLE_IDENTIFIER = "org.isqlviewer.ui.ResourceBundle";
071: private static final LocalMessages messages = new LocalMessages(
072: BUNDLE_IDENTIFIER);
073:
074: private DefaultComboBoxModel selectionModel = new DefaultComboBoxModel();
075: private JComboBox previousSelections = new JComboBox(selectionModel);
076: private JButton okButton = new JButton(messages
077: .getMessage("VariableSubstitutor.okbutton"));
078: private JButton cancelButton = new JButton(messages
079: .getMessage("VariableSubstitutor.cancelbutton"));
080: private String substitution = null;
081:
082: public VariableSubstitutor(Frame frameParent, String variableName,
083: Preferences preferences) {
084:
085: super (frameParent, messages.format("VariableSubstitutor.title",
086: variableName), true);
087: try {
088: String[] selectionValues = preferences.keys();
089: for (int i = 0; i < selectionValues.length; i++) {
090: selectionModel.addElement(selectionValues[i]);
091: }
092: } catch (BackingStoreException e) {
093: }
094: initializeUI(variableName);
095: }
096:
097: public String getSubstitution() {
098:
099: return substitution;
100: }
101:
102: public void actionPerformed(ActionEvent e) {
103:
104: Object src = e.getSource();
105: if (src instanceof JTextField || src.equals(okButton)) {
106: JTextField field = (JTextField) previousSelections
107: .getEditor().getEditorComponent();
108: substitution = field.getText();
109: dispose();
110: } else if (src.equals(cancelButton)) {
111: substitution = null;
112: dispose();
113: }
114: }
115:
116: @Override
117: protected void processWindowEvent(WindowEvent e) {
118:
119: switch (e.getID()) {
120: case WindowEvent.WINDOW_OPENED:
121: previousSelections.requestFocus();
122: setLocationRelativeTo(getOwner());
123: break;
124: }
125: super .processWindowEvent(e);
126: }
127:
128: private void initializeUI(String variableName) {
129:
130: Component component = null;
131: Object constraint = null;
132:
133: JPanel contentPanel = new JPanel(new GridBagLayout());
134:
135: component = new JLabel(variableName);
136: ((JLabel) component).setLabelFor(previousSelections);
137: constraint = constrain(0, 1, 1, 1, 0.0d, 0.0d,
138: GridBagConstraints.WEST, GridBagConstraints.NONE);
139: contentPanel.add(component, constraint);
140:
141: previousSelections.setEditable(true);
142: previousSelections.setMaximumRowCount(5);
143: previousSelections.setRequestFocusEnabled(true);
144: ((JTextField) previousSelections.getEditor()
145: .getEditorComponent()).addActionListener(this );
146: component = previousSelections;
147: constraint = constrain(1, 1, 1, 1, 1.0d, 0.0d,
148: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
149: contentPanel.add(component, constraint);
150:
151: component = Box.createVerticalStrut(10);
152: constraint = constrain(0, 2, 2, 1, 0.0d, 0.0d,
153: GridBagConstraints.WEST, GridBagConstraints.NONE);
154: contentPanel.add(component, constraint);
155:
156: JComponent commandPanel = new JPanel(new GridBagLayout());
157: JButton actionButton = null;
158:
159: constraint = constrain(0, 0, 1, 1, 1.0d, 0.0d,
160: GridBagConstraints.CENTER,
161: GridBagConstraints.HORIZONTAL);
162: commandPanel.add(Box.createHorizontalStrut(10), constraint);
163:
164: constraint = constrain(1, 0, 1, 1, 1.0d, 0.0d,
165: GridBagConstraints.CENTER,
166: GridBagConstraints.HORIZONTAL);
167: commandPanel.add(Box.createHorizontalGlue(), constraint);
168:
169: actionButton = okButton;
170: actionButton.addActionListener(this );
171: actionButton.setActionCommand(Integer.toString(0));
172: actionButton.setToolTipText(messages
173: .getMessage("VariableSubstitutor.okbutton.tip"));
174: constraint = constrain(2, 0, 1, 1, 0.0d, 0.0d,
175: GridBagConstraints.CENTER, GridBagConstraints.NONE);
176: commandPanel.add(actionButton, constraint);
177:
178: actionButton = cancelButton;
179: actionButton.addActionListener(this );
180: actionButton.setActionCommand(Integer.toString(1));
181: actionButton.setToolTipText(messages
182: .getMessage("VariableSubstitutor.cancelbutton.tip"));
183: constraint = constrain(3, 0, 1, 1, 0.0d, 0.0d,
184: GridBagConstraints.CENTER, GridBagConstraints.NONE);
185: commandPanel.add(actionButton, constraint);
186:
187: constraint = constrain(4, 0, 1, 1, 0.0d, 0.0d,
188: GridBagConstraints.CENTER,
189: GridBagConstraints.HORIZONTAL);
190: commandPanel.add(Box.createHorizontalStrut(10), constraint);
191:
192: constraint = constrain(0, 1, 5, 1, 0.0d, 0.0d,
193: GridBagConstraints.CENTER,
194: GridBagConstraints.HORIZONTAL);
195: commandPanel.add(Box.createVerticalStrut(10), constraint);
196:
197: getRootPane().setDefaultButton(okButton);
198: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
199:
200: Container container = new JPanel(new BorderLayout());
201: container.add(contentPanel, BorderLayout.CENTER);
202: container.add(commandPanel, BorderLayout.SOUTH);
203: container.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
204: container.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
205: container.add(Box.createHorizontalStrut(10), BorderLayout.EAST);
206:
207: setContentPane(container);
208: setResizable(false);
209: pack();
210: }
211:
212: private static GridBagConstraints constrain(int x, int y, int w,
213: int h, double wx, double wy, int a, int f) {
214:
215: UI_CONSTRAINT.gridx = x;
216: UI_CONSTRAINT.gridy = y;
217: UI_CONSTRAINT.gridwidth = w;
218: UI_CONSTRAINT.gridheight = h;
219: UI_CONSTRAINT.weightx = wx;
220: UI_CONSTRAINT.weighty = wy;
221: UI_CONSTRAINT.anchor = a;
222: UI_CONSTRAINT.fill = f;
223: return UI_CONSTRAINT;
224: }
225: }
|