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.support.components;
14  
15  import java.awt.Color;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.Font;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.swing.BorderFactory;
23  import javax.swing.ImageIcon;
24  import javax.swing.JComponent;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTable;
29  import javax.swing.SwingConstants;
30  import javax.swing.table.DefaultTableCellRenderer;
31  import javax.swing.table.TableModel;
32  
33  import org.jdesktop.swingx.JXTable;
34  import org.jdesktop.swingx.VerticalLayout;
35  
36  import com.eviware.soapui.support.UISupport;
37  
38  public class MetricsPanel extends JPanel
39  {
40  	private Map<String, Metric> metrics = new HashMap<String, Metric>();
41  	private Map<String, MetricsSection> sections = new HashMap<String, MetricsSection>();
42  
43  	public MetricsPanel()
44  	{
45  		super( new VerticalLayout() );
46  		setBackground( Color.WHITE );
47  	}
48  
49  	public MetricsSection addSection( String name )
50  	{
51  		MetricsSection section = new MetricsSection( name );
52  		sections.put( name, section );
53  		add( section );
54  		return section;
55  	}
56  
57  	public enum MetricType
58  	{
59  		STRING, URL
60  	};
61  
62  	public class Metric
63  	{
64  		private final JLabel label;
65  
66  		public Metric( JLabel label )
67  		{
68  			this.label = label;
69  		}
70  
71  		public void set( String value )
72  		{
73  			label.setText( value );
74  		}
75  
76  		public void set( int value )
77  		{
78  			set( String.valueOf( value ) );
79  		}
80  	}
81  
82  	public class MetricsSection extends JCollapsiblePanel
83  	{
84  		private MetricsForm form;
85  
86  		public MetricsSection( String name )
87  		{
88  			super( name );
89  
90  			form = new MetricsForm();
91  			setContentPanel( form.getPanel() );
92  		}
93  
94  		public Metric addMetric( ImageIcon icon, String label, MetricType type )
95  		{
96  			return form.addMetric( label, icon, type == MetricType.URL );
97  		}
98  
99  		public Metric addMetric( ImageIcon icon, String label )
100 		{
101 			return addMetric( icon, label, MetricType.STRING );
102 		}
103 
104 		public Metric addMetric( String label )
105 		{
106 			return addMetric( null, label, MetricType.STRING );
107 		}
108 
109 		public void finish()
110 		{
111 			form.finish();
112 		}
113 
114 		public MetricsSection clear()
115 		{
116 			form = new MetricsForm();
117 			setContentPanel( form.getPanel() );
118 
119 			return this;
120 		}
121 
122 		public Metric addMetric( String label, MetricType type )
123 		{
124 			return addMetric( null, label, type );
125 		}
126 
127 		public JXTable addTable( TableModel model )
128 		{
129 			JXTable table = new JXTable( model );
130 			table.setBorder( null );
131 			table.setShowGrid( false );
132 			table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
133 			table.setSortable( false );
134 			table.getColumn( 0 ).setWidth( 195 );
135 			table.getColumn( 0 ).setMinWidth( 195 );
136 
137 			InternalHeaderRenderer internalHeaderRenderer = new InternalHeaderRenderer( table.getTableHeader()
138 					.getBackground() );
139 			InternalCellRenderer internalCellRenderer = new InternalCellRenderer();
140 
141 			for( int c = 0; c < table.getColumnCount(); c++ )
142 			{
143 				table.getColumn( c ).setHeaderRenderer( internalHeaderRenderer );
144 				table.getColumn( c ).setCellRenderer( internalCellRenderer );
145 			}
146 
147 			table.getTableHeader().setReorderingAllowed( false );
148 			table.getTableHeader().setBackground( Color.WHITE );
149 
150 			JScrollPane scrollPane = new JScrollPane( table );
151 			scrollPane.setBorder( BorderFactory.createEmptyBorder( 0, 14, 0, 14 ) );
152 			form.addComponent( scrollPane );
153 			table.setPreferredScrollableViewportSize( new Dimension( 100, 250 ) );
154 			scrollPane.setBackground( Color.WHITE );
155 			scrollPane.getViewport().setBackground( Color.WHITE );
156 			scrollPane.setOpaque( true );
157 
158 			table.setBackground( Color.WHITE );
159 			table.setOpaque( true );
160 
161 			return table;
162 		}
163 	}
164 
165 	public MetricsSection getSection( String name )
166 	{
167 		return sections.get( name );
168 	}
169 
170 	public boolean setMetric( String label, int value )
171 	{
172 		return setMetric( label, String.valueOf( value ) );
173 	}
174 
175 	public boolean setMetric( String label, String value )
176 	{
177 		if( !hasMetric( label ) )
178 			return false;
179 
180 		metrics.get( label ).set( value );
181 		return true;
182 	}
183 
184 	public boolean hasMetric( String name )
185 	{
186 		return metrics.containsKey( name );
187 	}
188 
189 	private class MetricsForm extends SimpleForm
190 	{
191 		private Dimension labelDimensions = new Dimension( 200, 16 );
192 
193 		public MetricsForm()
194 		{
195 			super();
196 
197 			addSpace( 7 );
198 			setRowSpacing( 3 );
199 		}
200 
201 		public JPanel finish()
202 		{
203 			addSpace( 7 );
204 
205 			JPanel formPanel = getPanel();
206 			formPanel.setBackground( Color.WHITE );
207 			formPanel.setOpaque( true );
208 
209 			return formPanel;
210 		}
211 
212 		public Metric addMetric( String labelText, ImageIcon icon, boolean isHyperlink )
213 		{
214 			return addMetric( labelText, "", icon, isHyperlink );
215 		}
216 
217 		public Metric addMetric( String labelText, ImageIcon icon )
218 		{
219 			return addMetric( labelText, "", icon, false );
220 		}
221 
222 		public Metric addMetric( String labelText, String text, ImageIcon icon, boolean isHyperlink )
223 		{
224 			JLabel label = new JLabel( labelText, icon, SwingConstants.LEFT );
225 			UISupport.setFixedSize( label, labelDimensions );
226 			label.setIconTextGap( 5 );
227 
228 			label.setBorder( BorderFactory.createEmptyBorder( 2, icon == null ? 16 : 14, 0, 0 ) );
229 
230 			JLabel textField = null;
231 
232 			if( isHyperlink )
233 			{
234 				textField = append( labelText, label, new JHyperlinkLabel( text ) );
235 			}
236 			else
237 			{
238 				textField = append( labelText, label, new JLabel( text ) );
239 			}
240 
241 			textField.setBorder( BorderFactory.createEmptyBorder( 2, 0, 0, 0 ) );
242 			textField.setBackground( Color.WHITE );
243 
244 			Metric metric = new Metric( textField );
245 			metrics.put( labelText, metric );
246 			return metric;
247 		}
248 	}
249 
250 	public static class InternalHeaderRenderer extends DefaultTableCellRenderer
251 	{
252 		private Font boldFont;
253 		private final Color color;
254 
255 		public InternalHeaderRenderer( Color color )
256 		{
257 			super();
258 			this.color = color;
259 
260 			setHorizontalAlignment( SwingConstants.LEFT );
261 			boldFont = getFont().deriveFont( Font.BOLD );
262 		}
263 
264 		public InternalHeaderRenderer()
265 		{
266 			this( null );
267 		}
268 
269 		@Override
270 		public Component getTableCellRendererComponent( JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4,
271 				int arg5 )
272 		{
273 			JComponent result = ( JComponent )super.getTableCellRendererComponent( arg0, arg1, arg2, arg3, arg4, arg5 );
274 			setFont( boldFont );
275 			if( color != null )
276 				setBackground( color );
277 			setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEtchedBorder(), BorderFactory
278 					.createEmptyBorder( 0, 2, 1, 2 ) ) );
279 			return result;
280 		}
281 	}
282 
283 	private class InternalCellRenderer extends DefaultTableCellRenderer
284 	{
285 		public InternalCellRenderer()
286 		{
287 			super();
288 
289 			setHorizontalAlignment( SwingConstants.LEFT );
290 		}
291 
292 		@Override
293 		public Component getTableCellRendererComponent( JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4,
294 				int arg5 )
295 		{
296 			Component result = super.getTableCellRendererComponent( arg0, arg1, arg2, arg3, arg4, arg5 );
297 			setBorder( BorderFactory.createEmptyBorder( 3, 1, 3, 2 ) );
298 			return result;
299 		}
300 	}
301 }