001: /*
002: * Created on Jul 6, 2005
003: */
004: package org.columba.core.gui.docking;
005:
006: import java.awt.BorderLayout;
007: import java.awt.Color;
008: import java.awt.Dimension;
009: import java.awt.GradientPaint;
010: import java.awt.Graphics;
011: import java.awt.Graphics2D;
012: import java.awt.Insets;
013: import java.awt.RenderingHints;
014: import java.awt.event.MouseEvent;
015: import java.awt.event.MouseListener;
016: import java.util.Vector;
017:
018: import javax.swing.Action;
019: import javax.swing.BorderFactory;
020: import javax.swing.ImageIcon;
021: import javax.swing.JButton;
022: import javax.swing.JComponent;
023: import javax.swing.JLabel;
024: import javax.swing.JPanel;
025: import javax.swing.border.Border;
026:
027: import org.columba.core.gui.base.RoundedBorder;
028:
029: /**
030: */
031: class TitleBar extends JPanel {
032:
033: protected JLabel label;
034:
035: private Vector rightButtonVector = new Vector();
036:
037: private Vector leftButtonVector = new Vector();
038:
039: private Color fillColor;
040:
041: private Color midColor;
042:
043: private Color buttonBackground;
044:
045: private boolean active = false;
046:
047: public TitleBar(String text, Color midColor, Color fillColor) {
048: super ();
049:
050: this .midColor = midColor;
051:
052: this .fillColor = fillColor;
053:
054: setOpaque(false);
055:
056: label = new JLabel(text);
057:
058: // label.setFont(UIManager.getFont("Label.font").deriveFont(Font.PLAIN));
059:
060: setLayout(new BorderLayout());
061:
062: layoutComponents();
063:
064: // setBorder(BorderFactory.createEmptyBorder(2, 5, 3, 0));
065: //setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
066: }
067:
068: private void layoutComponents() {
069: removeAll();
070:
071: add(label, BorderLayout.CENTER);
072:
073: JPanel buttonEastPanel = new JPanel();
074: buttonEastPanel.setOpaque(false);
075: // ButtonBarBuilder builder = new ButtonBarBuilder(buttonEastPanel);
076: //
077: // builder.addGlue();
078:
079: // JPanel p = new JPanel();
080: // p.setLayout(new FlowLayout());
081:
082: for (int i = 0; i < rightButtonVector.size(); i++) {
083: // builder.addFixedNarrow((JButton) rightButtonVector.get(i));
084: buttonEastPanel.add((JButton) rightButtonVector.get(i));
085: // if ( i<rightButtonVector.size()-1)
086: // builder.addStrut(Sizes.pixel(2));
087: }
088:
089: add(buttonEastPanel, BorderLayout.EAST);
090:
091: JPanel buttonWestPanel = new JPanel();
092: buttonWestPanel.setOpaque(false);
093: // ButtonBarBuilder builder2 = new ButtonBarBuilder(buttonWestPanel);
094:
095: // builder2.addGlue();
096:
097: for (int i = 0; i < leftButtonVector.size(); i++) {
098: buttonWestPanel.add((JButton) leftButtonVector.get(i));
099: // 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 addButton(ImageIcon icon, Action action,
110: String border) {
111: if (icon == null)
112: throw new IllegalArgumentException("icon == null");
113: if (action == null)
114: throw new IllegalArgumentException("action == null");
115: if (border == null)
116: throw new IllegalArgumentException("border == null");
117:
118: JButton b = new TitleBarButton(icon);
119: b.setAction(action);
120:
121: if (border.equals(BorderLayout.EAST))
122: rightButtonVector.add(b);
123: else if (border.equals(BorderLayout.WEST))
124: leftButtonVector.add(b);
125:
126: layoutComponents();
127:
128: return b;
129: }
130:
131: public void setTitle(String title) {
132: if (title == null)
133: title = "";
134:
135: label.setText(title);
136: }
137:
138: public String getTitle() {
139: return label.getText();
140: }
141:
142: public void setTitleColor(Color activeTitleColor,
143: Color activeButtonBackground, boolean active) {
144:
145: this .active = active;
146:
147: label.setForeground(activeTitleColor);
148:
149: buttonBackground = activeButtonBackground;
150:
151: }
152:
153: // public void setInactiveTitleColor(Color inactiveTitleColor,
154: // Color inactiveButtonBackground) {
155: //
156: // label.setForeground(inactiveTitleColor);
157: //
158: // buttonBackground = inactiveButtonBackground;
159: //
160: // }
161:
162: public void setMidColor(Color color) {
163: this .midColor = color;
164:
165: }
166:
167: public void setFillColor(Color fillColor) {
168: this .fillColor = fillColor;
169:
170: }
171:
172: protected void paintComponent(Graphics g) {
173: ((Graphics2D) g).setRenderingHint(
174: RenderingHints.KEY_ANTIALIASING,
175: RenderingHints.VALUE_ANTIALIAS_ON);
176:
177: int h = getHeight();
178: int w = getWidth();
179:
180: Graphics2D g2 = (Graphics2D) g;
181:
182: // g2.setColor(fillColor);
183: // g2.fillRect(0,0,w,h);
184:
185: if (active) {
186: GradientPaint firstHalf = new GradientPaint(0, 0,
187: fillColor, 0, h, midColor);
188: g2.setPaint(firstHalf);
189: g2.fillRect(0, 0, w, h);
190: } else {
191: g2.setColor(fillColor);
192: g2.fillRect(0, 0, w, h);
193: }
194:
195: // GradientPaint painter = new GradientPaint(0, 0, midColor.brighter(),
196: // 0, 5, midColor);
197: // g2.setPaint(painter);
198: //
199: // g2.fillRect(0,0,w,5);
200: //
201: // new GradientPaint(0, h-1, midColor, 0, h-1-5, Color.red);
202: // g2.setPaint(painter);
203: //
204: // g2.fillRect(0,h-5,w,5);
205:
206: // GeneralPath path = generatePath(h, w);
207: // g2.setColor(fillColor);
208: // g2.fill(path);
209: //
210: // path = generateTopPath(w);
211: // GradientPaint painter = new GradientPaint(0, 0, startColor, 0, 5,
212: // midColor);
213: // g2.setPaint(painter);
214: // g2.fill(path);
215: //
216: // path = generatePath(h, w);
217: // g2.setColor(UIManager.getColor("controlDkShadow"));
218: // g2.draw(path);
219:
220: ((Graphics2D) g).setRenderingHint(
221: RenderingHints.KEY_ANTIALIASING,
222: RenderingHints.VALUE_ANTIALIAS_DEFAULT);
223: }
224:
225: public void updateUI() {
226: super .updateUI();
227:
228: // if (label != null) {
229: // label.setForeground(UIManager.getColor("Menu.selectionForeground"));
230: // label.setFont(UIManager.getFont("Label.font")
231: // .deriveFont(Font.PLAIN));
232: // }
233: //
234: // gradient = new GradientPainter(ACTIVE_START_COLOR, ACTIVE_MID_COLOR);
235: //
236: // setStartColor(ACTIVE_START_COLOR);
237: // setMidColor(ACTIVE_MID_COLOR);
238:
239: }
240:
241: // private GeneralPath generateTopPath(int w) {
242: // GeneralPath path;
243: // path = new GeneralPath();
244: // // top right
245: // path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90, Arc2D.OPEN),
246: // true);
247: // // top
248: // path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
249: // // top left
250: // path.append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN), true);
251: //
252: // return path;
253: // }
254:
255: // private GeneralPath generatePath(int h, int w) {
256: // GeneralPath path = new GeneralPath();
257: // // top right
258: // path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90, Arc2D.OPEN),
259: // true);
260: // // top
261: // path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
262: // // top left
263: // path.append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN), true);
264: // // left
265: // path.append(new Line2D.Float(0, 5, 0, h - 1), true);
266: // // bottom
267: // path.append(new Line2D.Float(0, h - 1, w - 1, h - 1), true);
268: // // right
269: // path.append(new Line2D.Float(w - 1, 5, w - 1, h - 1), true);
270: // return path;
271: // }
272:
273: // class GradientPainter {
274: // private Color startColor;
275: //
276: // private Color fillColor;
277: //
278: // private Color midColor;
279: //
280: // public GradientPainter(Color start, Color mid, Color fill) {
281: // startColor = start;
282: // midColor = mid;
283: // fillColor = fill;
284: // }
285: //
286: // public void paintGradient(JComponent comp, Graphics g) {
287: //
288: // ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
289: // RenderingHints.VALUE_ANTIALIAS_ON);
290: //
291: // int h = comp.getHeight() + 1;
292: // int w = comp.getWidth();
293: //
294: // int mid = w;
295: //
296: // Graphics2D g2 = (Graphics2D) g;
297: //
298: // GeneralPath path = generatePath(h, w);
299: //
300: // g2.setColor(fillColor);
301: //
302: // g2.fill(path);
303: //
304: // path = generateTopPath(w);
305: //
306: // Color gradientStart = startColor;
307: // Color gradientEnd = midColor;
308: // GradientPaint painter = new GradientPaint(0, 0, gradientStart, 0,
309: // 5, gradientEnd);
310: // g2.setPaint(painter);
311: // g2.fill(path);
312: //
313: // path = generatePath(h, w);
314: // g2.setColor(UIManager.getColor("controlDkShadow"));
315: // g2.draw(path);
316: // ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
317: // RenderingHints.VALUE_ANTIALIAS_DEFAULT);
318: // }
319: //
320: // private GeneralPath generateTopPath(int w) {
321: // GeneralPath path;
322: // path = new GeneralPath();
323: // // top right
324: // path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90,
325: // Arc2D.OPEN), true);
326: // // top
327: // path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
328: // // top left
329: // path
330: // .append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN),
331: // true);
332: //
333: // return path;
334: // }
335: //
336: // private GeneralPath generatePath(int h, int w) {
337: // GeneralPath path = new GeneralPath();
338: // // top right
339: // path.append(new Arc2D.Float(w - 10 - 1, 0, 10, 10, 0, 90,
340: // Arc2D.OPEN), true);
341: // // top
342: // path.append(new Line2D.Float(5, 0, w - 5 - 1, 0), true);
343: // // top left
344: // path
345: // .append(new Arc2D.Float(0, 0, 10, 10, 90, 90, Arc2D.OPEN),
346: // true);
347: // // left
348: // path.append(new Line2D.Float(0, 5, 0, h - 1), true);
349: // // bottom
350: // path.append(new Line2D.Float(0, h - 1, w - 1, h - 1), true);
351: // // right
352: // path.append(new Line2D.Float(w - 1, 5, w - 1, h - 1), true);
353: // return path;
354: // }
355: //
356: // public Color getMidColor() {
357: // return midColor;
358: // }
359: //
360: // public void setMidColor(Color midColor) {
361: // this.midColor = midColor;
362: // }
363: //
364: // public Color getStartColor() {
365: // return startColor;
366: // }
367: //
368: // public void setStartColor(Color startColor) {
369: // this.startColor = startColor;
370: // }
371: //
372: // public void setFillColor(Color fillColor) {
373: // this.fillColor = fillColor;
374: // }
375: // }
376:
377: class TitleBarButton extends JButton implements MouseListener {
378:
379: private final Border LINK_BORDER = BorderFactory
380: .createEmptyBorder(1, 1, 1, 1);
381:
382: boolean entered = false;
383:
384: private ImageIcon icon;
385:
386: TitleBarButton(ImageIcon icon) {
387: super ();
388:
389: this .icon = icon;
390:
391: setOpaque(false);
392: setBorder(LINK_BORDER);
393:
394: setPreferredSize(new Dimension(12, 12));
395:
396: //setMargin(new Insets(0,0,0,0));
397:
398: addMouseListener(this );
399: }
400:
401: public void paintComponent(Graphics g) {
402: if (entered) {
403: g.setColor(buttonBackground);
404: g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1,
405: 1, 1);
406: }
407:
408: icon.paintIcon(this , g, 0, 0);
409: }
410:
411: public void mouseClicked(MouseEvent e) {
412: }
413:
414: public void mousePressed(MouseEvent e) {
415: }
416:
417: public void mouseReleased(MouseEvent e) {
418: }
419:
420: public void mouseEntered(MouseEvent e) {
421: entered = true;
422:
423: ((JComponent) e.getComponent())
424: .setBorder(new RoundedBorder(buttonBackground));
425:
426: repaint();
427: }
428:
429: public void mouseExited(MouseEvent e) {
430: entered = false;
431:
432: ((JComponent) e.getComponent()).setBorder(LINK_BORDER);
433:
434: repaint();
435: }
436: }
437: }
|