1. /*
  2. * @(#)JWindow.java 1.53 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.PropertyChangeListener;
  11. import java.util.Locale;
  12. import java.util.Vector;
  13. import java.io.Serializable;
  14. import javax.accessibility.*;
  15. /**
  16. * A <code>JWindow</code> is a container that can be displayed anywhere on the
  17. * user's desktop. It does not have the title bar, window-management buttons,
  18. * or other trimmings associated with a <code>JFrame</code>, but it is still a
  19. * "first-class citizen" of the user's desktop, and can exist anywhere
  20. * on it.
  21. * <p>
  22. * The <code>JWindow</code> component contains a <code>JRootPane</code>
  23. * as its only child. The <code>contentPane</code> should be the parent
  24. * of any children of the <code>JWindow</code>.
  25. * From the older <code>java.awt.Window</code> object you would normally do
  26. * something like this:
  27. * <pre>
  28. * window.add(child);
  29. * </pre>
  30. * However, using <code>JWindow</code> you would code:
  31. * <pre>
  32. * window.getContentPane().add(child);
  33. * </pre>
  34. * The same is true of setting <code>LayoutManager</code>s, removing components,
  35. * listing children, etc. All these methods should normally be sent to
  36. * the <code>contentPane</code> instead of the <code>JWindow</code> itself.
  37. * The <code>contentPane</code> will always be non-<code>null</code>.
  38. * Attempting to set it to <code>null</code> will cause the <code>JWindow</code>
  39. * to throw an exception. The default <code>contentPane</code> will have a
  40. * <code>BorderLayout</code> manager set on it.
  41. * <p>
  42. * Please see the {@link JRootPane} documentation for a complete description of
  43. * the <code>contentPane</code>, <code>glassPane</code>, and
  44. * <code>layeredPane</code> components.
  45. * <p>
  46. * In a multi-screen environment, you can create a <code>JWindow</code>
  47. * on a different screen device. See {@link java.awt.Window} for more
  48. * information.
  49. * <p>
  50. * For the keyboard keys used by this component in the standard Look and
  51. * Feel (L&F) renditions, see the
  52. * <a href="doc-files/Key-Index.html#JWindow"><code>JWindow</code> key assignments</a>.
  53. * <p>
  54. * <strong>Warning:</strong>
  55. * Serialized objects of this class will not be compatible with
  56. * future Swing releases. The current serialization support is
  57. * appropriate for short term storage or RMI between applications running
  58. * the same version of Swing. As of 1.4, support for long term storage
  59. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  60. * has been added to the <code>java.beans</code> package.
  61. * Please see {@link java.beans.XMLEncoder}.
  62. *
  63. * @see JRootPane
  64. *
  65. * @beaninfo
  66. * attribute: isContainer true
  67. * attribute: containerDelegate getContentPane
  68. * description: A toplevel window which has no system border or controls.
  69. *
  70. * @version 1.53 01/23/03
  71. * @author David Kloba
  72. */
  73. public class JWindow extends Window implements Accessible, RootPaneContainer
  74. {
  75. /**
  76. * The <code>JRootPane</code> instance that manages the
  77. * <code>contentPane</code>
  78. * and optional <code>menuBar</code> for this frame, as well as the
  79. * <code>glassPane</code>.
  80. *
  81. * @see #getRootPane
  82. * @see #setRootPane
  83. */
  84. protected JRootPane rootPane;
  85. /**
  86. * If true then calls to <code>add</code> and <code>setLayout</code>
  87. * will cause an exception to be thrown.
  88. *
  89. * @see #isRootPaneCheckingEnabled
  90. * @see #setRootPaneCheckingEnabled
  91. */
  92. protected boolean rootPaneCheckingEnabled = false;
  93. /**
  94. * Creates a window with no specified owner. This window will not be
  95. * focusable.
  96. * <p>
  97. * This constructor sets the component's locale property to the value
  98. * returned by <code>JComponent.getDefaultLocale</code>.
  99. *
  100. * @throws HeadlessException if
  101. * <code>GraphicsEnvironment.isHeadless()</code> returns true.
  102. * @see java.awt.GraphicsEnvironment#isHeadless
  103. * @see #isFocusableWindow
  104. * @see JComponent#getDefaultLocale
  105. */
  106. public JWindow() {
  107. this((Frame)null);
  108. }
  109. /**
  110. * Creates a window with the specified <code>GraphicsConfiguration</code>
  111. * of a screen device. This window will not be focusable.
  112. * <p>
  113. * This constructor sets the component's locale property to the value
  114. * returned by <code>JComponent.getDefaultLocale</code>.
  115. *
  116. * @param gc the <code>GraphicsConfiguration</code> that is used
  117. * to construct the new window with; if gc is <code>null</code>,
  118. * the system default <code>GraphicsConfiguration</code>
  119. * is assumed
  120. * @throws HeadlessException If
  121. * <code>GraphicsEnvironment.isHeadless()</code> returns true.
  122. * @throws IllegalArgumentException if <code>gc</code> is not from
  123. * a screen device.
  124. *
  125. * @see java.awt.GraphicsEnvironment#isHeadless
  126. * @see #isFocusableWindow
  127. * @see JComponent#getDefaultLocale
  128. *
  129. * @since 1.3
  130. */
  131. public JWindow(GraphicsConfiguration gc) {
  132. this(null, gc);
  133. super.setFocusableWindowState(false);
  134. }
  135. /**
  136. * Creates a window with the specified owner frame.
  137. * If <code>owner</code> is <code>null</code>, the shared owner
  138. * will be used and this window will not be focusable. Also,
  139. * this window will not be focusable unless its owner is showing
  140. * on the screen.
  141. * <p>
  142. * This constructor sets the component's locale property to the value
  143. * returned by <code>JComponent.getDefaultLocale</code>.
  144. *
  145. * @param owner the frame from which the window is displayed
  146. * @throws HeadlessException if GraphicsEnvironment.isHeadless()
  147. * returns true.
  148. * @see java.awt.GraphicsEnvironment#isHeadless
  149. * @see #isFocusableWindow
  150. * @see JComponent#getDefaultLocale
  151. */
  152. public JWindow(Frame owner) {
  153. super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner);
  154. windowInit();
  155. }
  156. /**
  157. * Creates a window with the specified owner window. This window
  158. * will not be focusable unless its owner is showing on the screen.
  159. * If <code>owner</code> is <code>null</code>, the shared owner
  160. * will be used and this window will not be focusable.
  161. * <p>
  162. * This constructor sets the component's locale property to the value
  163. * returned by <code>JComponent.getDefaultLocale</code>.
  164. *
  165. * @param owner the window from which the window is displayed
  166. * @throws HeadlessException if
  167. * <code>GraphicsEnvironment.isHeadless()</code> returns true.
  168. * @see java.awt.GraphicsEnvironment#isHeadless
  169. * @see #isFocusableWindow
  170. * @see JComponent#getDefaultLocale
  171. */
  172. public JWindow(Window owner) {
  173. super(owner == null ? (Window)SwingUtilities.getSharedOwnerFrame() :
  174. owner);
  175. windowInit();
  176. }
  177. /**
  178. * Creates a window with the specified owner window and
  179. * <code>GraphicsConfiguration</code> of a screen device. If
  180. * <code>owner</code> is <code>null</code>, the shared owner will be used
  181. * and this window will not be focusable.
  182. * <p>
  183. * This constructor sets the component's locale property to the value
  184. * returned by <code>JComponent.getDefaultLocale</code>.
  185. *
  186. * @param owner the window from which the window is displayed
  187. * @param gc the <code>GraphicsConfiguration</code> that is used
  188. * to construct the new window with; if gc is <code>null</code>,
  189. * the system default <code>GraphicsConfiguration</code>
  190. * is assumed, unless <code>owner</code> is also null, in which
  191. * case the <code>GraphicsConfiguration</code> from the
  192. * shared owner frame will be used.
  193. * @throws HeadlessException if
  194. * <code>GraphicsEnvironment.isHeadless()</code> returns true.
  195. * @throws IllegalArgumentException if <code>gc</code> is not from
  196. * a screen device.
  197. *
  198. * @see java.awt.GraphicsEnvironment#isHeadless
  199. * @see #isFocusableWindow
  200. * @see JComponent#getDefaultLocale
  201. *
  202. * @since 1.3
  203. */
  204. public JWindow(Window owner, GraphicsConfiguration gc) {
  205. super(owner == null ? (Window)SwingUtilities.getSharedOwnerFrame() :
  206. owner, gc);
  207. windowInit();
  208. }
  209. /**
  210. * Called by the constructors to init the <code>JWindow</code> properly.
  211. */
  212. protected void windowInit() {
  213. setLocale( JComponent.getDefaultLocale() );
  214. setRootPane(createRootPane());
  215. setRootPaneCheckingEnabled(true);
  216. }
  217. /**
  218. * Called by the constructor methods to create the default
  219. * <code>rootPane</code>.
  220. */
  221. protected JRootPane createRootPane() {
  222. return new JRootPane();
  223. }
  224. /**
  225. * Returns whether calls to <code>add</code> and
  226. * <code>setLayout</code> will cause an exception to be thrown.
  227. *
  228. * @return true if <code>add</code> and <code>setLayout</code>
  229. * are checked
  230. *
  231. * @see #addImpl
  232. * @see #setLayout
  233. * @see #setRootPaneCheckingEnabled
  234. */
  235. protected boolean isRootPaneCheckingEnabled() {
  236. return rootPaneCheckingEnabled;
  237. }
  238. /**
  239. * Calls <code>paint(g)</code>. This method was overridden to
  240. * prevent an unnecessary call to clear the background.
  241. *
  242. * @param g the <code>Graphics</code> context in which to paint
  243. */
  244. public void update(Graphics g) {
  245. paint(g);
  246. }
  247. /**
  248. * Determines whether calls to <code>add</code> and
  249. * <code>setLayout</code> will cause an exception to be thrown.
  250. *
  251. * @param enabled a boolean value, true if checking is to be
  252. * enabled, which cause the exceptions to be thrown
  253. *
  254. * @see #addImpl
  255. * @see #setLayout
  256. * @see #isRootPaneCheckingEnabled
  257. * @beaninfo
  258. * hidden: true
  259. * description: Whether the add and setLayout methods throw exceptions when invoked.
  260. */
  261. protected void setRootPaneCheckingEnabled(boolean enabled) {
  262. rootPaneCheckingEnabled = enabled;
  263. }
  264. /**
  265. * Creates a string with a message that can be used for a
  266. * runtime exception. The message will look like:
  267. * <pre>
  268. * "Do not use JWindow.add() use JWindow.getContentPane().add() instead"
  269. * </pre>
  270. *
  271. * @param op a <code>String</code> indicating the attempted operation;
  272. * in the example above, the operation string is "add"
  273. * @return a string containing the message to be used for the exception
  274. */
  275. private Error createRootPaneException(String op) {
  276. String type = getClass().getName();
  277. return new Error(
  278. "Do not use " + type + "." + op + "() use "
  279. + type + ".getContentPane()." + op + "() instead");
  280. }
  281. /**
  282. * By default, children may not be added directly to a this component,
  283. * they must be added to its <code>contentPane</code> instead. For example:
  284. * <pre>
  285. * thisComponent.getContentPane().add(child)
  286. * </pre>
  287. * An attempt to add to directly to this component will cause an
  288. * runtime exception to be thrown. Subclasses can disable this
  289. * behavior.
  290. *
  291. * @param comp the component to be enhanced
  292. * @param constraints the constraints to be enforced on the component
  293. * @param index the index of the component
  294. *
  295. * @see #setRootPaneCheckingEnabled
  296. * @exception Error if called with <code>rootPaneChecking</code> true
  297. */
  298. protected void addImpl(Component comp, Object constraints, int index)
  299. {
  300. if(isRootPaneCheckingEnabled()) {
  301. throw createRootPaneException("add");
  302. }
  303. else {
  304. super.addImpl(comp, constraints, index);
  305. }
  306. }
  307. /**
  308. * Removes the specified component from this container.
  309. * @param comp the component to be removed
  310. * @see #add
  311. */
  312. public void remove(Component comp) {
  313. if (comp == rootPane) {
  314. super.remove(comp);
  315. } else {
  316. // Client mistake, but we need to handle it to avoid a
  317. // common object leak in client applications.
  318. getContentPane().remove(comp);
  319. }
  320. }
  321. /**
  322. * By default the layout of this component may not be set,
  323. * the layout of its contentPane should be set instead.
  324. * For example:
  325. * <pre>
  326. * thisComponent.getContentPane().setLayout(new GridLayout(1, 2))
  327. * </pre>
  328. * An attempt to set the layout of this component will cause an
  329. * runtime exception to be thrown. Subclasses can disable this
  330. * behavior.
  331. *
  332. * @param manager the layout manager for the window
  333. * @see #setRootPaneCheckingEnabled
  334. * @exception Error if called with <code>rootPaneChecking</code> true
  335. */
  336. public void setLayout(LayoutManager manager) {
  337. if(isRootPaneCheckingEnabled()) {
  338. throw createRootPaneException("setLayout");
  339. }
  340. else {
  341. super.setLayout(manager);
  342. }
  343. }
  344. /**
  345. * Returns the <code>rootPane</code> object for this window.
  346. * @return the <code>rootPane</code> property for this window
  347. *
  348. * @see #setRootPane
  349. * @see RootPaneContainer#getRootPane
  350. */
  351. public JRootPane getRootPane() {
  352. return rootPane;
  353. }
  354. /**
  355. * Sets the new <code>rootPane</code> object for this window.
  356. * This method is called by the constructor.
  357. *
  358. * @param root the new <code>rootPane</code> property
  359. * @see #getRootPane
  360. *
  361. * @beaninfo
  362. * hidden: true
  363. * description: the RootPane object for this window.
  364. */
  365. protected void setRootPane(JRootPane root) {
  366. if(rootPane != null) {
  367. remove(rootPane);
  368. }
  369. rootPane = root;
  370. if(rootPane != null) {
  371. boolean checkingEnabled = isRootPaneCheckingEnabled();
  372. try {
  373. setRootPaneCheckingEnabled(false);
  374. add(rootPane, BorderLayout.CENTER);
  375. }
  376. finally {
  377. setRootPaneCheckingEnabled(checkingEnabled);
  378. }
  379. }
  380. }
  381. /**
  382. * Returns the <code>Container</code> which is the <code>contentPane</code>
  383. * for this window.
  384. *
  385. * @return the <code>contentPane</code> property
  386. * @see #setContentPane
  387. * @see RootPaneContainer#getContentPane
  388. */
  389. public Container getContentPane() {
  390. return getRootPane().getContentPane();
  391. }
  392. /**
  393. * Sets the <code>contentPane</code> property for this window.
  394. * This method is called by the constructor.
  395. *
  396. * @param contentPane the new <code>contentPane</code>
  397. *
  398. * @exception IllegalComponentStateException (a runtime
  399. * exception) if the content pane parameter is <code>null</code>
  400. * @see #getContentPane
  401. * @see RootPaneContainer#setContentPane
  402. *
  403. * @beaninfo
  404. * hidden: true
  405. * description: The client area of the window where child
  406. * components are normally inserted.
  407. */
  408. public void setContentPane(Container contentPane) {
  409. getRootPane().setContentPane(contentPane);
  410. }
  411. /**
  412. * Returns the <code>layeredPane</code> object for this window.
  413. *
  414. * @return the <code>layeredPane</code> property
  415. * @see #setLayeredPane
  416. * @see RootPaneContainer#getLayeredPane
  417. */
  418. public JLayeredPane getLayeredPane() {
  419. return getRootPane().getLayeredPane();
  420. }
  421. /**
  422. * Sets the <code>layeredPane</code> property.
  423. * This method is called by the constructor.
  424. *
  425. * @param layeredPane the new <code>layeredPane</code> object
  426. *
  427. * @exception IllegalComponentStateException (a runtime
  428. * exception) if the content pane parameter is <code>null</code>
  429. * @see #getLayeredPane
  430. * @see RootPaneContainer#setLayeredPane
  431. *
  432. * @beaninfo
  433. * hidden: true
  434. * description: The pane which holds the various window layers.
  435. */
  436. public void setLayeredPane(JLayeredPane layeredPane) {
  437. getRootPane().setLayeredPane(layeredPane);
  438. }
  439. /**
  440. * Returns the <code>glassPane Component</code> for this window.
  441. *
  442. * @return the <code>glassPane</code> property
  443. * @see #setGlassPane
  444. * @see RootPaneContainer#getGlassPane
  445. */
  446. public Component getGlassPane() {
  447. return getRootPane().getGlassPane();
  448. }
  449. /**
  450. * Sets the <code>glassPane</code> property.
  451. * This method is called by the constructor.
  452. * @param glassPane the <code>glassPane</code> object for this window
  453. *
  454. * @see #getGlassPane
  455. * @see RootPaneContainer#setGlassPane
  456. *
  457. * @beaninfo
  458. * hidden: true
  459. * description: A transparent pane used for menu rendering.
  460. */
  461. public void setGlassPane(Component glassPane) {
  462. getRootPane().setGlassPane(glassPane);
  463. }
  464. /**
  465. * Returns a string representation of this <code>JWindow</code>.
  466. * This method
  467. * is intended to be used only for debugging purposes, and the
  468. * content and format of the returned string may vary between
  469. * implementations. The returned string may be empty but may not
  470. * be <code>null</code>.
  471. *
  472. * @return a string representation of this <code>JWindow</code>
  473. */
  474. protected String paramString() {
  475. String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
  476. "true" : "false");
  477. return super.paramString() +
  478. ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
  479. }
  480. /////////////////
  481. // Accessibility support
  482. ////////////////
  483. /** The accessible context property. */
  484. protected AccessibleContext accessibleContext = null;
  485. /**
  486. * Gets the AccessibleContext associated with this JWindow.
  487. * For JWindows, the AccessibleContext takes the form of an
  488. * AccessibleJWindow.
  489. * A new AccessibleJWindow instance is created if necessary.
  490. *
  491. * @return an AccessibleJWindow that serves as the
  492. * AccessibleContext of this JWindow
  493. */
  494. public AccessibleContext getAccessibleContext() {
  495. if (accessibleContext == null) {
  496. accessibleContext = new AccessibleJWindow();
  497. }
  498. return accessibleContext;
  499. }
  500. /**
  501. * This class implements accessibility support for the
  502. * <code>JWindow</code> class. It provides an implementation of the
  503. * Java Accessibility API appropriate to window user-interface
  504. * elements.
  505. */
  506. protected class AccessibleJWindow extends AccessibleAWTWindow {
  507. // everything is in the new parent, AccessibleAWTWindow
  508. }
  509. }