1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.gauger;
21
22 import java.awt.Font;
23 import java.awt.FontMetrics;
24 import java.awt.Graphics2D;
25 import java.awt.Rectangle;
26
27 import com.cosylab.gui.components.Gauger;
28 import com.cosylab.util.PrintfFormat;
29
30
31
32
33
34
35
36
37
38 public class ValueLabel
39 {
40 private boolean unitsVisible = true;
41
42 private Gauger gauger = null;
43
44
45 private String DEFAULT_FORMAT = "%0.2f";
46 private String DEFAULT_TEXT = "val";
47 private String text = DEFAULT_TEXT;
48
49 private String formatString = DEFAULT_FORMAT;
50 private String units = "";
51 private PrintfFormat formatter;
52 private double lastValue = 0.0;
53 private Rectangle bounds;
54 private boolean changeSignificant = true;
55 private int x;
56 private int y;
57
58
59
60
61
62
63 public ValueLabel(Gauger gauger)
64 {
65 super();
66 this.gauger = gauger;
67
68
69
70
71
72
73 bounds = new Rectangle(0, 0, 1, 16);
74 }
75
76
77
78
79
80
81 public Rectangle getBounds()
82 {
83 return bounds;
84 }
85
86
87
88
89
90
91 public String getFormat()
92 {
93 return formatString;
94 }
95
96
97
98
99
100
101 public String getUnits()
102 {
103 return units;
104 }
105
106 private PrintfFormat getFormatter()
107 {
108 if(formatter == null) {
109 formatter = new PrintfFormat(formatString + " " + (unitsVisible ? units : ""));
110 }
111
112 return formatter;
113 }
114
115
116
117
118
119
120
121 public void setValue(double value)
122 {
123 String newText;
124 try {
125 newText = getFormatter().sprintf(value);
126 } catch (Exception e) {
127 newText = Double.toString(value);
128 }
129 lastValue = value;
130
131 if(!newText.equals(text)) {
132 text = newText;
133
134 changeSignificant = true;
135 } else {
136 changeSignificant = false;
137 }
138 }
139
140
141
142
143
144
145 public boolean isChangeSignificant()
146 {
147 return changeSignificant;
148 }
149
150
151
152
153
154
155 public void draw(Graphics2D g)
156 {
157 Font f = gauger.getValueLabelFont();
158 g.setFont(f);
159
160
161 FontMetrics metrics = g.getFontMetrics();
162
163
164
165 int width = metrics.stringWidth(text);
166 int height = metrics.getHeight();
167 bounds.setBounds((int)(x - width / 2),
168 (int)(y - (metrics.getAscent() + metrics.getDescent()) / 2), width,
169 height);
170 g.drawString(text, x - width/2, y + height/2 );
171 }
172
173
174
175
176
177
178
179 public void setPosition(int x, int y)
180 {
181 this.x = x;
182 this.y = y;
183 }
184
185
186
187
188
189
190
191 public void setFormat(String newFormat)
192 {
193 if(newFormat != formatString) {
194 formatString = newFormat;
195
196 formatter = null;
197 setValue(lastValue);
198 }
199 }
200
201
202
203
204
205
206
207 public void setUnits(String newUnits)
208 {
209 units = newUnits;
210 formatter = null;
211 setValue(lastValue);
212 }
213
214
215
216
217
218
219 public void setUnitsVisible(boolean b) {
220 this.unitsVisible = b;
221 formatter = null;
222 setValue(lastValue);
223 }
224
225
226
227
228
229
230 public boolean isUnitsVisible() {
231 return unitsVisible;
232 }
233 }
234
235