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.util.ArrayList;
    16  import java.util.Arrays;
    17  import java.util.List;
    18
    19  import javax.swing.AbstractListModel;
    20  import javax.swing.ComboBoxModel;
    21
    22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
    23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepListenerAdapter;
    24
    25  public class TestStepPropertyComboBoxModel extends AbstractListModel implements ComboBoxModel
    26  {
    27  	private WsdlTestStep testStep;
    28  	private List<String> names;
    29  	private String selectedName;
    30  	private InternalTestStepListener testStepListener = new InternalTestStepListener();
    31
    32  	public TestStepPropertyComboBoxModel( WsdlTestStep testStep )
    33  	{
    34  		this.testStep = testStep;
    35
    36  		names = new ArrayList<String>();
    37
    38  		if( testStep != null )
    39  		{
    40  			names.addAll( Arrays.asList( testStep.getPropertyNames() ));
    41  			testStep.addTestStepListener( testStepListener );
    42  		}
    43  	}
    44
    45  	public void release()
    46  	{
    47  		if( testStep != null )
    48  			testStep.removeTestStepListener( testStepListener );
    49  	}
    50
    51  	public WsdlTestStep getTestStep()
    52  	{
    53  		return testStep;
    54  	}
    55
    56  	public void setTestStep( WsdlTestStep testStep )
    57  	{
    58  		if( this.testStep != null )
    59  		{
    60  			this.testStep.removeTestStepListener( testStepListener );
    61  		}
    62
    63  		int sz = names.size();
    64  		if( sz > 0 )
    65  		{
    66  			names.clear();
    67  			fireIntervalRemoved( this, 0, sz-1 );
    68  		}
    69
    70  		this.testStep = testStep;
    71  		if( testStep != null )
    72  		{
    73  			testStep.addTestStepListener( testStepListener );
    74  			names.addAll( Arrays.asList( testStep.getPropertyNames() ));
    75  			if( !names.isEmpty())
    76  			{
    77  				fireIntervalAdded( this, 0, names.size()-1 );
    78  			}
    79  		}
    80
    81  		setSelectedItem( null );
    82  	}
    83
    84  	public Object getElementAt( int index )
    85  	{
    86  		return names.get( index );
    87  	}
    88
    89  	public int getSize()
    90  	{
    91  		return names.size();
    92  	}
    93
    94  	private final class InternalTestStepListener extends WsdlTestStepListenerAdapter
    95  	{
    96  		@Override
    97  		public void propertyAdded( String name )
    98  		{
    99  			names.add( name );
    100 			fireIntervalAdded( TestStepPropertyComboBoxModel.this, names.size()-1, names.size()-1 );
    101 		}
    102
    103 		@Override
    104 		public void propertyRemoved( String name )
    105 		{
    106 			int ix = names.indexOf( name );
    107 			if( ix >= 0 )
    108 			{
    109 				names.remove( ix );
    110 				fireIntervalRemoved( TestStepPropertyComboBoxModel.this, ix, ix );
    111
    112 				if( name.equals( selectedName ))
    113 					setSelectedItem( null );
    114 			}
    115 		}
    116
    117 		@Override
    118 		public void propertyRenamed( String oldName, String newName )
    119 		{
    120 			int ix = names.indexOf( oldName );
    121 			fireContentsChanged( TestStepPropertyComboBoxModel.this, ix, ix );
    122
    123 			if( oldName.equals( selectedName ))
    124 				setSelectedItem( newName );
    125 		}
    126 	}
    127
    128 	public Object getSelectedItem()
    129 	{
    130 		return selectedName;
    131 	}
    132
    133 	public void setSelectedItem( Object anItem )
    134 	{
    135 		if( anItem == null && selectedName == null )
    136 			return;
    137
    138 		if( anItem != null && selectedName != null && anItem.equals( selectedName))
    139 			return;
    140
    141 		selectedName = anItem == null ? null : anItem.toString();
    142
    143 		fireContentsChanged( this, -1, -1 );
    144 	}
    145 }