1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.components;
14
15 import java.awt.BorderLayout;
16 import java.awt.Dimension;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.swing.Action;
23 import javax.swing.BorderFactory;
24 import javax.swing.Box;
25 import javax.swing.BoxLayout;
26 import javax.swing.DefaultListModel;
27 import javax.swing.JButton;
28 import javax.swing.JList;
29 import javax.swing.JPanel;
30 import javax.swing.JScrollPane;
31 import javax.swing.event.ListSelectionEvent;
32 import javax.swing.event.ListSelectionListener;
33
34 import com.eviware.soapui.SoapUI;
35 import com.eviware.soapui.support.UISupport;
36 import com.eviware.soapui.support.types.StringList;
37
38 public class StringListFormComponent extends JPanel implements JFormComponent, ActionListener
39 {
40 private DefaultListModel listModel;
41 private String defaultValue = null;
42 private JButton addButton;
43 private JButton removeButton;
44 private JList list;
45 private JButton editButton;
46 private Box buttonBox;
47 private List<JButton> buttons = new ArrayList<JButton>();
48
49 public StringListFormComponent( String tooltip )
50 {
51 this( tooltip, false, null );
52 }
53
54 public StringListFormComponent( String tooltip, boolean editOnly )
55 {
56 this( tooltip, editOnly, null );
57 }
58
59 public StringListFormComponent( String tooltip, boolean editOnly, String defaultValue )
60 {
61 super( new BorderLayout() );
62
63 this.defaultValue = defaultValue;
64 listModel = new DefaultListModel();
65 list = new JList( listModel );
66 list.setToolTipText( tooltip );
67 JScrollPane scrollPane = new JScrollPane( list );
68 scrollPane.setPreferredSize( new Dimension( 300, 70 ) );
69 add( scrollPane, BorderLayout.CENTER );
70 buttonBox = new Box( BoxLayout.Y_AXIS );
71 buttonBox.setBorder( BorderFactory.createEmptyBorder( 0, 5, 0, 0 ) );
72
73 if( !editOnly )
74 {
75 addButton = new JButton( "Add.." );
76 addButton.addActionListener( this );
77 buttonBox.add( addButton );
78 buttonBox.add( Box.createVerticalStrut( 5 ) );
79 }
80
81 editButton = new JButton( "Edit.." );
82 editButton.addActionListener( this );
83 buttons.add( editButton );
84 buttonBox.add( editButton );
85
86 if( !editOnly )
87 {
88 buttonBox.add( Box.createVerticalStrut( 5 ) );
89 removeButton = new JButton( "Remove.." );
90 removeButton.addActionListener( this );
91 buttonBox.add( removeButton );
92 buttons.add( removeButton );
93 }
94
95 add( buttonBox, BorderLayout.EAST );
96
97 list.addListSelectionListener( new ListSelectionListener()
98 {
99
100 public void valueChanged( ListSelectionEvent e )
101 {
102 setButtonState();
103 }
104 } );
105
106 setButtonState();
107 }
108
109 public void addButton( Action action, boolean requireSelection )
110 {
111 buttonBox.add( Box.createVerticalStrut( 5 ) );
112 JButton button = new JButton( action );
113 buttonBox.add( button );
114
115 if( requireSelection )
116 {
117 buttons.add( button );
118 setButtonState();
119 }
120 }
121
122 public void setValue( String value )
123 {
124 String[] oldData = getData();
125 listModel.clear();
126
127 try
128 {
129 StringList stringList = StringList.fromXml( value );
130
131 String[] files = stringList.toStringArray();
132 for( String file : files )
133 if( file.trim().length() > 0 )
134 listModel.addElement( file );
135
136 firePropertyChange( "data", oldData, getData() );
137 }
138 catch( Exception e )
139 {
140 SoapUI.logError( e );
141 }
142 }
143
144 public String getValue()
145 {
146 StringList result = new StringList( listModel.toArray() );
147 return result.toXml();
148 }
149
150 public JList getList()
151 {
152 return list;
153 }
154
155 public void actionPerformed( ActionEvent e )
156 {
157 String[] oldData = getData();
158
159 if( e.getSource() == addButton )
160 {
161 String value = UISupport.prompt( "Specify value to add", "Add..", defaultValue );
162 if( value != null )
163 {
164 listModel.addElement( value );
165 firePropertyChange( "options", oldData, getData() );
166 }
167 }
168 else
169 {
170 int selectedIndex = list.getSelectedIndex();
171
172 if( e.getSource() == removeButton && selectedIndex != -1 )
173 {
174 Object elm = listModel.getElementAt( selectedIndex );
175 if( UISupport.confirm( "Remove [" + elm.toString() + "] from list", "Remove" ) )
176 {
177 listModel.remove( selectedIndex );
178 firePropertyChange( "options", oldData, getData() );
179 }
180 }
181 else if( e.getSource() == editButton && selectedIndex != -1 )
182 {
183 String elm = ( String )listModel.getElementAt( selectedIndex );
184 String value = UISupport.prompt( "Specify value", "Edit..", elm );
185
186 if( value != null )
187 {
188 listModel.setElementAt( value, selectedIndex );
189 firePropertyChange( "options", oldData, getData() );
190 }
191 }
192 }
193 }
194
195 public void setButtonState()
196 {
197 boolean b = list.getSelectedIndex() != -1;
198 for( JButton button : buttons )
199 button.setEnabled( b );
200 }
201
202 public String[] getData()
203 {
204 String[] result = new String[listModel.size()];
205 for( int c = 0; c < result.length; c++ )
206 result[c] = ( String )listModel.get( c );
207
208 return result;
209 }
210
211 public void setData( String[] strings )
212 {
213 String[] oldData = getData();
214
215 listModel.clear();
216 if( strings != null )
217 {
218 for( String str : strings )
219 {
220 listModel.addElement( str );
221 }
222 }
223
224 firePropertyChange( "options", oldData, getData() );
225 }
226
227 public String[] getOptions()
228 {
229 return getData();
230 }
231
232 public void setOptions( String[] options )
233 {
234 setData( options );
235 }
236
237 public void setEnabled( boolean b )
238 {
239 addButton.setEnabled( b );
240 list.setEnabled( b );
241 if( b )
242 setButtonState();
243 }
244 }