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.impl.swing;
    14
    15  import java.awt.event.ItemEvent;
    16  import java.awt.event.ItemListener;
    17
    18  import javax.swing.ComboBoxModel;
    19  import javax.swing.DefaultComboBoxModel;
    20  import javax.swing.JComboBox;
    21
    22  import com.eviware.x.form.XFormOptionsField;
    23
    24  public class JComboBoxFormField extends AbstractSwingXFormField<JComboBox> implements ItemListener, XFormOptionsField
    25  {
    26  	public JComboBoxFormField(Object[] values)
    27  	{
    28  		super( new JComboBox() );
    29
    30  		setOptions( values );
    31
    32  		getComponent().addItemListener( this );
    33  	}
    34
    35  	public void setValue(String value)
    36  	{
    37  		getComponent().setSelectedItem( value );
    38  	}
    39
    40  	public String getValue()
    41  	{
    42  		Object selectedItem = getComponent().getSelectedItem();
    43  		return selectedItem == null ? null : selectedItem.toString();
    44  	}
    45
    46  	public void itemStateChanged(ItemEvent e)
    47  	{
    48  		Object selectedItem = getComponent().getSelectedItem();
    49  		fireValueChanged( selectedItem == null ? null : selectedItem.toString(), null );
    50  	}
    51
    52     public void addItem(String value)
    53     {
    54        getComponent().addItem(value);
    55     }
    56
    57  	public void setOptions(Object[] values)
    58  	{
    59  		String selectedItem = getValue();
    60  		DefaultComboBoxModel model = new DefaultComboBoxModel( values );
    61
    62  		if( values.length > 0 && values[0] == null )
    63  		{
    64  			model.removeElementAt( 0 );
    65  			getComponent().setEditable( true );
    66  		}
    67  		else
    68  		{
    69  			getComponent().setEditable( false );
    70  		}
    71
    72  		getComponent().setModel( model);
    73
    74  		if( selectedItem != null )
    75  			getComponent().setSelectedItem( selectedItem );
    76  		else if( getComponent().isEditable() )
    77  			getComponent().setSelectedItem( "" );
    78  	}
    79
    80  	public String[] getOptions()
    81  	{
    82  		ComboBoxModel model = getComponent().getModel();
    83
    84  		String [] result = new String[model.getSize()];
    85  		for( int c = 0; c < result.length; c++ )
    86  			result[c] = model.getElementAt( c ).toString();
    87
    88  		return result;
    89  	}
    90
    91  	public String[] getSelectedOptions()
    92  	{
    93  		return new String[] { getValue() };
    94  	}
    95
    96  	public void setSelectedOptions( String[] options )
    97  	{
    98  		setValue( options.length > 0 ? options[0] : null );
    99  	}
    100
    101 	public int[] getSelectedIndexes()
    102 	{
    103 		return new int[] {getComponent().getSelectedIndex()};
    104 	}
    105 }