package java.awt;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.awt.peer.WindowPeer;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.EventListener;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.accessibility.AccessibleState;
import javax.accessibility.AccessibleStateSet;
public class Window extends Container implements Accessible
{
private static final long serialVersionUID = 4497834738069338734L;
private String warningString = null;
private int windowSerializedDataVersion = 0;
private int state = 0;
private boolean focusableWindowState = true;
private transient Vector ownedWindows = new Vector();
private transient WindowListener windowListener;
private transient WindowFocusListener windowFocusListener;
private transient WindowStateListener windowStateListener;
private transient GraphicsConfiguration graphicsConfiguration;
private transient boolean shown;
private transient Component windowFocusOwner;
private static transient long next_window_number;
protected class AccessibleAWTWindow extends AccessibleAWTContainer
{
public AccessibleRole getAccessibleRole()
{
return AccessibleRole.WINDOW;
}
public AccessibleStateSet getAccessibleStateSet()
{
AccessibleStateSet states = super.getAccessibleStateSet();
if (isActive())
states.add(AccessibleState.ACTIVE);
return states;
}
}
Window()
{
visible = false;
focusCycleRoot = true;
setLayout(new BorderLayout());
addWindowFocusListener (new WindowAdapter ()
{
public void windowGainedFocus (WindowEvent event)
{
if (windowFocusOwner != null)
{
EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue ();
synchronized (eq)
{
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
Component currentFocusOwner = manager.getGlobalPermanentFocusOwner ();
if (currentFocusOwner != null)
{
eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST,
false, windowFocusOwner));
eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED,
false, currentFocusOwner));
}
else
eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false));
}
}
}
});
}
Window(GraphicsConfiguration gc)
{
this();
graphicsConfiguration = gc;
}
public Window(Frame owner)
{
this (owner, owner.getGraphicsConfiguration ());
}
public Window(Window owner)
{
this (owner, owner.getGraphicsConfiguration ());
}
public Window(Window owner, GraphicsConfiguration gc)
{
this ();
synchronized (getTreeLock())
{
if (owner == null)
throw new IllegalArgumentException ("owner must not be null");
parent = owner;
owner.ownedWindows.add(new WeakReference(this));
}
SecurityManager s = System.getSecurityManager();
if (s != null && ! s.checkTopLevelWindow(this))
warningString = System.getProperty("awt.appletWarning");
if (gc != null
&& gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN)
throw new IllegalArgumentException ("gc must be from a screen device");
if (gc == null)
graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
else
graphicsConfiguration = gc;
}
GraphicsConfiguration getGraphicsConfigurationImpl()
{
if (graphicsConfiguration != null)
return graphicsConfiguration;
return super.getGraphicsConfigurationImpl();
}
public void addNotify()
{
if (peer == null)
peer = getToolkit().createWindow(this);
super.addNotify();
}
public void pack()
{
if (parent != null && !parent.isDisplayable())
parent.addNotify();
if (peer == null)
addNotify();
setSize(getPreferredSize());
validate();
}
public void show()
{
if (parent != null && !parent.isDisplayable())
parent.addNotify();
if (peer == null)
addNotify();
synchronized (getTreeLock())
{
Iterator e = ownedWindows.iterator();
while(e.hasNext())
{
Window w = (Window)(((Reference) e.next()).get());
if (w != null)
{
if (w.isVisible())
w.getPeer().setVisible(true);
}
else
e.remove();
}
}
validate();
super.show();
toFront();
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
manager.setGlobalFocusedWindow (this);
if (!shown)
{
FocusTraversalPolicy policy = getFocusTraversalPolicy ();
Component initialFocusOwner = null;
if (policy != null)
initialFocusOwner = policy.getInitialComponent (this);
if (initialFocusOwner != null)
initialFocusOwner.requestFocusInWindow ();
shown = true;
}
}
public void hide()
{
synchronized (getTreeLock ())
{
Iterator e = ownedWindows.iterator();
while(e.hasNext())
{
Window w = (Window)(((Reference) e.next()).get());
if (w != null)
{
if (w.isVisible() && w.getPeer() != null)
w.getPeer().setVisible(false);
}
else
e.remove();
}
}
super.hide();
}
public boolean isDisplayable()
{
if (super.isDisplayable())
return true;
return peer != null;
}
public void dispose()
{
hide();
synchronized (getTreeLock ())
{
Iterator e = ownedWindows.iterator();
while(e.hasNext())
{
Window w = (Window)(((Reference) e.next()).get());
if (w != null)
w.dispose();
else
e.remove();
}
for (int i = 0; i < ncomponents; ++i)
component[i].removeNotify();
this.removeNotify();
WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED);
getToolkit().getSystemEventQueue().postEvent(we);
}
}
public void toBack()
{
if (peer != null)
{
WindowPeer wp = (WindowPeer) peer;
wp.toBack();
}
}
public void toFront()
{
if (peer != null)
{
WindowPeer wp = (WindowPeer) peer;
wp.toFront();
}
}
public Toolkit getToolkit()
{
return Toolkit.getDefaultToolkit();
}
public final String getWarningString()
{
return warningString;
}
public Locale getLocale()
{
return locale == null ? Locale.getDefault() : locale;
}
public void setCursor(Cursor cursor)
{
super.setCursor(cursor);
}
public Window getOwner()
{
return (Window) parent;
}
public Window[] getOwnedWindows()
{
Window [] trimmedList;
synchronized (getTreeLock ())
{
Window [] validList = new Window [ownedWindows.size()];
Iterator e = ownedWindows.iterator();
int numValid = 0;
while (e.hasNext())
{
Window w = (Window)(((Reference) e.next()).get());
if (w != null)
validList[numValid++] = w;
else
e.remove();
}
if (numValid != validList.length)
{
trimmedList = new Window [numValid];
System.arraycopy (validList, 0, trimmedList, 0, numValid);
}
else
trimmedList = validList;
}
return trimmedList;
}
public synchronized void addWindowListener(WindowListener listener)
{
windowListener = AWTEventMulticaster.add(windowListener, listener);
}
public synchronized void removeWindowListener(WindowListener listener)
{
windowListener = AWTEventMulticaster.remove(windowListener, listener);
}
public synchronized WindowListener[] getWindowListeners()
{
return (WindowListener[])
AWTEventMulticaster.getListeners(windowListener,
WindowListener.class);
}
public synchronized WindowFocusListener[] getWindowFocusListeners()
{
return (WindowFocusListener[])
AWTEventMulticaster.getListeners(windowFocusListener,
WindowFocusListener.class);
}
public synchronized WindowStateListener[] getWindowStateListeners()
{
return (WindowStateListener[])
AWTEventMulticaster.getListeners(windowStateListener,
WindowStateListener.class);
}
public void addWindowFocusListener (WindowFocusListener wfl)
{
windowFocusListener = AWTEventMulticaster.add (windowFocusListener, wfl);
}
public void addWindowStateListener (WindowStateListener wsl)
{
windowStateListener = AWTEventMulticaster.add (windowStateListener, wsl);
}
public void removeWindowFocusListener (WindowFocusListener wfl)
{
windowFocusListener = AWTEventMulticaster.remove (windowFocusListener, wfl);
}
public void removeWindowStateListener (WindowStateListener wsl)
{
windowStateListener = AWTEventMulticaster.remove (windowStateListener, wsl);
}
public EventListener[] getListeners(Class listenerType)
{
if (listenerType == WindowListener.class)
return getWindowListeners();
return super.getListeners(listenerType);
}
void dispatchEventImpl(AWTEvent e)
{
if (e.id <= WindowEvent.WINDOW_LAST
&& e.id >= WindowEvent.WINDOW_FIRST
&& (windowListener != null
|| windowFocusListener != null
|| windowStateListener != null
|| (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0))
processEvent(e);
else
super.dispatchEventImpl(e);
}
protected void processEvent(AWTEvent evt)
{
if (evt instanceof WindowEvent)
processWindowEvent((WindowEvent) evt);
else
super.processEvent(evt);
}
protected void processWindowEvent(WindowEvent evt)
{
int id = evt.getID();
if (id == WindowEvent.WINDOW_GAINED_FOCUS
|| id == WindowEvent.WINDOW_LOST_FOCUS)
processWindowFocusEvent (evt);
else if (id == WindowEvent.WINDOW_STATE_CHANGED)
processWindowStateEvent (evt);
else
{
if (windowListener != null)
{
switch (evt.getID())
{
case WindowEvent.WINDOW_ACTIVATED:
windowListener.windowActivated(evt);
break;
case WindowEvent.WINDOW_CLOSED:
windowListener.windowClosed(evt);
break;
case WindowEvent.WINDOW_CLOSING:
windowListener.windowClosing(evt);
break;
case WindowEvent.WINDOW_DEACTIVATED:
windowListener.windowDeactivated(evt);
break;
case WindowEvent.WINDOW_DEICONIFIED:
windowListener.windowDeiconified(evt);
break;
case WindowEvent.WINDOW_ICONIFIED:
windowListener.windowIconified(evt);
break;
case WindowEvent.WINDOW_OPENED:
windowListener.windowOpened(evt);
break;
default:
break;
}
}
}
}
public boolean isActive()
{
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
return manager.getActiveWindow() == this;
}
public boolean isFocused()
{
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
return manager.getFocusedWindow() == this;
}
public Component getFocusOwner ()
{
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
Window activeWindow = manager.getActiveWindow ();
if (activeWindow == this)
return manager.getFocusOwner ();
else
return windowFocusOwner;
}
void setFocusOwner (Component windowFocusOwner)
{
this.windowFocusOwner = windowFocusOwner;
}
public boolean postEvent(Event e)
{
return handleEvent (e);
}
public boolean isShowing()
{
return super.isShowing();
}
public void setLocationRelativeTo (Component c)
{
if (c == null || !c.isShowing ())
{
int x = 0;
int y = 0;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
Point center = ge.getCenterPoint ();
x = center.x - (width / 2);
y = center.y - (height / 2);
setLocation (x, y);
}
}
public void applyResourceBundle(ResourceBundle rb)
{
throw new Error ("Not implemented");
}
public void applyResourceBundle(String rbName)
{
ResourceBundle rb = ResourceBundle.getBundle(rbName, Locale.getDefault(),
ClassLoader.getSystemClassLoader());
if (rb != null)
applyResourceBundle(rb);
}
public AccessibleContext getAccessibleContext()
{
if (accessibleContext == null)
accessibleContext = new AccessibleAWTWindow();
return accessibleContext;
}
public GraphicsConfiguration getGraphicsConfiguration()
{
if (graphicsConfiguration != null) return graphicsConfiguration;
if (peer != null) return peer.getGraphicsConfiguration();
return null;
}
protected void processWindowFocusEvent(WindowEvent event)
{
if (windowFocusListener != null)
{
switch (event.getID ())
{
case WindowEvent.WINDOW_GAINED_FOCUS:
windowFocusListener.windowGainedFocus (event);
break;
case WindowEvent.WINDOW_LOST_FOCUS:
windowFocusListener.windowLostFocus (event);
break;
default:
break;
}
}
}
protected void processWindowStateEvent(WindowEvent event)
{
if (windowStateListener != null
&& event.getID () == WindowEvent.WINDOW_STATE_CHANGED)
windowStateListener.windowStateChanged (event);
}
public final boolean isFocusableWindow ()
{
if (getFocusableWindowState () == false)
return false;
if (this instanceof Dialog
|| this instanceof Frame)
return true;
return false;
}
public boolean getFocusableWindowState ()
{
return focusableWindowState;
}
public void setFocusableWindowState (boolean focusableWindowState)
{
this.focusableWindowState = focusableWindowState;
}
void setBoundsCallback (int x, int y, int w, int h)
{
if (this.x == x && this.y == y && width == w && height == h)
return;
invalidate();
boolean resized = width != w || height != h;
boolean moved = this.x != x || this.y != y;
this.x = x;
this.y = y;
width = w;
height = h;
if (resized && isShowing ())
{
ComponentEvent ce =
new ComponentEvent(this, ComponentEvent.COMPONENT_RESIZED);
getToolkit().getSystemEventQueue().postEvent(ce);
}
if (moved && isShowing ())
{
ComponentEvent ce =
new ComponentEvent(this, ComponentEvent.COMPONENT_MOVED);
getToolkit().getSystemEventQueue().postEvent(ce);
}
}
String generateName()
{
return "win" + getUniqueLong();
}
private static synchronized long getUniqueLong()
{
return next_window_number++;
}
}