001    /*
002     * $Id: JXHeader.java,v 1.2 2006/03/22 19:05:47 rbair 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.Color;
025    import java.awt.Font;
026    import java.awt.GridBagConstraints;
027    import java.awt.GridBagLayout;
028    import java.awt.Insets;
029    import javax.swing.Icon;
030    import javax.swing.JLabel;
031    import javax.swing.JSeparator;
032    import javax.swing.UIManager;
033    import org.jdesktop.swingx.painter.gradient.BasicGradientPainter;
034    
035    /**
036     * <p>A simple component that consists of a title, a description, and an icon.
037     * An example of such a component can be seen on
038     * <a href="http://jext.free.fr/header.png">Romain Guys ProgX website</a></p>
039     *
040     * <p>The JXHeader is sufficiently configurable to be useable. The description area
041     * accepts HTML (the same set of HTML that {@link org.jdesktop.swingx.JXEditorPane} 
042     * supports), and thus allows a great deal of flexibility. The icon and the
043     * various bits of text is configurable. The JXHeader itself extends JXPanel, and
044     * so allows a gradient to be painted in the background.</p>
045     *
046     * <p>If I were to reconstruct the JXHeader shown in the above screenshot, I might
047     * do so like this:<br/>
048     * <pre><code>
049     *      JXHeader header = new JXHeader();
050     *      header.setTitle("Timing Framework Spline Editor");
051     *      header.setDescription("<html><body>" +
052     *          "Drag control points in the display to change the shape of the spline<br/>" +
053     *          "Click the Copy Code button to generate the corrosponding Java code." +
054     *          "</body></html>");
055     *      Icon icon = new ImageIcon(getClass().getResource("tools.png"));
056     *      header.setIcon(icon);
057     *      header.setDrawGradient(true);
058     * </code></pre></p>
059     * 
060     * @author rbair
061     */
062    public class JXHeader extends JXPanel {
063        private JLabel titleLabel;
064        private JXEditorPane descriptionPane;
065        private JLabel imagePanel;
066        
067        /** Creates a new instance of JXHeader */
068        public JXHeader() {
069            initGui();
070        }
071        
072        private void initGui() {
073            setLayout(new GridBagLayout());
074            
075            titleLabel = new JLabel("Title");
076            titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
077            add(titleLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 11), 0, 0));
078            
079            descriptionPane = new JXEditorPane();
080            descriptionPane.setContentType("text/html");
081            descriptionPane.setEditable(false);
082            descriptionPane.setOpaque(false);
083            descriptionPane.setText("<html><body><p> </p><p> </p></body></html>");
084            add(descriptionPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 24, 0, 11), 0, 0));
085    
086            imagePanel = new JLabel();
087            add(imagePanel, new GridBagConstraints(1, 0, 1, 2, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(12, 0, 11, 11), 0, 0));
088    
089            add(new JSeparator(), new GridBagConstraints(0, 2, 2, 1, 1.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
090            
091            setBackgroundPainter(new BasicGradientPainter(0, 0, Color.WHITE, 1, 0, UIManager.getColor("control")));
092        }
093        
094        /**
095         * @inheritDoc
096         * Passes enabled state on to the child components
097         */
098        @Override
099        public void setEnabled(boolean enabled) {
100            titleLabel.setEnabled(enabled);
101            descriptionPane.setEnabled(enabled);
102            imagePanel.setEnabled(enabled);
103            super.setEnabled(enabled);
104        }
105        
106        public void setTitle(String title) {
107            String old = getTitle();
108            titleLabel.setText(title);
109            firePropertyChange("title", old, getTitle());
110        }
111        
112        public String getTitle() {
113            return titleLabel.getText();
114        }
115        
116        public void setDescription(String description) {
117            String old = getDescription();
118            descriptionPane.setText(description);
119            firePropertyChange("description", old, getDescription());
120        }
121        
122        public String getDescription() {
123            return descriptionPane.getText();
124        }
125        
126        public void setIcon(Icon icon) {
127            Icon old = getIcon();
128            imagePanel.setIcon(icon);
129            firePropertyChange("icon", old, getIcon());
130        }
131        
132        public Icon getIcon() {
133            return imagePanel.getIcon();
134        }
135    }