KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DockableLayout.java -- a more flexible BorderLayout
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2005 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 javax.swing.*;
27 import java.awt.event.*;
28 import java.awt.*;
29 import org.gjt.sp.jedit.*;
30 import org.gjt.sp.util.Log;
31 //}}}
32

33 public class DockableLayout implements LayoutManager2
34 {
35     // for backwards compatibility with plugins that fiddle with
36
// jEdit's UI layout
37
static final String JavaDoc CENTER = BorderLayout.CENTER;
38
39     public static final String JavaDoc TOP_TOOLBARS = "top-toolbars";
40     public static final String JavaDoc BOTTOM_TOOLBARS = "bottom-toolbars";
41
42     static final String JavaDoc TOP_BUTTONS = "top-buttons";
43     static final String JavaDoc LEFT_BUTTONS = "left-buttons";
44     static final String JavaDoc BOTTOM_BUTTONS = "bottom-buttons";
45     static final String JavaDoc RIGHT_BUTTONS = "right-buttons";
46
47     private boolean alternateLayout;
48     private Component topToolbars, bottomToolbars;
49     private Component center;
50
51     /* No good */
52     private DockablePanel top;
53     private DockablePanel left;
54     private DockablePanel bottom;
55     private DockablePanel right;
56
57     private Component topButtons, leftButtons, bottomButtons, rightButtons;
58
59     //{{{ getAlternateLayout() method
60
public boolean setAlternateLayout()
61     {
62         return alternateLayout;
63     } //}}}
64

65     //{{{ setAlternateLayout() method
66
public void setAlternateLayout(boolean alternateLayout)
67     {
68         this.alternateLayout = alternateLayout;
69     } //}}}
70

71     //{{{ addLayoutComponent() method
72
public void addLayoutComponent(String JavaDoc name, Component comp)
73     {
74         addLayoutComponent(comp,name);
75     } //}}}
76

77     //{{{ addLayoutComponent() method
78
public void addLayoutComponent(Component comp, Object JavaDoc cons)
79     {
80         if(cons == null || CENTER.equals(cons))
81             center = comp;
82         else if(TOP_TOOLBARS.equals(cons))
83             topToolbars = comp;
84         else if(BOTTOM_TOOLBARS.equals(cons))
85             bottomToolbars = comp;
86         else if(DockableWindowManager.TOP.equals(cons))
87             top = (DockablePanel)comp;
88         else if(DockableWindowManager.LEFT.equals(cons))
89             left = (DockablePanel)comp;
90         else if(DockableWindowManager.BOTTOM.equals(cons))
91             bottom = (DockablePanel)comp;
92         else if(DockableWindowManager.RIGHT.equals(cons))
93             right = (DockablePanel)comp;
94         else if(TOP_BUTTONS.equals(cons))
95             topButtons = comp;
96         else if(LEFT_BUTTONS.equals(cons))
97             leftButtons = comp;
98         else if(BOTTOM_BUTTONS.equals(cons))
99             bottomButtons = comp;
100         else if(RIGHT_BUTTONS.equals(cons))
101             rightButtons = comp;
102     } //}}}
103

104     //{{{ removeLayoutComponent() method
105
public void removeLayoutComponent(Component comp)
106     {
107         if(center == comp)
108             center = null;
109         else if(comp == topToolbars)
110             topToolbars = null;
111         else if(comp == bottomToolbars)
112             bottomToolbars = null;
113         else if(comp == top)
114             top = null;
115         else if(comp == left)
116             left = null;
117         else if(comp == bottom)
118             bottom = null;
119         else if(comp == right)
120             right = null;
121     } //}}}
122

123     //{{{ preferredLayoutSize() method
124
public Dimension preferredLayoutSize(Container parent)
125     {
126         Dimension prefSize = new Dimension(0,0);
127         Dimension _top = top.getPreferredSize();
128         Dimension _left = left.getPreferredSize();
129         Dimension _bottom = bottom.getPreferredSize();
130         Dimension _right = right.getPreferredSize();
131         Dimension _topButtons = topButtons.getPreferredSize();
132         Dimension _leftButtons = leftButtons.getPreferredSize();
133         Dimension _bottomButtons = bottomButtons.getPreferredSize();
134         Dimension _rightButtons = rightButtons.getPreferredSize();
135         Dimension _center = (center == null
136             ? new Dimension(0,0)
137             : center.getPreferredSize());
138         Dimension _topToolbars = (topToolbars == null
139             ? new Dimension(0,0)
140             : topToolbars.getPreferredSize());
141         Dimension _bottomToolbars = (bottomToolbars == null
142             ? new Dimension(0,0)
143             : bottomToolbars.getPreferredSize());
144
145         prefSize.height = _top.height + _bottom.height + _center.height
146             + _topButtons.height + _bottomButtons.height
147             + _topToolbars.height + _bottomToolbars.height;
148         prefSize.width = _left.width + _right.width
149             + Math.max(_center.width,
150             Math.max(_topToolbars.width,_bottomToolbars.width))
151             + _leftButtons.width + _rightButtons.width;
152
153         return prefSize;
154     } //}}}
155

156     //{{{ minimumLayoutSize() method
157
public Dimension minimumLayoutSize(Container parent)
158     {
159         // I'm lazy
160
return preferredLayoutSize(parent);
161     } //}}}
162

163     //{{{ maximumLayoutSize() method
164
public Dimension maximumLayoutSize(Container parent)
165     {
166         return new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE);
167     } //}}}
168

169     //{{{ layoutContainer() method
170
public void layoutContainer(Container parent)
171     {
172         Dimension size = parent.getSize();
173
174         Dimension _topToolbars = (topToolbars == null
175             ? new Dimension(0,0)
176             : topToolbars.getPreferredSize());
177         Dimension _bottomToolbars = (bottomToolbars == null
178             ? new Dimension(0,0)
179             : bottomToolbars.getPreferredSize());
180
181         int topButtonHeight = -1;
182         int bottomButtonHeight = -1;
183         int leftButtonWidth = -1;
184         int rightButtonWidth = -1;
185
186         Dimension _top = top.getPreferredSize();
187         Dimension _left = left.getPreferredSize();
188         Dimension _bottom = bottom.getPreferredSize();
189         Dimension _right = right.getPreferredSize();
190
191         int topHeight = _top.height;
192         int bottomHeight = _bottom.height;
193         int leftWidth = _left.width;
194         int rightWidth = _right.width;
195
196         boolean topEmpty = ((Container)topButtons)
197             .getComponentCount() <= 2;
198         boolean leftEmpty = ((Container)leftButtons)
199             .getComponentCount() <= 2;
200         boolean bottomEmpty = ((Container)bottomButtons)
201             .getComponentCount() <= 2;
202         boolean rightEmpty = ((Container)rightButtons)
203             .getComponentCount() <= 2;
204
205         Dimension closeBoxSize;
206         if(((Container)topButtons).getComponentCount() == 0)
207             closeBoxSize = new Dimension(0,0);
208         else
209         {
210             closeBoxSize = ((Container)topButtons)
211                 .getComponent(0).getPreferredSize();
212         }
213
214         int closeBoxWidth = Math.max(closeBoxSize.width,
215             closeBoxSize.height) + 1;
216
217         if(alternateLayout)
218         {
219             //{{{ Lay out independent buttons
220
int _width = size.width;
221
222             int padding = (leftEmpty&&rightEmpty)
223                 ? 0 : closeBoxWidth;
224
225             topButtonHeight = top.getWindowContainer()
226                 .getWrappedDimension(_width
227                 - closeBoxWidth * 2);
228             topButtons.setBounds(
229                 padding,
230                 0,
231                 size.width - padding * 2,
232                 topButtonHeight);
233
234             bottomButtonHeight = bottom.getWindowContainer()
235                 .getWrappedDimension(_width);
236             bottomButtons.setBounds(
237                 padding,
238                 size.height - bottomButtonHeight,
239                 size.width - padding * 2,
240                 bottomButtonHeight);
241
242             int _height = size.height
243                 - topButtonHeight
244                 - bottomButtonHeight;
245             //}}}
246

247             //{{{ Lay out dependent buttons
248
leftButtonWidth = left.getWindowContainer()
249                 .getWrappedDimension(_height);
250             leftButtons.setBounds(
251                 0,
252                 topHeight + topButtonHeight,
253                 leftButtonWidth,
254                 _height - topHeight - bottomHeight);
255
256             rightButtonWidth = right.getWindowContainer()
257                 .getWrappedDimension(_height);
258             rightButtons.setBounds(
259                 size.width - rightButtonWidth,
260                 topHeight + topButtonHeight,
261                 rightButtonWidth,
262                 _height - topHeight - bottomHeight);
263             //}}}
264

265             int[] dimensions = adjustDockingAreasToFit(
266                 size,
267                 topHeight,
268                 leftWidth,
269                 bottomHeight,
270                 rightWidth,
271                 topButtonHeight,
272                 leftButtonWidth,
273                 bottomButtonHeight,
274                 rightButtonWidth,
275                 _topToolbars,
276                 _bottomToolbars);
277
278             topHeight = dimensions[0];
279             leftWidth = dimensions[1];
280             bottomHeight = dimensions[2];
281             rightWidth = dimensions[3];
282
283             //{{{ Lay out docking areas
284
top.setBounds(
285                 0,
286                 topButtonHeight,
287                 size.width,
288                 topHeight);
289
290             bottom.setBounds(
291                 0,
292                 size.height
293                 - bottomHeight
294                 - bottomButtonHeight,
295                 size.width,
296                 bottomHeight);
297
298             left.setBounds(
299                 leftButtonWidth,
300                 topButtonHeight + topHeight,
301                 leftWidth,
302                 _height - topHeight - bottomHeight);
303
304             right.setBounds(
305                 _width - rightButtonWidth - rightWidth,
306                 topButtonHeight + topHeight,
307                 rightWidth,
308                 _height - topHeight - bottomHeight); //}}}
309
}
310         else
311         {
312             //{{{ Lay out independent buttons
313
int _height = size.height;
314
315             int padding = (topEmpty && bottomEmpty
316                 ? 0 : closeBoxWidth);
317
318             leftButtonWidth = left.getWindowContainer()
319                 .getWrappedDimension(_height
320                 - closeBoxWidth * 2);
321             leftButtons.setBounds(
322                 0,
323                 padding,
324                 leftButtonWidth,
325                 _height - padding * 2);
326
327             rightButtonWidth = right.getWindowContainer()
328                 .getWrappedDimension(_height);
329             rightButtons.setBounds(
330                 size.width - rightButtonWidth,
331                 padding,
332                 rightButtonWidth,
333                 _height - padding * 2);
334
335             int _width = size.width
336                 - leftButtonWidth
337                 - rightButtonWidth;
338             //}}}
339

340             //{{{ Lay out dependent buttons
341
topButtonHeight = top.getWindowContainer()
342                 .getWrappedDimension(_width);
343             topButtons.setBounds(
344                 leftButtonWidth + leftWidth,
345                 0,
346                 _width - leftWidth - rightWidth,
347                 topButtonHeight);
348
349             bottomButtonHeight = bottom.getWindowContainer()
350                 .getWrappedDimension(_width);
351             bottomButtons.setBounds(
352                 leftButtonWidth + leftWidth,
353                 _height - bottomButtonHeight,
354                 _width - leftWidth - rightWidth,
355                 bottomButtonHeight); //}}}
356

357             int[] dimensions = adjustDockingAreasToFit(
358                 size,
359                 topHeight,
360                 leftWidth,
361                 bottomHeight,
362                 rightWidth,
363                 topButtonHeight,
364                 leftButtonWidth,
365                 bottomButtonHeight,
366                 rightButtonWidth,
367                 _topToolbars,
368                 _bottomToolbars);
369
370             topHeight = dimensions[0];
371             leftWidth = dimensions[1];
372             bottomHeight = dimensions[2];
373             rightWidth = dimensions[3];
374
375             //{{{ Lay out docking areas
376
top.setBounds(
377                 leftButtonWidth + leftWidth,
378                 topButtonHeight,
379                 _width - leftWidth - rightWidth,
380                 topHeight);
381
382             bottom.setBounds(
383                 leftButtonWidth + leftWidth,
384                 size.height - bottomHeight - bottomButtonHeight,
385                 _width - leftWidth - rightWidth,
386                 bottomHeight);
387
388             left.setBounds(
389                 leftButtonWidth,
390                 0,
391                 leftWidth,
392                 _height);
393
394             right.setBounds(
395                 size.width - rightWidth - rightButtonWidth,
396                 0,
397                 rightWidth,
398                 _height); //}}}
399
}
400
401         //{{{ Position tool bars if they are managed by us
402
if(topToolbars != null)
403         {
404             topToolbars.setBounds(
405                 leftButtonWidth + leftWidth,
406                 topButtonHeight + topHeight,
407                 size.width - leftWidth - rightWidth
408                 - leftButtonWidth - rightButtonWidth,
409                 _topToolbars.height);
410         }
411
412         if(bottomToolbars != null)
413         {
414             bottomToolbars.setBounds(
415                 leftButtonWidth + leftWidth,
416                 size.height - bottomHeight
417                 - bottomButtonHeight
418                 - _bottomToolbars.height,
419                 size.width - leftWidth - rightWidth
420                 - leftButtonWidth - rightButtonWidth,
421                 _bottomToolbars.height);
422         } //}}}
423

424         //{{{ Position center (edit pane, or split pane)
425
if(center != null)
426         {
427             center.setBounds(
428                 leftButtonWidth + leftWidth,
429                 topButtonHeight + topHeight
430                 + _topToolbars.height,
431                 size.width
432                 - leftWidth
433                 - rightWidth
434                 - leftButtonWidth
435                 - rightButtonWidth,
436                 size.height
437                 - topHeight
438                 - topButtonHeight
439                 - bottomHeight
440                 - bottomButtonHeight
441                 - _topToolbars.height
442                 - _bottomToolbars.height);
443         } //}}}
444
} //}}}
445

446     //{{{ adjustDockingAreasToFit() method
447
private int[] adjustDockingAreasToFit(
448         Dimension size,
449         int topHeight,
450         int leftWidth,
451         int bottomHeight,
452         int rightWidth,
453         int topButtonHeight,
454         int leftButtonWidth,
455         int bottomButtonHeight,
456         int rightButtonWidth,
457         Dimension _topToolbars,
458         Dimension _bottomToolbars)
459     {
460         int maxTopHeight = size.height - bottomHeight
461             - topButtonHeight - bottomButtonHeight
462             - _topToolbars.height - _bottomToolbars.height;
463         topHeight = Math.min(Math.max(0,maxTopHeight),
464             topHeight);
465         leftWidth = Math.min(Math.max(0,
466             size.width - leftButtonWidth
467             - rightButtonWidth - rightWidth),leftWidth);
468         int maxBottomHeight = size.height - topHeight
469             - topButtonHeight - bottomButtonHeight
470             - _topToolbars.height - _bottomToolbars.height;
471         bottomHeight = Math.min(Math.max(0,maxBottomHeight),
472             bottomHeight);
473         rightWidth = Math.min(Math.max(0,
474             size.width - leftButtonWidth
475             - rightButtonWidth - leftWidth),rightWidth);
476
477         top.getWindowContainer().setDimension(topHeight);
478         left.getWindowContainer().setDimension(leftWidth);
479         bottom.getWindowContainer().setDimension(bottomHeight);
480         right.getWindowContainer().setDimension(rightWidth);
481
482         return new int[] {
483             topHeight,
484             leftWidth,
485             bottomHeight,
486             rightWidth
487         };
488     } //}}}
489

490     //{{{ getLayoutAlignmentX() method
491
public float getLayoutAlignmentX(Container target)
492     {
493         return 0.5f;
494     } //}}}
495

496     //{{{ getLayoutAlignmentY() method
497
public float getLayoutAlignmentY(Container target)
498     {
499         return 0.5f;
500     } //}}}
501

502     //{{{ invalidateLayout() method
503
public void invalidateLayout(Container target) {}
504     //}}}
505
}
506
Popular Tags