Source code: cor/gui/prf/JspmPerfValueBar.java
1 /*-----------------------------------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (C) */
4 /* */
5 /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU */
6 /* General Public License as published by the Free Software Foundation; either version 2 of the */
7 /* License, or (at your option) any later version. */
8 /* */
9 /* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; */
10 /* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
11 /* PURPOSE. See the GNU General Public License for more details. */
12 /* */
13 /* You should have received a copy of the GNU General Public License along with this program; if */
14 /* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
15 /* 02111-1307 USA */
16 /* */
17 /*-----------------------------------------------------------------------------------------------------*/
18 /* */
19 /* $Author: strand01 $ $Revision: 1.3 $ $Date: 2001/12/20 14:23:02 $ */
20 /* */
21 /*-----------------------------------------------------------------------------------------------------*/
22
23 package cor.gui.prf;
24
25 // Java classes
26 import java.awt.*;
27 import java.awt.event.*;
28 import java.util.*;
29
30 // Swing classes
31 import javax.swing.*;
32 import javax.swing.event.*;
33
34 // JSPM classes
35 import cor.gui.*;
36
37 /**
38 * JSPM Performance value bar.
39 *
40 * This class provides the value bar which shows the current value or a resource together with the threshold and
41 * the status. The status is displayed as color of the bar. The thresholds are red (critical) or orange (warning)
42 * lines on top of the bar.
43 *
44 * The bar scales itself according to the threshold and the current value.
45 *
46 * @since 0.0.10
47 * @author Steve Randall (strand012001@yahoo.com)
48 * @version 0.0.10
49 * @date 13/11/2001
50 */
51 public class JspmPerfValueBar extends JPanel
52 {
53 /**
54 * Dimension of the bar
55 */
56 private Dimension size;
57
58 /**
59 * Minimum value, can be negative
60 */
61 private int max = 0;
62
63 /**
64 * Maximum value.
65 */
66 private int min = 0;
67
68 /**
69 * Current value.
70 */
71 private int value = 0;
72
73 /**
74 * Current value.
75 */
76 private int minCrit = 0;
77
78 /**
79 * Current value.
80 */
81 private int minWarn = 0;
82
83 /**
84 * Current value.
85 */
86 private int maxWarn = 0;
87
88 /**
89 * Current value.
90 */
91 private int maxCrit = 0;
92
93 /**
94 * Current value.
95 */
96 private int status = 0;
97
98 /**
99 * Color array
100 */
101 private Color[] colors = { Color.darkGray, Color.green, Color.yellow, Color.orange, Color.orange, Color.red, Color.blue };
102
103 /**
104 * Constructor
105 *
106 * @param min (int) minimum value, can be negative.
107 * @param max (int) maximum value.
108 * @param value (int) current value
109 * @param minCrit (int) minimum critical threshold.
110 * @param minWarn (int) minimum warning threshold.
111 * @param maxWarn (int) maximum warnung threshold.
112 * @param maxCrit (int) maximum critical threshold.
113 * @param status (int) current status, this determines the color of the bar.
114 */
115 public JspmPerfValueBar( int min, int max, int value, int minCrit, int minWarn, int maxWarn, int maxCrit, int status )
116 {
117 super( false );
118
119 this.min = min;
120 this.max = max;
121 this.value = value;
122 this.minCrit = minCrit;
123 this.minWarn = minWarn;
124 this.maxWarn = maxWarn;
125 this.maxCrit = maxCrit;
126 this.status = status;
127
128 size = new Dimension( 400, 16 );
129 setPreferredSize( size );
130 setBorder( BorderFactory.createLoweredBevelBorder() );
131 }
132
133 /**
134 * Paint the bar.
135 *
136 * @param g (Graphics) graphics environment
137 */
138 public void paint( Graphics g )
139 {
140 int val;
141 super.paint( g );
142
143 // Paint the value bar
144 g.setColor( colors[status] );
145 val = (int)(((double)value/max)*( size.width - 4 ) );
146 g.fill3DRect( 2, 2, val, 12, true );
147
148 Font font = new Font( "Ariel", Font.PLAIN, 10 );
149 FontMetrics fontMetrics = getFontMetrics( font );
150 g.setFont( font );
151
152 int x = 0;
153 if( value < max/10 )
154 x = 20;
155 else
156 x = val - fontMetrics.stringWidth( "" + value ) - 5;
157
158 g.setColor( Color.black );
159 g.drawString( "" + value, x, 11 );
160
161 g.setColor( Color.orange );
162 val = (int)(((double)minWarn/max)*( size.width - 4 ) );
163 g.fill3DRect( val, 2, 4, 12, true );
164
165 val = (int)(((double)maxWarn/max)*( size.width - 4 ) );
166 g.fill3DRect( val, 2, 4, 12, true );
167
168 g.setColor( Color.red );
169 val = (int)(((double)minCrit/max)*( size.width - 4 ) );
170 g.fill3DRect( val, 2, 4, 12, true );
171
172 val = (int)(((double)maxCrit/max)*( size.width - 4 ) );
173 g.fill3DRect( val, 2, 4, 12, true );
174 }
175
176 /**
177 * Sets the new thresholds
178 *
179 * @param minCrit (int) minimum critical threshold.
180 * @param minWarn (int) minimum warning threshold.
181 * @param maxWarn (int) maximum warnung threshold.
182 * @param maxCrit (int) maximum critical threshold.
183 */
184 public void setThresholds( int minCrit, int minWarn, int maxWarn, int maxCrit )
185 {
186 this.minCrit = minCrit;
187 this.minWarn = minWarn;
188 this.maxWarn = maxWarn;
189 this.maxCrit = maxCrit;
190 repaint();
191 }
192 };