KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > swing > DetachablePanel


1 package org.antlr.works.swing;
2
3 import org.antlr.works.prefs.AWPrefs;
4 import org.antlr.works.utils.IconManager;
5 import org.antlr.xjlib.appkit.frame.XJDialog;
6
7 import javax.swing.*;
8 import java.awt.*;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11 import java.beans.PropertyChangeEvent JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13 /*
14
15 [The "BSD licence"]
16 Copyright (c) 2005-2006 Jean Bovet
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
21 are met:
22
23 1. Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
25 2. Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
28 3. The name of the author may not be used to endorse or promote products
29 derived from this software without specific prior written permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 */

43
44 public class DetachablePanel extends JPanel {
45
46     protected DetachablePanelDelegate delegate;
47     protected JPanel mainPanel;
48     protected TitlePanel titlePanel;
49     protected String JavaDoc title;
50
51     protected boolean detached = false;
52     protected JButton detach;
53     protected XJDialog window;
54     protected int tag;
55
56     public DetachablePanel(String JavaDoc title, DetachablePanelDelegate delegate) {
57         super(new BorderLayout());
58
59         this.delegate = delegate;
60         this.title = title;
61
62         createTitleBar(title);
63
64         mainPanel = new JPanel(new BorderLayout());
65         add(mainPanel, BorderLayout.CENTER);
66
67         KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
68         focusManager.addPropertyChangeListener(
69             new PropertyChangeListener JavaDoc() {
70                 public void propertyChange(PropertyChangeEvent JavaDoc e) {
71                     String JavaDoc prop = e.getPropertyName();
72                     if(prop.equals("focusOwner") && e.getNewValue() != null) {
73                         Component c = (Component)e.getNewValue();
74                         if(isParentOf(c))
75                             focusGained();
76                         else
77                             focusLost();
78                     }
79                 }
80             }
81         );
82     }
83
84     public void createTitleBar(String JavaDoc title) {
85         Box box = Box.createHorizontalBox();
86         JLabel l = new JLabel(title);
87         l.setFont(l.getFont().deriveFont(11.0f));
88         l.setForeground(Color.white);
89         box.add(Box.createHorizontalStrut(2));
90         box.add(l);
91         box.add(Box.createHorizontalGlue());
92         box.add(detach = createDetachButton());
93
94         titlePanel = new TitlePanel();
95         titlePanel.setMinimumSize(new Dimension(0, 15));
96         titlePanel.setMaximumSize(new Dimension(0, 15));
97         titlePanel.setPreferredSize(new Dimension(0, 15));
98         titlePanel.add(box);
99
100         super.add(titlePanel, BorderLayout.NORTH);
101     }
102
103     public JButton createDetachButton() {
104         JButton detach = new JButton(IconManager.shared().getIconDetach());
105         detach.setBorder(null);
106         detach.setBorderPainted(false);
107         detach.setOpaque(false);
108         detach.setFocusable(false);
109
110         detach.addActionListener(new ActionListener JavaDoc() {
111             public void actionPerformed(ActionEvent JavaDoc e) {
112                 if(detached)
113                     attach();
114                 else
115                     detach();
116             }
117         });
118         return detach;
119     }
120
121     public boolean isParentOf(Component c) {
122         Component p = c.getParent();
123         return p != null && (p == this || isParentOf(p));
124     }
125
126     public void focusGained() {
127         titlePanel.setFocused(true);
128     }
129
130     public void focusLost() {
131         titlePanel.setFocused(false);
132     }
133
134     public void setTag(int tag) {
135         this.tag = tag;
136     }
137
138     public int getTag() {
139         return tag;
140     }
141
142     public void setVisible(boolean flag) {
143         super.setVisible(flag);
144         if(detached)
145             window.setVisible(flag);
146     }
147
148     public boolean isDetached() {
149         return detached;
150     }
151
152     private Point previousPosition;
153     private Dimension previousSize;
154
155     public void detach() {
156         detached = true;
157
158         Point p = getLocationOnScreen();
159
160         delegate.panelDoDetach(this);
161
162         detach.setIcon(IconManager.shared().getIconAttach());
163
164         if(AWPrefs.getDetachableChildren())
165             window = new DetachableWindow(delegate.panelParentContainer());
166         else
167             window = new DetachableWindow(null);
168         window.setTitle(title);
169         if(previousPosition == null) {
170             window.setPosition(p.x, p.y);
171         } else {
172             window.setPosition(previousPosition);
173         }
174
175         if(previousSize == null) {
176             window.setSize(getWidth(), getHeight());
177         } else {
178             window.setSize(previousSize);
179         }
180
181         window.getContentPane().add(this);
182
183         window.setVisible(true);
184     }
185
186     public void attach() {
187         detached = false;
188         previousPosition = window.getPosition();
189         previousSize = window.getSize();
190         window.getContentPane().remove(0);
191         window.close();
192         detach.setIcon(IconManager.shared().getIconDetach());
193         delegate.panelDoAttach(this);
194     }
195
196     public class TitlePanel extends JPanel {
197
198         public boolean focused = false;
199
200         public TitlePanel() {
201             super(new BorderLayout());
202         }
203
204         public void setFocused(boolean flag) {
205             this.focused = flag;
206             repaint();
207         }
208
209         public void paintComponent(Graphics g) {
210             Color startColor;
211             Color endColor;
212             if(focused) {
213                 startColor = new Color(0.1f, 0.6f, 0.9f);
214                 endColor = new Color(0.8f, 0.9f, 1.0f);
215             } else {
216                 startColor = new Color(0.7f, 0.7f, 0.7f);
217                 endColor = new Color(0.9f, 0.9f, 0.9f);
218             }
219
220             int x = getVisibleRect().width;
221             int y = getVisibleRect().height;
222
223             GradientPaint gradient = new GradientPaint(0, 0, startColor, x, y, endColor);
224
225             Graphics2D g2d = (Graphics2D)g;
226             g2d.setPaint(gradient);
227             g2d.fillRect(0, 0, x, y);
228         }
229     }
230
231     public class DetachableWindow extends XJDialog {
232
233         public DetachableWindow(Container container) {
234             super(container, false);
235         }
236
237         public void dialogWillCloseCancel() {
238             if(detached) {
239                 DetachablePanel.this.setVisible(false);
240                 delegate.panelDoClose(DetachablePanel.this);
241             }
242         }
243     }
244 }
245
Popular Tags