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