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.CosyUIElements;
23 import com.cosylab.gui.components.util.FontHelper;
24 import com.cosylab.gui.components.util.IconHelper;
25
26 import java.awt.BorderLayout;
27 import java.awt.Color;
28 import java.awt.Component;
29 import java.awt.Container;
30 import java.awt.Font;
31
32 import javax.swing.AbstractButton;
33 import javax.swing.Action;
34 import javax.swing.Icon;
35 import javax.swing.JComponent;
36 import javax.swing.JPanel;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class TitledPanel extends JPanel
64 {
65 private static final long serialVersionUID = 1L;
66 protected PanelTitleBar bar = null;
67 private Container contentPane = null;
68
69
70
71 private boolean initialized = false;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public TitledPanel(String name, String title, Icon icon)
87 {
88 this(name, title, icon, 20);
89 }
90
91 private TitledPanel(String name, PanelTitleBar bar)
92 {
93 super(new BorderLayout());
94 this.bar = bar;
95 setName(name);
96 }
97
98
99
100
101 public TitledPanel()
102 {
103 this("TitledPanel", "Title",
104 IconHelper.createIcon("icons/general/ComposeMail16.gif"));
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public TitledPanel(String name, String title, Icon icon, int titleBarHeight)
120 {
121 this(name, new PanelTitleBar(title, icon, titleBarHeight));
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public void addTitleBarButton(AbstractButton button)
137 {
138 bar.addButton(button);
139 }
140
141
142
143
144
145
146
147 public void addTitleBarButton(int index, AbstractButton button)
148 {
149 bar.addButton(index, button);
150 }
151
152
153
154
155
156
157 public void removeTitleBarButton(AbstractButton button)
158 {
159 bar.removeButton(button);
160 }
161
162
163
164
165
166
167 public void addTitleBarAction(Action action)
168 {
169 bar.addActionButton(action);
170 }
171
172
173
174
175
176
177
178
179 public void addTitleBarAction(int index, Action action)
180 {
181 bar.addActionButton(index, action);
182 }
183
184
185
186
187 public boolean containsTitleBarAction(Action action)
188 {
189 return bar.containsAction(action);
190 }
191
192
193
194
195 public boolean containsTitleBarAction(AbstractButton button)
196 {
197 return bar.containsButton(button);
198 }
199
200
201
202
203
204
205
206 public void removeTitleBarAction(Action action)
207 {
208 bar.removeActionButton(action);
209 }
210
211 private void initialize()
212 {
213 initialized = true;
214 super.setFocusable(false);
215 bar.getTitleLabel().setFont(FontHelper.getDefaultFont());
216 bar.setVisible(true);
217
218 super.add(bar, BorderLayout.NORTH);
219 }
220
221
222
223
224
225
226
227
228 public void setTitle(String title)
229 {
230 bar.setTitle(title);
231 }
232
233
234
235
236
237
238
239
240 public String getTitle()
241 {
242 return bar.getTitle();
243 }
244
245
246
247
248
249
250 public Color getTitleBackgroundLeft()
251 {
252 return bar.getBackgroundStart();
253 }
254
255
256
257
258
259
260 public Color getTitleBackgroundRight()
261 {
262 return bar.getBackground();
263 }
264
265
266
267
268
269
270
271 public void setTitleBackground(Color left, Color right)
272 {
273 if (left.equals(right)) {
274 bar.setBackground(right);
275 bar.getTitleLabel().setGradientEnabled(false);
276 } else {
277 bar.getTitleLabel().setGradientEnabled(true);
278 bar.setBackground(left, right);
279 }
280 }
281
282
283
284
285
286
287 public void setTitleBackgroundLeft(Color left)
288 {
289 bar.getTitleLabel().setGradientEnabled(true);
290 bar.setBackgroundStart(left);
291 }
292
293
294
295
296
297
298 public void setTitleBackgroundRight(Color right)
299 {
300 bar.getTitleLabel().setGradientEnabled(true);
301 bar.setBackground(right);
302 }
303
304
305
306
307
308
309 public Icon getIcon()
310 {
311 return bar.getIcon();
312 }
313
314
315
316
317
318
319 public void setIcon(Icon icon)
320 {
321 bar.setIcon(icon);
322 }
323
324
325
326
327
328
329
330 public Container getContentPane()
331 {
332 if (contentPane == null) {
333 contentPane = new JPanel();
334 contentPane.setName(getName() + "ContentPane");
335 }
336
337 return contentPane;
338 }
339
340
341
342
343
344
345
346 public void setContentPane(Container content)
347 {
348 if (!initialized) {
349 initialize();
350 }
351
352 if (this.contentPane != content) {
353 if (this.contentPane != null) {
354 remove(this.contentPane);
355 }
356
357 if ((content instanceof JComponent)
358 && (((JComponent)content).getBorder() == null)) {
359 ((JComponent)content).setBorder(CosyUIElements.getPlainBorder(
360 true));
361 }
362
363 this.contentPane = content;
364 super.add(content, BorderLayout.CENTER);
365 }
366
367 invalidate();
368 repaint();
369 }
370
371 protected GradientLabel getTitleBarLabel()
372 {
373 return bar.getTitleLabel();
374 }
375
376
377
378
379
380
381 public void setTitleBarVisible(boolean visible)
382 {
383 boolean old = bar.isVisible();
384
385 if (old != visible) {
386 bar.setVisible(visible);
387 firePropertyChange("titleBarVisible", old, visible);
388 revalidate();
389 repaint();
390 }
391 }
392
393
394
395
396
397
398 public boolean isTitleBarVisible()
399 {
400 return bar.isVisible();
401 }
402
403
404
405
406
407 public Component add(Component comp, int index)
408 {
409 if (!initialized) {
410 initialize();
411 }
412
413 if (comp instanceof AbstractButton) {
414 addTitleBarButton((AbstractButton)comp);
415
416 return comp;
417 }
418
419 if (comp != bar && comp != contentPane) {
420 setContentPane((JComponent)comp);
421 }
422
423 return comp;
424 }
425
426
427
428
429
430 public void add(Component comp, Object constraints, int index)
431 {
432 if (!initialized) {
433 initialize();
434 }
435
436 if (comp instanceof AbstractButton) {
437 addTitleBarButton((AbstractButton)comp);
438
439 return;
440 }
441
442 if (comp != bar && comp != contentPane) {
443 setContentPane((JComponent)comp);
444 }
445 }
446
447
448
449
450
451 public void add(Component comp, Object constraints)
452 {
453 if (!initialized) {
454 initialize();
455 }
456
457 if (comp instanceof AbstractButton) {
458 addTitleBarButton((AbstractButton)comp);
459
460 return;
461 }
462
463 if (comp != bar && comp != contentPane) {
464 setContentPane((JComponent)comp);
465 }
466 }
467
468
469
470
471
472 public Component add(Component comp)
473 {
474 if (!initialized) {
475 initialize();
476 }
477
478 if (comp instanceof AbstractButton) {
479 addTitleBarButton((AbstractButton)comp);
480 } else if (comp != bar && comp != contentPane) {
481 setContentPane((JComponent)comp);
482 }
483
484 return comp;
485 }
486
487
488
489
490
491 public Component add(String name, Component comp)
492 {
493 if (!initialized) {
494 initialize();
495 }
496
497 if (comp instanceof AbstractButton) {
498 addTitleBarButton((AbstractButton)comp);
499
500 return comp;
501 }
502
503 if (comp != bar && comp != contentPane) {
504 setContentPane((JComponent)comp);
505 }
506
507 return comp;
508 }
509
510
511
512
513
514 public void setFont(Font font)
515 {
516 if (bar != null) {
517 bar.setFont(font);
518 }
519
520 super.setFont(font);
521 }
522
523
524
525
526
527 public void addNotify()
528 {
529 if (!initialized) {
530 initialize();
531 }
532
533 super.addNotify();
534 }
535
536
537
538
539
540 public void remove(Component comp)
541 {
542 if (comp instanceof AbstractButton) {
543 removeTitleBarButton((AbstractButton)comp);
544
545 return;
546 }
547
548 super.remove(comp);
549 }
550 }
551
552