PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/DockableLayout.java

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