|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.zig_project.gpl.common.gfx.WindowHandler
WindowHandler is a highly convienent and versatile widget to manage shutting down a VM in a windowed session. Once every window has closed, and all WindowHandlers are marked safe for shut-down, a final call is made to System.exit(0). The requirement is that each window to be monitored must have a WindowHandler added as a WindowListener to it.
Implementation: Each WindowHandler is implemented as a counter of the number of open windows on the workspace, based on the number of windowOpened and windowClosed events dispatched. Internally, another reference counter is maintained counting the number of active WindowHandlers. On construction, a WindowHandler is initially active, and is not deactivated until it has counted all windows as closed, and the handler is marked as safetoexit (true by default); and is reactivated as soon as another window is opened.
This is well suited for single window test cases:
public class Sample1 { public static void main(String[] args) { Frame f=new Frame("Example"); Label l=new Label("Hi"); f.add(l); new WindowHandler().APCS(f);} }Another example would be for multi-window environments.
public class Sample2 { private static final WindowHandler handler=new WindowHandler(); public static void main(String[] args) { String[] labels={"Hi", "Hi there", "This is just one more example"}; for (int i=labels.length-1; i>=0; i--) { Frame f=new Frame("Example "+(i+1)); Label l=new Label(labels[i]); f.add(l); handler.APCS(f);}} }Lastly, since these examples delegate exit calls to WindowHandler, we can trust WindowHandler to behave expectably when launching a subprocess within the same VM, for example:
public class Sample3 { public static void main(String[] args) { Sample1.main(args); Sample2.main(args);} }
Another feature of WindowHandler are window centering methods. Java 1.4 introduces Window.setLocationRelativeTo(Component). However, these methods are considered to be more versatile in that they will make a best attempt for compatability in VMs as early as 1.1, but will utilize features in newer VMs to be as aware as possible of insets in the screen, as well as multi-monitor systems. Lastly, once a window is centered, it is checked to ensure that it fits on screen, and is shrunken if necessary.
These examples provide very simplistic windows. Often times however, it is necessary to perform some operation before actually allowing the VM to exit, or even allowing the Window to close. While it is possible to add a custom window listener to each window, in addition to a WindowHandler, the java specification does not document nor require an order in which window listeners are notified of events. For this reason, WindowHandler provides a constructor accepting a WindowListener. All events recieved by a WindowHandler are first delegated to the registered listener, allowing that listener to call the handler's setExitOnClose method if necessary. By default, the handler uses a BasicWindowListener to automatically close any window that posts a windowClosing event.
Constructor Summary | |
WindowHandler()
Creates a default WindowHandler, which uses a BasicWindowListener |
|
WindowHandler(WindowListener wl)
Creates a WindowHandler which will delegate all window events to the registered listener before continuing processing. |
Method Summary | |
void |
APCS(Window w)
Add (this as a listener), Pack, Center, and Show |
static void |
centerWindow(Window w)
General purpose: centers the window. |
static void |
centerWindowOnScreen(Window w)
Centers the window within a screen. |
static void |
centerWindowOver(Window w,
Component c)
Centers the window ontop of the provided component. |
static void |
centerWindowOver(Window w,
Rectangle r)
Centers the window over a rectangular section of device space. |
protected void |
finalize()
If this handler were active at the time of finalization, we might have a serious logic error, so it is best for now to deactivate it. |
int |
getOpenFrameCount()
Returns the current number of frames counted by this handler as open. |
void |
setExitOnClose(boolean b)
Instructs the WindowHandler whether or not it is safe to terminate the VM once all windows have been closed. |
boolean |
willExitOnClose()
Returns the current state of the handler's safetoexit flag. |
void |
windowActivated(WindowEvent evt)
Only passed to the registered listener. |
void |
windowClosed(WindowEvent evt)
Decrements the number of open frames, if the source of the event is a frame, and possibly deactivates the handler. |
void |
windowClosing(WindowEvent evt)
Only passed to the registered listener. |
void |
windowDeactivated(WindowEvent evt)
Only passed to the registered listener. |
void |
windowDeiconified(WindowEvent evt)
Only passed to the registered listener. |
void |
windowIconified(WindowEvent evt)
Only passed to the registered listener. |
void |
windowOpened(WindowEvent evt)
Activates the handler and increments the number of open frames, if the source of the event was a Frame. |
boolean |
wouldExitOnNextClose()
Determines whether or not this handler would deactivate on the next windowClosed event. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public WindowHandler(WindowListener wl)
public WindowHandler()
Method Detail |
public static void centerWindow(Window w)
public static void centerWindowOnScreen(Window w)
public static void centerWindowOver(Window w, Component c)
public static void centerWindowOver(Window w, Rectangle r)
public final void APCS(Window w)
public void setExitOnClose(boolean b)
willExitOnClose()
protected void finalize() throws Throwable
Throwable
public boolean willExitOnClose()
setExitOnClose(boolean)
public int getOpenFrameCount()
public boolean wouldExitOnNextClose()
public void windowActivated(WindowEvent evt)
windowActivated
in interface WindowListener
public void windowDeactivated(WindowEvent evt)
windowDeactivated
in interface WindowListener
public void windowDeiconified(WindowEvent evt)
windowDeiconified
in interface WindowListener
public void windowIconified(WindowEvent evt)
windowIconified
in interface WindowListener
public void windowOpened(WindowEvent evt)
windowOpened
in interface WindowListener
public void windowClosing(WindowEvent evt)
windowClosing
in interface WindowListener
public void windowClosed(WindowEvent evt)
windowClosed
in interface WindowListener
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |