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.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  		for( String value : values )
    59  		{
    60  			selected.add( false );
    61  			listModel.addElement( value );
    62  		}
    63
    64  		list = new JList( listModel );
    65  		list.setCellRenderer( new CheckListCellRenderer() );
    66  		list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    67  		list.addMouseListener(new MouseAdapter()
    68        {
    69           public void mousePressed(MouseEvent e)
    70           {
    71              int index = list.locationToIndex(e.getPoint());
    72
    73              if (index != -1)
    74              {
    75              	selected.set( index, !selected.get(  index ));
    76                 list.repaint();
    77              }
    78           }
    79        });
    80
    81  		getComponent().add( new JScrollPane( list ), BorderLayout.CENTER );
    82  		getComponent().add( buildToolbar(), BorderLayout.SOUTH );
    83  		getComponent().setSize( new Dimension( 400, 120 ) );
    84  		getComponent().setMaximumSize( new Dimension( 400, 120 ) );
    85  		getComponent().setPreferredSize( new Dimension( 400, 120 ) );
    86  		getComponent().setMinimumSize( new Dimension( 400, 120 ) );
    87  	}
    88
    89  	private Component buildToolbar()
    90  	{
    91  		JXToolBar toolbar = UISupport.createSmallToolbar();
    92
    93  		toolbar.addFixed( new JButton( new SelectAllAction() ));
    94  		toolbar.addRelatedGap();
    95  		toolbar.addFixed( new JButton( new UnselectAllAction() ));
    96
    97  		return toolbar;
    98  	}
    99
    100 	public String getValue()
    101 	{
    102 		return ( String ) list.getSelectedValue();
    103 	}
    104
    105 	public void setValue( String value )
    106 	{
    107 		int index = listModel.indexOf( value );
    108 		selected.set( index, true );
    109 		list.setSelectedIndex( index );
    110 	}
    111
    112 	public void addItem( String value )
    113 	{
    114 		listModel.addElement( value );
    115 		selected.add(  false );
    116 	}
    117
    118 	public String[] getOptions()
    119 	{
    120 		String [] options = new String[listModel.size()];
    121 		for( int c = 0; c < options.length; c++ )
    122 			options[c] = ( String ) listModel.get( c );
    123 		return options;
    124 	}
    125
    126 	public String[] getSelectedOptions()
    127 	{
    128 		List<String> result = new ArrayList<String>();
    129
    130 		for( int c = 0; c < selected.size(); c++ )
    131 		{
    132 			if( selected.get( c ))
    133 				result.add( ( String ) listModel.get( c ));
    134 		}
    135
    136 		return result.toArray( new String[result.size()] );
    137 	}
    138
    139 	public void setOptions( Object[] values )
    140 	{
    141 		listModel.clear();
    142 		selected.clear();
    143 		for( Object value : values )
    144 		{
    145 			selected.add( false );
    146 			listModel.addElement( value );
    147 		}
    148 	}
    149
    150 	public class CheckListCellRenderer extends JCheckBox implements ListCellRenderer
    151 	{
    152 		public CheckListCellRenderer()
    153 		{
    154 			setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3  ) );
    155 		}
    156
    157 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
    158 					boolean cellHasFocus )
    159 		{
    160 			setText( value.toString() );
    161 			setSelected( selected.get( index ) );
    162
    163 			if (isSelected)
    164 			{
    165 				setBackground(list.getSelectionBackground());
    166 				setForeground(list.getSelectionForeground());
    167 			}
    168 			else
    169 			{
    170 				setBackground(list.getBackground());
    171 				setForeground(list.getForeground());
    172 			}
    173
    174 			return this;
    175 		}
    176 	}
    177
    178 	public void setSelectedOptions( String[] options )
    179 	{
    180 		List<String> asList = Arrays.asList( options );
    181
    182 		for( int c = 0; c < selected.size(); c++ )
    183 		{
    184 			selected.set( c, asList.contains( listModel.get( c )));
    185 		}
    186
    187 		list.repaint();
    188 	}
    189
    190 	private class SelectAllAction extends AbstractAction
    191 	{
    192 		public SelectAllAction()
    193 		{
    194 			super( "Select all" );
    195 			putValue( SHORT_DESCRIPTION, "Selects all items in the list" );
    196 		}
    197
    198 		public void actionPerformed( ActionEvent e )
    199 		{
    200 			setSelectedOptions( getOptions() );
    201 		}
    202 	}
    203
    204 	private class UnselectAllAction extends AbstractAction
    205 	{
    206 		public UnselectAllAction()
    207 		{
    208 			super( "Unselect all" );
    209 			putValue( SHORT_DESCRIPTION, "Unselects all items in the list" );
    210 		}
    211
    212 		public void actionPerformed( ActionEvent e )
    213 		{
    214 			setSelectedOptions( new String[0] );
    215 		}
    216 	}
    217
    218 	public int[] getSelectedIndexes()
    219 	{
    220 		int cnt = 0;
    221
    222 		for( int c = 0; c < selected.size(); c++ )
    223 		{
    224 			if( selected.get( c ))
    225 				cnt++;
    226 		}
    227
    228 		int [] result = new int[cnt];
    229 		cnt = 0;
    230
    231 		for( int c = 0; c < selected.size(); c++ )
    232 		{
    233 			if( selected.get( c ))
    234 				result[cnt++] = c;
    235 		}
    236
    237 		return result;
    238 	}
    239 }