View Javadoc

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