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.awt.BorderLayout;
016: import java.awt.Dimension;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.List;
020:
021: import javax.swing.BorderFactory;
022: import javax.swing.ImageIcon;
023: import javax.swing.JDialog;
024: import javax.swing.JPanel;
025: import javax.swing.JTabbedPane;
026:
027: import com.eviware.soapui.support.UISupport;
028: import com.eviware.soapui.support.action.swing.ActionList;
029: import com.eviware.soapui.support.components.JButtonBar;
030: import com.eviware.soapui.support.types.StringToStringMap;
031: import com.eviware.x.form.ValidationMessage;
032: import com.eviware.x.form.XForm;
033: import com.eviware.x.form.XFormDialog;
034: import com.eviware.x.form.XFormField;
035:
036: public class JTabbedFormDialog extends SwingXFormDialog {
037: private JDialog dialog;
038: private List<SwingXFormImpl> forms = new ArrayList<SwingXFormImpl>();
039: private JTabbedPane tabs;
040:
041: public JTabbedFormDialog(String name, XForm[] forms,
042: ActionList actions, String description, ImageIcon icon) {
043: dialog = new JDialog(UISupport.getMainFrame(), name, true);
044: tabs = new JTabbedPane();
045: for (XForm form : forms) {
046: SwingXFormImpl swingFormImpl = ((SwingXFormImpl) form);
047: this .forms.add(swingFormImpl);
048:
049: JPanel panel = swingFormImpl.getPanel();
050: panel
051: .setBorder(BorderFactory.createEmptyBorder(0, 0, 5,
052: 0));
053:
054: tabs.addTab(form.getName(), panel);
055: }
056:
057: JButtonBar buttons = UISupport.initDialogActions(actions,
058: dialog);
059:
060: if (description != null || icon != null)
061: dialog.getContentPane()
062: .add(
063: UISupport.buildDescription(name,
064: description, icon),
065: BorderLayout.NORTH);
066:
067: dialog.getContentPane().add(
068: UISupport.createTabPanel(tabs, false),
069: BorderLayout.CENTER);
070:
071: buttons.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
072: dialog.getContentPane().add(buttons, BorderLayout.SOUTH);
073: dialog.pack();
074: Dimension size = dialog.getSize();
075: if (size.getHeight() < 300) {
076: dialog.setSize(new Dimension((int) size.getWidth(), 300));
077: }
078: }
079:
080: public void setValues(StringToStringMap values) {
081: for (XForm form : forms)
082: form.setValues(values);
083: }
084:
085: public void setOptions(String field, Object[] options) {
086: for (XForm form : forms)
087: form.setOptions(field, options);
088: }
089:
090: public XFormField getFormField(String name) {
091: for (XForm form : forms) {
092: XFormField formField = form.getFormField(name);
093: if (formField != null)
094: return formField;
095: }
096:
097: return null;
098: }
099:
100: public StringToStringMap getValues() {
101: StringToStringMap result = new StringToStringMap();
102:
103: for (XForm form : forms)
104: result.putAll(form.getValues());
105:
106: return result;
107: }
108:
109: public void setVisible(boolean visible) {
110: if (visible)
111: tabs.setSelectedIndex(0);
112:
113: UISupport.centerDialog(dialog);
114:
115: dialog.setVisible(visible);
116: }
117:
118: public boolean validate() {
119: for (int i = 0; i < forms.size(); i++) {
120: XFormField[] formFields = forms.get(i).getFormFields();
121: for (int c = 0; c < formFields.length; c++) {
122: ValidationMessage[] messages = formFields[c].validate();
123: if (messages != null && messages.length > 0) {
124: tabs.setSelectedIndex(i);
125: ((AbstractSwingXFormField) messages[0]
126: .getFormField()).getComponent()
127: .requestFocus();
128: UISupport
129: .showErrorMessage(messages[0].getMessage());
130: return false;
131: }
132: }
133: }
134:
135: return true;
136: }
137:
138: public void setFormFieldProperty(String name, Object value) {
139: for (XForm form : forms)
140: form.setFormFieldProperty(name, value);
141: }
142:
143: public String getValue(String field) {
144: for (XForm form : forms) {
145: if (form.getComponent(field) != null)
146: return form.getComponent(field).getValue();
147: }
148:
149: return null;
150: }
151:
152: public void setValue(String field, String value) {
153: for (XForm form : forms) {
154: if (form.getComponent(field) != null)
155: form.getComponent(field).setValue(value);
156: }
157: }
158:
159: public int getValueIndex(String name) {
160: for (SwingXFormImpl form : forms) {
161: if (form.getComponent(name) != null) {
162: String[] options = form.getOptions(name);
163: if (options == null)
164: return -1;
165:
166: return Arrays.asList(options).indexOf(
167: form.getComponentValue(name));
168: }
169: }
170:
171: return -1;
172: }
173:
174: public boolean show() {
175: setReturnValue(XFormDialog.CANCEL_OPTION);
176: show(new StringToStringMap());
177: return getReturnValue() == XFormDialog.OK_OPTION;
178: }
179:
180: public void setWidth(int i) {
181: dialog.setPreferredSize(new Dimension(i, (int) dialog
182: .getPreferredSize().getHeight()));
183: }
184: }
|