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.awt.event.ActionListener;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import javax.swing.Action;
023: import javax.swing.BorderFactory;
024: import javax.swing.Box;
025: import javax.swing.BoxLayout;
026: import javax.swing.DefaultListModel;
027: import javax.swing.JButton;
028: import javax.swing.JList;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.event.ListSelectionEvent;
032: import javax.swing.event.ListSelectionListener;
033:
034: import com.eviware.soapui.SoapUI;
035: import com.eviware.soapui.support.UISupport;
036: import com.eviware.soapui.support.types.StringList;
037:
038: public class StringListFormComponent extends JPanel implements
039: JFormComponent, ActionListener {
040: private DefaultListModel listModel;
041: private JButton addButton;
042: private JButton removeButton;
043: private JList list;
044: private JButton editButton;
045: private Box buttonBox;
046: private List<JButton> buttons = new ArrayList<JButton>();
047:
048: public StringListFormComponent(String tooltip) {
049: this (tooltip, false);
050: }
051:
052: public StringListFormComponent(String tooltip, boolean editOnly) {
053: super (new BorderLayout());
054:
055: listModel = new DefaultListModel();
056: list = new JList(listModel);
057: list.setToolTipText(tooltip);
058: JScrollPane scrollPane = new JScrollPane(list);
059: scrollPane.setPreferredSize(new Dimension(300, 70));
060: add(scrollPane, BorderLayout.CENTER);
061: buttonBox = new Box(BoxLayout.Y_AXIS);
062: buttonBox
063: .setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
064:
065: if (!editOnly) {
066: addButton = new JButton("Add..");
067: addButton.addActionListener(this );
068: buttonBox.add(addButton);
069: buttonBox.add(Box.createVerticalStrut(5));
070: }
071:
072: editButton = new JButton("Edit..");
073: editButton.addActionListener(this );
074: buttons.add(editButton);
075: buttonBox.add(editButton);
076:
077: if (!editOnly) {
078: buttonBox.add(Box.createVerticalStrut(5));
079: removeButton = new JButton("Remove..");
080: removeButton.addActionListener(this );
081: buttonBox.add(removeButton);
082: buttons.add(removeButton);
083: }
084:
085: add(buttonBox, BorderLayout.EAST);
086:
087: list.addListSelectionListener(new ListSelectionListener() {
088:
089: public void valueChanged(ListSelectionEvent e) {
090: setButtonState();
091: }
092: });
093:
094: setButtonState();
095: }
096:
097: public void addButton(Action action, boolean requireSelection) {
098: buttonBox.add(Box.createVerticalStrut(5));
099: JButton button = new JButton(action);
100: buttonBox.add(button);
101:
102: if (requireSelection) {
103: buttons.add(button);
104: setButtonState();
105: }
106: }
107:
108: public void setValue(String value) {
109: listModel.clear();
110:
111: try {
112: StringList stringList = StringList.fromXml(value);
113:
114: String[] files = stringList.toStringArray();
115: for (String file : files)
116: if (file.trim().length() > 0)
117: listModel.addElement(file);
118: } catch (Exception e) {
119: SoapUI.logError(e);
120: }
121: }
122:
123: public String getValue() {
124: StringList result = new StringList(listModel.toArray());
125: return result.toXml();
126: }
127:
128: public JList getList() {
129: return list;
130: }
131:
132: public void actionPerformed(ActionEvent arg0) {
133: if (arg0.getSource() == addButton) {
134: String value = UISupport.prompt("Specify value to add",
135: "Add..", (String) null);
136: if (value != null) {
137: listModel.addElement(value);
138: }
139: } else {
140: int selectedIndex = list.getSelectedIndex();
141:
142: if (arg0.getSource() == removeButton && selectedIndex != -1) {
143: Object elm = listModel.getElementAt(selectedIndex);
144: if (UISupport.confirm("Remove [" + elm.toString()
145: + "] from list", "Remove")) {
146: listModel.remove(selectedIndex);
147: }
148: } else if (arg0.getSource() == editButton
149: && selectedIndex != -1) {
150: String elm = (String) listModel
151: .getElementAt(selectedIndex);
152: String value = UISupport.prompt("Specify value",
153: "Edit..", elm);
154:
155: if (value != null) {
156: listModel.setElementAt(value, selectedIndex);
157: }
158: }
159: }
160: }
161:
162: public void setButtonState() {
163: boolean b = list.getSelectedIndex() != -1;
164: for (JButton button : buttons)
165: button.setEnabled(b);
166: }
167:
168: public String[] getData() {
169: String[] result = new String[listModel.size()];
170: for (int c = 0; c < result.length; c++)
171: result[c] = (String) listModel.get(c);
172:
173: return result;
174: }
175:
176: public void setData(String[] strings) {
177: listModel.clear();
178: for (String str : strings) {
179: listModel.addElement(str);
180: }
181: }
182: }
|