View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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  			public void valueChanged( ListSelectionEvent e )
100 			{
101 				setButtonState();
102 			}} );
103 		
104 		setButtonState();
105 	}
106 	
107 	public void addButton( Action action, boolean requireSelection )
108 	{
109 		buttonBox.add( Box.createVerticalStrut( 5 ));
110 		JButton button = new JButton( action );
111 		buttonBox.add( button);
112 		
113 		if( requireSelection )
114 		{
115 			buttons.add( button );
116 			setButtonState();
117 		}
118 	}
119 	
120 	public void setValue(String value)
121 	{
122 		String[] oldData = getData();
123 		listModel.clear();
124 
125 		try
126 		{
127 			StringList stringList = StringList.fromXml( value );
128 			
129 			String[] files = stringList.toStringArray();
130 			for( String file : files )
131 				if( file.trim().length() > 0)
132 					listModel.addElement( file );
133 			
134 			firePropertyChange("data", oldData, getData());
135 		}
136 		catch( Exception e )
137 		{
138 			SoapUI.logError( e );
139 		}
140 	}
141 
142 	public String getValue()
143 	{
144 		StringList result = new StringList( listModel.toArray() );
145 		return result.toXml();
146 	}
147 
148 	public JList getList()
149 	{
150 		return list;
151 	}
152 	
153 	public void actionPerformed( ActionEvent e )
154 	{
155 		String[] oldData = getData();
156 		
157 		if( e.getSource() == addButton )
158 		{
159 			String value = UISupport.prompt( "Specify value to add", "Add..", defaultValue );
160          if( value != null )
161 		   {
162 		     	listModel.addElement( value );
163 		     	firePropertyChange("options", oldData, getData());
164 		   }
165 		}
166 		else
167 		{
168 			int selectedIndex = list.getSelectedIndex();
169 			
170 			if( e.getSource() == removeButton && selectedIndex != -1 )
171 			{
172 				Object elm = listModel.getElementAt( selectedIndex );
173 				if( UISupport.confirm( "Remove [" + elm.toString() + "] from list", "Remove" ))
174 				{
175 					listModel.remove( selectedIndex );
176 					firePropertyChange("options", oldData, getData());
177 				}
178 			}
179 			else if( e.getSource() == editButton && selectedIndex != -1 )
180 			{
181 				String elm = ( String ) listModel.getElementAt( selectedIndex );
182 				String value = UISupport.prompt( "Specify value", "Edit..", elm );
183 				
184 				if( value != null )
185 				{
186 					listModel.setElementAt( value, selectedIndex );
187 					firePropertyChange("options", oldData, getData());
188 				}
189 			}
190 		}
191 	}
192 
193 	public void setButtonState()
194 	{
195 		boolean b = list.getSelectedIndex() != -1;
196 		for( JButton button : buttons )
197 			button.setEnabled( b );
198 	}
199 
200 	public String [] getData()
201 	{
202 		String [] result = new String[listModel.size()];
203 		for( int c = 0; c < result.length; c++ )
204 			result[c] = ( String ) listModel.get( c );
205 		
206 		return result;
207 	}
208 
209 	public void setData( String[] strings )
210 	{
211 		String[] oldData = getData();
212 		
213 		listModel.clear();
214 		if( strings != null )
215 		{
216 			for( String str : strings )
217 			{
218 				listModel.addElement( str );
219 			}
220 		}
221 		
222 		firePropertyChange("options", oldData, getData());
223 	}
224 	
225 	public String [] getOptions()
226 	{
227 		return getData();
228 	}
229 	
230 	public void setOptions( String [] options )
231 	{
232 		setData( options );
233 	}
234 }