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;
21
22 import com.cosylab.gui.components.util.FontHelper;
23 import com.cosylab.gui.components.util.PaintHelper;
24
25 import com.cosylab.logging.DebugLogger;
26
27 import java.awt.Dimension;
28 import java.awt.Font;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.event.ComponentAdapter;
32 import java.awt.event.ComponentEvent;
33 import java.awt.event.HierarchyEvent;
34 import java.awt.event.HierarchyListener;
35
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 import javax.swing.Icon;
40 import javax.swing.JLabel;
41 import javax.swing.SwingConstants;
42
43
44
45
46
47
48
49
50
51
52
53 public class ResizableTextLabel extends JLabel
54 {
55 private static final long serialVersionUID = 1L;
56 private boolean resizable;
57 private boolean enhanced;
58 private int columns;
59 private Dimension preferredSize;
60 private Dimension minimumSize;
61 private Font userFont;
62 private int minFontSize = 0;
63 private int maxFontSize = Integer.MAX_VALUE;
64 private final Logger logger = DebugLogger.getLogger("RTL",
65 Level.OFF);
66
67
68
69
70
71
72
73
74
75
76 protected class ResizableAdapter extends ComponentAdapter implements HierarchyListener
77 {
78
79
80
81
82
83
84
85 public void componentResized(ComponentEvent e)
86 {
87 if (resizable) {
88 resize();
89 }
90 }
91
92
93
94
95
96
97
98
99 public void hierarchyChanged(HierarchyEvent e) {
100 if (resizable) {
101 resize();
102 }
103
104 }
105
106
107 }
108
109
110
111
112
113
114
115
116
117
118 public ResizableTextLabel(String text, Icon icon, int horizontalAlignment)
119 {
120 super(text, icon, horizontalAlignment);
121 addComponentListener(new ResizableAdapter());
122 addHierarchyListener(new ResizableAdapter());
123 setFont(FontHelper.getDefaultFont());
124 }
125
126
127
128
129
130
131
132
133 public ResizableTextLabel(String arg0, int arg1)
134 {
135 this(arg0, null, arg1);
136 }
137
138
139
140
141
142
143 public ResizableTextLabel(String arg0)
144 {
145 this(arg0, null, SwingConstants.LEADING);
146 }
147
148
149
150
151
152
153
154
155
156 public ResizableTextLabel(Icon image, int horizontalAlignment)
157 {
158 this(null, image, horizontalAlignment);
159 }
160
161
162
163
164
165
166 public ResizableTextLabel(Icon image)
167 {
168 this(null, image, SwingConstants.LEADING);
169 }
170
171
172
173
174 public ResizableTextLabel()
175 {
176 this(" ", null, SwingConstants.LEADING);
177 }
178
179
180
181
182
183
184
185
186
187 public void setColumns(int newColumns)
188 {
189 int oldColumns = columns;
190
191 if (oldColumns == newColumns) {
192 return;
193 }
194
195 columns = newColumns;
196 firePropertyChange("columns", oldColumns, newColumns);
197
198 if (resizable) {
199 resize();
200 }
201 }
202
203
204
205
206
207
208 public int getColumns()
209 {
210 return columns;
211 }
212
213
214
215
216
217
218 public boolean isResizable()
219 {
220 return resizable;
221 }
222
223
224
225
226
227
228 public void setResizable(boolean newResizable)
229 {
230 boolean oldResizable = resizable;
231
232 if (oldResizable == newResizable) {
233 return;
234 }
235
236 resizable = newResizable;
237 firePropertyChange("resizable", oldResizable, newResizable);
238
239 if (resizable) {
240 resize();
241 } else {
242 if (userFont != null) {
243 super.setFont(userFont);
244 } else {
245 super.setFont(FontHelper.getDefaultFont());
246 }
247 }
248 }
249
250
251
252
253
254
255
256 public void setEnhanced(boolean newEnhanced)
257 {
258 boolean oldEnhanced = enhanced;
259
260 if (oldEnhanced == newEnhanced) {
261 return;
262 }
263
264 enhanced = newEnhanced;
265 firePropertyChange("enhanced", oldEnhanced, newEnhanced);
266 repaint();
267 }
268
269
270
271
272
273
274 public boolean isEnhanced()
275 {
276 return enhanced;
277 }
278
279
280
281
282
283
284
285
286 public void setText(String text)
287 {
288 if (text != null && text.length() < 1) {
289 text = " ";
290 }
291
292 super.setText(text);
293
294 if (resizable) {
295 resize();
296 }
297 }
298
299
300
301
302
303
304
305
306
307
308
309
310
311 public void adjustSizeToFont(int fontSize, int newColumns)
312 {
313 Font font = FontHelper.getFontWithSize(fontSize, getFont());
314 int length = getFontMetrics(font).stringWidth("m") * newColumns;
315
316 super.setSize((int)(length / 0.9), fontSize * (20 / 14));
317 setColumns(newColumns);
318
319
320 if (!resizable) {
321 super.setFont(font);
322 }
323 }
324
325 protected void resize()
326 {
327 logger.fine("resizing");
328
329 Font f = FontHelper.calculateFittingFont(this, getFont(), getText(),
330 columns, minFontSize, maxFontSize);
331
332 if (f != getFont()) {
333 super.setFont(f);
334 }
335 }
336
337
338
339
340
341
342 public void addNotify()
343 {
344 super.addNotify();
345
346 if (resizable) {
347 resize();
348 }
349 }
350
351
352
353
354
355
356
357
358 public Dimension getPreferredSize()
359 {
360 if (preferredSize != null) {
361 return preferredSize;
362 } else if (resizable) {
363 Font font;
364
365 if (userFont == null) {
366 font = FontHelper.getDefaultFont();
367 } else {
368 font = userFont;
369 }
370
371 int height = font.getSize() * 20 / 13;
372 int width = getFontMetrics(font).stringWidth("m") * 10 / 8 * columns;
373
374 if (columns == 0 && getText() != null) {
375 width = getFontMetrics(font).stringWidth(getText()) * 10 / 8;
376 }
377
378 if (width == 0) {
379 width = 1;
380 }
381
382 return new Dimension(width, height);
383 } else {
384 return super.getPreferredSize();
385 }
386 }
387
388
389
390
391
392
393 public void setPreferredSize(Dimension newPreferredSize)
394 {
395 preferredSize = newPreferredSize;
396 super.setPreferredSize(newPreferredSize);
397 }
398
399
400
401
402
403
404 public Dimension getMinimumSize()
405 {
406 if (minimumSize != null) {
407 return minimumSize;
408 } else if (resizable) {
409 return getPreferredSize();
410 } else {
411 return super.getMinimumSize();
412 }
413 }
414
415
416
417
418
419
420 public void setMinimumSize(Dimension newMinimumSize)
421 {
422 minimumSize = newMinimumSize;
423 super.setMinimumSize(newMinimumSize);
424 }
425
426
427
428
429
430
431
432 protected void paintComponent(Graphics g)
433 {
434 logger.fine("painting");
435
436 if (enhanced) {
437 ((Graphics2D)g).addRenderingHints(PaintHelper.getAntialiasingHints());
438 }
439
440 super.paintComponent(g);
441 }
442
443
444
445
446
447
448 public int getMaximumFontSize()
449 {
450 return maxFontSize;
451 }
452
453
454
455
456
457
458 public int getMinimumFontSize()
459 {
460 return minFontSize;
461 }
462
463
464
465
466
467
468 public void setMaximumFontSize(int newMax)
469 {
470 int oldMax = maxFontSize;
471
472 if (oldMax == newMax) {
473 return;
474 }
475
476 maxFontSize = newMax;
477 firePropertyChange("maximumFontSize", oldMax, newMax);
478 resize();
479 }
480
481
482
483
484
485
486 public void setMinimumFontSize(int newMin)
487 {
488 int oldMin = minFontSize;
489
490 if (oldMin == newMin) {
491 return;
492 }
493
494 minFontSize = newMin;
495 firePropertyChange("minimumFontSize", oldMin, newMin);
496 resize();
497 }
498 }
499
500