1
2
3
4
5
6
7
8
9
10
11
12
13 package com/eviware/soapui/support/components/package-summary.html">> 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 JButton addButton;
42 private JButton removeButton;
43 private JList list;
44 private JButton editButton;
45 private Box buttonBox;
46 private List<JButton> buttons = new ArrayList<JButton>();
47
48 public StringListFormComponent( String tooltip )
49 {
50 this( tooltip, false );
51 }
52
53 public StringListFormComponent( String tooltip, boolean editOnly )
54 {
55 super( new BorderLayout() );
56
57 listModel = new DefaultListModel();
58 list = new JList(listModel);
59 list.setToolTipText( tooltip );
60 JScrollPane scrollPane = new JScrollPane( list);
61 scrollPane.setPreferredSize( new Dimension( 300, 70 ) );
62 add( scrollPane, BorderLayout.CENTER);
63 buttonBox = new Box(BoxLayout.Y_AXIS);
64 buttonBox.setBorder( BorderFactory.createEmptyBorder( 0, 5, 0, 0 ));
65
66 if( !editOnly )
67 {
68 addButton = new JButton( "Add..");
69 addButton.addActionListener( this );
70 buttonBox.add( addButton );
71 buttonBox.add( Box.createVerticalStrut( 5 ));
72 }
73
74 editButton = new JButton( "Edit..");
75 editButton.addActionListener( this );
76 buttons.add( editButton );
77 buttonBox.add( editButton );
78
79 if( !editOnly )
80 {
81 buttonBox.add( Box.createVerticalStrut( 5 ));
82 removeButton = new JButton( "Remove.." );
83 removeButton.addActionListener( this );
84 buttonBox.add( removeButton );
85 buttons.add( removeButton );
86 }
87
88 add( buttonBox, BorderLayout.EAST );
89
90 list.addListSelectionListener( new ListSelectionListener() {
91
92 public void valueChanged( ListSelectionEvent e )
93 {
94 setButtonState();
95 }} );
96
97 setButtonState();
98 }
99
100 public void addButton( Action action, boolean requireSelection )
101 {
102 buttonBox.add( Box.createVerticalStrut( 5 ));
103 JButton button = new JButton( action );
104 buttonBox.add( button);
105
106 if( requireSelection )
107 {
108 buttons.add( button );
109 setButtonState();
110 }
111 }
112
113 public void setValue(String value)
114 {
115 listModel.clear();
116
117 try
118 {
119 StringList stringList = StringList.fromXml( value );
120
121 String[] files = stringList.toStringArray();
122 for( String file : files )
123 if( file.trim().length() > 0)
124 listModel.addElement( file );
125 }
126 catch( Exception e )
127 {
128 SoapUI.logError( e );
129 }
130 }
131
132 public String getValue()
133 {
134 StringList result = new StringList( listModel.toArray() );
135 return result.toXml();
136 }
137
138 public JList getList()
139 {
140 return list;
141 }
142
143 public void actionPerformed( ActionEvent arg0 )
144 {
145 if( arg0.getSource() == addButton )
146 {
147 String value = UISupport.prompt( "Specify value to add", "Add..", (String)null );
148 if( value != null )
149 {
150 listModel.addElement( value );
151 }
152 }
153 else
154 {
155 int selectedIndex = list.getSelectedIndex();
156
157 if( arg0.getSource() == removeButton && selectedIndex != -1 )
158 {
159 Object elm = listModel.getElementAt( selectedIndex );
160 if( UISupport.confirm( "Remove [" + elm.toString() + "] from list", "Remove" ))
161 {
162 listModel.remove( selectedIndex );
163 }
164 }
165 else if( arg0.getSource() == editButton && selectedIndex != -1 )
166 {
167 String elm = ( String ) listModel.getElementAt( selectedIndex );
168 String value = UISupport.prompt( "Specify value", "Edit..", elm );
169
170 if( value != null )
171 {
172 listModel.setElementAt( value, selectedIndex );
173 }
174 }
175 }
176 }
177
178 public void setButtonState()
179 {
180 boolean b = list.getSelectedIndex() != -1;
181 for( JButton button : buttons )
182 button.setEnabled( b );
183 }
184
185 public String [] getData()
186 {
187 String [] result = new String[listModel.size()];
188 for( int c = 0; c < result.length; c++ )
189 result[c] = ( String ) listModel.get( c );
190
191 return result;
192 }
193
194 public void setData( String[] strings )
195 {
196 listModel.clear();
197 for( String str : strings )
198 {
199 listModel.addElement( str );
200 }
201 }
202 }