001: package jimm.datavision.gui;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.awt.*;
006: import javax.swing.*;
007: import javax.swing.border.Border;
008: import java.awt.GridBagLayout;
009: import java.awt.GridBagConstraints;
010:
011: /**
012: * Lays out a bunch of label/edit widget pairs. Optionally creates
013: * the edit widget for you. This is not a layout manager <i>per se</i>.
014: * Calling any of the <code>add</code>* methods creates label/edit
015: * widget pairs. Calling <code>getPanel</code> returns a panel containing
016: * the labels and edit widgets, arranged for your pleasure.
017: *
018: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
019: */
020: public class EditFieldLayout {
021:
022: /* ================================================================ */
023: /** Represents a label/component pair. */
024: static class Row {
025: protected JLabel label;
026: protected Component component;
027:
028: Row(JLabel l, Component c) {
029: label = l;
030: component = c;
031: }
032: }
033:
034: /* ================================================================ */
035:
036: protected ArrayList rows;
037: protected Border border;
038: protected JPanel panel;
039:
040: public EditFieldLayout() {
041: rows = new ArrayList();
042: }
043:
044: /**
045: * Adds the label/component pair to the layout and returns the component.
046: * If <var>label</var> does not end with a colon, one will be added.
047: * <var>label</var> may be <code>null</code>, in which case no label is
048: * displayed.
049: * <p>
050: * All the other <code>add*</code> methods call this one.
051: *
052: * @param label a possibly <code>null</code> label string
053: * @param c a GUI component
054: * @return the component
055: */
056: public Component add(String label, Component c) {
057: if (label == null || label.length() == 0)
058: label = "";
059: else if (!label.endsWith(":"))
060: label += ":";
061:
062: rows.add(new Row(new JLabel(label), c));
063: return c;
064: }
065:
066: /**
067: * Creates a text field and adds it and the label.
068: *
069: * @param label a possibly <code>null</code> label string
070: * @return the new text field
071: */
072: public JTextField addTextField(String label) {
073: return (JTextField) add(label, new JTextField());
074: }
075:
076: /**
077: * Creates a text field and adds it and the label.
078: *
079: * @param label a possibly <code>null</code> label string
080: * @param columns the text field's size
081: * @return the new text field
082: */
083: public JTextField addTextField(String label, int columns) {
084: return (JTextField) add(label, new JTextField(columns));
085: }
086:
087: /**
088: * Creates a text field and adds it and the label.
089: *
090: * @param label a possibly <code>null</code> label string
091: * @param text the text field's initial text
092: * @return the new text field
093: */
094: public JTextField addTextField(String label, String text) {
095: return (JTextField) add(label, new JTextField(text == null ? ""
096: : text));
097: }
098:
099: /**
100: * Creates a text field and adds it and the label.
101: *
102: * @param label a possibly <code>null</code> label string
103: * @param text the text field's initial text
104: * @param columns the text field's size
105: * @return the new text field
106: */
107: public JTextField addTextField(String label, String text,
108: int columns) {
109: return (JTextField) add(label, new JTextField(text == null ? ""
110: : text, columns));
111: }
112:
113: /**
114: * Creates a text area and adds it and the label.
115: *
116: * @param label a possibly <code>null</code> label string
117: * @return the new text area
118: */
119: public JTextArea addTextArea(String label) {
120: JTextArea area = new JTextArea();
121: area.setBorder(BorderFactory.createLoweredBevelBorder());
122: add(label, area);
123: return area;
124: }
125:
126: /**
127: * Creates a text area and adds it and the label.
128: *
129: * @param label a possibly <code>null</code> label string
130: * @param rows the text field's height
131: * @param cols the text field's width
132: * @return the new text area
133: */
134: public JTextArea addTextArea(String label, int rows, int cols) {
135: JTextArea area = new JTextArea(rows, cols);
136: area.setBorder(BorderFactory.createLoweredBevelBorder());
137: add(label, area);
138: return area;
139: }
140:
141: /**
142: * Creates a text area and adds it and the label.
143: *
144: * @param label a possibly <code>null</code> label string
145: * @param text the text field's initial text
146: * @return the new text area
147: */
148: public JTextArea addTextArea(String label, String text) {
149: JTextArea area = new JTextArea(text == null ? "" : text);
150: area.setBorder(BorderFactory.createLoweredBevelBorder());
151: add(label, area);
152: return area;
153: }
154:
155: /**
156: * Creates a text area and adds it and the label.
157: *
158: * @param label a possibly <code>null</code> label string
159: * @param text the text field's initial text
160: * @param rows the text field's height
161: * @param cols the text field's width
162: * @return the new text area
163: */
164: public JTextArea addTextArea(String label, String text, int rows,
165: int cols) {
166: JTextArea area = new JTextArea(text == null ? "" : text, rows,
167: cols);
168: JScrollPane scroller = new JScrollPane(area);
169: add(label, scroller);
170: return area;
171: }
172:
173: /**
174: * Creates a check box and adds it and the label.
175: *
176: * @param label a possibly <code>null</code> label string
177: * @return the new check box
178: */
179: public JCheckBox addCheckBox(String label) {
180: return addCheckBox(label, 0);
181: }
182:
183: /**
184: * Creates a check box and adds it and the label.
185: *
186: * @param label a possibly <code>null</code> label string
187: * @param key the mnemonic key (a <code>KeyEvent</code> constant)
188: * @return the new check box
189: */
190: public JCheckBox addCheckBox(String label, int key) {
191: JCheckBox checkBox = new JCheckBox(label);
192: checkBox.setMnemonic(key);
193: return (JCheckBox) add(null, checkBox);
194: }
195:
196: /**
197: * Creates a combo box and adds it and the label.
198: *
199: * @param label a possibly <code>null</code> label string
200: * @param items an array of objects
201: * @return the new combo box
202: */
203: public JComboBox addComboBox(String label, Object[] items) {
204: return addComboBox(label, items, false);
205: }
206:
207: /**
208: * Creates a combo box and adds it and the label.
209: *
210: * @param label a possibly <code>null</code> label string
211: * @param items an array of objects
212: * @param editable if <code>true</code>, the combo box will allow custom
213: * value entry by the user
214: * @return the new combo box
215: */
216: public JComboBox addComboBox(String label, Object[] items,
217: boolean editable) {
218: JComboBox comboBox = new JComboBox(items);
219: comboBox.setEditable(editable);
220: return (JComboBox) add(label, comboBox);
221: }
222:
223: /**
224: * Creates two labels and adds them.
225: *
226: * @param label a possibly <code>null</code> label string
227: * @param text text for the right-hand label
228: * @return the new right-hand label
229: */
230: public JLabel addLabel(String label, String text) {
231: return (JLabel) add(label, new JLabel(text == null ? "" : text));
232: }
233:
234: /**
235: * Creates a password field and adds it and the label.
236: *
237: * @param label a possibly <code>null</code> label string
238: * @return the new password field
239: */
240: public JPasswordField addPasswordField(String label) {
241: return (JPasswordField) add(label, new JPasswordField());
242: }
243:
244: /**
245: * Creates a password field and adds it and the label.
246: *
247: * @param label a possibly <code>null</code> label string
248: * @param columns the password field's size
249: * @return the new password field
250: */
251: public JPasswordField addPasswordField(String label, int columns) {
252: return (JPasswordField) add(label, new JPasswordField(columns));
253: }
254:
255: /**
256: * Creates a password field and adds it and the label.
257: *
258: * @param label a possibly <code>null</code> label string
259: * @param password the initial password text
260: * @return the new password field
261: */
262: public JPasswordField addPasswordField(String label, String password) {
263: return (JPasswordField) add(label, new JPasswordField(
264: password == null ? "" : password));
265: }
266:
267: /**
268: * Creates a password field and adds it and the label.
269: *
270: * @param label a possibly <code>null</code> label string
271: * @param password the initial password text
272: * @param columns the password field's size
273: * @return the new password field
274: */
275: public JPasswordField addPasswordField(String label,
276: String password, int columns) {
277: return (JPasswordField) add(label, new JPasswordField(
278: password == null ? "" : password, columns));
279: }
280:
281: /**
282: * Creates a button and adds it to the right-hand side, under the fields.
283: *
284: * @param label a button label
285: * @return the new button
286: */
287: public JButton addButton(String label) {
288: return (JButton) add(null, new JButton(label));
289: }
290:
291: /**
292: * Creates an empty row.
293: */
294: public void skipRow() {
295: rows.add(null);
296: }
297:
298: /**
299: * Creates an empty border the same size on all sides.
300: *
301: * @param allSides the width of the border
302: */
303: public void setBorder(int allSides) {
304: setBorder(allSides, allSides, allSides, allSides);
305: }
306:
307: /**
308: * Creates an empty border on all sides.
309: *
310: * @param top top border size
311: * @param left left border size
312: * @param bottom bottom border size
313: * @param right right border size
314: */
315: public void setBorder(int top, int left, int bottom, int right) {
316: border = BorderFactory.createEmptyBorder(top, left, bottom,
317: right);
318: }
319:
320: /**
321: * Returns the panel containing all the labels and edit widgets. Lazily
322: * instantiates the panel.
323: *
324: * @return the panel containing all the labels and edit widgets
325: */
326: public JPanel getPanel() {
327: if (panel == null)
328: buildPanel();
329: return panel;
330: }
331:
332: /**
333: * Builds the panel.
334: */
335: protected void buildPanel() {
336: GridBagLayout bag = new GridBagLayout();
337: GridBagConstraints c = new GridBagConstraints();
338: c.insets = new Insets(6, 6, 6, 6);
339: panel = new JPanel();
340: panel.setLayout(bag);
341: if (border != null)
342: panel.setBorder(border);
343:
344: c.gridy = 0;
345: for (Iterator iter = rows.iterator(); iter.hasNext(); ++c.gridy) {
346: Row row = (Row) iter.next();
347: if (row == null)
348: continue;
349:
350: if (row.label != null) {
351: c.gridx = 0;
352: c.anchor = GridBagConstraints.NORTHEAST;
353: bag.setConstraints(row.label, c);
354: panel.add(row.label);
355: }
356:
357: if (row.component != null) {
358: c.gridx = 1;
359: c.anchor = GridBagConstraints.NORTHWEST;
360: bag.setConstraints(row.component, c);
361: panel.add(row.component);
362: }
363: }
364: }
365:
366: }
|