001: /*
002: * Copyright (C) 2003 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: InputBox.java$
021: * $FileID: 4806$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 9/1/03 9:27 PM$
026: * $Comment: Debug.$
027: */
028: package org.sourcejammer.client.gui.dialog;
029:
030: import java.awt.BorderLayout;
031: import java.awt.Component;
032: import java.awt.Container;
033: import java.awt.Dialog;
034: import java.awt.Frame;
035: import java.awt.GridLayout;
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038:
039: import javax.swing.BorderFactory;
040: import javax.swing.JButton;
041: import javax.swing.JCheckBox;
042: import javax.swing.JLabel;
043: import javax.swing.JPanel;
044: import javax.swing.JPasswordField;
045: import javax.swing.JTextField;
046:
047: import org.sourcejammer.client.DisplayTextLibrary;
048: import org.sourcejammer.client.gui.CommandCentral;
049: import org.sourcejammer.util.BadMethodArgumentException;
050: import org.sourcejammer.util.EnumeratedType;
051:
052: /**
053: * Title: $FileName: InputBox.java$
054: * @author $AuthorName: Rob MacGrogan$
055: * @version $VerNum: 2$<br><br>
056: *
057: * $Description: Easily generates a small dialog containing
058: * input fields (text box, password, or checkbox).$
059: * $KeyWordsOff: $<br><br>
060: */
061: public class InputBox {
062:
063: private SJDialog inputDialog = null;
064: private JPanel inputPanel = null;
065:
066: private String[] inputLabels = null;
067: private String[] defaultValues = null;
068: private String title = null;
069: private boolean modal = true;
070: private String okButtonText = null;
071: private FieldType[] fieldTypes = null;
072:
073: private boolean showDefaults = false;
074: private boolean useFieldTypes = false;
075: private String[] inputValues = null;
076:
077: public static final class FieldType extends EnumeratedType {
078: public FieldType(String value) {
079: setValue(value);
080: }
081:
082: public static final FieldType TEXT = new FieldType("text");
083: public static final FieldType PASSWORD = new FieldType(
084: "password");
085: public static final FieldType CHECKBOX = new FieldType(
086: "checkbox");
087: }
088:
089: public InputBox() {
090: }
091:
092: public void showInputBox() {
093: showInputBox(CommandCentral.getInstance().getRootAppFrame());
094: }
095:
096: public void showInputBox(Frame f) {
097: validate();
098: inputDialog = new SJDialog(f, title, modal);
099: buildInputBox(f);
100: }
101:
102: public void showInputBox(Dialog d) {
103: validate();
104: inputDialog = new SJDialog(d, title, modal);
105: buildInputBox(d);
106: }
107:
108: private JButton buildOKButton(String okButtonText) {
109: JButton jbOKButton = new JButton(okButtonText);
110: jbOKButton.addActionListener(new ActionListener() {
111: public void actionPerformed(ActionEvent ev) {
112: Component[] components = inputPanel.getComponents();
113: int iValueCounter = 0;
114: inputValues = new String[components.length];
115: for (int i = 0; i < components.length; i++) {
116: if (components[i] instanceof JTextField) {
117: JTextField fld = (JTextField) components[i];
118: inputValues[iValueCounter] = fld.getText();
119: iValueCounter++;
120: } else if (components[i] instanceof JCheckBox) {
121: JCheckBox chk = (JCheckBox) components[i];
122: boolean checked = chk.isSelected();
123: inputValues[iValueCounter] = Boolean
124: .toString(checked);
125: iValueCounter++;
126: }
127: }
128: inputDialog.dispose();
129: }
130: } //end anon class
131: );
132: jbOKButton.setDefaultCapable(true);
133: return jbOKButton;
134: }
135:
136: private JButton buildCancelButton() {
137: JButton jbCancelButton = new JButton(DisplayTextLibrary
138: .displayText(DisplayTextLibrary.BTN_CANCEL));
139: jbCancelButton.addActionListener(new ActionListener() {
140: public void actionPerformed(ActionEvent ev) {
141: inputValues = null;
142: inputDialog.dispose();
143: }
144: } //end anon class
145: );
146: return jbCancelButton;
147: }
148:
149: private void buildInputBox(Component c) {
150: CommandCentral oCommand = CommandCentral.getInstance();
151:
152: if (okButtonText == null) {
153: okButtonText = DisplayTextLibrary
154: .displayText(DisplayTextLibrary.BTN_OK);
155: }
156:
157: JButton okButton = buildOKButton(okButtonText);
158:
159: JButton cancelButton = buildCancelButton();
160:
161: JPanel buttonPanel = new JPanel();
162: buttonPanel.add(okButton);
163: buttonPanel.add(cancelButton);
164:
165: int iNumInputs = inputLabels.length;
166: inputPanel = new JPanel(new GridLayout(iNumInputs, 1));
167: JPanel labelPanel = new JPanel(new GridLayout(iNumInputs, 1));
168:
169: for (int i = 0; i < iNumInputs; i++) {
170: Component inputComponent = null;
171: if (useFieldTypes && fieldTypes[i] == FieldType.PASSWORD) {
172: labelPanel.add(new JLabel(inputLabels[i] + ": ",
173: JLabel.RIGHT));
174: JPasswordField fld = new JPasswordField();
175: if (showDefaults) {
176: fld.setText(defaultValues[i]);
177: }
178: inputPanel.add(fld);
179: inputComponent = fld;
180: } else if (useFieldTypes
181: && fieldTypes[i] == FieldType.CHECKBOX) {
182: JCheckBox chk = new JCheckBox(inputLabels[i]);
183: if (showDefaults) {
184: String defaultVal = defaultValues[i];
185: if (defaultVal.equals("1")
186: || defaultVal.equalsIgnoreCase("true")) {
187: chk.setSelected(true);
188: }
189: }
190: labelPanel.add(new JLabel(" "));
191: inputPanel.add(chk);
192: inputComponent = chk;
193: } else {
194: labelPanel.add(new JLabel(inputLabels[i] + ": ",
195: JLabel.RIGHT));
196: JTextField fld = new JTextField();
197: if (showDefaults) {
198: fld.setText(defaultValues[i]);
199: }
200: inputPanel.add(fld);
201: inputComponent = fld;
202: }
203:
204: if (i == 0) {
205: inputDialog.getRootPane().setNextFocusableComponent(
206: inputComponent);
207: }
208: }
209:
210: JPanel content = new JPanel(new BorderLayout(3, 3));
211: content.add(labelPanel, BorderLayout.WEST);
212: content.add(inputPanel, BorderLayout.CENTER);
213: content.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
214:
215: Container dialogContentPane = inputDialog.getContentPane();
216:
217: JPanel mainPanel = new JPanel(new BorderLayout(3, 3));
218: mainPanel
219: .setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
220: mainPanel.add(content, BorderLayout.CENTER);
221: mainPanel.add(buttonPanel, BorderLayout.SOUTH);
222:
223: dialogContentPane.add(mainPanel, BorderLayout.CENTER);
224:
225: dialogContentPane.add(buttonPanel, BorderLayout.SOUTH);
226: inputDialog.getRootPane().setDefaultButton(okButton);
227: inputDialog.pack();
228:
229: inputDialog.setLocationRelativeTo(c);
230: inputDialog.show();
231: }
232:
233: private void validate() {
234: if (defaultValues != null) {
235: showDefaults = true;
236: if (defaultValues.length != inputLabels.length) {
237: throw new BadMethodArgumentException(
238: "Number of default values does not match number of labels.");
239: }
240: }
241: if (fieldTypes != null) {
242: useFieldTypes = true;
243: if (fieldTypes.length != inputLabels.length) {
244: throw new BadMethodArgumentException(
245: "Number of field types does not match number of labels.");
246: }
247: }
248: }
249:
250: /**
251: * Sets the defaultValues.
252: * @param defaultValues The defaultValues to set
253: */
254: public void setDefaultValues(String[] defaultValues) {
255: this .defaultValues = defaultValues;
256: }
257:
258: /**
259: * Sets the fieldTypes.
260: * @param fieldTypes The fieldTypes to set
261: */
262: public void setFieldTypes(FieldType[] fieldTypes) {
263: this .fieldTypes = fieldTypes;
264: }
265:
266: /**
267: * Sets the inputLabels.
268: * @param inputLabels The inputLabels to set
269: */
270: public void setInputLabels(String[] inputLabels) {
271: this .inputLabels = inputLabels;
272: }
273:
274: /**
275: * Sets the modal.
276: * @param modal The modal to set
277: */
278: public void setModal(boolean modal) {
279: this .modal = modal;
280: }
281:
282: /**
283: * Sets the okButtonText.
284: * @param okButtonText The okButtonText to set
285: */
286: public void setOkButtonText(String okButtonText) {
287: this .okButtonText = okButtonText;
288: }
289:
290: /**
291: * Sets the title.
292: * @param title The title to set
293: */
294: public void setTitle(String title) {
295: this .title = title;
296: }
297:
298: /**
299: * Returns the values the user entered in the generated dialog.
300: * @return String[]
301: */
302: public String[] getInputValues() {
303: return inputValues;
304: }
305:
306: }
|