KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > docking > TitleBar


1 /*
2  * Created on Jul 6, 2005
3  */

4 package org.columba.core.gui.docking;
5
6 import java.awt.BorderLayout JavaDoc;
7 import java.awt.Color JavaDoc;
8 import java.awt.Dimension JavaDoc;
9 import java.awt.GradientPaint JavaDoc;
10 import java.awt.Graphics JavaDoc;
11 import java.awt.Graphics2D JavaDoc;
12 import java.awt.Insets JavaDoc;
13 import java.awt.RenderingHints JavaDoc;
14 import java.awt.event.MouseEvent JavaDoc;
15 import java.awt.event.MouseListener JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import javax.swing.Action JavaDoc;
19 import javax.swing.BorderFactory JavaDoc;
20 import javax.swing.ImageIcon JavaDoc;
21 import javax.swing.JButton JavaDoc;
22 import javax.swing.JComponent JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.border.Border JavaDoc;
26
27 import org.columba.core.gui.base.RoundedBorder;
28
29 /**
30  */

31 class TitleBar extends JPanel JavaDoc {
32
33     protected JLabel JavaDoc label;
34
35     private Vector JavaDoc rightButtonVector = new Vector JavaDoc();
36
37     private Vector JavaDoc leftButtonVector = new Vector JavaDoc();
38
39     private Color JavaDoc fillColor;
40
41     private Color JavaDoc midColor;
42
43     private Color JavaDoc buttonBackground;
44
45     private boolean active = false;
46
47     public TitleBar(String JavaDoc text, Color JavaDoc midColor, Color JavaDoc fillColor) {
48         super();
49
50         this.midColor = midColor;
51
52         this.fillColor = fillColor;
53
54         setOpaque(false);
55
56         label = new JLabel JavaDoc(text);
57
58         // label.setFont(UIManager.getFont("Label.font").deriveFont(Font.PLAIN));
59

60         setLayout(new BorderLayout JavaDoc());
61
62         layoutComponents();
63
64         // setBorder(BorderFactory.createEmptyBorder(2, 5, 3, 0));
65
//setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
66
}
67
68     private void layoutComponents() {
69         removeAll();
70
71         add(label, BorderLayout.CENTER);
72
73         JPanel JavaDoc buttonEastPanel = new JPanel JavaDoc();
74         buttonEastPanel.setOpaque(false);
75         // ButtonBarBuilder builder = new ButtonBarBuilder(buttonEastPanel);
76
//
77
// builder.addGlue();
78

79         // JPanel p = new JPanel();
80
// p.setLayout(new FlowLayout());
81

82         for (int i = 0; i < rightButtonVector.size(); i++) {
83             // builder.addFixedNarrow((JButton) rightButtonVector.get(i));
84
buttonEastPanel.add((JButton JavaDoc) rightButtonVector.get(i));
85             // if ( i<rightButtonVector.size()-1)
86
// builder.addStrut(Sizes.pixel(2));
87
}
88
89         add(buttonEastPanel, BorderLayout.EAST);
90
91         JPanel JavaDoc buttonWestPanel = new JPanel JavaDoc();
92         buttonWestPanel.setOpaque(false);
93         // ButtonBarBuilder builder2 = new ButtonBarBuilder(buttonWestPanel);
94

95         // builder2.addGlue();
96

97         for (int i = 0; i < leftButtonVector.size(); i++) {
98             buttonWestPanel.add((JButton JavaDoc) leftButtonVector.get(i));
99             // builder2.addFixedNarrow((JButton) leftButtonVector.get(i));
100
// builder2.addStrut(Sizes.pixel(2));
101
}
102
103         // builder2.addStrut(Sizes.pixel(4));
104

105         add(buttonWestPanel, BorderLayout.WEST);
106
107     }
108
109     public JButton JavaDoc addButton(ImageIcon JavaDoc icon, Action JavaDoc action, String JavaDoc border) {
110         if (icon == null)
111             throw new IllegalArgumentException JavaDoc("icon == null");
112         if (action == null)
113             throw new IllegalArgumentException JavaDoc("action == null");
114         if (border == null)
115             throw new IllegalArgumentException JavaDoc("border == null");
116
117         JButton JavaDoc b = new TitleBarButton(icon);
118         b.setAction(action);
119
120         if (border.equals(BorderLayout.EAST))
121             rightButtonVector.add(b);
122         else if (border.equals(BorderLayout.WEST))
123             leftButtonVector.add(b);
124
125         layoutComponents();
126
127         return b;
128     }
129
130     public void setTitle(String JavaDoc title) {
131         if (title == null)
132             title = "";
133
134         label.setText(title);
135     }
136
137     public String JavaDoc getTitle() {
138         return label.getText();
139     }
140
141     public void setTitleColor(Color JavaDoc activeTitleColor,
142             Color JavaDoc activeButtonBackground, boolean active) {
143
144         this.active = active;
145
146         label.setForeground(activeTitleColor);
147
148         buttonBackground = activeButtonBackground;
149
150     }
151
152     // public void setInactiveTitleColor(Color inactiveTitleColor,
153
// Color inactiveButtonBackground) {
154
//
155
// label.setForeground(inactiveTitleColor);
156
//
157
// buttonBackground = inactiveButtonBackground;
158
//
159
// }
160

161     public void setMidColor(Color JavaDoc color) {
162         this.midColor = color;
163
164     }
165
166     public void setFillColor(Color JavaDoc fillColor) {
167         this.fillColor = fillColor;
168
169     }
170
171     protected void paintComponent(Graphics JavaDoc g) {
172         ((Graphics2D JavaDoc) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
173                 RenderingHints.VALUE_ANTIALIAS_ON);
174
175         int h = getHeight();
176         int w = getWidth();
177
178         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
179
180         // g2.setColor(fillColor);
181
// g2.fillRect(0,0,w,h);
182

183         if ( active ) {
184             GradientPaint JavaDoc firstHalf = new GradientPaint JavaDoc(0, 0, fillColor, 0, h,
185                     midColor);
186             g2.setPaint(firstHalf);
187             g2.fillRect(0, 0, w, h);
188         } else {
189             g2.setColor(fillColor);
190             g2.fillRect(0,0,w,h);
191         }
192
193         // GradientPaint painter = new GradientPaint(0, 0, midColor.brighter(),
194
// 0, 5, midColor);
195
// g2.setPaint(painter);
196
//
197
// g2.fillRect(0,0,w,5);
198
//
199
// new GradientPaint(0, h-1, midColor, 0, h-1-5, Color.red);
200
// g2.setPaint(painter);
201
//
202
// g2.fillRect(0,h-5,w,5);
203

204         // GeneralPath path = generatePath(h, w);
205
// g2.setColor(fillColor);
206
// g2.fill(path);
207
//
208
// path = generateTopPath(w);
209
// GradientPaint painter = new GradientPaint(0, 0, startColor, 0, 5,
210
// midColor);
211
// g2.setPaint(painter);
212
// g2.fill(path);
213
//
214
// path = generatePath(h, w);
215
// g2.setColor(UIManager.getColor("controlDkShadow"));
216
// g2.draw(path);
217

218         ((Graphics2D JavaDoc) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
219                 RenderingHints.VALUE_ANTIALIAS_DEFAULT);
220     }
221
222     public void updateUI() {
223         super.updateUI();
224
225         // if (label != null) {
226
// label.setForeground(UIManager.getColor("Menu.selectionForeground"));
227
// label.setFont(UIManager.getFont("Label.font")
228
// .deriveFont(Font.PLAIN));
229
// }
230
//
231
// gradient = new GradientPainter(ACTIVE_START_COLOR, ACTIVE_MID_COLOR);
232
//
233
// setStartColor(ACTIVE_START_COLOR);
234
// setMidColor(ACTIVE_MID_COLOR);
235

236     }
237
238     // private GeneralPath generateTopPath(int w) {
239
// GeneralPath path;
240
// path = new GeneralPath();
241
// // top right
242
// path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90, Arc2D.OPEN),
243
// true);
244
// // top
245
// path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
246
// // top left
247
// path.append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN), true);
248
//
249
// return path;
250
// }
251

252     // private GeneralPath generatePath(int h, int w) {
253
// GeneralPath path = new GeneralPath();
254
// // top right
255
// path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90, Arc2D.OPEN),
256
// true);
257
// // top
258
// path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
259
// // top left
260
// path.append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN), true);
261
// // left
262
// path.append(new Line2D.Float(0, 5, 0, h - 1), true);
263
// // bottom
264
// path.append(new Line2D.Float(0, h - 1, w - 1, h - 1), true);
265
// // right
266
// path.append(new Line2D.Float(w - 1, 5, w - 1, h - 1), true);
267
// return path;
268
// }
269

270     // class GradientPainter {
271
// private Color startColor;
272
//
273
// private Color fillColor;
274
//
275
// private Color midColor;
276
//
277
// public GradientPainter(Color start, Color mid, Color fill) {
278
// startColor = start;
279
// midColor = mid;
280
// fillColor = fill;
281
// }
282
//
283
// public void paintGradient(JComponent comp, Graphics g) {
284
//
285
// ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
286
// RenderingHints.VALUE_ANTIALIAS_ON);
287
//
288
// int h = comp.getHeight() + 1;
289
// int w = comp.getWidth();
290
//
291
// int mid = w;
292
//
293
// Graphics2D g2 = (Graphics2D) g;
294
//
295
// GeneralPath path = generatePath(h, w);
296
//
297
// g2.setColor(fillColor);
298
//
299
// g2.fill(path);
300
//
301
// path = generateTopPath(w);
302
//
303
// Color gradientStart = startColor;
304
// Color gradientEnd = midColor;
305
// GradientPaint painter = new GradientPaint(0, 0, gradientStart, 0,
306
// 5, gradientEnd);
307
// g2.setPaint(painter);
308
// g2.fill(path);
309
//
310
// path = generatePath(h, w);
311
// g2.setColor(UIManager.getColor("controlDkShadow"));
312
// g2.draw(path);
313
// ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
314
// RenderingHints.VALUE_ANTIALIAS_DEFAULT);
315
// }
316
//
317
// private GeneralPath generateTopPath(int w) {
318
// GeneralPath path;
319
// path = new GeneralPath();
320
// // top right
321
// path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90,
322
// Arc2D.OPEN), true);
323
// // top
324
// path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
325
// // top left
326
// path
327
// .append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN),
328
// true);
329
//
330
// return path;
331
// }
332
//
333
// private GeneralPath generatePath(int h, int w) {
334
// GeneralPath path = new GeneralPath();
335
// // top right
336
// path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90,
337
// Arc2D.OPEN), true);
338
// // top
339
// path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
340
// // top left
341
// path
342
// .append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN),
343
// true);
344
// // left
345
// path.append(new Line2D.Float(0, 5, 0, h - 1), true);
346
// // bottom
347
// path.append(new Line2D.Float(0, h - 1, w - 1, h - 1), true);
348
// // right
349
// path.append(new Line2D.Float(w - 1, 5, w - 1, h - 1), true);
350
// return path;
351
// }
352
//
353
// public Color getMidColor() {
354
// return midColor;
355
// }
356
//
357
// public void setMidColor(Color midColor) {
358
// this.midColor = midColor;
359
// }
360
//
361
// public Color getStartColor() {
362
// return startColor;
363
// }
364
//
365
// public void setStartColor(Color startColor) {
366
// this.startColor = startColor;
367
// }
368
//
369
// public void setFillColor(Color fillColor) {
370
// this.fillColor = fillColor;
371
// }
372
// }
373

374     class TitleBarButton extends JButton JavaDoc implements MouseListener JavaDoc {
375
376         private final Border JavaDoc LINK_BORDER = BorderFactory.createEmptyBorder(1,
377                 1, 1, 1);
378
379         boolean entered = false;
380
381         private ImageIcon JavaDoc icon;
382
383         TitleBarButton(ImageIcon JavaDoc icon) {
384             super();
385
386             this.icon = icon;
387
388             setOpaque(false);
389             setBorder(LINK_BORDER);
390
391             setPreferredSize(new Dimension JavaDoc(12, 12));
392
393             //setMargin(new Insets(0,0,0,0));
394

395             addMouseListener(this);
396         }
397
398         public void paintComponent(Graphics JavaDoc g) {
399             if (entered) {
400                 g.setColor(buttonBackground);
401                 g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 1, 1);
402             }
403
404             icon.paintIcon(this, g, 0, 0);
405         }
406
407         public void mouseClicked(MouseEvent JavaDoc e) {
408         }
409
410         public void mousePressed(MouseEvent JavaDoc e) {
411         }
412
413         public void mouseReleased(MouseEvent JavaDoc e) {
414         }
415
416         public void mouseEntered(MouseEvent JavaDoc e) {
417             entered = true;
418
419             ((JComponent JavaDoc) e.getComponent()).setBorder(new RoundedBorder(
420                     buttonBackground));
421
422             repaint();
423         }
424
425         public void mouseExited(MouseEvent JavaDoc e) {
426             entered = false;
427
428             ((JComponent JavaDoc) e.getComponent()).setBorder(LINK_BORDER);
429
430             repaint();
431         }
432     }
433 }
434
Popular Tags