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.x.impl.swing;
014:
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: import javax.swing.BorderFactory;
020: import javax.swing.JLabel;
021: import javax.swing.JPanel;
022: import javax.swing.JSeparator;
023: import javax.swing.border.Border;
024:
025: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.NamespaceTable;
026: import com.eviware.soapui.model.iface.Interface;
027: import com.eviware.soapui.support.types.StringToStringMap;
028: import com.eviware.x.form.XForm;
029: import com.eviware.x.form.XFormField;
030: import com.eviware.x.form.XFormOptionsField;
031: import com.eviware.x.form.XFormTextField;
032: import com.jgoodies.forms.layout.CellConstraints;
033: import com.jgoodies.forms.layout.FormLayout;
034: import com.jgoodies.forms.layout.RowSpec;
035:
036: public class SwingXFormImpl implements XForm {
037: private JPanel panel;
038: private CellConstraints cc = new CellConstraints();
039: private FormLayout layout;
040: private RowSpec rowSpec;
041: private int rowSpacing = 5;
042: private Map<String, XFormField> components = new HashMap<String, XFormField>();
043: private String rowAlignment = "top";
044: private String name;
045:
046: public SwingXFormImpl(String name) {
047: this .name = name;
048: layout = new FormLayout(
049: "5px,right:pref,5px,left:default,5px:grow(1.0)");
050: panel = new JPanel(layout);
051: rowSpec = new RowSpec(rowAlignment + ":pref");
052: }
053:
054: public String getName() {
055: return name;
056: }
057:
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: public JPanel getPanel() {
063: return panel;
064: }
065:
066: public void addSpace(int size) {
067: if (size > 0)
068: layout.appendRow(new RowSpec(size + "px"));
069: }
070:
071: public XFormField addCheckBox(String name, String description) {
072: JCheckBoxFormField checkBox = new JCheckBoxFormField(
073: description == null ? name : description);
074: addComponent(name, checkBox);
075: return checkBox;
076: }
077:
078: public XFormField addComponent(String label,
079: XFormField formComponent) {
080: components.put(label, formComponent);
081:
082: if (rowSpacing > 0 && !components.isEmpty())
083: addSpace(rowSpacing);
084:
085: layout.appendRow(rowSpec);
086:
087: int row = layout.getRowCount();
088:
089: AbstractSwingXFormField swingFormComponent = (AbstractSwingXFormField) formComponent;
090:
091: if (label != null) {
092: JLabel jlabel = new JLabel(label);
093: jlabel.setBorder(BorderFactory
094: .createEmptyBorder(2, 0, 0, 0));
095: panel.add(jlabel, cc.xy(2, row));
096: }
097:
098: panel.add(swingFormComponent.getComponent(), cc.xy(4, row));
099: components.put(label, formComponent);
100:
101: return formComponent;
102: }
103:
104: public XFormOptionsField addComboBox(String name, Object[] values,
105: String description) {
106: JComboBoxFormField comboBox = new JComboBoxFormField(values);
107: comboBox.setToolTip(description);
108: addComponent(name, comboBox);
109: return comboBox;
110: }
111:
112: // public <T extends ModelItem> XFormOptionsField addComboBox( String name, Object [] values, String description,
113: // final T target, final SoapUIAction<T> action )
114: // {
115: // // TODO Not implemented for Swing
116: // return null;
117: // }
118:
119: public void addSeparator() {
120: addSeparator(null);
121: }
122:
123: public void addSeparator(String label) {
124: addSpace(rowSpacing);
125:
126: layout.appendRow(rowSpec);
127: int row = layout.getRowCount();
128:
129: if (label == null)
130: panel.add(new JSeparator(), cc.xywh(2, row, 3, 1));
131: else
132: panel.add(new JLabel(label), cc.xywh(2, row, 3, 1));
133: }
134:
135: public XFormTextField addTextField(String name, String description,
136: FieldType type) {
137: if (type == FieldType.FOLDER || type == FieldType.FILE
138: || type == FieldType.PROJECT_FOLDER
139: || type == FieldType.PROJECT_FILE) {
140: return (XFormTextField) addComponent(name,
141: new FileFormField(description, type));
142: } else if (type == FieldType.PASSWORD) {
143: JPasswordFieldFormField pwdField = new JPasswordFieldFormField();
144: pwdField.getComponent().setColumns(30);
145: pwdField.setToolTip(description);
146: addComponent(name, pwdField);
147: return pwdField;
148: } else if (type == FieldType.TEXTAREA) {
149: JTextAreaFormField field = new JTextAreaFormField();
150: field.getTextArea().setColumns(40);
151: field.getTextArea().setRows(5);
152: field.setToolTip(description);
153: addComponent(name, field);
154: return field;
155: } else {
156: JTextFieldFormField textField = new JTextFieldFormField();
157: textField.getComponent().setColumns(40);
158: textField.setToolTip(description);
159: addComponent(name, textField);
160: return textField;
161: }
162: }
163:
164: public void setComponentValue(String label, String value) {
165: XFormField component = getComponent(label);
166: if (component != null)
167: component.setValue(value);
168: }
169:
170: public String getComponentValue(String name) {
171: XFormField component = getComponent(name);
172: return component == null ? null : component.getValue();
173: }
174:
175: public XFormField getComponent(String label) {
176: return components.get(label);
177: }
178:
179: public void setBorder(Border border) {
180: panel.setBorder(border);
181: }
182:
183: public XFormField addComponent(XFormField component) {
184: if (rowSpacing > 0 && !components.isEmpty())
185: addSpace(rowSpacing);
186:
187: layout.appendRow(rowSpec);
188: int row = layout.getRowCount();
189:
190: AbstractSwingXFormField swingFormComponent = (AbstractSwingXFormField) component;
191: panel.add(swingFormComponent.getComponent(), cc.xyw(1, row, 4));
192:
193: return component;
194: }
195:
196: public void setValues(StringToStringMap values) {
197: for (Iterator<String> i = values.keySet().iterator(); i
198: .hasNext();) {
199: String key = i.next();
200: setComponentValue(key, values.get(key));
201: }
202: }
203:
204: public StringToStringMap getValues() {
205: StringToStringMap values = new StringToStringMap();
206:
207: for (Iterator<String> i = components.keySet().iterator(); i
208: .hasNext();) {
209: String key = i.next();
210: values.put(key, getComponentValue(key));
211: }
212:
213: return values;
214: }
215:
216: public void addNameSpaceTable(String label, Interface modelItem) {
217: addComponent(label, new NamespaceTable(modelItem));
218: }
219:
220: public void setOptions(String name, Object[] values) {
221: XFormOptionsField combo = (XFormOptionsField) getComponent(name);
222: if (combo != null)
223: combo.setOptions(values);
224: }
225:
226: public void addLabel(String name, String label) {
227: addComponent(name, new JLabelFormField(label));
228: }
229:
230: public XFormField[] getFormFields() {
231: return components.values().toArray(
232: new XFormField[components.size()]);
233: }
234:
235: public void setFormFieldProperty(String name, Object value) {
236: for (XFormField field : components.values()) {
237: field.setProperty(name, value);
238: }
239: }
240:
241: public String[] getOptions(String name) {
242: XFormField combo = getComponent(name);
243: if (combo instanceof XFormOptionsField)
244: return ((XFormOptionsField) combo).getOptions();
245:
246: return null;
247: }
248:
249: public XFormField getFormField(String name) {
250: return components.get(name);
251: }
252: }
|