PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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