ScrollPane.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* ScrollPane.java -- Scrolling window
  2. Copyright (C) 1999, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.peer.ScrollPanePeer;
  33. import java.awt.peer.ContainerPeer;
  34. import java.awt.peer.ComponentPeer;
  35. import java.io.Serializable;
  36. import javax.accessibility.Accessible;
  37. /**
  38. * This widget provides a scrollable region that allows a single
  39. * subcomponent to be viewed through a smaller window.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. */
  43. public class ScrollPane extends Container implements Accessible, Serializable
  44. {
  45. /*
  46. * Static Variables
  47. */
  48. /**
  49. * Constant indicating that scrollbars are created as needed in this
  50. * windows.
  51. */
  52. public static final int SCROLLBARS_AS_NEEDED = 0;
  53. /**
  54. * Constant indicating that scrollbars are always displayed in this
  55. * window.
  56. */
  57. public static final int SCROLLBARS_ALWAYS = 1;
  58. /**
  59. * Constant indicating that scrollbars are never displayed in this window.
  60. */
  61. public static final int SCROLLBARS_NEVER = 2;
  62. // Serialization constant
  63. private static final long serialVersionUID = 7956609840827222915L;
  64. /*************************************************************************/
  65. /*
  66. * Instance Variables
  67. */
  68. /**
  69. * @serial The horizontal scrollbar for this window. The methods
  70. * <code>setMinimum()</code>, <code>setMaximum</code>, and
  71. * <code>setVisibleAmount</code> must not be called on this scrollbar.
  72. */
  73. private ScrollPaneAdjustable hAdjustable;
  74. /**
  75. * @serial The vertical scrollbar for this window. The methods
  76. * <code>setMinimum()</code>, <code>setMaximum</code>, and
  77. * <code>setVisibleAmount</code> must not be called on this scrollbar.
  78. */
  79. private ScrollPaneAdjustable vAdjustable;
  80. /**
  81. * @serial Indicates when scrollbars are displayed in this window, will
  82. * be one of the constants from this class.
  83. */
  84. private int scrollbarDisplayPolicy;
  85. // Current scroll position
  86. private Point scrollPosition = new Point(0, 0);
  87. /*************************************************************************/
  88. /*
  89. * Constructors
  90. */
  91. /**
  92. * Initializes a new instance of <code>ScrollPane</code> with a default
  93. * scrollbar policy of <code>SCROLLBARS_AS_NEEDED</code>.
  94. *
  95. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  96. */
  97. public
  98. ScrollPane()
  99. {
  100. this(SCROLLBARS_AS_NEEDED);
  101. }
  102. /*************************************************************************/
  103. /**
  104. * Initializes a new instance of <code>ScrollPane</code> with the
  105. * specified scrollbar policy.
  106. *
  107. * @param scrollbarDisplayPolicy When to display scrollbars, which must
  108. * be one of the constants defined in this class.
  109. *
  110. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  111. */
  112. public
  113. ScrollPane(int scrollbarDisplayPolicy)
  114. {
  115. if (GraphicsEnvironment.isHeadless ())
  116. throw new HeadlessException ();
  117. this.scrollbarDisplayPolicy = scrollbarDisplayPolicy;
  118. if (scrollbarDisplayPolicy != SCROLLBARS_ALWAYS
  119. && scrollbarDisplayPolicy != SCROLLBARS_AS_NEEDED
  120. && scrollbarDisplayPolicy != SCROLLBARS_NEVER)
  121. throw new IllegalArgumentException("Bad scrollbarDisplayPolicy: " +
  122. scrollbarDisplayPolicy);
  123. if (scrollbarDisplayPolicy != SCROLLBARS_NEVER)
  124. {
  125. hAdjustable = new ScrollPaneAdjustable(Scrollbar.HORIZONTAL);
  126. vAdjustable = new ScrollPaneAdjustable(Scrollbar.VERTICAL);
  127. }
  128. }
  129. /*************************************************************************/
  130. /*
  131. * Instance Variables
  132. */
  133. /**
  134. * Returns the current scrollbar display policy.
  135. *
  136. * @return The current scrollbar display policy.
  137. */
  138. public int
  139. getScrollbarDisplayPolicy()
  140. {
  141. return(scrollbarDisplayPolicy);
  142. }
  143. /*************************************************************************/
  144. /**
  145. * Returns the horizontal scrollbar for this object. If the scrollbar
  146. * display policy is set to <code>SCROLLBARS_NEVER</code> then this
  147. * will be <code>null</code>.
  148. *
  149. * @return The horizontal scrollbar for this window.
  150. */
  151. public Adjustable
  152. getHAdjustable()
  153. {
  154. return(hAdjustable);
  155. }
  156. /*************************************************************************/
  157. /**
  158. * Returns the vertical scrollbar for this object. If the scrollbar
  159. * display policy is set to <code>SCROLLBARS_NEVER</code> then this
  160. * will be <code>null</code>.
  161. *
  162. * @return The horizontal scrollbar for this window.
  163. */
  164. public Adjustable
  165. getVAdjustable()
  166. {
  167. return(vAdjustable);
  168. }
  169. /*************************************************************************/
  170. /**
  171. * Returns the current viewport size. The viewport is the region of
  172. * this object's window where the child is actually displayed.
  173. *
  174. * @return The viewport size.
  175. */
  176. public Dimension
  177. getViewportSize()
  178. {
  179. Dimension viewsize = getSize();
  180. Insets insets = getInsets();
  181. viewsize.width = viewsize.width - (insets.left + insets.right);
  182. viewsize.height = viewsize.height - (insets.top + insets.bottom);
  183. ScrollPaneAdjustable v = (ScrollPaneAdjustable)getVAdjustable();
  184. ScrollPaneAdjustable h = (ScrollPaneAdjustable)getHAdjustable();
  185. if ((v != null) && v.isVisible())
  186. viewsize.width = viewsize.width - v.getSize().width;
  187. if ((h != null) && h.isVisible())
  188. viewsize.height = viewsize.height - v.getSize().height;
  189. return(viewsize);
  190. }
  191. /*************************************************************************/
  192. /**
  193. * Returns the height of a horizontal scrollbar.
  194. *
  195. * @return The height of a horizontal scrollbar.
  196. */
  197. public int
  198. getHScrollbarHeight()
  199. {
  200. ScrollPanePeer spp = (ScrollPanePeer)getPeer();
  201. if (spp != null)
  202. return(spp.getHScrollbarHeight());
  203. else
  204. return(0); // FIXME: What to do here?
  205. }
  206. /*************************************************************************/
  207. /**
  208. * Returns the width of a vertical scrollbar.
  209. *
  210. * @return The width of a vertical scrollbar.
  211. */
  212. public int
  213. getVScrollbarWidth()
  214. {
  215. ScrollPanePeer spp = (ScrollPanePeer)getPeer();
  216. if (spp != null)
  217. return(spp.getVScrollbarWidth());
  218. else
  219. return(0); // FIXME: What to do here?
  220. }
  221. /*************************************************************************/
  222. /**
  223. * Returns the current scroll position of the viewport.
  224. *
  225. * @return The current scroll position of the viewport.
  226. */
  227. public Point
  228. getScrollPosition()
  229. {
  230. int x = 0;
  231. int y = 0;
  232. Adjustable v = getVAdjustable();
  233. Adjustable h = getHAdjustable();
  234. if (v != null)
  235. y = v.getValue();
  236. if (h != null)
  237. x = h.getValue();
  238. return(new Point(x, y));
  239. }
  240. /*************************************************************************/
  241. /**
  242. * Sets the scroll position to the specified value.
  243. *
  244. * @param scrollPosition The new scrollPosition.
  245. *
  246. * @exception IllegalArgumentException If the specified value is outside
  247. * the legal scrolling range.
  248. */
  249. public void
  250. setScrollPosition(Point scrollPosition) throws IllegalArgumentException
  251. {
  252. setScrollPosition(scrollPosition.x, scrollPosition.y);
  253. }
  254. /*************************************************************************/
  255. /**
  256. * Sets the scroll position to the specified value.
  257. *
  258. * @param x The new X coordinate of the scroll position.
  259. * @param y The new Y coordinate of the scroll position.
  260. *
  261. * @exception IllegalArgumentException If the specified value is outside
  262. * the legal scrolling range.
  263. */
  264. public void
  265. setScrollPosition(int x, int y)
  266. {
  267. Adjustable h = getHAdjustable();
  268. Adjustable v = getVAdjustable();
  269. if (h != null)
  270. h.setValue(x);
  271. if (v != null)
  272. v.setValue(y);
  273. ScrollPanePeer spp = (ScrollPanePeer)getPeer();
  274. if (spp != null)
  275. spp.setScrollPosition(x, y);
  276. }
  277. /*************************************************************************/
  278. /**
  279. * Notifies this object that it should create its native peer.
  280. */
  281. public void
  282. addNotify()
  283. {
  284. if (getPeer() == null)
  285. return;
  286. setPeer((ComponentPeer)getToolkit().createScrollPane(this));
  287. if (hAdjustable != null)
  288. hAdjustable.addNotify();
  289. if (vAdjustable != null)
  290. vAdjustable.removeNotify();
  291. }
  292. /*************************************************************************/
  293. /**
  294. * Notifies this object that it should destroy its native peers.
  295. */
  296. public void
  297. removeNotify()
  298. {
  299. if (hAdjustable != null)
  300. hAdjustable.removeNotify();
  301. if (vAdjustable != null)
  302. vAdjustable.removeNotify();
  303. super.removeNotify();
  304. }
  305. /*************************************************************************/
  306. /**
  307. * Adds the specified child component to this container. A
  308. * <code>ScrollPane</code> can have at most one child, so if a second
  309. * one is added, then first one is removed.
  310. *
  311. * @param component The component to add to this container.
  312. * @param constraints A list of layout constraints for this object.
  313. * @param index The index at which to add the child, which is ignored
  314. * in this implementation.
  315. */
  316. public final void
  317. addImpl(Component component, Object constraints, int index)
  318. {
  319. Component[] list = getComponents();
  320. if ((list != null) && (list.length > 0))
  321. remove(list[0]);
  322. super.addImpl(component, constraints, -1);
  323. doLayout();
  324. }
  325. /*************************************************************************/
  326. /**
  327. * Lays out this component. This consists of resizing the sole child
  328. * component to its perferred size.
  329. */
  330. public void
  331. doLayout()
  332. {
  333. Component[] list = getComponents();
  334. if ((list != null) && (list.length > 0))
  335. {
  336. Dimension dim = list[0].getPreferredSize();
  337. list[0].resize(dim);
  338. Point p = getScrollPosition();
  339. if (p.x > dim.width)
  340. p.x = dim.width;
  341. if (p.y > dim.height)
  342. p.y = dim.height;
  343. setScrollPosition(p);
  344. }
  345. }
  346. /*************************************************************************/
  347. /**
  348. * Lays out this component. This consists of resizing the sole child
  349. * component to its perferred size.
  350. *
  351. * @deprecated This method is deprecated in favor of
  352. * <code>doLayout()</code>.
  353. */
  354. public void
  355. layout()
  356. {
  357. doLayout();
  358. }
  359. /*************************************************************************/
  360. /**
  361. * This method overrides its superclass method to ensure no layout
  362. * manager is set for this container. <code>ScrollPane</code>'s do
  363. * not have layout managers.
  364. *
  365. * @param layoutManager Ignored
  366. */
  367. public final void
  368. setLayout(LayoutManager layoutManager)
  369. {
  370. return;
  371. }
  372. /*************************************************************************/
  373. /**
  374. * Prints all of the components in this container.
  375. *
  376. * @param graphics The desired graphics context for printing.
  377. */
  378. public void
  379. printComponents(Graphics graphics)
  380. {
  381. super.printComponents(graphics);
  382. }
  383. /*************************************************************************/
  384. /**
  385. * Returns a debug string for this object.
  386. *
  387. * @return A debug string for this object.
  388. */
  389. public String
  390. paramString()
  391. {
  392. return(getClass().getName());
  393. }
  394. } // class ScrollPane