PageRenderTime 105ms CodeModel.GetById 40ms app.highlight 33ms RepoModel.GetById 27ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/DockableLayout.java

#
Java | 462 lines | 350 code | 57 blank | 55 comment | 40 complexity | 6064f46da0a55ffc2d21e2cbe8c3e329 MD5 | raw file
  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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import java.awt.*;
 27//}}}
 28
 29public class DockableLayout implements LayoutManager2
 30{
 31	// for backwards compatibility with plugins that fiddle with
 32	// jEdit's UI layout
 33	static final String CENTER = BorderLayout.CENTER;
 34
 35	static final String TOP_BUTTONS = "top-buttons";
 36	static final String LEFT_BUTTONS = "left-buttons";
 37	static final String BOTTOM_BUTTONS = "bottom-buttons";
 38	static final String RIGHT_BUTTONS = "right-buttons";
 39
 40	private boolean alternateLayout;
 41	private Component center;
 42
 43	/* No good */
 44	private DockablePanel top;
 45	private DockablePanel left;
 46	private DockablePanel bottom;
 47	private DockablePanel right;
 48
 49	private Component topButtons, leftButtons, bottomButtons, rightButtons;
 50
 51	//{{{ isAlternateLayout() method
 52	/**
 53	 * jEdit View option: wide horizontal docking areas versus tall vertical docking areas 
 54	 * @return true if using the "alternate layout"
 55	 */
 56	public boolean isAlternateLayout()
 57	{
 58		return alternateLayout;
 59	} //}}}
 60
 61	//{{{ setAlternateLayout() method
 62	public void setAlternateLayout(boolean alternateLayout)
 63	{
 64		this.alternateLayout = alternateLayout;
 65	} //}}}
 66
 67	//{{{ addLayoutComponent() method
 68	public void addLayoutComponent(String name, Component comp)
 69	{
 70		addLayoutComponent(comp, name);
 71	} //}}}
 72
 73	//{{{ addLayoutComponent() method
 74	public void addLayoutComponent(Component comp, Object cons)
 75	{
 76		if(cons == null || CENTER.equals(cons))
 77			center = comp;
 78		else if(DockableWindowManager.TOP.equals(cons))
 79			top = (DockablePanel)comp;
 80		else if(DockableWindowManager.LEFT.equals(cons))
 81			left = (DockablePanel)comp;
 82		else if(DockableWindowManager.BOTTOM.equals(cons))
 83			bottom = (DockablePanel)comp;
 84		else if(DockableWindowManager.RIGHT.equals(cons))
 85			right = (DockablePanel)comp;
 86		else if(TOP_BUTTONS.equals(cons))
 87			topButtons = comp;
 88		else if(LEFT_BUTTONS.equals(cons))
 89			leftButtons = comp;
 90		else if(BOTTOM_BUTTONS.equals(cons))
 91			bottomButtons = comp;
 92		else if(RIGHT_BUTTONS.equals(cons))
 93			rightButtons = comp;
 94	} //}}}
 95
 96	//{{{ removeLayoutComponent() method
 97	public void removeLayoutComponent(Component comp)
 98	{
 99		if(center == comp)
100			center = null;
101		else if(comp == top)
102			top = null;
103		else if(comp == left)
104			left = null;
105		else if(comp == bottom)
106			bottom = null;
107		else if(comp == right)
108			right = null;
109	} //}}}
110
111	//{{{ preferredLayoutSize() method
112	public Dimension preferredLayoutSize(Container parent)
113	{
114		Dimension prefSize = new Dimension(0,0);
115		Dimension _top = top.getPreferredSize();
116		Dimension _left = left.getPreferredSize();
117		Dimension _bottom = bottom.getPreferredSize();
118		Dimension _right = right.getPreferredSize();
119		Dimension _topButtons = topButtons.getPreferredSize();
120		Dimension _leftButtons = leftButtons.getPreferredSize();
121		Dimension _bottomButtons = bottomButtons.getPreferredSize();
122		Dimension _rightButtons = rightButtons.getPreferredSize();
123		Dimension _center = (center == null
124			? new Dimension(0,0)
125			: center.getPreferredSize());
126		Dimension _topToolbars = new Dimension(0,0);
127		Dimension _bottomToolbars = new Dimension(0,0);
128
129		prefSize.height = _top.height + _bottom.height + _center.height
130			+ _topButtons.height + _bottomButtons.height
131			+ _topToolbars.height + _bottomToolbars.height;
132		prefSize.width = _left.width + _right.width
133			+ Math.max(_center.width,
134			Math.max(_topToolbars.width,_bottomToolbars.width))
135			+ _leftButtons.width + _rightButtons.width;
136
137		return prefSize;
138	} //}}}
139
140	//{{{ minimumLayoutSize() method
141	public Dimension minimumLayoutSize(Container parent)
142	{
143		// I'm lazy
144		return preferredLayoutSize(parent);
145	} //}}}
146
147	//{{{ maximumLayoutSize() method
148	public Dimension maximumLayoutSize(Container parent)
149	{
150		return new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE);
151	} //}}}
152
153	//{{{ layoutContainer() method
154	public void layoutContainer(Container parent)
155	{
156		Dimension size = parent.getSize();
157
158		Dimension _topToolbars = new Dimension(0,0);
159		Dimension _bottomToolbars = new Dimension(0,0);
160
161		int topButtonHeight = -1;
162		int bottomButtonHeight = -1;
163		int leftButtonWidth = -1;
164		int rightButtonWidth = -1;
165
166		Dimension _top = top.getPreferredSize();
167		Dimension _left = left.getPreferredSize();
168		Dimension _bottom = bottom.getPreferredSize();
169		Dimension _right = right.getPreferredSize();
170
171		int topHeight = _top.height;
172		int bottomHeight = _bottom.height;
173		int leftWidth = _left.width;
174		int rightWidth = _right.width;
175
176		boolean topEmpty = ((Container)topButtons)
177			.getComponentCount() <= 2;
178		boolean leftEmpty = ((Container)leftButtons)
179			.getComponentCount() <= 2;
180		boolean bottomEmpty = ((Container)bottomButtons)
181			.getComponentCount() <= 2;
182		boolean rightEmpty = ((Container)rightButtons)
183			.getComponentCount() <= 2;
184
185		Dimension closeBoxSize;
186		if(((Container)topButtons).getComponentCount() == 0)
187			closeBoxSize = new Dimension(0,0);
188		else
189		{
190			closeBoxSize = ((Container)topButtons)
191				.getComponent(0).getPreferredSize();
192		}
193
194		int closeBoxWidth = Math.max(closeBoxSize.width,
195			closeBoxSize.height) + 1;
196
197		if(alternateLayout)
198		{
199			//{{{ Lay out independent buttons
200			int _width = size.width;
201
202			int padding = (leftEmpty&&rightEmpty)
203				? 0 : closeBoxWidth;
204
205			topButtonHeight = top.getWindowContainer()
206				.getWrappedDimension(_width
207				- closeBoxWidth * 2);
208			topButtons.setBounds(
209				padding,
210				0,
211				size.width - padding * 2,
212				topButtonHeight);
213
214			bottomButtonHeight = bottom.getWindowContainer()
215				.getWrappedDimension(_width);
216			bottomButtons.setBounds(
217				padding,
218				size.height - bottomButtonHeight,
219				size.width - padding * 2,
220				bottomButtonHeight);
221
222			int _height = size.height
223				- topButtonHeight
224				- bottomButtonHeight;
225			//}}}
226
227			//{{{ Lay out dependent buttons
228			leftButtonWidth = left.getWindowContainer()
229				.getWrappedDimension(_height);
230			leftButtons.setBounds(
231				0,
232				topHeight + topButtonHeight,
233				leftButtonWidth,
234				_height - topHeight - bottomHeight);
235
236			rightButtonWidth = right.getWindowContainer()
237				.getWrappedDimension(_height);
238			rightButtons.setBounds(
239				size.width - rightButtonWidth,
240				topHeight + topButtonHeight,
241				rightButtonWidth,
242				_height - topHeight - bottomHeight);
243			//}}}
244
245			int[] dimensions = adjustDockingAreasToFit(
246				size,
247				topHeight,
248				leftWidth,
249				bottomHeight,
250				rightWidth,
251				topButtonHeight,
252				leftButtonWidth,
253				bottomButtonHeight,
254				rightButtonWidth,
255				_topToolbars,
256				_bottomToolbars);
257
258			topHeight = dimensions[0];
259			leftWidth = dimensions[1];
260			bottomHeight = dimensions[2];
261			rightWidth = dimensions[3];
262
263			//{{{ Lay out docking areas
264			top.setBounds(
265				0,
266				topButtonHeight,
267				size.width,
268				topHeight);
269
270			bottom.setBounds(
271				0,
272				size.height
273				- bottomHeight
274				- bottomButtonHeight,
275				size.width,
276				bottomHeight);
277
278			left.setBounds(
279				leftButtonWidth,
280				topButtonHeight + topHeight,
281				leftWidth,
282				_height - topHeight - bottomHeight);
283
284			right.setBounds(
285				_width - rightButtonWidth - rightWidth,
286				topButtonHeight + topHeight,
287				rightWidth,
288				_height - topHeight - bottomHeight); //}}}
289		}
290		else
291		{
292			//{{{ Lay out independent buttons
293			int _height = size.height;
294
295			int padding = (topEmpty && bottomEmpty
296				? 0 : closeBoxWidth);
297
298			leftButtonWidth = left.getWindowContainer()
299				.getWrappedDimension(_height
300				- closeBoxWidth * 2);
301			leftButtons.setBounds(
302				0,
303				padding,
304				leftButtonWidth,
305				_height - padding * 2);
306
307			rightButtonWidth = right.getWindowContainer()
308				.getWrappedDimension(_height);
309			rightButtons.setBounds(
310				size.width - rightButtonWidth,
311				padding,
312				rightButtonWidth,
313				_height - padding * 2);
314
315			int _width = size.width
316				- leftButtonWidth
317				- rightButtonWidth;
318			//}}}
319
320			//{{{ Lay out dependent buttons
321			topButtonHeight = top.getWindowContainer()
322				.getWrappedDimension(_width);
323			topButtons.setBounds(
324				leftButtonWidth + leftWidth,
325				0,
326				_width - leftWidth - rightWidth,
327				topButtonHeight);
328
329			bottomButtonHeight = bottom.getWindowContainer()
330				.getWrappedDimension(_width);
331			bottomButtons.setBounds(
332				leftButtonWidth + leftWidth,
333				_height - bottomButtonHeight,
334				_width - leftWidth - rightWidth,
335				bottomButtonHeight); //}}}
336
337			int[] dimensions = adjustDockingAreasToFit(
338				size,
339				topHeight,
340				leftWidth,
341				bottomHeight,
342				rightWidth,
343				topButtonHeight,
344				leftButtonWidth,
345				bottomButtonHeight,
346				rightButtonWidth,
347				_topToolbars,
348				_bottomToolbars);
349
350			topHeight = dimensions[0];
351			leftWidth = dimensions[1];
352			bottomHeight = dimensions[2];
353			rightWidth = dimensions[3];
354
355			//{{{ Lay out docking areas
356			top.setBounds(
357				leftButtonWidth + leftWidth,
358				topButtonHeight,
359				_width - leftWidth - rightWidth,
360				topHeight);
361
362			bottom.setBounds(
363				leftButtonWidth + leftWidth,
364				size.height - bottomHeight - bottomButtonHeight,
365				_width - leftWidth - rightWidth,
366				bottomHeight);
367
368			left.setBounds(
369				leftButtonWidth,
370				0,
371				leftWidth,
372				_height);
373
374			right.setBounds(
375				size.width - rightWidth - rightButtonWidth,
376				0,
377				rightWidth,
378				_height); //}}}
379		}
380
381		//{{{ Position center (edit pane, or split pane)
382		if(center != null)
383		{
384			center.setBounds(
385				leftButtonWidth + leftWidth,
386				topButtonHeight + topHeight
387				+ _topToolbars.height,
388				size.width
389				- leftWidth
390				- rightWidth
391				- leftButtonWidth
392				- rightButtonWidth,
393				size.height
394				- topHeight
395				- topButtonHeight
396				- bottomHeight
397				- bottomButtonHeight
398				- _topToolbars.height
399				- _bottomToolbars.height);
400		} //}}}
401	} //}}}
402
403	//{{{ adjustDockingAreasToFit() method
404	private int[] adjustDockingAreasToFit(
405		Dimension size,
406		int topHeight,
407		int leftWidth,
408		int bottomHeight,
409		int rightWidth,
410		int topButtonHeight,
411		int leftButtonWidth,
412		int bottomButtonHeight,
413		int rightButtonWidth,
414		Dimension _topToolbars,
415		Dimension _bottomToolbars)
416	{
417		int maxTopHeight = size.height - bottomHeight
418			- topButtonHeight - bottomButtonHeight
419			- _topToolbars.height - _bottomToolbars.height;
420		topHeight = Math.min(Math.max(0,maxTopHeight),
421			topHeight);
422		leftWidth = Math.min(Math.max(0,
423			size.width - leftButtonWidth
424			- rightButtonWidth - rightWidth),leftWidth);
425		int maxBottomHeight = size.height - topHeight
426			- topButtonHeight - bottomButtonHeight
427			- _topToolbars.height - _bottomToolbars.height;
428		bottomHeight = Math.min(Math.max(0,maxBottomHeight),
429			bottomHeight);
430		rightWidth = Math.min(Math.max(0,
431			size.width - leftButtonWidth
432			- rightButtonWidth - leftWidth),rightWidth);
433
434		top.getWindowContainer().setDimension(topHeight);
435		left.getWindowContainer().setDimension(leftWidth);
436		bottom.getWindowContainer().setDimension(bottomHeight);
437		right.getWindowContainer().setDimension(rightWidth);
438
439		return new int[] {
440			topHeight,
441			leftWidth,
442			bottomHeight,
443			rightWidth
444		};
445	} //}}}
446
447	//{{{ getLayoutAlignmentX() method
448	public float getLayoutAlignmentX(Container target)
449	{
450		return 0.5f;
451	} //}}}
452
453	//{{{ getLayoutAlignmentY() method
454	public float getLayoutAlignmentY(Container target)
455	{
456		return 0.5f;
457	} //}}}
458
459	//{{{ invalidateLayout() method
460	public void invalidateLayout(Container target) {}
461	//}}}
462}