001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.components;
014:
015: import java.awt.event.ComponentAdapter;
016: import java.awt.event.ComponentEvent;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.Map;
020:
021: import javax.swing.Action;
022: import javax.swing.ComboBoxModel;
023: import javax.swing.JButton;
024: import javax.swing.JCheckBox;
025: import javax.swing.JComboBox;
026: import javax.swing.JComponent;
027: import javax.swing.JLabel;
028: import javax.swing.JList;
029: import javax.swing.JPanel;
030: import javax.swing.JPasswordField;
031: import javax.swing.JSeparator;
032: import javax.swing.JTextField;
033: import javax.swing.border.Border;
034: import javax.swing.text.JTextComponent;
035:
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038: import com.jgoodies.forms.layout.RowSpec;
039:
040: /**
041: * Utility-class for creating forms
042: */
043:
044: public class SimpleForm {
045: private JPanel panel;
046: private CellConstraints cc = new CellConstraints();
047: private FormLayout layout;
048: private RowSpec rowSpec;
049: private int rowSpacing = 5;
050: private Map<String, JComponent> components = new HashMap<String, JComponent>();
051: private Map<JComboBox, Object[]> comboBoxMaps = new HashMap<JComboBox, Object[]>();
052: private String rowAlignment = "center";
053: private Map<String, String> hiddenValues;
054: private boolean appended;
055:
056: public SimpleForm() {
057: this (5);
058: }
059:
060: public SimpleForm(String layout) {
061: this (new FormLayout(layout));
062: }
063:
064: public SimpleForm(FormLayout layout) {
065: this .layout = layout;
066: panel = new JPanel(layout);
067: rowSpec = new RowSpec(rowAlignment + ":pref");
068: }
069:
070: public SimpleForm(int indent) {
071: this (indent
072: + "px:none,right:pref,10px,left:default,5px:grow(1.0)");
073: }
074:
075: public JPanel getPanel() {
076: return panel;
077: }
078:
079: public String getRowAlignment() {
080: return rowAlignment;
081: }
082:
083: public void setRowAlignment(String rowAlignment) {
084: this .rowAlignment = rowAlignment;
085: }
086:
087: public int getRowSpacing() {
088: return rowSpacing;
089: }
090:
091: public void setRowSpacing(int rowSpacing) {
092: this .rowSpacing = rowSpacing;
093: }
094:
095: public void addHiddenValue(String name, String value) {
096: if (hiddenValues == null)
097: hiddenValues = new HashMap<String, String>();
098:
099: hiddenValues.put(name, value);
100: }
101:
102: public JButton addRightButton(Action action) {
103: if (rowSpacing > 0 && !components.isEmpty())
104: addSpace(rowSpacing);
105:
106: layout.appendRow(rowSpec);
107: int row = layout.getRowCount();
108:
109: JButton button = new JButton(action);
110: panel.add(button, cc.xy(4, row, "right,bottom"));
111: return button;
112: }
113:
114: public void addSpace() {
115: addSpace(rowSpacing);
116: }
117:
118: public void addSpace(int size) {
119: if (size > 0)
120: layout.appendRow(new RowSpec(size + "px"));
121: }
122:
123: public void addRightComponent(JComponent component) {
124: if (rowSpacing > 0 && !components.isEmpty())
125: addSpace(rowSpacing);
126:
127: layout.appendRow(rowSpec);
128: int row = layout.getRowCount();
129:
130: panel.add(component, cc.xy(4, row, "right,bottom"));
131: }
132:
133: public JCheckBox appendCheckBox(String caption, String label,
134: boolean selected) {
135: JCheckBox checkBox = new JCheckBox(label, selected);
136: components.put(caption, checkBox);
137: append(caption, checkBox);
138: return checkBox;
139: }
140:
141: public void append(String label, JComponent component) {
142: append(label, component, null);
143: }
144:
145: public JComboBox appendComboBox(String label, Map values) {
146: Object[] valueArray = new Object[values.size()];
147: Object[] keyArray = new Object[values.size()];
148:
149: int ix = 0;
150: for (Iterator i = values.keySet().iterator(); i.hasNext(); ix++) {
151: keyArray[ix] = i.next();
152: valueArray[ix] = values.get(keyArray[ix]);
153: }
154:
155: JComboBox comboBox = new JComboBox(valueArray);
156:
157: comboBoxMaps.put(comboBox, keyArray);
158:
159: append(label, comboBox);
160: return comboBox;
161: }
162:
163: public JComboBox appendComboBox(String label, Object[] values,
164: String tooltip) {
165: JComboBox comboBox = new JComboBox(values);
166: comboBox.setToolTipText(tooltip);
167: append(label, comboBox);
168: return comboBox;
169: }
170:
171: public JComboBox appendComboBox(String label, ComboBoxModel model,
172: String tooltip) {
173: JComboBox comboBox = new JComboBox(model);
174: comboBox.setToolTipText(tooltip);
175: append(label, comboBox);
176: return comboBox;
177: }
178:
179: public void appendFixed(String label, JComponent component) {
180: append(label, component, "left:pref");
181: }
182:
183: public void append(String label, JComponent component,
184: String alignments) {
185: int spaceRowIndex = -1;
186:
187: if (rowSpacing > 0 && appended) {
188: addSpace(rowSpacing);
189: spaceRowIndex = layout.getRowCount();
190: }
191:
192: layout.appendRow(rowSpec);
193: int row = layout.getRowCount();
194:
195: if (label != null) {
196: JLabel jlabel = new JLabel(label);
197: panel.add(jlabel, cc.xy(2, row));
198:
199: component.addComponentListener(new LabelHider(jlabel,
200: spaceRowIndex));
201: } else
202: component.addComponentListener(new LabelHider(null,
203: spaceRowIndex));
204:
205: if (alignments == null)
206: panel.add(component, cc.xy(4, row));
207: else
208: panel.add(component, cc.xy(4, row, alignments));
209:
210: components.put(label, component);
211: appended = true;
212: }
213:
214: public void appendSeparator() {
215: if (appended && rowSpacing > 0)
216: addSpace(rowSpacing);
217:
218: layout.appendRow(rowSpec);
219: int row = layout.getRowCount();
220:
221: panel.add(new JSeparator(), cc.xywh(2, row, 3, 1));
222: appended = true;
223: }
224:
225: public JTextField appendTextField(String label, String tooltip) {
226: JTextField textField = new JTextField();
227: textField.setColumns(30);
228: textField.setToolTipText(tooltip);
229: append(label, textField);
230: return textField;
231: }
232:
233: public JTextField appendPasswordField(String label, String tooltip) {
234: JPasswordField textField = new JPasswordField();
235: textField.setColumns(30);
236: textField.setToolTipText(tooltip);
237: append(label, textField);
238: return textField;
239: }
240:
241: public void setComponentValue(String label, String value) {
242: JComponent component = getComponent(label);
243:
244: if (component instanceof JTextComponent) {
245: ((JTextComponent) component).setText(value);
246: } else if (component instanceof JComboBox) {
247: JComboBox comboBox = ((JComboBox) component);
248: comboBox.setSelectedItem(value);
249: } else if (component instanceof JList) {
250: ((JList) component).setSelectedValue(value, true);
251: } else if (component instanceof JCheckBox) {
252: ((JCheckBox) component).setSelected(Boolean.valueOf(value));
253: } else if (component instanceof JFormComponent) {
254: ((JFormComponent) component).setValue(value);
255: }
256: }
257:
258: public String getComponentValue(String label) {
259: JComponent component = getComponent(label);
260: if (component == null) {
261: return (String) (hiddenValues == null ? null : hiddenValues
262: .get(label));
263: }
264:
265: if (component instanceof JTextComponent) {
266: return ((JTextComponent) component).getText();
267: }
268:
269: if (component instanceof JComboBox) {
270: JComboBox comboBox = ((JComboBox) component);
271: int selectedIndex = comboBox.getSelectedIndex();
272: if (selectedIndex != -1) {
273: if (comboBoxMaps.containsKey(component)) {
274: Object[] keys = (Object[]) comboBoxMaps
275: .get(comboBox);
276: Object value = keys[selectedIndex];
277: return (String) value == null ? null : value
278: .toString(); // Added support for enums
279: } else {
280: Object value = comboBox.getSelectedItem();
281: return (String) value == null ? null : value
282: .toString(); // Added support for enums
283: }
284: }
285: }
286:
287: if (component instanceof JList) {
288: return (String) ((JList) component).getSelectedValue();
289: }
290:
291: if (component instanceof JCheckBox) {
292: return String.valueOf(((JCheckBox) component).isSelected());
293: }
294:
295: else if (component instanceof JFormComponent) {
296: return ((JFormComponent) component).getValue();
297: }
298:
299: return null;
300: }
301:
302: public JComponent getComponent(String label) {
303: return (JComponent) components.get(label);
304: }
305:
306: public void setBorder(Border border) {
307: panel.setBorder(border);
308: }
309:
310: public int getRowCount() {
311: return layout.getRowCount();
312: }
313:
314: public void addComponent(JComponent component) {
315: layout.appendRow(rowSpec);
316: int row = layout.getRowCount();
317:
318: panel.add(component, cc.xyw(2, row, 4));
319: }
320:
321: public void setValues(Map<String, String> values) {
322: for (Iterator<String> i = values.keySet().iterator(); i
323: .hasNext();) {
324: String key = i.next();
325: setComponentValue(key, values.get(key));
326: }
327: }
328:
329: public void getValues(Map<String, String> values) {
330: for (Iterator<String> i = components.keySet().iterator(); i
331: .hasNext();) {
332: String key = i.next();
333: values.put(key, getComponentValue(key));
334: }
335: }
336:
337: public void append(JComponent component) {
338: int spaceRowIndex = -1;
339:
340: if (rowSpacing > 0 && appended) {
341: addSpace(rowSpacing);
342: spaceRowIndex = layout.getRowCount();
343: }
344:
345: layout.appendRow(rowSpec);
346: int row = layout.getRowCount();
347:
348: panel.add(component, cc.xyw(2, row, 4));
349:
350: component.addComponentListener(new LabelHider(null,
351: spaceRowIndex));
352:
353: appended = true;
354: }
355:
356: private final class LabelHider extends ComponentAdapter {
357: private final JLabel jlabel;
358: private final int rowIndex;
359:
360: public LabelHider(JLabel jlabel, int i) {
361: this .jlabel = jlabel;
362: this .rowIndex = i;
363: }
364:
365: public void componentHidden(ComponentEvent e) {
366: if (jlabel != null)
367: jlabel.setVisible(false);
368:
369: if (rowIndex >= 0 && rowIndex < layout.getRowCount())
370: layout.setRowSpec(rowIndex, new RowSpec("0px"));
371: }
372:
373: public void componentShown(ComponentEvent e) {
374: if (jlabel != null)
375: jlabel.setVisible(true);
376:
377: if (rowIndex >= 0 && rowIndex < layout.getRowCount())
378: layout.setRowSpec(rowIndex, new RowSpec(rowSpacing
379: + "px"));
380: }
381: }
382: }
|