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.MouseAdapter;
    19  import java.awt.event.MouseEvent;
    20  import java.util.ArrayList;
    21  import java.util.Arrays;
    22  import java.util.List;
    23
    24  import javax.swing.BorderFactory;
    25  import javax.swing.DefaultListModel;
    26  import javax.swing.JCheckBox;
    27  import javax.swing.JList;
    28  import javax.swing.JPanel;
    29  import javax.swing.JScrollPane;
    30  import javax.swing.ListCellRenderer;
    31  import javax.swing.ListSelectionModel;
    32
    33  import com.eviware.x.form.XFormOptionsField;
    34  import com.eviware.x.impl.swing.AbstractSwingXFormField;
    35
    36  /***
    37   * Swing-Specific multi-select list
    38   *
    39   * @author ole.matzura
    40   */
    41
    42  public class XFormMultiSelectList extends AbstractSwingXFormField<JPanel> implements XFormOptionsField
    43  {
    44  	private JList list;
    45  	private DefaultListModel listModel;
    46  	private List<Boolean> selected = new ArrayList<Boolean>();
    47
    48  	public XFormMultiSelectList( String [] values )
    49  	{
    50  		super( new JPanel( new BorderLayout() ) );
    51
    52  		listModel = new DefaultListModel();
    53  		for( String value : values )
    54  		{
    55  			selected.add( false );
    56  			listModel.addElement( value );
    57  		}
    58
    59  		list = new JList( listModel );
    60  		list.setCellRenderer( new CheckListCellRenderer() );
    61  		list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    62  		list.addMouseListener(new MouseAdapter()
    63        {
    64           public void mousePressed(MouseEvent e)
    65           {
    66              int index = list.locationToIndex(e.getPoint());
    67
    68              if (index != -1)
    69              {
    70              	selected.set( index, !selected.get(  index ));
    71                 list.repaint();
    72              }
    73           }
    74        });
    75
    76  		getComponent().add( new JScrollPane( list ), BorderLayout.CENTER );
    77  		getComponent().setSize( new Dimension( 400, 120 ) );
    78  		getComponent().setMaximumSize( new Dimension( 400, 120 ) );
    79  		getComponent().setPreferredSize( new Dimension( 400, 120 ) );
    80  		getComponent().setMinimumSize( new Dimension( 400, 120 ) );
    81  	}
    82
    83  	public String getValue()
    84  	{
    85  		return ( String ) list.getSelectedValue();
    86  	}
    87
    88  	public void setValue( String value )
    89  	{
    90  		int index = listModel.indexOf( value );
    91  		selected.set( index, true );
    92  		list.setSelectedIndex( index );
    93  	}
    94
    95  	public void addItem( String value )
    96  	{
    97  		listModel.addElement( value );
    98  		selected.add(  false );
    99  	}
    100
    101 	public String[] getOptions()
    102 	{
    103 		String [] options = new String[listModel.size()];
    104 		for( int c = 0; c < options.length; c++ )
    105 			options[c] = ( String ) listModel.get( c );
    106 		return options;
    107 	}
    108
    109 	public String[] getSelectedOptions()
    110 	{
    111 		List<String> result = new ArrayList<String>();
    112
    113 		for( int c = 0; c < selected.size(); c++ )
    114 		{
    115 			if( selected.get( c ))
    116 				result.add( ( String ) listModel.get( c ));
    117 		}
    118
    119 		return result.toArray( new String[result.size()] );
    120 	}
    121
    122 	public void setOptions( Object[] values )
    123 	{
    124 		listModel.clear();
    125 		selected.clear();
    126 		for( Object value : values )
    127 		{
    128 			selected.add( false );
    129 			listModel.addElement( value );
    130 		}
    131 	}
    132
    133 	public class CheckListCellRenderer extends JCheckBox implements ListCellRenderer
    134 	{
    135 		public CheckListCellRenderer()
    136 		{
    137 			setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3  ) );
    138 		}
    139
    140 		public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
    141 					boolean cellHasFocus )
    142 		{
    143 			setText( value.toString() );
    144 			setSelected( selected.get( index ) );
    145
    146 			if (isSelected)
    147 			{
    148 				setBackground(list.getSelectionBackground());
    149 				setForeground(list.getSelectionForeground());
    150 			}
    151 			else
    152 			{
    153 				setBackground(list.getBackground());
    154 				setForeground(list.getForeground());
    155 			}
    156
    157 			return this;
    158 		}
    159 	}
    160
    161 	public void setSelectedOptions( String[] options )
    162 	{
    163 		List<String> asList = Arrays.asList( options );
    164
    165 		for( int c = 0; c < selected.size(); c++ )
    166 		{
    167 			selected.set( c, asList.contains( listModel.get( c )));
    168 		}
    169
    170 		list.repaint();
    171 	}
    172 }