Window.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /* Window.java --
  2. Copyright (C) 1999, 2000, 2002 Free Software Foundation
  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.event.WindowEvent;
  33. import java.awt.event.WindowListener;
  34. import java.awt.peer.WindowPeer;
  35. import java.awt.peer.ComponentPeer;
  36. import java.util.EventListener;
  37. import java.util.Locale;
  38. import java.util.ResourceBundle;
  39. /**
  40. * This class represents a top-level window with no decorations.
  41. *
  42. * @author Aaron M. Renn (arenn@urbanophile.com)
  43. * @author Warren Levy <warrenl@cygnus.com>
  44. */
  45. public class Window extends Container
  46. {
  47. // Serialized fields, from Sun's serialization spec.
  48. // private FocusManager focusMgr; // FIXME: what is this?
  49. private String warningString = null;
  50. private int state = 0;
  51. private int windowSerializedDataVersion = 0; // FIXME
  52. private transient WindowListener windowListener;
  53. private transient GraphicsConfiguration graphicsConfiguration;
  54. /**
  55. * This (package access) constructor is used by subclasses that want
  56. * to build windows that do not have parents. Eg. toplevel
  57. * application frames. Subclasses cannot call super(null), since
  58. * null is an illegal argument.
  59. */
  60. Window()
  61. {
  62. setVisible(false);
  63. setLayout((LayoutManager) new BorderLayout());
  64. }
  65. Window(GraphicsConfiguration gc)
  66. {
  67. this();
  68. graphicsConfiguration = gc;
  69. }
  70. /**
  71. * Initializes a new instance of <code>Window</code> with the specified
  72. * parent. The window will initially be invisible.
  73. *
  74. * @param parent The owning <code>Frame</code> of this window.
  75. *
  76. * @exception IllegalArgumentException If the owner's GraphicsConfiguration
  77. * is not from a screen device, or if owner is null; this exception is always
  78. * thrown when GraphicsEnvironment.isHeadless returns true.
  79. */
  80. public Window(Frame owner)
  81. {
  82. this (owner, owner.getGraphicsConfiguration ());
  83. }
  84. /**
  85. * Initializes a new instance of <code>Window</code> with the specified
  86. * parent. The window will initially be invisible.
  87. *
  88. * @exception IllegalArgumentException If the owner's GraphicsConfiguration
  89. * is not from a screen device, or if owner is null; this exception is always
  90. * thrown when GraphicsEnvironment.isHeadless returns true.
  91. *
  92. * @since 1.2
  93. */
  94. public Window(Window owner)
  95. {
  96. this (owner, owner.getGraphicsConfiguration ());
  97. }
  98. /**
  99. * Initializes a new instance of <code>Window</code> with the specified
  100. * parent. The window will initially be invisible.
  101. *
  102. * @exception IllegalArgumentException If owner is null or if gc is not from a
  103. * screen device; this exception is always thrown when
  104. * GraphicsEnvironment.isHeadless returns true.
  105. *
  106. * @since 1.3
  107. */
  108. public Window(Window owner, GraphicsConfiguration gc)
  109. {
  110. this ();
  111. if (owner == null)
  112. throw new IllegalArgumentException ("owner must not be null");
  113. this.parent = owner;
  114. // FIXME: add to owner's "owned window" list
  115. //owner.owned.add(this); // this should be a weak reference
  116. /* FIXME: Security check
  117. SecurityManager.checkTopLevelWindow(...)
  118. */
  119. if (gc != null
  120. && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
  121. throw new IllegalArgumentException ("gc must be from a screen device");
  122. if (gc == null)
  123. graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
  124. .getDefaultScreenDevice()
  125. .getDefaultConfiguration();
  126. else
  127. graphicsConfiguration = gc;
  128. }
  129. GraphicsConfiguration getGraphicsConfigurationImpl()
  130. {
  131. if (graphicsConfiguration != null)
  132. return graphicsConfiguration;
  133. return super.getGraphicsConfigurationImpl();
  134. }
  135. /**
  136. * Disposes of the input methods and context, and removes the WeakReference
  137. * which formerly pointed to this Window from the parent's owned Window list.
  138. *
  139. * @exception Throwable The Exception raised by this method.
  140. */
  141. protected void finalize() throws Throwable
  142. {
  143. // FIXME: remove from owner's "owned window" list (Weak References)
  144. super.finalize();
  145. }
  146. /**
  147. * Creates the native peer for this window.
  148. */
  149. public void addNotify()
  150. {
  151. if (peer == null)
  152. peer = getToolkit().createWindow(this);
  153. super.addNotify();
  154. }
  155. /**
  156. * Relays out this window's child components at their preferred size.
  157. *
  158. * @specnote pack() doesn't appear to be called internally by show(), so
  159. * we duplicate some of the functionality.
  160. */
  161. public void pack()
  162. {
  163. if (parent != null && !parent.isDisplayable())
  164. parent.addNotify();
  165. if (peer == null)
  166. addNotify();
  167. setSize(getPreferredSize());
  168. validate();
  169. }
  170. /**
  171. * Makes this window visible and brings it to the front.
  172. */
  173. public void show()
  174. {
  175. if (parent != null && !parent.isDisplayable())
  176. parent.addNotify();
  177. if (peer == null)
  178. addNotify();
  179. validate();
  180. super.show();
  181. toFront();
  182. }
  183. public void hide()
  184. {
  185. // FIXME: call hide() on any "owned" children here.
  186. super.hide();
  187. }
  188. public boolean isDisplayable()
  189. {
  190. if (super.isDisplayable())
  191. return true;
  192. return peer != null;
  193. }
  194. /**
  195. * Called to free any resource associated with this window.
  196. */
  197. public void dispose()
  198. {
  199. hide();
  200. Window[] list = getOwnedWindows();
  201. for (int i=0; i<list.length; i++)
  202. list[i].dispose();
  203. for (int i = 0; i < ncomponents; ++i)
  204. component[i].removeNotify();
  205. this.removeNotify();
  206. }
  207. /**
  208. * Sends this window to the back so that all other windows display in
  209. * front of it.
  210. */
  211. public void toBack()
  212. {
  213. if (peer != null)
  214. {
  215. WindowPeer wp = (WindowPeer) peer;
  216. wp.toBack();
  217. }
  218. }
  219. /**
  220. * Brings this window to the front so that it displays in front of
  221. * any other windows.
  222. */
  223. public void toFront()
  224. {
  225. if (peer != null)
  226. {
  227. WindowPeer wp = (WindowPeer) peer;
  228. wp.toFront();
  229. }
  230. }
  231. /**
  232. * Returns the toolkit used to create this window.
  233. *
  234. * @return The toolkit used to create this window.
  235. *
  236. * @specnote Unlike Component.getToolkit, this implementation always
  237. * returns the value of Toolkit.getDefaultToolkit().
  238. */
  239. public Toolkit getToolkit()
  240. {
  241. return Toolkit.getDefaultToolkit();
  242. }
  243. /**
  244. * Returns the warning string that will be displayed if this window is
  245. * popped up by an unsecure applet or application.
  246. *
  247. * @return The unsecure window warning message.
  248. */
  249. public final String getWarningString()
  250. {
  251. boolean secure = true;
  252. /* boolean secure = SecurityManager.checkTopLevelWindow(...) */
  253. if (!secure)
  254. {
  255. if (warningString != null)
  256. return warningString;
  257. else
  258. {
  259. String warning = System.getProperty("awt.appletWarning");
  260. return warning;
  261. }
  262. }
  263. return null;
  264. }
  265. /**
  266. * Returns the locale that this window is configured for.
  267. *
  268. * @return The locale this window is configured for.
  269. */
  270. public Locale getLocale()
  271. {
  272. return locale == null ? Locale.getDefault() : locale;
  273. }
  274. /*
  275. /** @since 1.2
  276. public InputContext getInputContext()
  277. {
  278. // FIXME
  279. }
  280. */
  281. /**
  282. * Sets the cursor for this window to the specifiec cursor.
  283. *
  284. * @param cursor The new cursor for this window.
  285. */
  286. public void setCursor(Cursor cursor)
  287. {
  288. super.setCursor(cursor);
  289. }
  290. public Window getOwner()
  291. {
  292. return (Window) parent;
  293. }
  294. /** @since 1.2 */
  295. public Window[] getOwnedWindows()
  296. {
  297. // FIXME: return array containing all the windows this window currently
  298. // owns.
  299. return new Window[0];
  300. }
  301. /**
  302. * Adds the specified listener to the list of <code>WindowListeners</code>
  303. * that will receive events for this window.
  304. *
  305. * @param listener The <code>WindowListener</code> to add.
  306. */
  307. public synchronized void addWindowListener(WindowListener listener)
  308. {
  309. windowListener = AWTEventMulticaster.add(windowListener, listener);
  310. }
  311. /**
  312. * Removes the specified listener from the list of
  313. * <code>WindowListeners</code> that will receive events for this window.
  314. *
  315. * @param listener The <code>WindowListener</code> to remove.
  316. */
  317. public synchronized void removeWindowListener(WindowListener listener)
  318. {
  319. windowListener = AWTEventMulticaster.remove(windowListener, listener);
  320. }
  321. /**
  322. * Returns an array of all the window listeners registered on this window.
  323. *
  324. * @since 1.4
  325. */
  326. public synchronized WindowListener[] getWindowListeners()
  327. {
  328. return (WindowListener[])
  329. AWTEventMulticaster.getListeners(windowListener,
  330. WindowListener.class);
  331. }
  332. /**
  333. * Returns an array of all the objects currently registered as FooListeners
  334. * upon this Window. FooListeners are registered using the addFooListener
  335. * method.
  336. *
  337. * @exception ClassCastException If listenerType doesn't specify a class or
  338. * interface that implements java.util.EventListener.
  339. *
  340. * @since 1.3
  341. */
  342. public EventListener[] getListeners(Class listenerType)
  343. {
  344. if (listenerType == WindowListener.class)
  345. return getWindowListeners();
  346. return super.getListeners(listenerType);
  347. }
  348. void dispatchEventImpl(AWTEvent e)
  349. {
  350. // Make use of event id's in order to avoid multiple instanceof tests.
  351. if (e.id <= WindowEvent.WINDOW_LAST
  352. && e.id >= WindowEvent.WINDOW_FIRST
  353. && (windowListener != null
  354. || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
  355. processEvent(e);
  356. else
  357. super.dispatchEventImpl(e);
  358. }
  359. /**
  360. * Processes the specified event for this window. If the event is an
  361. * instance of <code>WindowEvent</code>, then
  362. * <code>processWindowEvent()</code> is called to process the event,
  363. * otherwise the superclass version of this method is invoked.
  364. *
  365. * @param event The event to process.
  366. */
  367. protected void processEvent(AWTEvent evt)
  368. {
  369. if (evt instanceof WindowEvent)
  370. processWindowEvent((WindowEvent) evt);
  371. else
  372. super.processEvent(evt);
  373. }
  374. /**
  375. * Dispatches this event to any listeners that are listening for
  376. * <code>WindowEvents</code> on this window. This method only gets
  377. * invoked if it is enabled via <code>enableEvents()</code> or if
  378. * a listener has been added.
  379. *
  380. * @param event The event to process.
  381. */
  382. protected void processWindowEvent(WindowEvent evt)
  383. {
  384. if (windowListener != null)
  385. {
  386. switch (evt.getID())
  387. {
  388. case WindowEvent.WINDOW_ACTIVATED:
  389. windowListener.windowActivated(evt);
  390. break;
  391. case WindowEvent.WINDOW_CLOSED:
  392. windowListener.windowClosed(evt);
  393. break;
  394. case WindowEvent.WINDOW_CLOSING:
  395. windowListener.windowClosing(evt);
  396. break;
  397. case WindowEvent.WINDOW_DEACTIVATED:
  398. windowListener.windowDeactivated(evt);
  399. break;
  400. case WindowEvent.WINDOW_DEICONIFIED:
  401. windowListener.windowDeiconified(evt);
  402. break;
  403. case WindowEvent.WINDOW_ICONIFIED:
  404. windowListener.windowIconified(evt);
  405. break;
  406. case WindowEvent.WINDOW_OPENED:
  407. windowListener.windowOpened(evt);
  408. break;
  409. }
  410. }
  411. }
  412. /**
  413. * Returns the child window that has focus if this window is active.
  414. * This method returns <code>null</code> if this window is not active
  415. * or no children have focus.
  416. *
  417. * @return The component that has focus, or <code>null</code> if no
  418. * component has focus.
  419. */
  420. public Component getFocusOwner()
  421. {
  422. // FIXME
  423. return null;
  424. }
  425. /**
  426. * Post a Java 1.0 event to the event queue.
  427. *
  428. * @param event The event to post.
  429. */
  430. public boolean postEvent(Event e)
  431. {
  432. // FIXME
  433. return false;
  434. }
  435. /**
  436. * Tests whether or not this window is visible on the screen.
  437. *
  438. * @return <code>true</code> if this window is visible, <code>false</code>
  439. * otherwise.
  440. */
  441. public boolean isShowing()
  442. {
  443. return super.isShowing();
  444. }
  445. /** @since 1.2 */
  446. public void applyResourceBundle(ResourceBundle rb)
  447. {
  448. // FIXME
  449. }
  450. /** @since 1.2 */
  451. public void applyResourceBundle(String rbName)
  452. {
  453. ResourceBundle rb = ResourceBundle.getBundle(rbName);
  454. if (rb != null)
  455. applyResourceBundle(rb);
  456. }
  457. /*
  458. public AccessibleContext getAccessibleContext()
  459. {
  460. // FIXME
  461. }
  462. */
  463. /**
  464. * Get graphics configuration. The implementation for Window will
  465. * not ask any parent containers, since Window is a toplevel
  466. * window and not actually embedded in the parent component.
  467. */
  468. public GraphicsConfiguration getGraphicsConfiguration()
  469. {
  470. if (graphicsConfiguration != null) return graphicsConfiguration;
  471. if (peer != null) return peer.getGraphicsConfiguration();
  472. return null;
  473. }
  474. }