View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / 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.support;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.Font;
19  import java.beans.PropertyChangeEvent;
20  import java.beans.PropertyChangeListener;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.JComponent;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTable;
29  import javax.swing.border.TitledBorder;
30  import javax.swing.table.AbstractTableModel;
31  import javax.swing.table.DefaultTableCellRenderer;
32  
33  import org.apache.commons.beanutils.PropertyUtils;
34  
35  /***
36   * Table for displaying property name/value pairs
37   * 
38   * @author Ole.Matzura
39   */
40  
41  public class PropertiesTable extends JPanel
42  {
43  	private PropertiesTableModel tableModel;
44  
45  	public PropertiesTable( String title, Object propertyObject )
46  	{
47  		super( new BorderLayout());
48  		
49  		tableModel = new PropertiesTableModel( propertyObject );
50  		JTable table = new JTable( tableModel );
51        table.getColumnModel().getColumn(0).setHeaderValue( "Property" );
52        table.getColumnModel().getColumn(1).setHeaderValue( "Value" );
53        table.getColumnModel().getColumn(1).setCellRenderer( new PropertiesTableCellRenderer() );
54        
55        add( new JScrollPane( table ), BorderLayout.CENTER );
56        TitledBorder titledBorder = BorderFactory.createTitledBorder( BorderFactory.createEmptyBorder(), title );
57        titledBorder.setTitleFont( titledBorder.getTitleFont().deriveFont( Font.PLAIN, 11 ));
58  		setBorder( titledBorder);
59  		
60  		table.setBackground( Color.WHITE );
61  	}
62  	
63  	public PropertiesTableModel getTableModel()
64  	{
65  		return tableModel;
66  	}
67  
68  	public void addProperty( String caption, String name)
69  	{
70  		addProperty( caption, name, false  );
71  	}
72  	
73  	public void addProperty( String caption, String name, boolean editable )
74  	{
75  		tableModel.addProperty( caption, name, editable  );
76  	}
77  	
78  	private class PropertiesTableModel extends AbstractTableModel implements PropertyChangeListener 
79     {
80  		private List<PropertyDescriptor> properties = new ArrayList<PropertyDescriptor>();
81  		private final Object propertyObject;
82  		
83  		public PropertiesTableModel(Object propertyObject)
84  		{
85  			this.propertyObject = propertyObject;
86  		}
87  		
88  		public void addProperty( String caption, String name, boolean editable )
89  		{
90  			properties.add( new PropertyDescriptor( caption, name, editable ));
91  		}
92  		
93  		public int getRowCount()
94  		{
95  			return properties.size();
96  		}
97  
98  		public int getColumnCount()
99  		{
100 			return 2;
101 		}
102 		
103 		public boolean isCellEditable(int rowIndex, int columnIndex)
104 		{
105 			if( columnIndex == 0 ) return false;
106 			return properties.get( rowIndex ).isEditable() && 
107 				PropertyUtils.isWriteable( propertyObject, properties.get( rowIndex ).getName() );
108 		}
109 		
110 		public void setValueAt(Object aValue, int rowIndex, int columnIndex)
111 		{
112 			try
113 			{
114 				if( columnIndex == 1 && properties.get( rowIndex ).isEditable() )
115 					PropertyUtils.setSimpleProperty( propertyObject, properties.get( rowIndex ).getName(), aValue );
116 			}
117 			catch ( Exception e)
118 			{
119 				e.printStackTrace();
120 			}
121 		}
122 
123 		public Object getValueAt(int rowIndex, int columnIndex)
124 		{
125 			try
126 			{
127 				switch (columnIndex)
128 				{
129 				case 0:
130 					return properties.get( rowIndex ).getCaption();
131 				case 1:
132 					return PropertyUtils.getSimpleProperty(propertyObject,
133 							properties.get(rowIndex).getName());
134 				}
135 			}
136 			catch (Exception e)
137 			{
138 				e.printStackTrace();
139 				return e.getMessage();
140 			}
141 			
142 			return null;
143 		}
144 
145 		public void propertyChange(PropertyChangeEvent evt)
146 		{
147 			String name = evt.getPropertyName();
148 			if( name.lastIndexOf( '@') > 0 ) 
149 				name = name.substring( name.lastIndexOf( '@') + 1);
150 			
151 			for( int c = 0; c < properties.size(); c++ )
152 			{
153 				if( properties.get( c ).getName().equals( name ))
154 				{
155 					fireTableCellUpdated( c, 1 );
156 					return;
157 				}
158 			}
159 		}}
160 	
161 	private class PropertyDescriptor
162 	{
163 		private final String caption;
164 		private final String name;
165 		private final boolean editable;
166 
167 		public PropertyDescriptor(String caption, String name, boolean editable)
168 		{
169 			this.caption = caption;
170 			this.name = name;
171 			this.editable = editable;
172 		}
173 
174 		public String getCaption()
175 		{
176 			return caption;
177 		}
178 
179 		public boolean isEditable()
180 		{
181 			return editable;
182 		}
183 
184 		public String getName()
185 		{
186 			return name;
187 		}
188 	}
189 
190 	private class PropertiesTableCellRenderer extends DefaultTableCellRenderer
191 	{
192 		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
193 		{
194 			Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
195 					row, column);
196 			
197 			if( component instanceof JComponent && value != null )
198 				((JComponent)component).setToolTipText( value.toString() );
199 			
200 			return component;
201 		}
202 		
203 	}
204 }