001: package com.opensymphony.workflow.designer.swing;
002:
003: import java.awt.*;
004: import java.awt.event.ActionListener;
005: import java.awt.event.ActionEvent;
006:
007: import javax.swing.*;
008: import javax.swing.border.AbstractBorder;
009:
010: public class FramePanel extends JPanel {
011: private JLabel titleLabel;
012: private GradientPanel gradientPanel;
013: private JPanel headerPanel;
014: private boolean isSelected;
015: private boolean closeable = false;
016:
017: /**
018: * Constructs a <code>FramePanel</code> for the specified
019: * icon, title, tool bar, and content panel.
020: */
021: public FramePanel(Icon frameIcon, String title, JToolBar bar,
022: JComponent content, boolean closeable) {
023: super (new BorderLayout());
024: this .isSelected = false;
025: this .closeable = closeable;
026: this .titleLabel = new JLabel(title, frameIcon,
027: SwingConstants.LEADING);
028: JPanel top = buildHeader(titleLabel, bar);
029:
030: add(top, BorderLayout.NORTH);
031: if (content != null) {
032: setContent(content);
033: }
034: setBorder(new ShadowBorder());
035: setSelected(true);
036: updateHeader();
037: }
038:
039: /**
040: * Constructs a <code>FramePanel</code> for the specified
041: * title, tool bar, and content panel.
042: */
043: public FramePanel(String title, JToolBar bar, JComponent c,
044: boolean closeable) {
045: this (null, title, bar, c, closeable);
046: }
047:
048: /**
049: * Constructs a <code>FramePanel</code> for the specified
050: * icon, and title.
051: */
052: public FramePanel(Icon icon, String title, boolean closeable) {
053: this (icon, title, null, null, closeable);
054: }
055:
056: /**
057: * Constructs a <code>FramePanel</code> for the specified title.
058: */
059: public FramePanel(String title, boolean closeable) {
060: this (null, title, null, null, closeable);
061: }
062:
063: public Icon getFrameIcon() {
064: return titleLabel.getIcon();
065: }
066:
067: public void setFrameIcon(Icon icon) {
068: titleLabel.setIcon(icon);
069: }
070:
071: public String getTitle() {
072: return titleLabel.getText();
073: }
074:
075: public void setTitle(String text) {
076: titleLabel.setText(text);
077: }
078:
079: public void setToolBar(JToolBar bar) {
080: if (bar != null) {
081: bar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
082: headerPanel.add(bar, BorderLayout.EAST);
083: }
084: }
085:
086: public Component getContent() {
087: return hasContent() ? getComponent(1) : null;
088: }
089:
090: /**
091: * Set the component to show in the panel.
092: */
093: public void setContent(Component content) {
094: if (hasContent()) {
095: remove(getContent());
096: }
097: add(content, BorderLayout.CENTER);
098: }
099:
100: public boolean isSelected() {
101: return isSelected;
102: }
103:
104: public void setSelected(boolean selected) {
105: isSelected = selected;
106: updateHeader();
107: }
108:
109: private JPanel buildHeader(JLabel label, JToolBar bar) {
110: gradientPanel = new GradientPanel(new BorderLayout(),
111: getHeaderBackground());
112: label.setOpaque(false);
113:
114: gradientPanel.add(label, BorderLayout.WEST);
115: gradientPanel.setBorder(BorderFactory.createEmptyBorder(3, 4,
116: 3, 1));
117:
118: headerPanel = new JPanel(new BorderLayout());
119: headerPanel.add(gradientPanel, BorderLayout.CENTER);
120: setToolBar(bar);
121: headerPanel.setBorder(new RaisedHeaderBorder());
122: headerPanel.setOpaque(false);
123: return headerPanel;
124: }
125:
126: private void updateHeader() {
127: gradientPanel.setBackground(getHeaderBackground());
128: gradientPanel.setOpaque(isSelected());
129: if (closeable) {
130: JButton button = new JButton(new ImageIcon(getClass()
131: .getResource("/images/close.gif")));
132: button.setBorderPainted(false);
133: button.setOpaque(false);
134: button.setMargin(new Insets(0, 0, 0, 0));
135: button.addActionListener(new ActionListener() {
136: public void actionPerformed(ActionEvent e) {
137: close();
138: }
139: });
140: gradientPanel.add(button, BorderLayout.EAST);
141: }
142: titleLabel.setForeground(getTextForeground(isSelected()));
143: headerPanel.repaint();
144: };
145:
146: public void close() {
147: if (closeable) {
148: JComponent parent = (JComponent) getParent();
149: //parent.remove(SimpleInternalFrame.this);
150: setVisible(false);
151: parent.revalidate();
152: }
153: }
154:
155: public void updateUI() {
156: super .updateUI();
157: if (titleLabel != null) {
158: updateHeader();
159: }
160: }
161:
162: private boolean hasContent() {
163: return getComponentCount() > 1;
164: }
165:
166: protected Color getTextForeground(boolean selected) {
167: Color c = UIManager
168: .getColor(selected ? "FramePanel.activeTitleForeground"
169: : "FramePanel.inactiveTitleForeground");
170: if (c != null) {
171: return c;
172: }
173: return UIManager
174: .getColor(selected ? "InternalFrame.activeTitleForeground"
175: : "Label.foreground");
176: }
177:
178: protected Color getHeaderBackground() {
179: Color c = UIManager
180: .getColor("FramePanel.activeTitleBackground");
181: if (c != null)
182: return c;
183: return UIManager
184: .getColor("InternalFrame.activeTitleBackground");
185: }
186:
187: private static class RaisedHeaderBorder extends AbstractBorder {
188: private static final Insets INSETS = new Insets(1, 1, 1, 0);
189:
190: public Insets getBorderInsets(Component c) {
191: return INSETS;
192: }
193:
194: public void paintBorder(Component c, Graphics g, int x, int y,
195: int w, int h) {
196: g.translate(x, y);
197: g.setColor(UIManager.getColor("controlLtHighlight"));
198: g.fillRect(0, 0, w, 1);
199: g.fillRect(0, 1, 1, h - 1);
200: g.setColor(UIManager.getColor("controlShadow"));
201: g.fillRect(0, h - 1, w, h);
202: g.translate(-x, -y);
203: }
204: }
205:
206: private static class ShadowBorder extends AbstractBorder {
207: private static final Insets INSETS = new Insets(1, 1, 3, 3);
208:
209: public Insets getBorderInsets(Component c) {
210: return INSETS;
211: }
212:
213: public void paintBorder(Component c, Graphics g, int x, int y,
214: int w, int h) {
215:
216: Color shadow = UIManager.getColor("controlShadow");
217: Color lightShadow = new Color(shadow.getRed(), shadow
218: .getGreen(), shadow.getBlue(), 170);
219: Color lighterShadow = new Color(shadow.getRed(), shadow
220: .getGreen(), shadow.getBlue(), 70);
221: g.translate(x, y);
222:
223: g.setColor(shadow);
224: g.fillRect(0, 0, w - 3, 1);
225: g.fillRect(0, 0, 1, h - 3);
226: g.fillRect(w - 3, 1, 1, h - 3);
227: g.fillRect(1, h - 3, w - 3, 1);
228: // Shadow line 1
229: g.setColor(lightShadow);
230: g.fillRect(w - 3, 0, 1, 1);
231: g.fillRect(0, h - 3, 1, 1);
232: g.fillRect(w - 2, 1, 1, h - 3);
233: g.fillRect(1, h - 2, w - 3, 1);
234: // Shadow line2
235: g.setColor(lighterShadow);
236: g.fillRect(w - 2, 0, 1, 1);
237: g.fillRect(0, h - 2, 1, 1);
238: g.fillRect(w - 2, h - 2, 1, 1);
239: g.fillRect(w - 1, 1, 1, h - 2);
240: g.fillRect(1, h - 1, w - 2, 1);
241: g.translate(-x, -y);
242: }
243: }
244:
245: private static class GradientPanel extends JPanel {
246: private GradientPanel(LayoutManager lm, Color background) {
247: super (lm);
248: setBackground(background);
249: }
250:
251: public void paintComponent(Graphics g) {
252: super .paintComponent(g);
253: if (!isOpaque()) {
254: return;
255: }
256: Color control = UIManager.getColor("control");
257: int width = getWidth();
258: int height = getHeight();
259:
260: Graphics2D g2 = (Graphics2D) g;
261: Paint storedPaint = g2.getPaint();
262: g2.setPaint(new GradientPaint(0, 0, getBackground(), width,
263: 0, control));
264: g2.fillRect(0, 0, width, height);
265: g2.setPaint(storedPaint);
266: }
267: }
268:
269: }
|