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.impl.wsdl.panels.request;
    14
    15  import java.util.ArrayList;
    16  import java.util.List;
    17
    18  import javax.swing.table.AbstractTableModel;
    19  import javax.swing.table.TableModel;
    20
    21  import com.eviware.soapui.support.types.StringToStringMap;
    22
    23  /***
    24   * TableModel for StringToString Maps
    25   *
    26   * @author ole.matzura
    27   */
    28
    29  public class StringToStringMapTableModel extends AbstractTableModel implements TableModel
    30  {
    31  	private StringToStringMap data;
    32  	private final String keyCaption;
    33  	private final String valueCaption;
    34  	private List<String> keyList;
    35  	private final boolean editable;
    36
    37  	public StringToStringMapTableModel( StringToStringMap data, String keyCaption, String valueCaption, boolean editable )
    38  	{
    39  		this.data = data;
    40  		this.keyCaption = keyCaption;
    41  		this.valueCaption = valueCaption;
    42  		this.editable = editable;
    43
    44  		keyList = data == null ? new ArrayList<String>() : new ArrayList<String>( data.keySet() );
    45  	}
    46
    47  	public int getColumnCount()
    48  	{
    49  		return 2;
    50  	}
    51
    52  	public String getColumnName( int arg0 )
    53  	{
    54  		return arg0 == 0 ? keyCaption : valueCaption;
    55  	}
    56
    57  	public boolean isCellEditable( int arg0, int arg1 )
    58  	{
    59  		return editable;
    60  	}
    61
    62  	public Class<?> getColumnClass( int arg0 )
    63  	{
    64  		return String.class;
    65  	}
    66
    67  	public void setValueAt( Object arg0, int arg1, int arg2 )
    68  	{
    69  		String oldKey = keyList.get( arg1 );
    70
    71  		if( arg2 == 0 )
    72  		{
    73  			String value = data.get( oldKey );
    74
    75  			data.remove( oldKey );
    76  			data.put( arg0.toString(), value );
    77
    78  			keyList.set( arg1, arg0.toString() );
    79  		}
    80  		else
    81  		{
    82  			data.put( oldKey, arg0.toString() );
    83  		}
    84
    85  		fireTableCellUpdated( arg1, arg2 );
    86  	}
    87
    88  	public int getRowCount()
    89  	{
    90  		return data == null ? 0 : data.size();
    91  	}
    92
    93  	public Object getValueAt( int arg0, int arg1 )
    94  	{
    95  		String str = keyList.get( arg0 );
    96  		return arg1 == 0 ? str : data.get( str);
    97  	}
    98
    99  	public void add( String key, String value )
    100 	{
    101 		if( keyList.contains( key ))
    102 		{
    103 			data.put( key, value );
    104 			fireTableCellUpdated( keyList.indexOf( key ), 1 );
    105 		}
    106 		else
    107 		{
    108 			data.put( key, value );
    109 			keyList.add( key );
    110 			fireTableRowsInserted( keyList.size()-1, keyList.size()-1 );
    111 		}
    112 	}
    113
    114 	public void remove( int row )
    115 	{
    116 		String key = keyList.get( row );
    117 		keyList.remove( row );
    118 		data.remove( key );
    119
    120 		fireTableRowsDeleted( row, row );
    121 	}
    122
    123 	public StringToStringMap getData()
    124 	{
    125 		return new StringToStringMap( data );
    126 	}
    127
    128 	public void setData( StringToStringMap data )
    129 	{
    130 		this.data = data == null ? new StringToStringMap() : data;
    131
    132 		keyList = new ArrayList<String>( this.data.keySet() );
    133 		fireTableDataChanged();
    134 	}
    135 }