001 /* 002 * $Id: JXTitledPanel.java,v 1.20 2006/05/14 15:55:54 dmouse Exp $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 022 package org.jdesktop.swingx; 023 024 import java.awt.BorderLayout; 025 import java.awt.Color; 026 import java.awt.Container; 027 import java.awt.Font; 028 029 import javax.swing.BorderFactory; 030 import javax.swing.JComponent; 031 import org.jdesktop.swingx.painter.Painter; 032 033 import org.jdesktop.swingx.plaf.JXTitledPanelAddon; 034 import org.jdesktop.swingx.plaf.LookAndFeelAddons; 035 import org.jdesktop.swingx.plaf.TitledPanelUI; 036 037 /** 038 * A special type of Panel that has a Title section and a Content section.<br> 039 * The following 3 properties can be set with the UIManager to change the look 040 * and feel of the JXTitledPanel: 041 * <ul> 042 * <li>JXTitledPanel.title.foreground</li> 043 * <li>JXTitledPanel.title.background</li> 044 * <li>JXTitledPanel.title.font</li> 045 * </ul> 046 * 047 * @author Richard Bair 048 * @author Nicola Ken Barozzi 049 * @author Jeanette Winzenburg 050 */ 051 public class JXTitledPanel extends JXPanel { 052 053 /** 054 * @see #getUIClassID // * 055 * @see #readObject 056 */ 057 static public final String uiClassID = "TitledPanelUI"; 058 059 public static final String LEFT_DECORATION = "JXTitledPanel.leftDecoration"; 060 061 public static final String RIGHT_DECORATION = "JXTitledPanel.rightDecoration"; 062 063 /** 064 * Initialization that would ideally be moved into various look and feel 065 * classes. 066 */ 067 static { 068 LookAndFeelAddons.contribute(new JXTitledPanelAddon()); 069 } 070 071 /** 072 * The text to use for the title 073 */ 074 private String title = ""; 075 076 /** 077 * The Font to use for the Title 078 */ 079 private Font titleFont; 080 081 /** 082 * The forground color to use for the Title (particularly for the text) 083 */ 084 private Color titleForeground; 085 086 /** 087 * The ContentPanel. Whatever this container is will be displayed in the 088 * Content section 089 */ 090 private Container contentPanel; 091 092 /** 093 * The Painter to use for painting the title section of the JXTitledPanel 094 */ 095 private Painter titlePainter; 096 097 /** 098 * Create a new JTitledPanel with an empty string for the title. 099 */ 100 public JXTitledPanel() { 101 this(""); 102 } 103 104 /** 105 * Create a new JTitledPanel with the given title as the title for the 106 * panel. 107 * 108 * @param title 109 */ 110 public JXTitledPanel(String title) { 111 this(title, new JXPanel()); 112 } 113 114 /** 115 * Create a new JTitledPanel with the given String as the title, and the 116 * given Container as the content panel. 117 * 118 * @param title 119 * @param content 120 */ 121 public JXTitledPanel(String title, Container content) { 122 setTitle(title); 123 setContentContainer(content); 124 } 125 126 /** 127 * Returns the look and feel (L&F) object that renders this component. 128 * 129 * @return the TitledPanelUI object that renders this component 130 * @since 1.4 131 */ 132 @Override 133 public TitledPanelUI getUI() { 134 return (TitledPanelUI) ui; 135 } 136 137 /** 138 * Sets the look and feel (L&F) object that renders this component. 139 * 140 * @param ui 141 * the TitledPanelUI L&F object 142 * @see javax.swing.UIDefaults#getUI 143 * @since 1.4 144 * @beaninfo bound: true hidden: true attribute: visualUpdate true 145 * description: The UI object that implements the Component's 146 * LookAndFeel. 147 */ 148 public void setUI(TitledPanelUI ui) { 149 super.setUI(ui); 150 } 151 152 /** 153 * Returns a string that specifies the name of the L&F class that renders 154 * this component. 155 * 156 * @return "TitledPanelUI" 157 * @see JComponent#getUIClassID 158 * @see javax.swing.UIDefaults#getUI 159 * @beaninfo expert: true description: A string that specifies the name of 160 * the L&F class. 161 */ 162 @Override 163 public String getUIClassID() { 164 return uiClassID; 165 } 166 167 /** 168 * Notification from the <code>UIManager</code> that the L&F has changed. 169 * Replaces the current UI object with the latest version from the 170 * <code>UIManager</code>. 171 * 172 * @see javax.swing.JComponent#updateUI 173 */ 174 @Override 175 public void updateUI() { 176 setUI((TitledPanelUI) LookAndFeelAddons 177 .getUI(this, TitledPanelUI.class)); 178 } 179 180 public String getTitle() { 181 return title; 182 } 183 184 public void setTitle(String title) { 185 String oldTitle = this.title; 186 this.title = (title == null ? "" : title); 187 // JW: fix swingx #9 - missing/incorrect notification 188 // let standard notification handle 189 // NOTE - "getting" the new property in the fire method is 190 // intentional: there's no way of missing any transformations 191 // on the parameter to set (like above: setting a 192 // value depending on whether the input is null). 193 firePropertyChange("title", oldTitle, getTitle()); 194 } 195 196 public Container getContentContainer() { 197 if (contentPanel == null) { 198 contentPanel = new JXPanel(); 199 ((JXPanel) contentPanel).setBorder(BorderFactory 200 .createEmptyBorder()); 201 this.add(contentPanel, BorderLayout.CENTER); 202 } 203 return contentPanel; 204 } 205 206 public void setContentContainer(Container contentPanel) { 207 if (this.contentPanel != null) { 208 remove(this.contentPanel); 209 } 210 add(contentPanel, BorderLayout.CENTER); 211 this.contentPanel = contentPanel; 212 } 213 214 /** 215 * Adds the given JComponent as a decoration on the right of the title 216 * 217 * @param decoration 218 */ 219 @Deprecated 220 public void addRightDecoration(JComponent decoration) { 221 getUI().setRightDecoration(decoration); 222 } 223 224 /** 225 * Adds the given JComponent as a decoration on the left of the title 226 * 227 * @param decoration 228 */ 229 @Deprecated 230 public void addLeftDecoration(JComponent decoration) { 231 getUI().setLeftDecoration(decoration); 232 } 233 234 /** 235 * Adds the given JComponent as a decoration on the right of the title 236 * 237 * @param decoration 238 */ 239 public void setRightDecoration(JComponent decoration) { 240 JComponent old = getRightDecoration(); 241 getUI().setRightDecoration(decoration); 242 firePropertyChange("rightDecoration", old, getRightDecoration()); 243 } 244 245 public JComponent getRightDecoration() { 246 return getUI().getRightDecoration(); 247 } 248 249 /** 250 * Adds the given JComponent as a decoration on the left of the title 251 * 252 * @param decoration 253 */ 254 public void setLeftDecoration(JComponent decoration) { 255 JComponent old = getLeftDecoration(); 256 getUI().setLeftDecoration(decoration); 257 firePropertyChange("leftDecoration", old, getLeftDecoration()); 258 } 259 260 public JComponent getLeftDecoration() { 261 return getUI().getLeftDecoration(); 262 } 263 264 public Font getTitleFont() { 265 return titleFont; 266 } 267 268 public void setTitleFont(Font titleFont) { 269 Font old = getTitleFont(); 270 this.titleFont = titleFont; 271 firePropertyChange("titleFont", old, getTitleFont()); 272 } 273 274 /** 275 * Set the Painter to use for painting the title section of the JXTitledPanel. 276 * This value may be null, which will cause the current look and feel to paint 277 * an appropriate look 278 * 279 * @param p The Painter to use. May be null 280 */ 281 public void setTitlePainter(Painter p) { 282 Painter old = getTitlePainter(); 283 this.titlePainter = p; 284 firePropertyChange("titlePainter", old, getTitlePainter()); 285 } 286 287 /** 288 * @return the Painter to use for painting the background of the title section 289 */ 290 public Painter getTitlePainter() { 291 return titlePainter; 292 } 293 294 public Color getTitleForeground() { 295 return titleForeground; 296 } 297 298 public void setTitleForeground(Color titleForeground) { 299 Color old = getTitleForeground(); 300 this.titleForeground = titleForeground; 301 firePropertyChange("titleForeground", old, getTitleForeground()); 302 } 303 304 }