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.rest.panels.resource;
14  
15  import javax.swing.table.AbstractTableModel;
16  import javax.swing.table.TableModel;
17  
18  import com.eviware.soapui.impl.rest.support.RestParamProperty;
19  import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder;
20  import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder.ParameterStyle;
21  import com.eviware.soapui.model.testsuite.TestPropertyListener;
22  
23  public class RestParamsTableModel extends AbstractTableModel implements TableModel, TestPropertyListener
24  {
25  	protected RestParamsPropertyHolder params;
26  
27  	public RestParamsTableModel( RestParamsPropertyHolder params )
28  	{
29  		this.params = params;
30  
31  		params.addTestPropertyListener( this );
32  	}
33  
34  	public void release()
35  	{
36  		params.removeTestPropertyListener( this );
37  	}
38  
39  	public int getColumnCount()
40  	{
41  		return 3;
42  	}
43  
44  	@Override
45  	public String getColumnName( int column )
46  	{
47  		switch( column )
48  		{
49  		case 0 :
50  			return "Name";
51  		case 1 :
52  			return "Default value";
53  		case 2 :
54  			return "Style";
55  		}
56  
57  		return null;
58  	}
59  
60  	@Override
61  	public Class<?> getColumnClass( int columnIndex )
62  	{
63  		return columnIndex < 2 ? String.class : ParameterStyle.class;
64  	}
65  
66  	@Override
67  	public boolean isCellEditable( int rowIndex, int columnIndex )
68  	{
69  		return true;
70  	}
71  
72  	public int getRowCount()
73  	{
74  		return params.getPropertyCount();
75  	}
76  
77  	public Object getValueAt( int rowIndex, int columnIndex )
78  	{
79  		RestParamProperty prop = params.getPropertyAt( rowIndex );
80  
81  		switch( columnIndex )
82  		{
83  		case 0 :
84  			return prop.getName();
85  		case 1 :
86  			return prop.getDefaultValue();
87  			// case 1 : return StringUtils.hasContent(prop.getValue()) ?
88  			// prop.getValue() : prop.getDefaultValue();
89  		case 2 :
90  			return prop.getStyle();
91  		}
92  
93  		return null;
94  	}
95  
96  	@Override
97  	public void setValueAt( Object value, int rowIndex, int columnIndex )
98  	{
99  		RestParamProperty prop = params.getPropertyAt( rowIndex );
100 
101 		switch( columnIndex )
102 		{
103 		case 0 :
104 			params.renameProperty( prop.getName(), value.toString() );
105 			return;
106 		case 1 :
107 			prop.setDefaultValue( value.toString() );
108 			prop.setValue( value.toString() );
109 			return;
110 		case 2 :
111 			prop.setStyle( ( ParameterStyle )value );
112 			return;
113 		}
114 	}
115 
116 	public RestParamProperty getParameterAt( int selectedRow )
117 	{
118 		return params.getPropertyAt( selectedRow );
119 	}
120 
121 	public void propertyAdded( String name )
122 	{
123 		fireTableDataChanged();
124 	}
125 
126 	public void propertyRemoved( String name )
127 	{
128 		fireTableDataChanged();
129 	}
130 
131 	public void propertyRenamed( String oldName, String newName )
132 	{
133 		fireTableDataChanged();
134 	}
135 
136 	public void propertyValueChanged( String name, String oldValue, String newValue )
137 	{
138 		fireTableCellUpdated( params.getPropertyIndex( name ), 1 );
139 	}
140 
141 	public void propertyMoved( String name, int oldIndex, int newIndex )
142 	{
143 		fireTableDataChanged();
144 	}
145 
146 	public void setParams( RestParamsPropertyHolder params )
147 	{
148 		this.params.removeTestPropertyListener( this );
149 		this.params = params;
150 		this.params.addTestPropertyListener( this );
151 
152 		fireTableDataChanged();
153 	}
154 }