View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.StringToStringsMap;
22  
23  /***
24   * TableModel for StringToString Maps
25   * 
26   * @author ole.matzura
27   */
28  
29  public class StringToStringsMapTableModel extends AbstractTableModel implements TableModel
30  {
31  	private final String keyCaption;
32  	private final String valueCaption;
33  	private List<NameValuePair> keyList = new ArrayList<NameValuePair>();
34  	private final boolean editable;
35  	private StringToStringsMap data;
36  
37  	public StringToStringsMapTableModel( StringToStringsMap data, String keyCaption, String valueCaption,
38  			boolean editable )
39  	{
40  		this.data = data;
41  		this.keyCaption = keyCaption;
42  		this.valueCaption = valueCaption;
43  		this.editable = editable;
44  
45  		setData( data );
46  	}
47  
48  	public int getColumnCount()
49  	{
50  		return 2;
51  	}
52  
53  	public String getColumnName( int arg0 )
54  	{
55  		return arg0 == 0 ? keyCaption : valueCaption;
56  	}
57  
58  	public boolean isCellEditable( int arg0, int arg1 )
59  	{
60  		return editable;
61  	}
62  
63  	public Class<?> getColumnClass( int arg0 )
64  	{
65  		return String.class;
66  	}
67  
68  	public void setValueAt( Object arg0, int arg1, int arg2 )
69  	{
70  		NameValuePair nvpair = keyList.get( arg1 );
71  
72  		// change name?
73  		if( arg2 == 0 )
74  		{
75  			data.get( nvpair.getKey() ).remove( nvpair.getIndex() );
76  			nvpair.setKey( String.valueOf( arg0 ) );
77  			data.put( nvpair.getKey(), nvpair.getIndex() );
78  
79  		}
80  		else if( arg2 == 1 )
81  		{
82  			data.replace( nvpair.getKey(), nvpair.getIndex(), String.valueOf( arg0 ) );
83  			nvpair.setValue( String.valueOf( arg0 ) );
84  		}
85  
86  		fireTableCellUpdated( arg1, arg2 );
87  	}
88  
89  	public int getRowCount()
90  	{
91  		return keyList.size();
92  	}
93  
94  	public Object getValueAt( int arg0, int arg1 )
95  	{
96  		return arg1 == 0 ? keyList.get( arg0 ).getKey() : keyList.get( arg0 ).getIndex();
97  	}
98  
99  	public void add( String key, String value )
100 	{
101 		data.add( key, value );
102 		keyList.add( new NameValuePair( key, value ) );
103 		fireTableRowsInserted( keyList.size() - 1, keyList.size() - 1 );
104 	}
105 
106 	public void remove( int row )
107 	{
108 		NameValuePair key = keyList.get( row );
109 		keyList.remove( row );
110 		data.remove( key.getKey(), key.getIndex() );
111 
112 		fireTableRowsDeleted( row, row );
113 	}
114 
115 	public StringToStringsMap getData()
116 	{
117 		return new StringToStringsMap( data );
118 	}
119 
120 	public synchronized void setData( StringToStringsMap newData )
121 	{
122 		data = newData == null ? new StringToStringsMap() : new StringToStringsMap( newData );
123 
124 		keyList.clear();
125 		for( String key : data.keySet() )
126 		{
127 			for( String value : data.get( key ) )
128 				keyList.add( new NameValuePair( key, value ) );
129 		}
130 
131 		fireTableDataChanged();
132 	}
133 
134 	private class NameValuePair
135 	{
136 		private String key;
137 		private String value;
138 
139 		public NameValuePair( String key, String value )
140 		{
141 			super();
142 			this.key = key;
143 			this.value = value;
144 		}
145 
146 		public void setKey( String key )
147 		{
148 			this.key = key;
149 		}
150 
151 		public String getKey()
152 		{
153 			return key;
154 		}
155 
156 		public void setValue( String value )
157 		{
158 			this.value = value;
159 		}
160 
161 		public String getIndex()
162 		{
163 			return value;
164 		}
165 	}
166 }