JXTitledPanel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXTitledPanel.java,v 1.17 2005/10/12 11:26:53 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.awt.BorderLayout;
  23. import java.awt.Color;
  24. import java.awt.Container;
  25. import java.awt.Font;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.JComponent;
  28. import org.jdesktop.swingx.plaf.JXTitledPanelAddon;
  29. import org.jdesktop.swingx.plaf.LookAndFeelAddons;
  30. import org.jdesktop.swingx.plaf.TitledPanelUI;
  31. /**
  32.  * A special type of Panel that has a Title section and a Content section.<br>
  33.  * The following 3 properties can be set with the UIManager to change the look
  34.  * and feel of the JXTitledPanel:
  35.  * <ul>
  36.  * <li>JXTitledPanel.title.foreground</li>
  37.  * <li>JXTitledPanel.title.background</li>
  38.  * <li>JXTitledPanel.title.font</li>
  39.  * </ul>
  40.  * 
  41.  * @author Richard Bair
  42.  * @author Nicola Ken Barozzi
  43.  * @author Jeanette Winzenburg
  44.  */
  45. public class JXTitledPanel extends JXPanel {
  46.     /**
  47.      * @see #getUIClassID // *
  48.      * @see #readObject
  49.      */
  50.     static public final String uiClassID = "TitledPanelUI";
  51.     public static final String LEFT_DECORATION = "JXTitledPanel.leftDecoration";
  52.     public static final String RIGHT_DECORATION = "JXTitledPanel.rightDecoration";
  53.     /**
  54.      * Initialization that would ideally be moved into various look and feel
  55.      * classes.
  56.      */
  57.     static {
  58.         LookAndFeelAddons.contribute(new JXTitledPanelAddon());
  59.     }
  60.     /**
  61.      * The text to use for the title
  62.      */
  63.     private String title = "";
  64.     /**
  65.      * The Font to use for the Title
  66.      */
  67.     private Font titleFont;
  68.     /**
  69.      * For the gradient, this is the background color to use for the dark part
  70.      * of the gradient
  71.      */
  72.     private Color titleDarkBackground;
  73.     /**
  74.      * For the gradient, this is the light color to use for the light part of
  75.      * the gradient
  76.      */
  77.     private Color titleLightBackground;
  78.     /**
  79.      * The forground color to use for the Title (particularly for the text)
  80.      */
  81.     private Color titleForeground;
  82.     /**
  83.      * The ContentPanel. Whatever this container is will be displayed in the
  84.      * Content section
  85.      */
  86.     private Container contentPanel;
  87.     /**
  88.      * Create a new JTitledPanel with an empty string for the title.
  89.      */
  90.     public JXTitledPanel() {
  91.         this("");
  92.     }
  93.     /**
  94.      * Create a new JTitledPanel with the given title as the title for the
  95.      * panel.
  96.      * 
  97.      * @param title
  98.      */
  99.     public JXTitledPanel(String title) {
  100.         this(title, new JXPanel());
  101.     }
  102.     /**
  103.      * Create a new JTitledPanel with the given String as the title, and the
  104.      * given Container as the content panel.
  105.      * 
  106.      * @param title
  107.      * @param content
  108.      */
  109.     public JXTitledPanel(String title, Container content) {
  110.         setTitle(title);
  111.         setContentContainer(content);
  112.     }
  113.     /**
  114.      * Returns the look and feel (L&F) object that renders this component.
  115.      * 
  116.      * @return the TitledPanelUI object that renders this component
  117.      * @since 1.4
  118.      */
  119.     public TitledPanelUI getUI() {
  120.         return (TitledPanelUI) ui;
  121.     }
  122.     /**
  123.      * Sets the look and feel (L&F) object that renders this component.
  124.      * 
  125.      * @param ui
  126.      *            the TitledPanelUI L&F object
  127.      * @see javax.swing.UIDefaults#getUI
  128.      * @since 1.4
  129.      * @beaninfo bound: true hidden: true attribute: visualUpdate true
  130.      *           description: The UI object that implements the Component's
  131.      *           LookAndFeel.
  132.      */
  133.     public void setUI(TitledPanelUI ui) {
  134.         super.setUI(ui);
  135.     }
  136.     /**
  137.      * Returns a string that specifies the name of the L&F class that renders
  138.      * this component.
  139.      * 
  140.      * @return "TitledPanelUI"
  141.      * @see JComponent#getUIClassID
  142.      * @see javax.swing.UIDefaults#getUI
  143.      * @beaninfo expert: true description: A string that specifies the name of
  144.      *           the L&F class.
  145.      */
  146.     public String getUIClassID() {
  147.         return uiClassID;
  148.     }
  149.     /**
  150.      * Notification from the <code>UIManager</code> that the L&F has changed.
  151.      * Replaces the current UI object with the latest version from the
  152.      * <code>UIManager</code>.
  153.      * 
  154.      * @see javax.swing.JComponent#updateUI
  155.      */
  156.     public void updateUI() {
  157.         setUI((TitledPanelUI) LookAndFeelAddons
  158.                 .getUI(this, TitledPanelUI.class));
  159.     }
  160.     public String getTitle() {
  161.         return title;
  162.     }
  163.     public void setTitle(String title) {
  164.         String oldTitle = this.title;
  165.         this.title = (title == null ? "" : title);
  166.         // JW: fix swingx #9 - missing/incorrect notification
  167.         // let standard notification handle
  168.         // NOTE - "getting" the new property in the fire method is
  169.         // intentional: there's no way of missing any transformations
  170.         // on the parameter to set (like above: setting a
  171.         // value depending on whether the input is null).
  172.         firePropertyChange("title", oldTitle, getTitle());
  173.     }
  174.     public Container getContentContainer() {
  175.         if (contentPanel == null) {
  176.             contentPanel = new JXPanel();
  177.             ((JXPanel) contentPanel).setBorder(BorderFactory
  178.                     .createEmptyBorder());
  179.             this.add(contentPanel, BorderLayout.CENTER);
  180.         }
  181.         return contentPanel;
  182.     }
  183.     public void setContentContainer(Container contentPanel) {
  184.         if (this.contentPanel != null) {
  185.             remove(this.contentPanel);
  186.         }
  187.         add(contentPanel, BorderLayout.CENTER);
  188.         this.contentPanel = contentPanel;
  189.     }
  190.     /**
  191.      * Adds the given JComponent as a decoration on the right of the title
  192.      * 
  193.      * @param decoration
  194.      */
  195.     public void addRightDecoration(JComponent decoration) {
  196.         getUI().addRightDecoration(decoration);
  197.     }
  198.     /**
  199.      * Adds the given JComponent as a decoration on the left of the title
  200.      * 
  201.      * @param decoration
  202.      */
  203.     public void addLeftDecoration(JComponent decoration) {
  204.         getUI().addLeftDecoration(decoration);
  205.     }
  206.     public Font getTitleFont() {
  207.         return titleFont;
  208.     }
  209.     public void setTitleFont(Font titleFont) {
  210.         Font old = getTitleFont();
  211.         this.titleFont = titleFont;
  212.         firePropertyChange("titleFont", old, getTitleFont());
  213.     }
  214.     public Color getTitleDarkBackground() {
  215.         return titleDarkBackground;
  216.     }
  217.     public void setTitleDarkBackground(Color titleDarkBackground) {
  218.         Color old = getTitleDarkBackground();
  219.         this.titleDarkBackground = titleDarkBackground;
  220.         firePropertyChange("titleDarkBackground", old, getTitleDarkBackground());
  221.     }
  222.     public Color getTitleLightBackground() {
  223.         return titleLightBackground;
  224.     }
  225.     public void setTitleLightBackground(Color titleLightBackground) {
  226.         Color old = getTitleLightBackground();
  227.         this.titleLightBackground = titleLightBackground;
  228.         firePropertyChange("titleLightBackground", old,
  229.                 getTitleLightBackground());
  230.     }
  231.     public Color getTitleForeground() {
  232.         return titleForeground;
  233.     }
  234.     public void setTitleForeground(Color titleForeground) {
  235.         Color old = getTitleForeground();
  236.         this.titleForeground = titleForeground;
  237.         firePropertyChange("titleForeground", old, getTitleForeground());
  238.     }
  239. }