]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/common/DockablePanel.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / gui / common / DockablePanel.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc.gui.common;\r
31 \r
32 import irc.EventDispatcher;\r
33 import irc.ListenerGroup;\r
34 \r
35 import java.awt.BorderLayout;\r
36 import java.awt.Color;\r
37 import java.awt.Component;\r
38 import java.awt.Frame;\r
39 import java.awt.Panel;\r
40 import java.awt.event.WindowEvent;\r
41 import java.awt.event.WindowListener;\r
42 \r
43 /**\r
44  * A panel that can be docked.\r
45  */\r
46 public class DockablePanel extends Panel implements WindowListener, Runnable {\r
47         private Component _comp;\r
48         private boolean _docked;\r
49         private Frame _frame;\r
50         private int _behaviour;\r
51         private ListenerGroup _listeners;\r
52 \r
53         /**\r
54          * Closing behaviour: the panel will be docked upon undocked window closure.\r
55          */\r
56         public static final int DOCK_ON_CLOSE = 0;\r
57 \r
58         /**\r
59          * Closing behaviour: nothing will be done upon undocked window closure.\r
60          */\r
61         public static final int DO_NOTHING_ON_CLOSE = 1;\r
62 \r
63         /**\r
64          * Create a new Dockable Panel using the given component inside, and the given\r
65          * color.\r
66          * \r
67          * @param c\r
68          *          the unique component of this panel.\r
69          * @param col\r
70          *          the background color when the component is undocked.\r
71          */\r
72         public DockablePanel(Component c, Color col) {\r
73                 setBackground(col);\r
74                 setLayout(new BorderLayout());\r
75                 _comp = c;\r
76                 _docked = true;\r
77                 add(_comp, BorderLayout.CENTER);\r
78                 validate();\r
79 \r
80                 _frame = new Frame();\r
81                 _frame.setLayout(new BorderLayout());\r
82                 _frame.addWindowListener(this);\r
83 \r
84                 _behaviour = DOCK_ON_CLOSE;\r
85                 _listeners = new ListenerGroup();\r
86         }\r
87 \r
88         /**\r
89          * Add a new dockable panel listener.\r
90          * \r
91          * @param lis\r
92          *          the listener to add.\r
93          */\r
94         public void addDockablePanelListener(DockablePanelListener lis) {\r
95                 _listeners.addListener(lis);\r
96         }\r
97 \r
98         /**\r
99          * Removes a dockable panel listener.\r
100          * \r
101          * @param lis\r
102          *          the listener to remove.\r
103          */\r
104         public void removeDockablePanelListener(DockablePanelListener lis) {\r
105                 _listeners.removeListener(lis);\r
106         }\r
107 \r
108         /**\r
109          * Set the undocked window closing behaviour.\r
110          * \r
111          * @param behaviour\r
112          *          the new behaviour. By default, the behaviour is DOCK_ON_CLOSE.\r
113          */\r
114         public void setClosingBehaviour(int behaviour) {\r
115                 _behaviour = behaviour;\r
116         }\r
117 \r
118         /**\r
119          * Get the undocked window closing behaviour.\r
120          * \r
121          * @return the window closing behaviour.\r
122          */\r
123         public int getClosingBehaviour() {\r
124                 return _behaviour;\r
125         }\r
126 \r
127         /**\r
128          * Get the component.\r
129          * \r
130          * @return the component.\r
131          */\r
132         public Component getComponent() {\r
133                 return _comp;\r
134         }\r
135 \r
136         /**\r
137          * Undock the panel.\r
138          * \r
139          * @param windowTitle\r
140          *          the undocked window title.\r
141          */\r
142         public void undock(String windowTitle) {\r
143                 if (_comp == null)\r
144                         return;\r
145                 if (!_docked)\r
146                         return;\r
147                 _docked = false;\r
148                 _comp.setVisible(true);\r
149                 remove(_comp);\r
150                 validate();\r
151                 _frame.add(_comp, BorderLayout.CENTER);\r
152                 _frame.pack();\r
153                 _frame.setTitle(windowTitle);\r
154                 _frame.show();\r
155         }\r
156 \r
157         /**\r
158          * Dock the panel.\r
159          */\r
160         public void dock() {\r
161                 if (_comp == null)\r
162                         return;\r
163                 if (_docked)\r
164                         return;\r
165                 _docked = true;\r
166                 _comp.setVisible(false);\r
167                 _frame.hide();\r
168                 _frame.remove(_comp);\r
169 \r
170                 add(_comp, BorderLayout.CENTER);\r
171                 _comp.setVisible(isVisible());\r
172                 validate();\r
173         }\r
174 \r
175         /**\r
176          * Check the dock state.\r
177          * \r
178          * @return if docked, false otherwise.\r
179          */\r
180         public boolean isDocked() {\r
181                 return _docked;\r
182         }\r
183 \r
184         /**\r
185          * If the source is undocked, this method will bring the undocked window to\r
186          * the front of the screen.\r
187          */\r
188         public void bring() {\r
189                 _frame.toFront();\r
190         }\r
191 \r
192         @Override\r
193         public void run() {\r
194                 if (_frame != null)\r
195                         _frame.dispose();\r
196                 _frame = null;\r
197         }\r
198 \r
199         /**\r
200          * Release this object.\r
201          */\r
202         public void release() {\r
203                 if (_frame == null)\r
204                         return;\r
205                 dock();\r
206                 _frame.removeAll();\r
207                 removeAll();\r
208                 _frame.removeWindowListener(this);\r
209                 // EventDispatcher.dispatchEventAsync(this,"disposeFrame",new Object[0]);\r
210                 Thread t = new Thread(this, "Frame disposal thread");\r
211                 t.start();\r
212                 _comp = null;\r
213         }\r
214 \r
215         @Override\r
216         public void setVisible(boolean b) {\r
217                 if (_comp == null)\r
218                         return;\r
219                 if (_docked)\r
220                         _comp.setVisible(b);\r
221                 super.setVisible(b);\r
222         }\r
223 \r
224         @Override\r
225         public void windowActivated(WindowEvent e) {\r
226                 // nothing here...\r
227         }\r
228 \r
229         @Override\r
230         public void windowClosed(WindowEvent e) {\r
231                 _listeners.sendEventAsync("DockablePanelWindowClosed", this);\r
232         }\r
233 \r
234         @Override\r
235         public void windowClosing(WindowEvent e) {\r
236                 _listeners.sendEventAsync("DockablePanelWindowClosing", this);\r
237                 if (_behaviour == DOCK_ON_CLOSE)\r
238                         EventDispatcher.dispatchEventAsync(this, "dock", new Object[0]);\r
239         }\r
240 \r
241         @Override\r
242         public void windowDeactivated(WindowEvent e) {\r
243                 // nothing here...\r
244         }\r
245 \r
246         @Override\r
247         public void windowDeiconified(WindowEvent e) {\r
248                 // nothing here...\r
249         }\r
250 \r
251         @Override\r
252         public void windowIconified(WindowEvent e) {\r
253                 // nothing here...\r
254         }\r
255 \r
256         @Override\r
257         public void windowOpened(WindowEvent e) {\r
258                 // nothing here...\r
259         }\r
260 \r
261 }\r