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.BorderLayout;
016: import java.awt.Dimension;
017: import java.awt.event.ActionEvent;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import javax.swing.AbstractAction;
022: import javax.swing.BorderFactory;
023: import javax.swing.DefaultComboBoxModel;
024: import javax.swing.ImageIcon;
025: import javax.swing.JComboBox;
026: import javax.swing.JComponent;
027: import javax.swing.JDialog;
028: import javax.swing.JList;
029: import javax.swing.JPanel;
030: import javax.swing.JSeparator;
031:
032: import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
033: import com.eviware.soapui.support.UISupport;
034: import com.eviware.soapui.support.action.swing.ActionList;
035: import com.eviware.soapui.support.action.swing.DefaultActionList;
036: import com.jgoodies.forms.layout.FormLayout;
037:
038: /**
039: * Utility for creating simple configuration dialogs
040: *
041: * @author Ole.Matzura
042: */
043:
044: public class SwingConfigurationDialogImpl implements
045: ConfigurationDialog {
046: private JDialog dialog;
047: private SimpleForm form;
048: private boolean result;
049: private Map<String, String> values;
050: private Map<String, FieldType> fieldTypes = new HashMap<String, FieldType>();
051: private final String title;
052: private Dimension size;
053: private JComponent content;
054: private ActionList actions;
055: private String description;
056: private ImageIcon icon;
057:
058: public SwingConfigurationDialogImpl(String title, String helpUrl,
059: String description, ImageIcon icon) {
060: this .title = title;
061: this .description = description;
062: this .icon = icon;
063: form = new SimpleForm(new FormLayout(
064: "10px,right:pref,10px,left:pref,5px"));
065:
066: actions = new DefaultActionList("Actions");
067: if (helpUrl != null) {
068: actions.addAction(new ShowOnlineHelpAction(helpUrl));
069: actions.addSeparator();
070: }
071:
072: actions.addAction(new OkAction());
073: actions.addAction(new CancelAction());
074: }
075:
076: public boolean show(Map<String, String> values) {
077: if (dialog == null)
078: buildDialog();
079:
080: this .values = values;
081:
082: result = false;
083:
084: form.setValues(values);
085:
086: if (size == null)
087: dialog.pack();
088: else
089: dialog.setSize(size);
090:
091: UISupport.showDialog(dialog);
092: return result;
093: }
094:
095: public Dimension getSize() {
096: return size;
097: }
098:
099: public void setSize(Dimension preferredSize) {
100: this .size = preferredSize;
101: }
102:
103: public void addTextField(String name, String tooltip) {
104: addTextField(name, tooltip, FieldType.TEXT);
105: }
106:
107: public void addTextField(String name, String tooltip, FieldType type) {
108: if (type == FieldType.DIRECTORY)
109: form.append(name, new DirectoryFormComponent(tooltip));
110: else
111: form.appendTextField(name, tooltip);
112:
113: fieldTypes.put(name, type);
114: }
115:
116: public void addCheckBox(String caption, String label,
117: boolean selected) {
118: form.appendCheckBox(caption, label, selected);
119: }
120:
121: public void setContent(JComponent content) {
122: this .content = content;
123: }
124:
125: private void buildDialog() {
126: dialog = new JDialog(UISupport.getMainFrame(), title, true);
127:
128: form.getPanel().setBorder(
129: BorderFactory.createEmptyBorder(5, 0, 5, 0));
130:
131: JPanel panel = new JPanel(new BorderLayout());
132: JComponent contentPanel = content == null ? form.getPanel()
133: : content;
134: panel.add(contentPanel, BorderLayout.CENTER);
135:
136: JButtonBar buttons = UISupport.initDialogActions(actions,
137: dialog);
138: buttons.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
139:
140: if (content == null) {
141: JPanel p = new JPanel(new BorderLayout());
142: p.add(new JSeparator(), BorderLayout.NORTH);
143: p.add(buttons, BorderLayout.CENTER);
144:
145: panel.add(p, BorderLayout.SOUTH);
146: } else {
147: panel.add(buttons, BorderLayout.SOUTH);
148: }
149:
150: if (description != null || icon != null)
151: dialog.getContentPane()
152: .add(
153: UISupport.buildDescription(title,
154: description, icon),
155: BorderLayout.NORTH);
156:
157: dialog.getContentPane().add(panel);
158: dialog.pack();
159: }
160:
161: private class OkAction extends AbstractAction {
162: public OkAction() {
163: super ("Ok");
164: }
165:
166: public void actionPerformed(ActionEvent e) {
167: result = true;
168:
169: form.getValues(values);
170: dialog.setVisible(false);
171: }
172: }
173:
174: private class CancelAction extends AbstractAction {
175: public CancelAction() {
176: super ("Cancel");
177: }
178:
179: public void actionPerformed(ActionEvent e) {
180: dialog.setVisible(false);
181: }
182: }
183:
184: public void addComboBox(String label, Object[] objects,
185: String tooltip) {
186: form.appendComboBox(label, objects, tooltip);
187: }
188:
189: public void setValues(String id, String[] values) {
190: JComponent component = form.getComponent(id);
191: if (component instanceof JComboBox) {
192: ((JComboBox) component).setModel(new DefaultComboBoxModel(
193: values));
194: } else if (component instanceof JList) {
195: ((JList) component).setModel(new DefaultComboBoxModel(
196: values));
197: } else
198: throw new RuntimeException("Could not set values on ["
199: + component + "]");
200: }
201:
202: public void addComboBox(String label, String tooltip) {
203: form.appendComboBox(label, new String[] {}, tooltip);
204: }
205:
206: public void addComponent(JComponent component) {
207: form.addComponent(component);
208: }
209:
210: public void getValues(Map<String, String> values) {
211: form.getValues(values);
212: }
213:
214: public ActionList getActions() {
215: return actions;
216: }
217:
218: public void hide() {
219: dialog.setVisible(false);
220: }
221: }
|