View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 			prop.setName( value.toString() );
105 			return;
106 		case 1 :
107 			prop.setDefaultValue( value.toString() );
108 			return;
109 		case 2 :
110 			prop.setStyle( ( ParameterStyle )value );
111 			return;
112 		}
113 	}
114 
115 	public RestParamProperty getParameterAt( int selectedRow )
116 	{
117 		return params.getPropertyAt( selectedRow );
118 	}
119 
120 	public void propertyAdded( String name )
121 	{
122 		fireTableDataChanged();
123 	}
124 
125 	public void propertyRemoved( String name )
126 	{
127 		fireTableDataChanged();
128 	}
129 
130 	public void propertyRenamed( String oldName, String newName )
131 	{
132 		fireTableDataChanged();
133 	}
134 
135 	public void propertyValueChanged( String name, String oldValue, String newValue )
136 	{
137 		fireTableCellUpdated( params.getPropertyIndex( name ), 1 );
138 	}
139 
140 	public void propertyMoved( String name, int oldIndex, int newIndex )
141 	{
142 		fireTableDataChanged();
143 	}
144 
145 	public void setParams( RestParamsPropertyHolder params )
146 	{
147 		this.params.removeTestPropertyListener( this );
148 		this.params = params;
149 		this.params.addTestPropertyListener( this );
150 		
151 		fireTableDataChanged();
152 	}
153 }