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