1 /*
2  *  Copyright (C) 2004 Kai Toedter
3  *  kai@toedter.com
4  *  www.toedter.com
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2
9  *  of the License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 package com.toedter.components;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.GradientPaint;
25 import java.awt.Graphics;
26 import java.awt.Graphics2D;
27 import java.awt.Paint;
28 
29 import javax.swing.BorderFactory;
30 import javax.swing.Icon;
31 import javax.swing.JComponent;
32 import javax.swing.JLabel;
33 import javax.swing.JPanel;
34 import javax.swing.border.Border;
35 
36 
37 /**
38  * A simple JPanel with a border and a title
39  *
40  * @author Kai Toedter
41  * @version $LastChangedRevision: 85 $
42  * @version $LastChangedDate: 2006-04-28 13:50:52 +0200 (Fr, 28 Apr 2006) $
43  */
44 public class JTitlePanel extends JPanel {
45 	private static final long serialVersionUID = 9104873267039717087L;
46 	protected JPanel northPanel;
47     protected JLabel label;
48 
49     /**
50      * Constructs a titled panel.
51      *
52      * @param title the title
53      * @param content the JComponent that contains the content
54      * @param outerBorder the outer border
55      */
JTitlePanel(String title, Icon icon, JComponent content, Border outerBorder)56     public JTitlePanel(String title, Icon icon, JComponent content, Border outerBorder) {
57         setLayout(new BorderLayout());
58 
59         label = new JLabel(title, icon, JLabel.LEADING);
60         label.setForeground(Color.WHITE);
61 
62         GradientPanel titlePanel = new GradientPanel(Color.BLACK);
63         titlePanel.setLayout(new BorderLayout());
64         titlePanel.add(label, BorderLayout.WEST);
65         int borderOffset = 2;
66         if(icon == null) {
67         	borderOffset += 1;
68         }
69         titlePanel.setBorder(BorderFactory.createEmptyBorder(borderOffset, 4, borderOffset, 1));
70         add(titlePanel, BorderLayout.NORTH);
71 
72         JPanel northPanel = new JPanel();
73         northPanel.setLayout(new BorderLayout());
74         northPanel.add(content,BorderLayout.NORTH);
75         northPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
76         add(northPanel, BorderLayout.CENTER);
77 
78         if (outerBorder == null) {
79             setBorder(BorderFactory.createLineBorder(Color.GRAY));
80         } else {
81             setBorder(BorderFactory.createCompoundBorder(outerBorder,
82                     BorderFactory.createLineBorder(Color.GRAY)));
83         }
84     }
85 
setTitle(String label, Icon icon)86     public void setTitle(String label, Icon icon) {
87     	this.label.setText(label);
88     	this.label.setIcon(icon);
89     }
90 
91     private static class GradientPanel extends JPanel {
92 		private static final long serialVersionUID = -6385751027379193053L;
93 
GradientPanel(Color background)94 		private GradientPanel(Color background) {
95             setBackground(background);
96         }
97 
paintComponent(Graphics g)98         public void paintComponent(Graphics g) {
99             super.paintComponent(g);
100 
101             if (isOpaque()) {
102                 // Color controlColor = UIManager.getColor("control");
103                 // Color controlColor = new Color(252, 198, 82);
104                 Color controlColor = new Color(99, 153, 255);
105                 int width = getWidth();
106                 int height = getHeight();
107 
108                 Graphics2D g2 = (Graphics2D) g;
109                 Paint oldPaint = g2.getPaint();
110                 g2.setPaint(new GradientPaint(0, 0, getBackground(), width, 0,
111                         controlColor));
112                 g2.fillRect(0, 0, width, height);
113                 g2.setPaint(oldPaint);
114             }
115         }
116     }
117 }
118