KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > FramePanel


1 package com.opensymphony.workflow.designer.swing;
2
3 import java.awt.*;
4 import java.awt.event.ActionListener JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6
7 import javax.swing.*;
8 import javax.swing.border.AbstractBorder JavaDoc;
9
10 public class FramePanel extends JPanel
11 {
12   private JLabel titleLabel;
13   private GradientPanel gradientPanel;
14   private JPanel headerPanel;
15   private boolean isSelected;
16   private boolean closeable = false;
17
18   /**
19    * Constructs a <code>FramePanel</code> for the specified
20    * icon, title, tool bar, and content panel.
21    */

22   public FramePanel(Icon frameIcon, String JavaDoc title, JToolBar bar, JComponent content, boolean closeable)
23   {
24     super(new BorderLayout());
25     this.isSelected = false;
26     this.closeable = closeable;
27     this.titleLabel = new JLabel(title, frameIcon, SwingConstants.LEADING);
28     JPanel top = buildHeader(titleLabel, bar);
29
30     add(top, BorderLayout.NORTH);
31     if(content != null)
32     {
33       setContent(content);
34     }
35     setBorder(new ShadowBorder());
36     setSelected(true);
37     updateHeader();
38   }
39
40   /**
41    * Constructs a <code>FramePanel</code> for the specified
42    * title, tool bar, and content panel.
43    */

44   public FramePanel(String JavaDoc title, JToolBar bar, JComponent c, boolean closeable)
45   {
46     this(null, title, bar, c, closeable);
47   }
48
49   /**
50    * Constructs a <code>FramePanel</code> for the specified
51    * icon, and title.
52    */

53   public FramePanel(Icon icon, String JavaDoc title, boolean closeable)
54   {
55     this(icon, title, null, null, closeable);
56   }
57
58   /**
59    * Constructs a <code>FramePanel</code> for the specified title.
60    */

61   public FramePanel(String JavaDoc title, boolean closeable)
62   {
63     this(null, title, null, null, closeable);
64   }
65
66
67   public Icon getFrameIcon()
68   {
69     return titleLabel.getIcon();
70   }
71
72   public void setFrameIcon(Icon icon)
73   {
74     titleLabel.setIcon(icon);
75   }
76
77   public String JavaDoc getTitle()
78   {
79     return titleLabel.getText();
80   }
81
82   public void setTitle(String JavaDoc text)
83   {
84     titleLabel.setText(text);
85   }
86
87   public void setToolBar(JToolBar bar)
88   {
89     if(bar != null)
90     {
91       bar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
92       headerPanel.add(bar, BorderLayout.EAST);
93     }
94   }
95
96   public Component getContent()
97   {
98     return hasContent() ? getComponent(1) : null;
99   }
100
101   /**
102    * Set the component to show in the panel.
103    */

104   public void setContent(Component content)
105   {
106     if(hasContent())
107     {
108       remove(getContent());
109     }
110     add(content, BorderLayout.CENTER);
111   }
112
113   public boolean isSelected()
114   {
115     return isSelected;
116   }
117
118   public void setSelected(boolean selected)
119   {
120     isSelected = selected;
121     updateHeader();
122   }
123
124   private JPanel buildHeader(JLabel label, JToolBar bar)
125   {
126     gradientPanel = new GradientPanel(new BorderLayout(), getHeaderBackground());
127     label.setOpaque(false);
128
129     gradientPanel.add(label, BorderLayout.WEST);
130     gradientPanel.setBorder(BorderFactory.createEmptyBorder(3, 4, 3, 1));
131
132     headerPanel = new JPanel(new BorderLayout());
133     headerPanel.add(gradientPanel, BorderLayout.CENTER);
134     setToolBar(bar);
135     headerPanel.setBorder(new RaisedHeaderBorder());
136     headerPanel.setOpaque(false);
137     return headerPanel;
138   }
139
140   private void updateHeader()
141   {
142     gradientPanel.setBackground(getHeaderBackground());
143     gradientPanel.setOpaque(isSelected());
144     if(closeable)
145     {
146       JButton button = new JButton(new ImageIcon(getClass().getResource("/images/close.gif")));
147       button.setBorderPainted(false);
148       button.setOpaque(false);
149       button.setMargin(new Insets(0, 0, 0, 0));
150       button.addActionListener(new ActionListener JavaDoc()
151       {
152         public void actionPerformed(ActionEvent JavaDoc e)
153         {
154           close();
155         }
156       });
157       gradientPanel.add(button, BorderLayout.EAST);
158     }
159     titleLabel.setForeground(getTextForeground(isSelected()));
160     headerPanel.repaint();
161   };
162
163   public void close()
164   {
165     if(closeable)
166     {
167       JComponent parent = (JComponent)getParent();
168       //parent.remove(SimpleInternalFrame.this);
169
setVisible(false);
170       parent.revalidate();
171     }
172   }
173
174   public void updateUI()
175   {
176     super.updateUI();
177     if(titleLabel != null)
178     {
179       updateHeader();
180     }
181   }
182
183   private boolean hasContent()
184   {
185     return getComponentCount() > 1;
186   }
187
188   protected Color getTextForeground(boolean selected)
189   {
190     Color c = UIManager.getColor(selected ? "FramePanel.activeTitleForeground" : "FramePanel.inactiveTitleForeground");
191     if(c != null)
192     {
193       return c;
194     }
195     return UIManager.getColor(selected ? "InternalFrame.activeTitleForeground" : "Label.foreground");
196   }
197
198   protected Color getHeaderBackground()
199   {
200     Color c = UIManager.getColor("FramePanel.activeTitleBackground");
201     if(c != null)
202       return c;
203     return UIManager.getColor("InternalFrame.activeTitleBackground");
204   }
205
206
207   private static class RaisedHeaderBorder extends AbstractBorder JavaDoc
208   {
209     private static final Insets INSETS = new Insets(1, 1, 1, 0);
210
211     public Insets getBorderInsets(Component c)
212     {
213       return INSETS;
214     }
215
216     public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
217     {
218       g.translate(x, y);
219       g.setColor(UIManager.getColor("controlLtHighlight"));
220       g.fillRect(0, 0, w, 1);
221       g.fillRect(0, 1, 1, h - 1);
222       g.setColor(UIManager.getColor("controlShadow"));
223       g.fillRect(0, h - 1, w, h);
224       g.translate(-x, -y);
225     }
226   }
227
228   private static class ShadowBorder extends AbstractBorder JavaDoc
229   {
230     private static final Insets INSETS = new Insets(1, 1, 3, 3);
231
232     public Insets getBorderInsets(Component c)
233     {
234       return INSETS;
235     }
236
237     public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
238     {
239
240       Color shadow = UIManager.getColor("controlShadow");
241       Color lightShadow = new Color(shadow.getRed(), shadow.getGreen(), shadow.getBlue(), 170);
242       Color lighterShadow = new Color(shadow.getRed(), shadow.getGreen(), shadow.getBlue(), 70);
243       g.translate(x, y);
244
245       g.setColor(shadow);
246       g.fillRect(0, 0, w - 3, 1);
247       g.fillRect(0, 0, 1, h - 3);
248       g.fillRect(w - 3, 1, 1, h - 3);
249       g.fillRect(1, h - 3, w - 3, 1);
250       // Shadow line 1
251
g.setColor(lightShadow);
252       g.fillRect(w - 3, 0, 1, 1);
253       g.fillRect(0, h - 3, 1, 1);
254       g.fillRect(w - 2, 1, 1, h - 3);
255       g.fillRect(1, h - 2, w - 3, 1);
256       // Shadow line2
257
g.setColor(lighterShadow);
258       g.fillRect(w - 2, 0, 1, 1);
259       g.fillRect(0, h - 2, 1, 1);
260       g.fillRect(w - 2, h - 2, 1, 1);
261       g.fillRect(w - 1, 1, 1, h - 2);
262       g.fillRect(1, h - 1, w - 2, 1);
263       g.translate(-x, -y);
264     }
265   }
266
267   private static class GradientPanel extends JPanel
268   {
269     private GradientPanel(LayoutManager lm, Color background)
270     {
271       super(lm);
272       setBackground(background);
273     }
274
275     public void paintComponent(Graphics g)
276     {
277       super.paintComponent(g);
278       if(!isOpaque())
279       {
280         return;
281       }
282       Color control = UIManager.getColor("control");
283       int width = getWidth();
284       int height = getHeight();
285
286       Graphics2D g2 = (Graphics2D)g;
287       Paint storedPaint = g2.getPaint();
288       g2.setPaint(new GradientPaint(0, 0, getBackground(), width, 0, control));
289       g2.fillRect(0, 0, width, height);
290       g2.setPaint(storedPaint);
291     }
292   }
293
294 }
Popular Tags