Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / awt / Window.java
blobd9e90c0ea4fc2be0468fa2c9c2948b24b89838a5
1 /* Window.java --
2 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2006 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)
9 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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
24 combination.
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. */
39 package java.awt;
41 import gnu.classpath.NotImplementedException;
43 import java.awt.event.ComponentEvent;
44 import java.awt.event.FocusEvent;
45 import java.awt.event.WindowAdapter;
46 import java.awt.event.WindowEvent;
47 import java.awt.event.WindowFocusListener;
48 import java.awt.event.WindowListener;
49 import java.awt.event.WindowStateListener;
50 import java.awt.image.BufferStrategy;
51 import java.awt.peer.WindowPeer;
52 import java.lang.ref.Reference;
53 import java.lang.ref.WeakReference;
54 import java.util.EventListener;
55 import java.util.Iterator;
56 import java.util.Locale;
57 import java.util.ResourceBundle;
58 import java.util.Vector;
60 import javax.accessibility.Accessible;
61 import javax.accessibility.AccessibleContext;
62 import javax.accessibility.AccessibleRole;
63 import javax.accessibility.AccessibleState;
64 import javax.accessibility.AccessibleStateSet;
66 /**
67 * This class represents a top-level window with no decorations.
69 * @author Aaron M. Renn (arenn@urbanophile.com)
70 * @author Warren Levy (warrenl@cygnus.com)
72 public class Window extends Container implements Accessible
74 private static final long serialVersionUID = 4497834738069338734L;
76 // Serialized fields, from Sun's serialization spec.
77 private String warningString = null;
78 private int windowSerializedDataVersion = 0; // FIXME
79 /** @since 1.2 */
80 // private FocusManager focusMgr; // FIXME: what is this?
81 /** @since 1.2 */
82 private int state = 0;
83 /** @since 1.4 */
84 private boolean focusableWindowState = true;
86 // A list of other top-level windows owned by this window.
87 private transient Vector ownedWindows = new Vector();
89 private transient WindowListener windowListener;
90 private transient WindowFocusListener windowFocusListener;
91 private transient WindowStateListener windowStateListener;
92 private transient GraphicsConfiguration graphicsConfiguration;
94 private transient boolean shown;
96 // This is package-private to avoid an accessor method.
97 transient Component windowFocusOwner;
100 * The number used to generate the name returned by getName.
102 private static transient long next_window_number;
104 protected class AccessibleAWTWindow extends AccessibleAWTContainer
106 private static final long serialVersionUID = 4215068635060671780L;
108 public AccessibleRole getAccessibleRole()
110 return AccessibleRole.WINDOW;
113 public AccessibleStateSet getAccessibleStateSet()
115 AccessibleStateSet states = super.getAccessibleStateSet();
116 if (isActive())
117 states.add(AccessibleState.ACTIVE);
118 return states;
122 /**
123 * This (package access) constructor is used by subclasses that want
124 * to build windows that do not have parents. Eg. toplevel
125 * application frames. Subclasses cannot call super(null), since
126 * null is an illegal argument.
128 Window()
130 visible = false;
131 // Windows are the only Containers that default to being focus
132 // cycle roots.
133 focusCycleRoot = true;
134 setLayout(new BorderLayout());
136 addWindowFocusListener (new WindowAdapter ()
138 public void windowGainedFocus (WindowEvent event)
140 if (windowFocusOwner != null)
142 // FIXME: move this section and the other similar
143 // sections in Component into a separate method.
144 EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
145 synchronized (eq)
147 KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
148 Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
149 if (currentFocusOwner != null)
151 eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
152 false, windowFocusOwner));
153 eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
154 false, currentFocusOwner));
156 else
157 eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
163 GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
164 graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration();
167 Window(GraphicsConfiguration gc)
169 this();
170 graphicsConfiguration = gc;
174 * Initializes a new instance of <code>Window</code> with the specified
175 * parent. The window will initially be invisible.
177 * @param owner The owning <code>Frame</code> of this window.
179 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
180 * is not from a screen device, or if owner is null; this exception is always
181 * thrown when GraphicsEnvironment.isHeadless returns true.
183 public Window(Frame owner)
185 this (owner, owner.getGraphicsConfiguration ());
189 * Initializes a new instance of <code>Window</code> with the specified
190 * parent. The window will initially be invisible.
192 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
193 * is not from a screen device, or if owner is null; this exception is always
194 * thrown when GraphicsEnvironment.isHeadless returns true.
196 * @since 1.2
198 public Window(Window owner)
200 this (owner, owner.getGraphicsConfiguration ());
204 * Initializes a new instance of <code>Window</code> with the specified
205 * parent. The window will initially be invisible.
207 * @exception IllegalArgumentException If owner is null or if gc is not from a
208 * screen device; this exception is always thrown when
209 * GraphicsEnvironment.isHeadless returns true.
211 * @since 1.3
213 public Window(Window owner, GraphicsConfiguration gc)
215 this ();
217 synchronized (getTreeLock())
219 if (owner == null)
220 throw new IllegalArgumentException ("owner must not be null");
222 parent = owner;
223 owner.ownedWindows.add(new WeakReference(this));
226 // FIXME: make this text visible in the window.
227 SecurityManager s = System.getSecurityManager();
228 if (s != null && ! s.checkTopLevelWindow(this))
229 warningString = System.getProperty("awt.appletWarning");
231 if (gc != null
232 && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
233 throw new IllegalArgumentException ("gc must be from a screen device");
235 if (gc == null)
236 graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
237 .getDefaultScreenDevice()
238 .getDefaultConfiguration();
239 else
240 graphicsConfiguration = gc;
243 GraphicsConfiguration getGraphicsConfigurationImpl()
245 if (graphicsConfiguration != null)
246 return graphicsConfiguration;
248 return super.getGraphicsConfigurationImpl();
252 * Creates the native peer for this window.
254 public void addNotify()
256 if (peer == null)
257 peer = getToolkit().createWindow(this);
258 super.addNotify();
262 * Relays out this window's child components at their preferred size.
264 * @specnote pack() doesn't appear to be called internally by show(), so
265 * we duplicate some of the functionality.
267 public void pack()
269 if (parent != null && !parent.isDisplayable())
270 parent.addNotify();
271 if (peer == null)
272 addNotify();
274 setSize(getPreferredSize());
276 validate();
280 * Shows on-screen this window and any of its owned windows for whom
281 * isVisible returns true.
283 public void show()
285 synchronized (getTreeLock())
287 if (parent != null && ! parent.isDisplayable())
288 parent.addNotify();
289 if (peer == null)
290 addNotify();
292 validate();
293 if (visible)
294 toFront();
295 else
297 super.show();
298 // Show visible owned windows.
299 Iterator e = ownedWindows.iterator();
300 while (e.hasNext())
302 Window w = (Window) (((Reference) e.next()).get());
303 if (w != null)
305 if (w.isVisible())
306 w.getPeer().setVisible(true);
308 else
309 // Remove null weak reference from ownedWindows.
310 // Unfortunately this can't be done in the Window's
311 // finalize method because there is no way to guarantee
312 // synchronous access to ownedWindows there.
313 e.remove();
316 KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
317 manager.setGlobalFocusedWindow(this);
319 if (! shown)
321 FocusTraversalPolicy policy = getFocusTraversalPolicy();
322 Component initialFocusOwner = null;
324 if (policy != null)
325 initialFocusOwner = policy.getInitialComponent(this);
327 if (initialFocusOwner != null)
328 initialFocusOwner.requestFocusInWindow();
330 shown = true;
335 public void hide()
337 // Hide visible owned windows.
338 synchronized (getTreeLock ())
340 Iterator e = ownedWindows.iterator();
341 while(e.hasNext())
343 Window w = (Window)(((Reference) e.next()).get());
344 if (w != null)
346 if (w.isVisible() && w.getPeer() != null)
347 w.getPeer().setVisible(false);
349 else
350 e.remove();
353 super.hide();
357 * Destroys any resources associated with this window. This includes
358 * all components in the window and all owned top-level windows.
360 public void dispose()
362 hide();
364 synchronized (getTreeLock ())
366 Iterator e = ownedWindows.iterator();
367 while(e.hasNext())
369 Window w = (Window)(((Reference) e.next()).get());
370 if (w != null)
371 w.dispose();
372 else
373 // Remove null weak reference from ownedWindows.
374 e.remove();
377 for (int i = 0; i < ncomponents; ++i)
378 component[i].removeNotify();
379 this.removeNotify();
381 // Post a WINDOW_CLOSED event.
382 WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
383 getToolkit().getSystemEventQueue().postEvent(we);
388 * Sends this window to the back so that all other windows display in
389 * front of it.
391 public void toBack()
393 if (peer != null)
395 WindowPeer wp = (WindowPeer) peer;
396 wp.toBack();
401 * Brings this window to the front so that it displays in front of
402 * any other windows.
404 public void toFront()
406 if (peer != null)
408 WindowPeer wp = (WindowPeer) peer;
409 wp.toFront();
414 * Returns the toolkit used to create this window.
416 * @return The toolkit used to create this window.
418 * @specnote Unlike Component.getToolkit, this implementation always
419 * returns the value of Toolkit.getDefaultToolkit().
421 public Toolkit getToolkit()
423 return Toolkit.getDefaultToolkit();
427 * Returns the warning string that will be displayed if this window is
428 * popped up by an unsecure applet or application.
430 * @return The unsecure window warning message.
432 public final String getWarningString()
434 return warningString;
438 * Returns the locale that this window is configured for.
440 * @return The locale this window is configured for.
442 public Locale getLocale()
444 return locale == null ? Locale.getDefault() : locale;
448 /** @since 1.2
449 public InputContext getInputContext()
451 // FIXME
456 * Sets the cursor for this window to the specifiec cursor.
458 * @param cursor The new cursor for this window.
460 public void setCursor(Cursor cursor)
462 super.setCursor(cursor);
465 public Window getOwner()
467 return (Window) parent;
470 /** @since 1.2 */
471 public Window[] getOwnedWindows()
473 Window [] trimmedList;
474 synchronized (getTreeLock ())
476 // Windows with non-null weak references in ownedWindows.
477 Window [] validList = new Window [ownedWindows.size()];
479 Iterator e = ownedWindows.iterator();
480 int numValid = 0;
481 while (e.hasNext())
483 Window w = (Window)(((Reference) e.next()).get());
484 if (w != null)
485 validList[numValid++] = w;
486 else
487 // Remove null weak reference from ownedWindows.
488 e.remove();
491 if (numValid != validList.length)
493 trimmedList = new Window [numValid];
494 System.arraycopy (validList, 0, trimmedList, 0, numValid);
496 else
497 trimmedList = validList;
499 return trimmedList;
503 * Adds the specified listener to the list of <code>WindowListeners</code>
504 * that will receive events for this window.
506 * @param listener The <code>WindowListener</code> to add.
508 public synchronized void addWindowListener(WindowListener listener)
510 windowListener = AWTEventMulticaster.add(windowListener, listener);
514 * Removes the specified listener from the list of
515 * <code>WindowListeners</code> that will receive events for this window.
517 * @param listener The <code>WindowListener</code> to remove.
519 public synchronized void removeWindowListener(WindowListener listener)
521 windowListener = AWTEventMulticaster.remove(windowListener, listener);
525 * Returns an array of all the window listeners registered on this window.
527 * @since 1.4
529 public synchronized WindowListener[] getWindowListeners()
531 return (WindowListener[])
532 AWTEventMulticaster.getListeners(windowListener,
533 WindowListener.class);
537 * Returns an array of all the window focus listeners registered on this
538 * window.
540 * @since 1.4
542 public synchronized WindowFocusListener[] getWindowFocusListeners()
544 return (WindowFocusListener[])
545 AWTEventMulticaster.getListeners(windowFocusListener,
546 WindowFocusListener.class);
550 * Returns an array of all the window state listeners registered on this
551 * window.
553 * @since 1.4
555 public synchronized WindowStateListener[] getWindowStateListeners()
557 return (WindowStateListener[])
558 AWTEventMulticaster.getListeners(windowStateListener,
559 WindowStateListener.class);
563 * Adds the specified listener to this window.
565 public void addWindowFocusListener (WindowFocusListener wfl)
567 windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
571 * Adds the specified listener to this window.
573 * @since 1.4
575 public void addWindowStateListener (WindowStateListener wsl)
577 windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
581 * Removes the specified listener from this window.
583 public void removeWindowFocusListener (WindowFocusListener wfl)
585 windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
589 * Removes the specified listener from this window.
591 * @since 1.4
593 public void removeWindowStateListener (WindowStateListener wsl)
595 windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
599 * Returns an array of all the objects currently registered as FooListeners
600 * upon this Window. FooListeners are registered using the addFooListener
601 * method.
603 * @exception ClassCastException If listenerType doesn't specify a class or
604 * interface that implements java.util.EventListener.
606 * @since 1.3
608 public EventListener[] getListeners(Class listenerType)
610 if (listenerType == WindowListener.class)
611 return getWindowListeners();
612 return super.getListeners(listenerType);
615 void dispatchEventImpl(AWTEvent e)
617 // Make use of event id's in order to avoid multiple instanceof tests.
618 if (e.id <= WindowEvent.WINDOW_LAST
619 && e.id >= WindowEvent.WINDOW_FIRST
620 && (windowListener != null
621 || windowFocusListener != null
622 || windowStateListener != null
623 || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
624 processEvent(e);
625 else
627 if (peer != null && (e.id == ComponentEvent.COMPONENT_RESIZED
628 || e.id == ComponentEvent.COMPONENT_MOVED))
630 Rectangle bounds = peer.getBounds();
631 x = bounds.x;
632 y = bounds.y;
633 height = bounds.height;
634 width = bounds.width;
636 if (e.id == ComponentEvent.COMPONENT_RESIZED)
638 invalidate();
639 validate();
642 super.dispatchEventImpl(e);
647 * Processes the specified event for this window. If the event is an
648 * instance of <code>WindowEvent</code>, then
649 * <code>processWindowEvent()</code> is called to process the event,
650 * otherwise the superclass version of this method is invoked.
652 * @param evt The event to process.
654 protected void processEvent(AWTEvent evt)
656 if (evt instanceof WindowEvent)
657 processWindowEvent((WindowEvent) evt);
658 else
659 super.processEvent(evt);
663 * Dispatches this event to any listeners that are listening for
664 * <code>WindowEvents</code> on this window. This method only gets
665 * invoked if it is enabled via <code>enableEvents()</code> or if
666 * a listener has been added.
668 * @param evt The event to process.
670 protected void processWindowEvent(WindowEvent evt)
672 int id = evt.getID();
674 if (id == WindowEvent.WINDOW_GAINED_FOCUS
675 || id == WindowEvent.WINDOW_LOST_FOCUS)
676 processWindowFocusEvent (evt);
677 else if (id == WindowEvent.WINDOW_STATE_CHANGED)
678 processWindowStateEvent (evt);
679 else
681 if (windowListener != null)
683 switch (evt.getID())
685 case WindowEvent.WINDOW_ACTIVATED:
686 windowListener.windowActivated(evt);
687 break;
689 case WindowEvent.WINDOW_CLOSED:
690 windowListener.windowClosed(evt);
691 break;
693 case WindowEvent.WINDOW_CLOSING:
694 windowListener.windowClosing(evt);
695 break;
697 case WindowEvent.WINDOW_DEACTIVATED:
698 windowListener.windowDeactivated(evt);
699 break;
701 case WindowEvent.WINDOW_DEICONIFIED:
702 windowListener.windowDeiconified(evt);
703 break;
705 case WindowEvent.WINDOW_ICONIFIED:
706 windowListener.windowIconified(evt);
707 break;
709 case WindowEvent.WINDOW_OPENED:
710 windowListener.windowOpened(evt);
711 break;
713 default:
714 break;
721 * Identifies if this window is active. The active window is a Frame or
722 * Dialog that has focus or owns the active window.
724 * @return true if active, else false.
725 * @since 1.4
727 public boolean isActive()
729 KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
730 return manager.getActiveWindow() == this;
734 * Identifies if this window is focused. A window is focused if it is the
735 * focus owner or it contains the focus owner.
737 * @return true if focused, else false.
738 * @since 1.4
740 public boolean isFocused()
742 KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
743 return manager.getFocusedWindow() == this;
747 * Returns the child window that has focus if this window is active.
748 * This method returns <code>null</code> if this window is not active
749 * or no children have focus.
751 * @return The component that has focus, or <code>null</code> if no
752 * component has focus.
754 public Component getFocusOwner ()
756 KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
758 Window activeWindow = manager.getActiveWindow ();
760 // The currently-focused Component belongs to the active Window.
761 if (activeWindow == this)
762 return manager.getFocusOwner ();
763 else
764 return null;
768 * Returns the child component of this window that would receive
769 * focus if this window were to become focused. If the window
770 * already has the top-level focus, then this method returns the
771 * same component as getFocusOwner. If no child component has
772 * requested focus within the window, then the initial focus owner
773 * is returned. If this is a non-focusable window, this method
774 * returns null.
776 * @return the child component of this window that most recently had
777 * the focus, or <code>null</code>
778 * @since 1.4
780 public Component getMostRecentFocusOwner ()
782 return windowFocusOwner;
786 * Set the focus owner for this window. This method is used to
787 * remember which component was focused when this window lost
788 * top-level focus, so that when it regains top-level focus the same
789 * child component can be refocused.
791 * @param windowFocusOwner the component in this window that owns
792 * the focus.
794 void setFocusOwner (Component windowFocusOwner)
796 this.windowFocusOwner = windowFocusOwner;
800 * Post a Java 1.0 event to the event queue.
802 * @param e The event to post.
804 * @deprecated
806 public boolean postEvent(Event e)
808 return handleEvent (e);
812 * Tests whether or not this window is visible on the screen.
814 * In contrast to the normal behaviour of Container, which is that
815 * a container is showing if its parent is visible and showing, a Window
816 * is even showing, if its parent (i.e. an invisible Frame) is not showing.
818 * @return <code>true</code> if this window is visible, <code>false</code>
819 * otherwise.
821 public boolean isShowing()
823 return isVisible();
826 public void setLocationRelativeTo(Component c)
828 int x = 0;
829 int y = 0;
831 if (c == null || !c.isShowing())
833 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
834 Point center = ge.getCenterPoint();
835 x = center.x - (width / 2);
836 y = center.y - (height / 2);
838 else
840 int cWidth = c.getWidth();
841 int cHeight = c.getHeight();
842 Dimension screenSize = getToolkit().getScreenSize();
844 x = c.getLocationOnScreen().x;
845 y = c.getLocationOnScreen().y;
847 // If bottom of component is cut off, window placed
848 // on the left or the right side of component
849 if ((y + cHeight) > screenSize.height)
851 // If the right side of the component is closer to the center
852 if ((screenSize.width / 2 - x) <= 0)
854 if ((x - width) >= 0)
855 x -= width;
856 else
857 x = 0;
859 else
861 if ((x + cWidth + width) <= screenSize.width)
862 x += cWidth;
863 else
864 x = screenSize.width - width;
867 y = screenSize.height - height;
869 else if (cWidth > width || cHeight > height)
871 // If right side of component is cut off
872 if ((x + width) > screenSize.width)
873 x = screenSize.width - width;
874 // If left side of component is cut off
875 else if (x < 0)
876 x = 0;
877 else
878 x += (cWidth - width) / 2;
880 y += (cHeight - height) / 2;
882 else
884 // If right side of component is cut off
885 if ((x + width) > screenSize.width)
886 x = screenSize.width - width;
887 // If left side of component is cut off
888 else if (x < 0 || (x - (width - cWidth) / 2) < 0)
889 x = 0;
890 else
891 x -= (width - cWidth) / 2;
893 if ((y - (height - cHeight) / 2) > 0)
894 y -= (height - cHeight) / 2;
895 else
896 y = 0;
900 setLocation(x, y);
904 * A BltBufferStrategy for windows.
906 private class WindowBltBufferStrategy extends BltBufferStrategy
909 * Creates a block transfer strategy for this window.
911 * @param numBuffers the number of buffers in this strategy
912 * @param accelerated true if the buffer should be accelerated,
913 * false otherwise
915 WindowBltBufferStrategy(int numBuffers, boolean accelerated)
917 super(numBuffers,
918 new BufferCapabilities(new ImageCapabilities(accelerated),
919 new ImageCapabilities(accelerated),
920 BufferCapabilities.FlipContents.COPIED));
925 * A FlipBufferStrategy for windows.
927 private class WindowFlipBufferStrategy extends FlipBufferStrategy
930 * Creates a flip buffer strategy for this window.
932 * @param numBuffers the number of buffers in this strategy
934 * @throws AWTException if the requested number of buffers is not
935 * supported
937 WindowFlipBufferStrategy(int numBuffers)
938 throws AWTException
940 super(numBuffers,
941 new BufferCapabilities(new ImageCapabilities(true),
942 new ImageCapabilities(true),
943 BufferCapabilities.FlipContents.COPIED));
948 * Creates a buffering strategy that manages how this window is
949 * repainted. This method attempts to create the optimum strategy
950 * based on the desired number of buffers. Hardware or software
951 * acceleration may be used.
953 * createBufferStrategy attempts different levels of optimization,
954 * but guarantees that some strategy with the requested number of
955 * buffers will be created even if it is not optimal. First it
956 * attempts to create a page flipping strategy, then an accelerated
957 * blitting strategy, then an unaccelerated blitting strategy.
959 * Calling this method causes any existing buffer strategy to be
960 * destroyed.
962 * @param numBuffers the number of buffers in this strategy
964 * @throws IllegalArgumentException if requested number of buffers
965 * is less than one
966 * @throws IllegalStateException if this window is not displayable
968 * @since 1.4
970 public void createBufferStrategy(int numBuffers)
972 if (numBuffers < 1)
973 throw new IllegalArgumentException("Window.createBufferStrategy: number"
974 + " of buffers is less than one");
976 if (!isDisplayable())
977 throw new IllegalStateException("Window.createBufferStrategy: window is"
978 + " not displayable");
980 BufferStrategy newStrategy = null;
982 // try a flipping strategy
985 newStrategy = new WindowFlipBufferStrategy(numBuffers);
987 catch (AWTException e)
991 // fall back to an accelerated blitting strategy
992 if (newStrategy == null)
993 newStrategy = new WindowBltBufferStrategy(numBuffers, true);
995 bufferStrategy = newStrategy;
999 * Creates a buffering strategy that manages how this window is
1000 * repainted. This method attempts to create a strategy based on
1001 * the specified capabilities and throws an exception if the
1002 * requested strategy is not supported.
1004 * Calling this method causes any existing buffer strategy to be
1005 * destroyed.
1007 * @param numBuffers the number of buffers in this strategy
1008 * @param caps the requested buffering capabilities
1010 * @throws AWTException if the requested capabilities are not
1011 * supported
1012 * @throws IllegalArgumentException if requested number of buffers
1013 * is less than one or if caps is null
1015 * @since 1.4
1017 public void createBufferStrategy(int numBuffers, BufferCapabilities caps)
1018 throws AWTException
1020 if (numBuffers < 1)
1021 throw new IllegalArgumentException("Window.createBufferStrategy: number"
1022 + " of buffers is less than one");
1024 if (caps == null)
1025 throw new IllegalArgumentException("Window.createBufferStrategy:"
1026 + " capabilities object is null");
1028 // a flipping strategy was requested
1029 if (caps.isPageFlipping())
1030 bufferStrategy = new WindowFlipBufferStrategy(numBuffers);
1031 else
1032 bufferStrategy = new WindowBltBufferStrategy(numBuffers, true);
1036 * Returns the buffer strategy used by the window.
1038 * @return the buffer strategy.
1039 * @since 1.4
1041 public BufferStrategy getBufferStrategy()
1043 return bufferStrategy;
1047 * @since 1.2
1049 * @deprecated
1051 public void applyResourceBundle(ResourceBundle rb)
1052 throws NotImplementedException
1054 throw new Error ("Not implemented");
1058 * @since 1.2
1060 * @deprecated
1062 public void applyResourceBundle(String rbName)
1064 ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
1065 ClassLoader.getSystemClassLoader());
1066 if (rb != null)
1067 applyResourceBundle(rb);
1071 * Gets the AccessibleContext associated with this <code>Window</code>.
1072 * The context is created, if necessary.
1074 * @return the associated context
1076 public AccessibleContext getAccessibleContext()
1078 /* Create the context if this is the first request */
1079 if (accessibleContext == null)
1080 accessibleContext = new AccessibleAWTWindow();
1081 return accessibleContext;
1084 /**
1085 * Get graphics configuration. The implementation for Window will
1086 * not ask any parent containers, since Window is a toplevel
1087 * window and not actually embedded in the parent component.
1089 public GraphicsConfiguration getGraphicsConfiguration()
1091 if (graphicsConfiguration != null) return graphicsConfiguration;
1092 if (peer != null) return peer.getGraphicsConfiguration();
1093 return null;
1096 protected void processWindowFocusEvent(WindowEvent event)
1098 if (windowFocusListener != null)
1100 switch (event.getID ())
1102 case WindowEvent.WINDOW_GAINED_FOCUS:
1103 windowFocusListener.windowGainedFocus (event);
1104 break;
1106 case WindowEvent.WINDOW_LOST_FOCUS:
1107 windowFocusListener.windowLostFocus (event);
1108 break;
1110 default:
1111 break;
1117 * @since 1.4
1119 protected void processWindowStateEvent(WindowEvent event)
1121 if (windowStateListener != null
1122 && event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
1123 windowStateListener.windowStateChanged (event);
1127 * Returns whether this <code>Window</code> can get the focus or not.
1129 * @since 1.4
1131 public final boolean isFocusableWindow ()
1133 if (getFocusableWindowState () == false)
1134 return false;
1136 if (this instanceof Dialog
1137 || this instanceof Frame)
1138 return true;
1140 // FIXME: Implement more possible cases for returning true.
1142 return false;
1146 * Returns the value of the focusableWindowState property.
1148 * @since 1.4
1150 public boolean getFocusableWindowState ()
1152 return focusableWindowState;
1156 * Sets the value of the focusableWindowState property.
1158 * @since 1.4
1160 public void setFocusableWindowState (boolean focusableWindowState)
1162 this.focusableWindowState = focusableWindowState;
1166 * Check whether this Container is a focus cycle root.
1167 * Returns always <code>true</code> as Windows are the
1168 * root of the focus cycle.
1170 * @return Always <code>true</code>.
1172 * @since 1.4
1174 public final boolean isFocusCycleRoot()
1176 return true;
1180 * Set whether or not this Container is the root of a focus
1181 * traversal cycle. Windows are the root of the focus cycle
1182 * and therefore this method does nothing.
1184 * @param focusCycleRoot ignored.
1186 * @since 1.4
1188 public final void setFocusCycleRoot(boolean focusCycleRoot)
1190 // calls to the method are ignored
1194 * Returns the root container that owns the focus cycle where this
1195 * component resides. Windows have no ancestors and this method
1196 * returns always <code>null</code>.
1198 * @return Always <code>null</code>.
1199 * @since 1.4
1201 public final Container getFocusCycleRootAncestor()
1203 return null;
1207 * Generate a unique name for this window.
1209 * @return A unique name for this window.
1211 String generateName()
1213 return "win" + getUniqueLong();
1216 private static synchronized long getUniqueLong()
1218 return next_window_number++;