PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 317 lines | 233 code | 34 blank | 50 comment | 52 complexity | b2f142e3867a7c559f565c737437e770 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. * 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. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import org.gjt.sp.jedit.jEdit;
  25. import java.awt.CardLayout;
  26. import java.awt.Cursor;
  27. import java.awt.Dimension;
  28. import java.awt.Insets;
  29. import java.awt.Point;
  30. import java.awt.event.MouseAdapter;
  31. import java.awt.event.MouseEvent;
  32. import java.awt.event.MouseMotionListener;
  33. import javax.swing.JPanel;
  34. import javax.swing.border.Border;
  35. //}}}
  36. /**
  37. * @version $Id: DockablePanel.java 13259 2008-08-10 20:54:46Z shlomy $
  38. */
  39. class DockablePanel extends JPanel
  40. {
  41. private PanelWindowContainer panel;
  42. private DockableWindowManagerImpl wm;
  43. //{{{ DockablePanel constructor
  44. DockablePanel(PanelWindowContainer panel)
  45. {
  46. super(new CardLayout());
  47. this.panel = panel;
  48. this.wm = panel.getDockableWindowManager();
  49. ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler();
  50. addMouseListener(resizeMouseHandler);
  51. addMouseMotionListener(resizeMouseHandler);
  52. } //}}}
  53. //{{{ getWindowContainer() method
  54. PanelWindowContainer getWindowContainer()
  55. {
  56. return panel;
  57. } //}}}
  58. //{{{ showDockable() method
  59. void showDockable(String name)
  60. {
  61. ((CardLayout)getLayout()).show(this,name);
  62. } //}}}
  63. //{{{ getMinimumSize() method
  64. public Dimension getMinimumSize()
  65. {
  66. return new Dimension(0,0);
  67. } //}}}
  68. //{{{ getPreferredSize() method
  69. public Dimension getPreferredSize()
  70. {
  71. final String position = panel.getPosition();
  72. final int dimension = panel.getDimension();
  73. if(panel.getCurrent() == null)
  74. return new Dimension(0,0);
  75. else
  76. {
  77. if(position.equals(DockableWindowManager.TOP)
  78. || position.equals(DockableWindowManager.BOTTOM))
  79. {
  80. if(dimension <= 0)
  81. {
  82. int height = super.getPreferredSize().height;
  83. panel.setDimension(height);
  84. }
  85. return new Dimension(0,
  86. dimension + PanelWindowContainer
  87. .SPLITTER_WIDTH);
  88. }
  89. else
  90. {
  91. if(dimension <= 0)
  92. {
  93. int width = super.getPreferredSize().width;
  94. panel.setDimension(width);
  95. }
  96. return new Dimension(dimension +
  97. PanelWindowContainer.SPLITTER_WIDTH,
  98. 0);
  99. }
  100. }
  101. } //}}}
  102. //{{{ setBounds() method
  103. public void setBounds(int x, int y, int width, int height)
  104. {
  105. final String position = panel.getPosition();
  106. final int dimension = panel.getDimension();
  107. if(position.equals(DockableWindowManager.TOP) ||
  108. position.equals(DockableWindowManager.BOTTOM))
  109. {
  110. if(dimension != 0 && height <= PanelWindowContainer.SPLITTER_WIDTH)
  111. panel.show((DockableWindowManagerImpl.Entry) null);
  112. else
  113. panel.setDimension(height);
  114. }
  115. else
  116. {
  117. if(dimension != 0 && width <= PanelWindowContainer.SPLITTER_WIDTH)
  118. panel.show((DockableWindowManagerImpl.Entry) null);
  119. else
  120. panel.setDimension(width);
  121. }
  122. super.setBounds(x,y,width,height);
  123. } //}}}
  124. /** This belong to ResizeMouseHandler but requires to be static. */
  125. static Point dragStart;
  126. //{{{ ResizeMouseHandler class
  127. class ResizeMouseHandler extends MouseAdapter implements MouseMotionListener
  128. {
  129. /** This is true if the mouse is on the split bar. */
  130. boolean canDrag;
  131. //{{{ mousePressed() method
  132. public void mousePressed(MouseEvent evt)
  133. {
  134. if(canDrag)
  135. {
  136. continuousLayout = jEdit.getBooleanProperty("appearance.continuousLayout");
  137. wm.setResizePos(panel.getDimension(),panel);
  138. dragStart = evt.getPoint();
  139. }
  140. } //}}}
  141. //{{{ mouseReleased() method
  142. public void mouseReleased(MouseEvent evt)
  143. {
  144. if(canDrag)
  145. {
  146. if (!continuousLayout)
  147. {
  148. panel.setDimension(wm.resizePos
  149. + PanelWindowContainer
  150. .SPLITTER_WIDTH);
  151. }
  152. wm.finishResizing();
  153. dragStart = null;
  154. wm.revalidate();
  155. }
  156. } //}}}
  157. //{{{ mouseMoved() method
  158. public void mouseMoved(MouseEvent evt)
  159. {
  160. Border border = getBorder();
  161. if(border == null)
  162. {
  163. // collapsed
  164. return;
  165. }
  166. String position = panel.getPosition();
  167. Insets insets = border.getBorderInsets(DockablePanel.this);
  168. canDrag = false;
  169. //{{{ Top...
  170. if(position.equals(DockableWindowManager.TOP))
  171. {
  172. if(evt.getY() >= getHeight() - insets.bottom)
  173. canDrag = true;
  174. } //}}}
  175. //{{{ Left...
  176. else if(position.equals(DockableWindowManager.LEFT))
  177. {
  178. if(evt.getX() >= getWidth() - insets.right)
  179. canDrag = true;
  180. } //}}}
  181. //{{{ Bottom...
  182. else if(position.equals(DockableWindowManager.BOTTOM))
  183. {
  184. if(evt.getY() <= insets.top)
  185. canDrag = true;
  186. } //}}}
  187. //{{{ Right...
  188. else if(position.equals(DockableWindowManager.RIGHT))
  189. {
  190. if(evt.getX() <= insets.left)
  191. canDrag = true;
  192. } //}}}
  193. if (dragStart == null)
  194. {
  195. if(canDrag)
  196. {
  197. wm.setCursor(Cursor.getPredefinedCursor(
  198. getAppropriateCursor()));
  199. }
  200. else
  201. {
  202. wm.setCursor(Cursor.getPredefinedCursor(
  203. Cursor.DEFAULT_CURSOR));
  204. }
  205. }
  206. } //}}}
  207. //{{{ mouseDragged() method
  208. public void mouseDragged(MouseEvent evt)
  209. {
  210. if(!canDrag)
  211. return;
  212. if(dragStart == null) // can't happen?
  213. return;
  214. int dimension = panel.getDimension();
  215. String position = panel.getPosition();
  216. int newSize = 0;
  217. //{{{ Top...
  218. if(position.equals(DockableWindowManager.TOP))
  219. {
  220. newSize = evt.getY();
  221. wm.setResizePos(
  222. evt.getY() - dragStart.y
  223. + dimension,
  224. panel);
  225. } //}}}
  226. //{{{ Left...
  227. else if(position.equals(DockableWindowManager.LEFT))
  228. {
  229. newSize = evt.getX();
  230. wm.setResizePos(evt.getX() - dragStart.x
  231. + dimension,
  232. panel);
  233. } //}}}
  234. //{{{ Bottom...
  235. else if(position.equals(DockableWindowManager.BOTTOM))
  236. {
  237. newSize = dimension - evt.getY();
  238. wm.setResizePos(dimension - evt.getY()
  239. + dragStart.y,
  240. panel);
  241. } //}}}
  242. //{{{ Right...
  243. else if(position.equals(DockableWindowManager.RIGHT))
  244. {
  245. newSize = dimension - evt.getX();
  246. wm.setResizePos(dimension - evt.getX()
  247. + dragStart.x,
  248. panel);
  249. } //}}}
  250. if (continuousLayout)
  251. {
  252. panel.setDimension(newSize
  253. + PanelWindowContainer.SPLITTER_WIDTH);
  254. wm.revalidate();
  255. }
  256. } //}}}
  257. //{{{ mouseExited() method
  258. public void mouseExited(MouseEvent evt)
  259. {
  260. if (dragStart == null)
  261. {
  262. wm.setCursor(Cursor.getPredefinedCursor(
  263. Cursor.DEFAULT_CURSOR));
  264. }
  265. } //}}}
  266. //{{{ getCursor() method
  267. private int getAppropriateCursor()
  268. {
  269. String position = panel.getPosition();
  270. if(position.equals(DockableWindowManager.TOP))
  271. return Cursor.N_RESIZE_CURSOR;
  272. else if(position.equals(DockableWindowManager.LEFT))
  273. return Cursor.W_RESIZE_CURSOR;
  274. else if(position.equals(DockableWindowManager.BOTTOM))
  275. return Cursor.S_RESIZE_CURSOR;
  276. else if(position.equals(DockableWindowManager.RIGHT))
  277. return Cursor.E_RESIZE_CURSOR;
  278. else
  279. throw new InternalError();
  280. } //}}}
  281. private boolean continuousLayout;
  282. } //}}}
  283. }