KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > DockablePanel


1 /*
2  * PanelWindowContainer.java - holds dockable windows
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import java.awt.CardLayout JavaDoc;
27 import java.awt.Cursor JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.Point JavaDoc;
31 import java.awt.event.MouseAdapter JavaDoc;
32 import java.awt.event.MouseEvent JavaDoc;
33 import java.awt.event.MouseMotionListener JavaDoc;
34
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.border.Border JavaDoc;
37 //}}}
38

39 /**
40  * @version $Id: DockablePanel.java 8690 2007-01-20 10:59:00Z kpouer $
41  */

42 class DockablePanel extends JPanel JavaDoc
43 {
44     private PanelWindowContainer panel;
45     private DockableWindowManager wm;
46
47     //{{{ DockablePanel constructor
48
DockablePanel(PanelWindowContainer panel)
49     {
50         super(new CardLayout JavaDoc());
51
52         this.panel = panel;
53         this.wm = panel.getDockableWindowManager();
54
55         ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler();
56         addMouseListener(resizeMouseHandler);
57         addMouseMotionListener(resizeMouseHandler);
58     } //}}}
59

60     //{{{ getWindowContainer() method
61
PanelWindowContainer getWindowContainer()
62     {
63         return panel;
64     } //}}}
65

66     //{{{ showDockable() method
67
void showDockable(String JavaDoc name)
68     {
69         ((CardLayout JavaDoc)getLayout()).show(this,name);
70     } //}}}
71

72     //{{{ getMinimumSize() method
73
public Dimension JavaDoc getMinimumSize()
74     {
75         return new Dimension JavaDoc(0,0);
76     } //}}}
77

78     //{{{ getPreferredSize() method
79
public Dimension JavaDoc getPreferredSize()
80     {
81         final String JavaDoc position = panel.getPosition();
82         final int dimension = panel.getDimension();
83
84         if(panel.getCurrent() == null)
85             return new Dimension JavaDoc(0,0);
86         else
87         {
88             if(position.equals(DockableWindowManager.TOP)
89                 || position.equals(DockableWindowManager.BOTTOM))
90             {
91                 if(dimension <= 0)
92                 {
93                     int height = super.getPreferredSize().height;
94                     panel.setDimension(height);
95                 }
96                 return new Dimension JavaDoc(0,
97                     dimension + PanelWindowContainer
98                     .SPLITTER_WIDTH);
99             }
100             else
101             {
102                 if(dimension <= 0)
103                 {
104                     int width = super.getPreferredSize().width;
105                     panel.setDimension(width);
106                 }
107                 return new Dimension JavaDoc(dimension +
108                     PanelWindowContainer.SPLITTER_WIDTH,
109                     0);
110             }
111         }
112     } //}}}
113

114     //{{{ setBounds() method
115
public void setBounds(int x, int y, int width, int height)
116     {
117         final String JavaDoc position = panel.getPosition();
118         final int dimension = panel.getDimension();
119
120         if(position.equals(DockableWindowManager.TOP) ||
121             position.equals(DockableWindowManager.BOTTOM))
122         {
123             if(dimension != 0 && height <= PanelWindowContainer.SPLITTER_WIDTH)
124                 panel.show(null);
125             else
126                 panel.setDimension(height);
127         }
128         else
129         {
130             if(dimension != 0 && width <= PanelWindowContainer.SPLITTER_WIDTH)
131                 panel.show(null);
132             else
133                 panel.setDimension(width);
134         }
135
136         super.setBounds(x,y,width,height);
137     } //}}}
138

139     //{{{ ResizeMouseHandler class
140
class ResizeMouseHandler extends MouseAdapter JavaDoc implements MouseMotionListener JavaDoc
141     {
142         /** This is true if the mouse is on the split bar. */
143         boolean canDrag;
144         Point JavaDoc dragStart;
145
146         //{{{ mousePressed() method
147
public void mousePressed(MouseEvent JavaDoc evt)
148         {
149             if(canDrag)
150             {
151                 wm.setResizePos(panel.getDimension(),panel);
152                 dragStart = evt.getPoint();
153             }
154         } //}}}
155

156         //{{{ mouseReleased() method
157
public void mouseReleased(MouseEvent JavaDoc evt)
158         {
159             if(canDrag)
160             {
161                 panel.setDimension(wm.resizePos
162                     + PanelWindowContainer
163                     .SPLITTER_WIDTH);
164                 wm.finishResizing();
165                 dragStart = null;
166                 wm.revalidate();
167             }
168         } //}}}
169

170         //{{{ mouseMoved() method
171
public void mouseMoved(MouseEvent JavaDoc evt)
172         {
173             Border JavaDoc border = getBorder();
174             if(border == null)
175             {
176                 // collapsed
177
return;
178             }
179
180             String JavaDoc position = panel.getPosition();
181
182             Insets JavaDoc insets = border.getBorderInsets(DockablePanel.this);
183             canDrag = false;
184             //{{{ Top...
185
if(position.equals(DockableWindowManager.TOP))
186             {
187                 if(evt.getY() >= getHeight() - insets.bottom)
188                     canDrag = true;
189             } //}}}
190
//{{{ Left...
191
else if(position.equals(DockableWindowManager.LEFT))
192             {
193                 if(evt.getX() >= getWidth() - insets.right)
194                     canDrag = true;
195             } //}}}
196
//{{{ Bottom...
197
else if(position.equals(DockableWindowManager.BOTTOM))
198             {
199                 if(evt.getY() <= insets.top)
200                     canDrag = true;
201             } //}}}
202
//{{{ Right...
203
else if(position.equals(DockableWindowManager.RIGHT))
204             {
205                 if(evt.getX() <= insets.left)
206                     canDrag = true;
207             } //}}}
208

209             if(canDrag)
210             {
211                 wm.setCursor(Cursor.getPredefinedCursor(
212                     getAppropriateCursor()));
213             }
214             else
215             {
216                 wm.setCursor(Cursor.getPredefinedCursor(
217                     Cursor.DEFAULT_CURSOR));
218             }
219         } //}}}
220

221         //{{{ mouseDragged() method
222
public void mouseDragged(MouseEvent JavaDoc evt)
223         {
224             if(!canDrag)
225                 return;
226
227             if(dragStart == null) // can't happen?
228
return;
229
230             int dimension = panel.getDimension();
231
232             String JavaDoc position = panel.getPosition();
233
234             //{{{ Top...
235
if(position.equals(DockableWindowManager.TOP))
236             {
237                 wm.setResizePos(
238                     evt.getY() - dragStart.y
239                     + dimension,
240                     panel);
241             } //}}}
242
//{{{ Left...
243
else if(position.equals(DockableWindowManager.LEFT))
244             {
245                 wm.setResizePos(evt.getX() - dragStart.x
246                     + dimension,
247                     panel);
248             } //}}}
249
//{{{ Bottom...
250
else if(position.equals(DockableWindowManager.BOTTOM))
251             {
252                 wm.setResizePos(dimension - evt.getY()
253                     + dragStart.y,
254                     panel);
255             } //}}}
256
//{{{ Right...
257
else if(position.equals(DockableWindowManager.RIGHT))
258             {
259                 wm.setResizePos(dimension - evt.getX()
260                     + dragStart.x,
261                     panel);
262             } //}}}
263
} //}}}
264

265         //{{{ mouseExited() method
266
public void mouseExited(MouseEvent JavaDoc evt)
267         {
268             if (dragStart == null)
269             {
270                 wm.setCursor(Cursor.getPredefinedCursor(
271                     Cursor.DEFAULT_CURSOR));
272             }
273         } //}}}
274

275         //{{{ getCursor() method
276
private int getAppropriateCursor()
277         {
278             String JavaDoc position = panel.getPosition();
279
280             if(position.equals(DockableWindowManager.TOP))
281                 return Cursor.N_RESIZE_CURSOR;
282             else if(position.equals(DockableWindowManager.LEFT))
283                 return Cursor.W_RESIZE_CURSOR;
284             else if(position.equals(DockableWindowManager.BOTTOM))
285                 return Cursor.S_RESIZE_CURSOR;
286             else if(position.equals(DockableWindowManager.RIGHT))
287                 return Cursor.E_RESIZE_CURSOR;
288             else
289                 throw new InternalError JavaDoc();
290         } //}}}
291
} //}}}
292
}
293
Popular Tags