1 4 package org.columba.core.gui.docking; 5 6 import java.awt.BorderLayout ; 7 import java.awt.Color ; 8 import java.awt.Dimension ; 9 import java.awt.GradientPaint ; 10 import java.awt.Graphics ; 11 import java.awt.Graphics2D ; 12 import java.awt.Insets ; 13 import java.awt.RenderingHints ; 14 import java.awt.event.MouseEvent ; 15 import java.awt.event.MouseListener ; 16 import java.util.Vector ; 17 18 import javax.swing.Action ; 19 import javax.swing.BorderFactory ; 20 import javax.swing.ImageIcon ; 21 import javax.swing.JButton ; 22 import javax.swing.JComponent ; 23 import javax.swing.JLabel ; 24 import javax.swing.JPanel ; 25 import javax.swing.border.Border ; 26 27 import org.columba.core.gui.base.RoundedBorder; 28 29 31 class TitleBar extends JPanel { 32 33 protected JLabel label; 34 35 private Vector rightButtonVector = new Vector (); 36 37 private Vector leftButtonVector = new Vector (); 38 39 private Color fillColor; 40 41 private Color midColor; 42 43 private Color buttonBackground; 44 45 private boolean active = false; 46 47 public TitleBar(String text, Color midColor, Color fillColor) { 48 super(); 49 50 this.midColor = midColor; 51 52 this.fillColor = fillColor; 53 54 setOpaque(false); 55 56 label = new JLabel (text); 57 58 60 setLayout(new BorderLayout ()); 61 62 layoutComponents(); 63 64 } 67 68 private void layoutComponents() { 69 removeAll(); 70 71 add(label, BorderLayout.CENTER); 72 73 JPanel buttonEastPanel = new JPanel (); 74 buttonEastPanel.setOpaque(false); 75 79 82 for (int i = 0; i < rightButtonVector.size(); i++) { 83 buttonEastPanel.add((JButton ) rightButtonVector.get(i)); 85 } 88 89 add(buttonEastPanel, BorderLayout.EAST); 90 91 JPanel buttonWestPanel = new JPanel (); 92 buttonWestPanel.setOpaque(false); 93 95 97 for (int i = 0; i < leftButtonVector.size(); i++) { 98 buttonWestPanel.add((JButton ) leftButtonVector.get(i)); 99 } 102 103 105 add(buttonWestPanel, BorderLayout.WEST); 106 107 } 108 109 public JButton addButton(ImageIcon icon, Action action, String border) { 110 if (icon == null) 111 throw new IllegalArgumentException ("icon == null"); 112 if (action == null) 113 throw new IllegalArgumentException ("action == null"); 114 if (border == null) 115 throw new IllegalArgumentException ("border == null"); 116 117 JButton 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 title) { 131 if (title == null) 132 title = ""; 133 134 label.setText(title); 135 } 136 137 public String getTitle() { 138 return label.getText(); 139 } 140 141 public void setTitleColor(Color activeTitleColor, 142 Color activeButtonBackground, boolean active) { 143 144 this.active = active; 145 146 label.setForeground(activeTitleColor); 147 148 buttonBackground = activeButtonBackground; 149 150 } 151 152 161 public void setMidColor(Color color) { 162 this.midColor = color; 163 164 } 165 166 public void setFillColor(Color fillColor) { 167 this.fillColor = fillColor; 168 169 } 170 171 protected void paintComponent(Graphics g) { 172 ((Graphics2D ) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, 173 RenderingHints.VALUE_ANTIALIAS_ON); 174 175 int h = getHeight(); 176 int w = getWidth(); 177 178 Graphics2D g2 = (Graphics2D ) g; 179 180 183 if ( active ) { 184 GradientPaint firstHalf = new GradientPaint (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 204 218 ((Graphics2D ) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, 219 RenderingHints.VALUE_ANTIALIAS_DEFAULT); 220 } 221 222 public void updateUI() { 223 super.updateUI(); 224 225 236 } 237 238 252 270 374 class TitleBarButton extends JButton implements MouseListener { 375 376 private final Border LINK_BORDER = BorderFactory.createEmptyBorder(1, 377 1, 1, 1); 378 379 boolean entered = false; 380 381 private ImageIcon icon; 382 383 TitleBarButton(ImageIcon icon) { 384 super(); 385 386 this.icon = icon; 387 388 setOpaque(false); 389 setBorder(LINK_BORDER); 390 391 setPreferredSize(new Dimension (12, 12)); 392 393 395 addMouseListener(this); 396 } 397 398 public void paintComponent(Graphics 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 e) { 408 } 409 410 public void mousePressed(MouseEvent e) { 411 } 412 413 public void mouseReleased(MouseEvent e) { 414 } 415 416 public void mouseEntered(MouseEvent e) { 417 entered = true; 418 419 ((JComponent ) e.getComponent()).setBorder(new RoundedBorder( 420 buttonBackground)); 421 422 repaint(); 423 } 424 425 public void mouseExited(MouseEvent e) { 426 entered = false; 427 428 ((JComponent ) e.getComponent()).setBorder(LINK_BORDER); 429 430 repaint(); 431 } 432 } 433 } 434 | Popular Tags |