1 7 8 package org.jdesktop.swing; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Color ; 12 import java.awt.Container ; 13 import java.awt.GradientPaint ; 14 import java.awt.Graphics ; 15 import java.awt.Graphics2D ; 16 import java.awt.GridBagConstraints ; 17 import java.awt.GridBagLayout ; 18 import java.awt.Image ; 19 import java.awt.Insets ; 20 import java.beans.PropertyChangeEvent ; 21 import java.beans.PropertyChangeListener ; 22 23 import javax.swing.BorderFactory ; 24 import javax.swing.ImageIcon ; 25 import javax.swing.JComponent ; 26 import javax.swing.JLabel ; 27 import javax.swing.JPanel ; 28 import javax.swing.UIManager ; 29 import javax.swing.plaf.ColorUIResource ; 30 import org.jdesktop.swing.utils.UIManagerUtils; 31 32 45 public class JXTitledPanel extends JPanel { 46 private static final ColorUIResource BLACK = new ColorUIResource (Color.BLACK); 47 private static final ColorUIResource WHITE = new ColorUIResource (Color.WHITE); 48 51 protected JLabel caption; 52 55 private String title = ""; 56 60 private Container contentPanel; 61 64 protected JPanel topPanel; 65 66 69 public JXTitledPanel() { 70 this(""); 71 } 72 73 77 public JXTitledPanel(String title) { 78 this.title = (title == null ? "" : title); 79 80 UIManagerUtils.initDefault("JTitledPanel.title.foreground", "black", BLACK); 82 Color c = UIManager.getColor("ComboBox.selectionBackground"); 83 if (c == null) { 85 c = UIManager.getColor("ProgressBar.background"); 86 if (c == null) { 87 c = Color.decode("0x80b2ea"); 88 } 89 } 90 UIManagerUtils.initDefault("JTitledPanel.title.darkBackground", "primary2", c); 91 UIManagerUtils.initDefault("JTitledPanel.title.lightBackground", "white", WHITE); 92 UIManagerUtils.initDefault("JTitledPanel.title.font", UIManager.getFont("Button.font")); 93 94 initGui(); 95 } 96 97 103 public JXTitledPanel(String title, Container content) { 104 this(title); 105 setContentContainer(content); 106 } 107 108 111 public String getTitle() { 112 return title; 113 } 114 115 118 public void setTitle(String title) { 119 String oldTitle = this.title; 120 this.title = (title == null ? "" : title); 121 caption.setText(title); 122 PropertyChangeEvent event = new PropertyChangeEvent (this, "title", oldTitle, title); 123 PropertyChangeListener [] listeners = this.getPropertyChangeListeners("title"); 124 for (int i = 0; i < listeners.length; i++) { 125 listeners[i].propertyChange(event); 126 } 127 } 128 129 132 private void initGui() { 133 137 Color titleColor = UIManager.getColor("JTitledPanel.title.foreground"); 140 141 142 this.setLayout(new BorderLayout ()); 143 144 contentPanel = new JPanel (); 145 ((JPanel )contentPanel).setBorder(BorderFactory.createEmptyBorder()); 146 this.add(contentPanel, BorderLayout.CENTER); 147 148 caption = new JLabel (title); 149 caption.setFont(UIManager.getFont("JTitledPanel.title.font")); 150 topPanel = new JGradientPanel(); 151 topPanel.setBorder(BorderFactory.createEmptyBorder()); 152 topPanel.setLayout(new GridBagLayout ()); 153 caption.setForeground(titleColor); 154 topPanel.add(caption, new GridBagConstraints (1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets (2, 12, 2, 4), 0, 0)); 155 this.add(topPanel, BorderLayout.NORTH); 156 157 this.setBorder(BorderFactory.createRaisedBevelBorder()); 158 159 setOpaque(false); 160 } 161 162 168 private static final class JGradientPanel extends JPanel { 169 private GradientPaint gp; 170 private double oldWidth = -1; 171 private double oldHeight = -1; 172 private ImageIcon helper = new ImageIcon (); 173 public JGradientPanel() { 174 } 175 176 178 185 protected void paintComponent(Graphics g) { 186 if (gp == null) { 187 gp = new GradientPaint (0, 0, UIManager.getColor("JTitledPanel.title.darkBackground"), getWidth(), getHeight(), UIManager.getColor("JTitledPanel.title.lightBackground")); 188 } 189 if (oldWidth != getWidth() || oldHeight != getHeight()) { 191 Image savedImg = createImage(getWidth(), getHeight()); 192 Graphics2D imgg = (Graphics2D )savedImg.getGraphics(); 193 imgg.setPaint(gp); 194 imgg.fillRect(0, 0, getWidth(), getHeight()); 195 oldWidth = getWidth(); 196 oldHeight = getHeight(); 197 helper.setImage(savedImg); 198 } 199 g.drawImage(helper.getImage(), 0, 0, getWidth(), getHeight(), helper.getImageObserver()); 201 } 202 203 } 204 205 208 public Container getContentContainer() { 209 return contentPanel; 210 } 211 212 public void setContentContainer(Container contentPanel) { 213 remove(this.contentPanel); 214 add(contentPanel, BorderLayout.CENTER); 215 this.contentPanel = contentPanel; 216 } 217 218 222 public void addRightDecoration(JComponent decoration) { 223 topPanel.add(decoration, new GridBagConstraints (2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets (0, 0, 0, 0), 0, 0)); 224 } 225 226 229 protected JPanel getTopPanel() { 230 return topPanel; 231 } 232 233 237 public void addLeftDecoration(JComponent decoration) { 238 topPanel.add(decoration, new GridBagConstraints (0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets (0, 0, 0, 0), 0, 0)); 239 } 240 241 } | Popular Tags |