2 Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.event.WindowEvent;
42 import java.awt.event.WindowFocusListener;
43 import java.awt.event.WindowListener;
44 import java.awt.event.WindowStateListener;
45 import java.awt.peer.WindowPeer;
46 import java.util.EventListener;
47 import java.util.Locale;
48 import java.util.ResourceBundle;
49 import javax.accessibility.Accessible;
50 import javax.accessibility.AccessibleContext;
53 * This class represents a top-level window with no decorations.
55 * @author Aaron M. Renn <arenn@urbanophile.com>
56 * @author Warren Levy <warrenl@cygnus.com>
58 public class Window extends Container implements Accessible
60 private static final long serialVersionUID = 4497834738069338734L;
62 // Serialized fields, from Sun's serialization spec.
63 private String warningString = null;
64 private int windowSerializedDataVersion = 0; // FIXME
66 // private FocusManager focusMgr; // FIXME: what is this?
68 private int state = 0;
70 private boolean focusableWindowState = true;
72 private transient WindowListener windowListener;
73 private transient WindowFocusListener windowFocusListener;
74 private transient WindowStateListener windowStateListener;
75 private transient GraphicsConfiguration graphicsConfiguration;
76 private transient AccessibleContext accessibleContext;
79 * This (package access) constructor is used by subclasses that want
80 * to build windows that do not have parents. Eg. toplevel
81 * application frames. Subclasses cannot call super(null), since
82 * null is an illegal argument.
87 setLayout(new BorderLayout());
90 Window(GraphicsConfiguration gc)
93 graphicsConfiguration = gc;
97 * Initializes a new instance of <code>Window</code> with the specified
98 * parent. The window will initially be invisible.
100 * @param parent The owning <code>Frame</code> of this window.
102 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
103 * is not from a screen device, or if owner is null; this exception is always
104 * thrown when GraphicsEnvironment.isHeadless returns true.
106 public Window(Frame owner)
108 this (owner, owner.getGraphicsConfiguration ());
112 * Initializes a new instance of <code>Window</code> with the specified
113 * parent. The window will initially be invisible.
115 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
116 * is not from a screen device, or if owner is null; this exception is always
117 * thrown when GraphicsEnvironment.isHeadless returns true.
121 public Window(Window owner)
123 this (owner, owner.getGraphicsConfiguration ());
127 * Initializes a new instance of <code>Window</code> with the specified
128 * parent. The window will initially be invisible.
130 * @exception IllegalArgumentException If owner is null or if gc is not from a
131 * screen device; this exception is always thrown when
132 * GraphicsEnvironment.isHeadless returns true.
136 public Window(Window owner, GraphicsConfiguration gc)
141 throw new IllegalArgumentException ("owner must not be null");
145 // FIXME: add to owner's "owned window" list
146 //owner.owned.add(this); // this should be a weak reference
148 /* FIXME: Security check
149 SecurityManager.checkTopLevelWindow(...)
153 && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
154 throw new IllegalArgumentException ("gc must be from a screen device");
156 // FIXME: until we implement this, it just causes AWT to crash.
158 // graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
159 // .getDefaultScreenDevice()
160 // .getDefaultConfiguration();
162 graphicsConfiguration = gc;
165 GraphicsConfiguration getGraphicsConfigurationImpl()
167 if (graphicsConfiguration != null)
168 return graphicsConfiguration;
170 return super.getGraphicsConfigurationImpl();
174 * Disposes of the input methods and context, and removes the WeakReference
175 * which formerly pointed to this Window from the parent's owned Window list.
177 * @exception Throwable The Exception raised by this method.
179 protected void finalize() throws Throwable
181 // FIXME: remove from owner's "owned window" list (Weak References)
186 * Creates the native peer for this window.
188 public void addNotify()
191 peer = getToolkit().createWindow(this);
196 * Relays out this window's child components at their preferred size.
198 * @specnote pack() doesn't appear to be called internally by show(), so
199 * we duplicate some of the functionality.
203 if (parent != null && !parent.isDisplayable())
208 setSize(getPreferredSize());
214 * Makes this window visible and brings it to the front.
218 if (parent != null && !parent.isDisplayable())
230 // FIXME: call hide() on any "owned" children here.
234 public boolean isDisplayable()
236 if (super.isDisplayable())
242 * Called to free any resource associated with this window.
244 public void dispose()
248 Window[] list = getOwnedWindows();
249 for (int i=0; i<list.length; i++)
252 for (int i = 0; i < ncomponents; ++i)
253 component[i].removeNotify();
258 * Sends this window to the back so that all other windows display in
265 WindowPeer wp = (WindowPeer) peer;
271 * Brings this window to the front so that it displays in front of
274 public void toFront()
278 WindowPeer wp = (WindowPeer) peer;
284 * Returns the toolkit used to create this window.
286 * @return The toolkit used to create this window.
288 * @specnote Unlike Component.getToolkit, this implementation always
289 * returns the value of Toolkit.getDefaultToolkit().
291 public Toolkit getToolkit()
293 return Toolkit.getDefaultToolkit();
297 * Returns the warning string that will be displayed if this window is
298 * popped up by an unsecure applet or application.
300 * @return The unsecure window warning message.
302 public final String getWarningString()
304 boolean secure = true;
305 /* boolean secure = SecurityManager.checkTopLevelWindow(...) */
309 if (warningString != null)
310 return warningString;
313 String warning = System.getProperty("awt.appletWarning");
321 * Returns the locale that this window is configured for.
323 * @return The locale this window is configured for.
325 public Locale getLocale()
327 return locale == null ? Locale.getDefault() : locale;
332 public InputContext getInputContext()
339 * Sets the cursor for this window to the specifiec cursor.
341 * @param cursor The new cursor for this window.
343 public void setCursor(Cursor cursor)
345 super.setCursor(cursor);
348 public Window getOwner()
350 return (Window) parent;
354 public Window[] getOwnedWindows()
356 // FIXME: return array containing all the windows this window currently
358 return new Window[0];
362 * Adds the specified listener to the list of <code>WindowListeners</code>
363 * that will receive events for this window.
365 * @param listener The <code>WindowListener</code> to add.
367 public synchronized void addWindowListener(WindowListener listener)
369 windowListener = AWTEventMulticaster.add(windowListener, listener);
373 * Removes the specified listener from the list of
374 * <code>WindowListeners</code> that will receive events for this window.
376 * @param listener The <code>WindowListener</code> to remove.
378 public synchronized void removeWindowListener(WindowListener listener)
380 windowListener = AWTEventMulticaster.remove(windowListener, listener);
384 * Returns an array of all the window listeners registered on this window.
388 public synchronized WindowListener[] getWindowListeners()
390 return (WindowListener[])
391 AWTEventMulticaster.getListeners(windowListener,
392 WindowListener.class);
396 * Returns an array of all the window focus listeners registered on this
401 public synchronized WindowFocusListener[] getWindowFocusListeners()
403 return (WindowFocusListener[])
404 AWTEventMulticaster.getListeners(windowFocusListener,
405 WindowFocusListener.class);
409 * Returns an array of all the window state listeners registered on this
414 public synchronized WindowStateListener[] getWindowStateListeners()
416 return (WindowStateListener[])
417 AWTEventMulticaster.getListeners(windowStateListener,
418 WindowStateListener.class);
422 * Adds the specified listener to this window.
424 public void addWindowFocusListener (WindowFocusListener wfl)
426 AWTEventMulticaster.add (windowFocusListener, wfl);
430 * Adds the specified listener to this window.
434 public void addWindowStateListener (WindowStateListener wsl)
436 AWTEventMulticaster.add (windowStateListener, wsl);
440 * Removes the specified listener from this window.
442 public void removeWindowFocusListener (WindowFocusListener wfl)
444 AWTEventMulticaster.remove (windowFocusListener, wfl);
448 * Removes the specified listener from this window.
452 public void removeWindowStateListener (WindowStateListener wsl)
454 AWTEventMulticaster.remove (windowStateListener, wsl);
458 * Returns an array of all the objects currently registered as FooListeners
459 * upon this Window. FooListeners are registered using the addFooListener
462 * @exception ClassCastException If listenerType doesn't specify a class or
463 * interface that implements java.util.EventListener.
467 public EventListener[] getListeners(Class listenerType)
469 if (listenerType == WindowListener.class)
470 return getWindowListeners();
471 return super.getListeners(listenerType);
474 void dispatchEventImpl(AWTEvent e)
476 // Make use of event id's in order to avoid multiple instanceof tests.
477 if (e.id <= WindowEvent.WINDOW_LAST
478 && e.id >= WindowEvent.WINDOW_FIRST
479 && (windowListener != null
480 || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
483 super.dispatchEventImpl(e);
487 * Processes the specified event for this window. If the event is an
488 * instance of <code>WindowEvent</code>, then
489 * <code>processWindowEvent()</code> is called to process the event,
490 * otherwise the superclass version of this method is invoked.
492 * @param event The event to process.
494 protected void processEvent(AWTEvent evt)
496 if (evt instanceof WindowEvent)
497 processWindowEvent((WindowEvent) evt);
499 super.processEvent(evt);
503 * Dispatches this event to any listeners that are listening for
504 * <code>WindowEvents</code> on this window. This method only gets
505 * invoked if it is enabled via <code>enableEvents()</code> or if
506 * a listener has been added.
508 * @param event The event to process.
510 protected void processWindowEvent(WindowEvent evt)
512 if (windowListener != null)
516 case WindowEvent.WINDOW_ACTIVATED:
517 windowListener.windowActivated(evt);
519 case WindowEvent.WINDOW_CLOSED:
520 windowListener.windowClosed(evt);
522 case WindowEvent.WINDOW_CLOSING:
523 windowListener.windowClosing(evt);
525 case WindowEvent.WINDOW_DEACTIVATED:
526 windowListener.windowDeactivated(evt);
528 case WindowEvent.WINDOW_DEICONIFIED:
529 windowListener.windowDeiconified(evt);
531 case WindowEvent.WINDOW_ICONIFIED:
532 windowListener.windowIconified(evt);
534 case WindowEvent.WINDOW_OPENED:
535 windowListener.windowOpened(evt);
537 case WindowEvent.WINDOW_GAINED_FOCUS:
538 case WindowEvent.WINDOW_LOST_FOCUS:
539 processWindowFocusEvent (evt);
541 case WindowEvent.WINDOW_STATE_CHANGED:
542 processWindowStateEvent (evt);
549 * Returns the child window that has focus if this window is active.
550 * This method returns <code>null</code> if this window is not active
551 * or no children have focus.
553 * @return The component that has focus, or <code>null</code> if no
554 * component has focus.
556 public Component getFocusOwner()
563 * Post a Java 1.0 event to the event queue.
565 * @param event The event to post.
569 public boolean postEvent(Event e)
576 * Tests whether or not this window is visible on the screen.
578 * @return <code>true</code> if this window is visible, <code>false</code>
581 public boolean isShowing()
583 return super.isShowing();
591 public void applyResourceBundle(ResourceBundle rb)
593 throw new Error ("Not implemented");
601 public void applyResourceBundle(String rbName)
603 ResourceBundle rb = ResourceBundle.getBundle(rbName);
605 applyResourceBundle(rb);
608 public AccessibleContext getAccessibleContext()
612 throw new Error ("Not implemented");
616 * Get graphics configuration. The implementation for Window will
617 * not ask any parent containers, since Window is a toplevel
618 * window and not actually embedded in the parent component.
620 public GraphicsConfiguration getGraphicsConfiguration()
622 if (graphicsConfiguration != null) return graphicsConfiguration;
623 if (peer != null) return peer.getGraphicsConfiguration();
627 protected void processWindowFocusEvent(WindowEvent event)
629 if (windowFocusListener != null)
631 switch (event.getID ())
633 case WindowEvent.WINDOW_GAINED_FOCUS:
634 windowFocusListener.windowGainedFocus (event);
637 case WindowEvent.WINDOW_LOST_FOCUS:
638 windowFocusListener.windowLostFocus (event);
650 protected void processWindowStateEvent(WindowEvent event)
652 if (windowStateListener != null
653 && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
654 windowStateListener.windowStateChanged (event);
658 * Returns whether this <code>Window</code> can get the focus or not.
662 public final boolean isFocusableWindow ()
664 if (getFocusableWindowState () == false)
667 if (this instanceof Dialog
668 || this instanceof Frame)
671 // FIXME: Implement more possible cases for returning true.
677 * Returns the value of the focusableWindowState property.
681 public boolean getFocusableWindowState ()
683 return focusableWindowState;
687 * Sets the value of the focusableWindowState property.
691 public void setFocusableWindowState (boolean focusableWindowState)
693 this.focusableWindowState = focusableWindowState;