View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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  public class StringToStringMapTableModel extends AbstractTableModel implements TableModel
24  {
25  	private StringToStringMap data;
26  	private final String keyCaption;
27  	private final String valueCaption;
28  	private List<String> keyList;
29  	private final boolean editable;
30  
31  	public StringToStringMapTableModel( StringToStringMap data, String keyCaption, String valueCaption, boolean editable )
32  	{
33  		this.data = data;
34  		this.keyCaption = keyCaption;
35  		this.valueCaption = valueCaption;
36  		this.editable = editable;
37  		
38  		keyList = data == null ? new ArrayList<String>() : new ArrayList<String>( data.keySet() );
39  	}
40  
41  	public int getColumnCount()
42  	{
43  		return 2;
44  	}
45  	
46  	public String getColumnName( int arg0 )
47  	{
48  		return arg0 == 0 ? keyCaption : valueCaption;
49  	}
50  
51  	public boolean isCellEditable( int arg0, int arg1 )
52  	{
53  		return editable;
54  	}
55  	
56  	public Class<?> getColumnClass( int arg0 )
57  	{
58  		return String.class;
59  	}
60  
61  	public void setValueAt( Object arg0, int arg1, int arg2 )
62  	{
63  		String oldKey = keyList.get( arg1 );
64  		
65  		if( arg2 == 0 )
66  		{
67  			String value = data.get( oldKey );
68  			
69  			data.remove( oldKey );
70  			data.put( arg0.toString(), value );
71  			
72  			keyList.set( arg1, arg0.toString() );
73  		}
74  		else
75  		{
76  			data.put( oldKey, arg0.toString() );
77  		}
78  
79  		fireTableCellUpdated( arg1, arg2 );
80  	}
81  
82  	public int getRowCount()
83  	{
84  		return data == null ? 0 : data.size();
85  	}
86  
87  	public Object getValueAt( int arg0, int arg1 )
88  	{
89  		String str = keyList.get( arg0 );
90  		return arg1 == 0 ? str : data.get( str);
91  	}
92  
93  	public void add( String key, String value )
94  	{
95  		if( keyList.contains( key ))
96  		{
97  			data.put( key, value );
98  			fireTableCellUpdated( keyList.indexOf( key ), 1 );
99  		}
100 		else
101 		{
102 			data.put( key, value );
103 			keyList.add( key );
104 			fireTableRowsInserted( keyList.size(), keyList.size() );
105 		}
106 	}
107 
108 	public void remove( int row )
109 	{
110 		String key = keyList.get( row );
111 		keyList.remove( row );
112 		data.remove( key );
113 		
114 		fireTableRowsDeleted( row, row );
115 	}
116 
117 	public StringToStringMap getData()
118 	{
119 		return new StringToStringMap( data );
120 	}
121 
122 	public void setData( StringToStringMap data )
123 	{
124 		this.data = data == null ? new StringToStringMap() : data;
125 		
126 		keyList = new ArrayList<String>( this.data.keySet() );
127 		fireTableDataChanged();
128 	}
129 }