1. /*
  2. * @(#)Box.java 1.36 00/08/05
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.beans.PropertyChangeListener;
  14. import java.util.Locale;
  15. import java.io.Serializable;
  16. import javax.accessibility.*;
  17. /**
  18. * A lightweight container
  19. * that uses a BoxLayout object as its layout manager.
  20. * Box provides several class methods
  21. * that are useful for containers using BoxLayout --
  22. * even non-Box containers.
  23. *
  24. * <p>
  25. *
  26. * The Box class can create several kinds
  27. * of invisible components
  28. * that affect layout:
  29. * glue, struts, and rigid areas.
  30. * If all the components your Box contains
  31. * have a fixed size,
  32. * you might want to use a glue component
  33. * (returned by <code>createGlue</code>)
  34. * to control the components' positions.
  35. * If you need a fixed amount of space between two components,
  36. * try using a strut
  37. * (<code>createHorizontalStrut</code> or <code>createVerticalStrut</code>).
  38. * If you need an invisible component
  39. * that always takes up the same amount of space,
  40. * get it by invoking <code>createRigidArea</code>.
  41. * <p>
  42. * <strong>Warning:</strong>
  43. * Serialized objects of this class will not be compatible with
  44. * future Swing releases. The current serialization support is appropriate
  45. * for short term storage or RMI between applications running the same
  46. * version of Swing. A future release of Swing will provide support for
  47. * long term persistence.
  48. *
  49. * @see BoxLayout
  50. *
  51. * @author Timothy Prinzing
  52. * @version 1.36 08/05/00
  53. */
  54. public class Box extends Container implements Accessible {
  55. /**
  56. * Creates a <code>Box</code> that displays its components
  57. * along the the specified axis.
  58. *
  59. * @param axis can be either <code>BoxLayout.X_AXIS</code>
  60. * (to display components from left to right) or
  61. * <code>BoxLayout.Y_AXIS</code>
  62. * (to display them from top to bottom)
  63. * @see #createHorizontalBox
  64. * @see #createVerticalBox
  65. */
  66. public Box(int axis) {
  67. super();
  68. super.setLayout(new BoxLayout(this, axis));
  69. }
  70. /**
  71. * Creates a <code>Box</code> that displays its components
  72. * from left to right.
  73. *
  74. * @return the box
  75. */
  76. public static Box createHorizontalBox() {
  77. return new Box(BoxLayout.X_AXIS);
  78. }
  79. /**
  80. * Creates a <code>Box</code> that displays its components
  81. * from top to bottom.
  82. *
  83. * @return the box
  84. */
  85. public static Box createVerticalBox() {
  86. return new Box(BoxLayout.Y_AXIS);
  87. }
  88. /**
  89. * Creates an invisible component that's always the specified size.
  90. * <!-- WHEN WOULD YOU USE THIS AS OPPOSED TO A STRUT? -->
  91. *
  92. * @param d the dimensions of the invisible component
  93. * @return the component
  94. * @see #createGlue
  95. * @see #createHorizontalStrut
  96. * @see #createVerticalStrut
  97. */
  98. public static Component createRigidArea(Dimension d) {
  99. return new Filler(d, d, d);
  100. }
  101. /**
  102. * Creates an invisible, fixed-width component.
  103. * In a horizontal box,
  104. * you typically use this method
  105. * to force a certain amount of space between two components.
  106. * In a vertical box,
  107. * you might use this method
  108. * to force the box to be at least the specified width.
  109. * The invisible component has no height
  110. * unless excess space is available,
  111. * in which case it takes its share of available space,
  112. * just like any other component that has no maximum height.
  113. *
  114. * @param width the width of the invisible component, in pixels >= 0
  115. * @return the component
  116. * @see #createVerticalStrut
  117. * @see #createGlue
  118. * @see #createRigidArea
  119. */
  120. public static Component createHorizontalStrut(int width) {
  121. // PENDING(jeff) change to Integer.MAX_VALUE. This hasn't been done
  122. // to date because BoxLayout alignment breaks.
  123. return new Filler(new Dimension(width,0), new Dimension(width,0),
  124. new Dimension(width, Short.MAX_VALUE));
  125. }
  126. /**
  127. * Creates an invisible, fixed-height component.
  128. * In a vertical box,
  129. * you typically use this method
  130. * to force a certain amount of space between two components.
  131. * In a horizontal box,
  132. * you might use this method
  133. * to force the box to be at least the specified height.
  134. * The invisible component has no width
  135. * unless excess space is available,
  136. * in which case it takes its share of available space,
  137. * just like any other component that has no maximum width.
  138. *
  139. * @param height the height of the invisible component, in pixels >= 0
  140. * @return the component
  141. * @see #createHorizontalStrut
  142. * @see #createGlue
  143. * @see #createRigidArea
  144. */
  145. public static Component createVerticalStrut(int height) {
  146. // PENDING(jeff) change to Integer.MAX_VALUE. This hasn't been done
  147. // to date because BoxLayout alignment breaks.
  148. return new Filler(new Dimension(0,height), new Dimension(0,height),
  149. new Dimension(Short.MAX_VALUE, height));
  150. }
  151. /**
  152. * Creates an invisible "glue" component
  153. * that can be useful in a Box
  154. * whose visible components have a maximum width
  155. * (for a horizontal box)
  156. * or height (for a vertical box).
  157. * You can think of the glue component
  158. * as being a gooey substance
  159. * that expands as much as necessary
  160. * to fill the space between its neighboring components.
  161. *
  162. * <p>
  163. *
  164. * For example, suppose you have
  165. * a horizontal box that contains two fixed-size components.
  166. * If the box gets extra space,
  167. * the fixed-size components won't become larger,
  168. * so where does the extra space go?
  169. * Without glue,
  170. * the extra space goes to the right of the second component.
  171. * If you put glue between the fixed-size components,
  172. * then the extra space goes there.
  173. * If you put glue before the first fixed-size component,
  174. * the extra space goes there,
  175. * and the fixed-size components are shoved against the right
  176. * edge of the box.
  177. * If you put glue before the first fixed-size component
  178. * and after the second fixed-size component,
  179. * the fixed-size components are centered in the box.
  180. *
  181. * <p>
  182. *
  183. * To use glue,
  184. * call <code>Box.createGlue</code>
  185. * and add the returned component to a container.
  186. * The glue component has no minimum or preferred size,
  187. * so it takes no space unless excess space is available.
  188. * If excess space is available,
  189. * then the glue component takes its share of available
  190. * horizontal or vertical space,
  191. * just like any other component that has no maximum width or height.
  192. *
  193. * @return the component
  194. */
  195. public static Component createGlue() {
  196. // PENDING(jeff) change to Integer.MAX_VALUE. This hasn't been done
  197. // to date because BoxLayout alignment breaks.
  198. return new Filler(new Dimension(0,0), new Dimension(0,0),
  199. new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
  200. }
  201. /**
  202. * Creates a horizontal glue component.
  203. *
  204. * @return the component
  205. */
  206. public static Component createHorizontalGlue() {
  207. // PENDING(jeff) change to Integer.MAX_VALUE. This hasn't been done
  208. // to date because BoxLayout alignment breaks.
  209. return new Filler(new Dimension(0,0), new Dimension(0,0),
  210. new Dimension(Short.MAX_VALUE, 0));
  211. }
  212. /**
  213. * Creates a vertical glue component.
  214. *
  215. * @return the component
  216. */
  217. public static Component createVerticalGlue() {
  218. // PENDING(jeff) change to Integer.MAX_VALUE. This hasn't been done
  219. // to date because BoxLayout alignment breaks.
  220. return new Filler(new Dimension(0,0), new Dimension(0,0),
  221. new Dimension(0, Short.MAX_VALUE));
  222. }
  223. /**
  224. * Throws an AWTError, since a Box can use only a BoxLayout.
  225. *
  226. * @param l the layout manager to use
  227. */
  228. public void setLayout(LayoutManager l) {
  229. throw new AWTError("Illegal request");
  230. }
  231. /**
  232. * An implementation of a lightweight component that participates in
  233. * layout but has no view.
  234. * <p>
  235. * <strong>Warning:</strong>
  236. * Serialized objects of this class will not be compatible with
  237. * future Swing releases. The current serialization support is appropriate
  238. * for short term storage or RMI between applications running the same
  239. * version of Swing. A future release of Swing will provide support for
  240. * long term persistence.
  241. */
  242. public static class Filler extends Component implements Accessible {
  243. /**
  244. * Constructor to create shape with the given size ranges.
  245. *
  246. * @param min Minimum size
  247. * @param pref Preferred size
  248. * @param max Maximum size
  249. */
  250. public Filler(Dimension min, Dimension pref, Dimension max) {
  251. reqMin = min;
  252. reqPref = pref;
  253. reqMax = max;
  254. }
  255. /**
  256. * Change the size requests for this shape. An invalidate() is
  257. * propagated upward as a result so that layout will eventually
  258. * happen with using the new sizes.
  259. *
  260. * @param min Value to return for getMinimumSize
  261. * @param pref Value to return for getPreferredSize
  262. * @param max Value to return for getMaximumSize
  263. */
  264. public void changeShape(Dimension min, Dimension pref, Dimension max) {
  265. reqMin = min;
  266. reqPref = pref;
  267. reqMax = max;
  268. invalidate();
  269. }
  270. // ---- Component methods ------------------------------------------
  271. /**
  272. * Returns the minimum size of the component.
  273. *
  274. * @return the size
  275. */
  276. public Dimension getMinimumSize() {
  277. return reqMin;
  278. }
  279. /**
  280. * Returns the preferred size of the component.
  281. *
  282. * @return the size
  283. */
  284. public Dimension getPreferredSize() {
  285. return reqPref;
  286. }
  287. /**
  288. * Returns the maximum size of the component.
  289. *
  290. * @return the size
  291. */
  292. public Dimension getMaximumSize() {
  293. return reqMax;
  294. }
  295. // ---- member variables ---------------------------------------
  296. private Dimension reqMin;
  297. private Dimension reqPref;
  298. private Dimension reqMax;
  299. /////////////////
  300. // Accessibility support for Box$Filler
  301. ////////////////
  302. /**
  303. * The currently set AccessibleContext object.
  304. */
  305. protected AccessibleContext accessibleContext = null;
  306. /**
  307. * Gets the AccessibleContext associated with this Box.Filler.
  308. * For box fillers, the AccessibleContext takes the form of an
  309. * AccessibleBoxFiller.
  310. * A new AccessibleAWTBoxFiller instance is created if necessary.
  311. *
  312. * @return an AccessibleBoxFiller that serves as the
  313. * AccessibleContext of this Box.Filler.
  314. */
  315. public AccessibleContext getAccessibleContext() {
  316. if (accessibleContext == null) {
  317. accessibleContext = new AccessibleBoxFiller();
  318. }
  319. return accessibleContext;
  320. }
  321. /**
  322. * This class implements accessibility support for the
  323. * <code>Box.Filler</code> class.
  324. */
  325. protected class AccessibleBoxFiller extends AccessibleAWTComponent {
  326. // AccessibleContext methods
  327. //
  328. /**
  329. * Gets the role of this object.
  330. *
  331. * @return an instance of AccessibleRole describing the role of
  332. * the object (AccessibleRole.FILLER)
  333. * @see AccessibleRole
  334. */
  335. public AccessibleRole getAccessibleRole() {
  336. return AccessibleRole.FILLER;
  337. }
  338. }
  339. }
  340. /////////////////
  341. // Accessibility support for Box
  342. ////////////////
  343. /**
  344. * The currently set AccessibleContext object.
  345. */
  346. protected AccessibleContext accessibleContext = null;
  347. /**
  348. * Gets the AccessibleContext associated with this Box.
  349. * For boxes, the AccessibleContext takes the form of an
  350. * AccessibleBox.
  351. * A new AccessibleAWTBox instance is created if necessary.
  352. *
  353. * @return an AccessibleBox that serves as the
  354. * AccessibleContext of this Box
  355. */
  356. public AccessibleContext getAccessibleContext() {
  357. if (accessibleContext == null) {
  358. accessibleContext = new AccessibleBox();
  359. }
  360. return accessibleContext;
  361. }
  362. /**
  363. * This class implements accessibility support for the
  364. * <code>Box</code> class.
  365. */
  366. protected class AccessibleBox extends AccessibleAWTContainer {
  367. // AccessibleContext methods
  368. //
  369. /**
  370. * Gets the role of this object.
  371. *
  372. * @return an instance of AccessibleRole describing the role of the
  373. * object (AccessibleRole.FILLER)
  374. * @see AccessibleRole
  375. */
  376. public AccessibleRole getAccessibleRole() {
  377. return AccessibleRole.FILLER;
  378. }
  379. } // inner class AccessibleBox
  380. }