View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.x.form.support;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.MouseAdapter;
20  import java.awt.event.MouseEvent;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import javax.swing.AbstractAction;
26  import javax.swing.BorderFactory;
27  import javax.swing.DefaultListModel;
28  import javax.swing.JButton;
29  import javax.swing.JCheckBox;
30  import javax.swing.JList;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.ListCellRenderer;
34  import javax.swing.ListSelectionModel;
35  
36  import com.eviware.soapui.support.UISupport;
37  import com.eviware.soapui.support.components.JXToolBar;
38  import com.eviware.x.form.XFormOptionsField;
39  import com.eviware.x.impl.swing.AbstractSwingXFormField;
40  
41  /***
42   * Swing-Specific multi-select list
43   * 
44   * @author ole.matzura
45   */
46  
47  public class XFormMultiSelectList extends AbstractSwingXFormField<JPanel> implements XFormOptionsField
48  {
49  	private JList list;
50  	private DefaultListModel listModel;
51  	private List<Boolean> selected = new ArrayList<Boolean>();
52  
53  	public XFormMultiSelectList( String[] values )
54  	{
55  		super( new JPanel( new BorderLayout() ) );
56  
57  		listModel = new DefaultListModel();
58  		if (values != null)
59  		{
60  			for (String value : values)
61  			{
62  				selected.add(false);
63  				listModel.addElement(value);
64  			}
65  		}
66  		list = new JList( listModel );
67  		list.setCellRenderer( new CheckListCellRenderer() );
68  		list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
69  		list.addMouseListener( new MouseAdapter()
70  		{
71  			public void mousePressed( MouseEvent e )
72  			{
73  				int index = list.locationToIndex( e.getPoint() );
74  
75  				if( index != -1 )
76  				{
77  					selected.set( index, !selected.get( index ) );
78  					list.repaint();
79  				}
80  			}
81  		} );
82  
83  		getComponent().add( new JScrollPane( list ), BorderLayout.CENTER );
84  		getComponent().add( buildToolbar(), BorderLayout.SOUTH );
85  		getComponent().setSize( new Dimension( 400, 120 ) );
86  		getComponent().setMaximumSize( new Dimension( 400, 120 ) );
87  		getComponent().setPreferredSize( new Dimension( 400, 120 ) );
88  		getComponent().setMinimumSize( new Dimension( 400, 120 ) );
89  	}
90  
91  	private Component buildToolbar()
92  	{
93  		JXToolBar toolbar = UISupport.createSmallToolbar();
94  
95  		toolbar.addFixed( new JButton( new SelectAllAction() ) );
96  		toolbar.addRelatedGap();
97  		toolbar.addFixed( new JButton( new UnselectAllAction() ) );
98  
99  		return toolbar;
100 	}
101 
102 	public String getValue()
103 	{
104 		return String.valueOf( list.getSelectedValue());
105 	}
106 
107 	public void setValue( String value )
108 	{
109 		int index = listModel.indexOf( value );
110 		selected.set( index, true );
111 		list.setSelectedIndex( index );
112 	}
113 
114 	public void addItem( Object value )
115 	{
116 		listModel.addElement( value );
117 		selected.add( false );
118 	}
119 
120 	public Object[] getOptions()
121 	{
122 		Object[] options = new Object[listModel.size()];
123 		for( int c = 0; c < options.length; c++ )
124 			options[c] = listModel.get( c );
125 		return options;
126 	}
127 
128 	public Object[] getSelectedOptions()
129 	{
130 		List<Object> result = new ArrayList<Object>();
131 
132 		for( int c = 0; c < selected.size(); c++ )
133 		{
134 			if( selected.get( c ) )
135 				result.add( listModel.get( c ) );
136 		}
137 
138 		return result.toArray();
139 	}
140 
141 	public void setOptions( Object[] values )
142 	{
143 		listModel.clear();
144 		selected.clear();
145 		for( Object value : values )
146 		{
147 			selected.add( false );
148 			listModel.addElement( value );
149 		}
150 	}
151 
152 	public class CheckListCellRenderer extends JCheckBox implements ListCellRenderer
153 	{
154 		public CheckListCellRenderer()
155 		{
156 			setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
157 		}
158 
159 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
160 				boolean cellHasFocus )
161 		{
162 			setText( value.toString() );
163 			setSelected( selected.get( index ) );
164 
165 			if( isSelected )
166 			{
167 				setBackground( list.getSelectionBackground() );
168 				setForeground( list.getSelectionForeground() );
169 			}
170 			else
171 			{
172 				setBackground( list.getBackground() );
173 				setForeground( list.getForeground() );
174 			}
175 
176 			return this;
177 		}
178 	}
179 
180 	public void setSelectedOptions( Object[] options )
181 	{
182 		List<Object> asList = Arrays.asList( options );
183 
184 		for( int c = 0; c < selected.size(); c++ )
185 		{
186 			selected.set( c, asList.contains( listModel.get( c ) ) );
187 		}
188 
189 		list.repaint();
190 	}
191 
192 	private class SelectAllAction extends AbstractAction
193 	{
194 		public SelectAllAction()
195 		{
196 			super( "Select all" );
197 			putValue( SHORT_DESCRIPTION, "Selects all items in the list" );
198 		}
199 
200 		public void actionPerformed( ActionEvent e )
201 		{
202 			setSelectedOptions( getOptions() );
203 		}
204 	}
205 
206 	private class UnselectAllAction extends AbstractAction
207 	{
208 		public UnselectAllAction()
209 		{
210 			super( "Unselect all" );
211 			putValue( SHORT_DESCRIPTION, "Unselects all items in the list" );
212 		}
213 
214 		public void actionPerformed( ActionEvent e )
215 		{
216 			setSelectedOptions( new String[0] );
217 		}
218 	}
219 
220 	public int[] getSelectedIndexes()
221 	{
222 		int cnt = 0;
223 
224 		for( int c = 0; c < selected.size(); c++ )
225 		{
226 			if( selected.get( c ) )
227 				cnt++ ;
228 		}
229 
230 		int[] result = new int[cnt];
231 		cnt = 0;
232 
233 		for( int c = 0; c < selected.size(); c++ )
234 		{
235 			if( selected.get( c ) )
236 				result[cnt++ ] = c;
237 		}
238 
239 		return result;
240 	}
241 }