com.mtp.gui
Class WindowWatcher

java.lang.Object
  |
  +--com.mtp.gui.WindowWatcher
All Implemented Interfaces:
AWTEventListener, EventListener
Direct Known Subclasses:
SystemExitWatcher

public class WindowWatcher
extends Object
implements AWTEventListener

Class that begins observing Window events as soon as it is created. Allows developer to retrieve windows and also control which will be observed.

Author:
Matthew Pekar

Field Summary
static long DEFAULT_WAIT_TIMEOUT
          Default timeout for waitTillWindowPresent(), waitTillWindowNotPresent() and other future wait functions.
protected  boolean filterAllWindows
          Whether to ignore all incoming windows.
protected  HashMap filteredHashCodes
          Ignore Window's with these hash codes.
protected  HashMap filteredNames
          Ignore Window's with these names.
protected  Integer id
          Next index to be used.
protected  boolean removeWindows
          Whether to remove Window's when removeWindow() is called.
protected  long waitTimeout
          Time in milliseconds for wait functions.
protected  HashMap windows
          HashMap of Window to Integer.
 
Constructor Summary
WindowWatcher()
          Default constructor.
 
Method Summary
protected  void addWindow(Window w)
          Hashes given Window at current id and increments current id by 1.
protected  void clearWindows()
          Clears windows HashMap with synchronization.
 boolean containsWindow(int id)
          Returns true if given Window id is being watched.
 boolean containsWindow(Window w)
          Returns true if given Window is being watched.
 HashMap copyWindows()
          Returns a new copy of the windows HashMap.
 void disconnect(boolean dispose)
          Removes as listener for window events and dispose()'s all windows if requested.
 void disposeWindows()
          Call dispose() on all windows then clear the HashMap.
 void eventDispatched(AWTEvent e)
          Update 'windows' based on WindowEvent.
protected  boolean filtered(Window w)
          Returns true if given Window should not be watched.
 void filterWindow(int hashCode)
          Filter Window's with this hash code.
 void filterWindow(String name)
          Don't let Window's with this name be added.
 boolean getFilterAllWindows()
           
 boolean getRemoveWindows()
          Return whether Window's are removed from our map as they disappear.
 Window getWindowByID(int id)
          Returns Window with given id or null if it's not present.
 Window getWindowByName(String name)
          Return Window with given name or null if none exists.
 Window getWindowByTitle(String title)
          Return Window with given title or null if none exists.
 int getWindowCount()
          Returns number of windows being watched.
 int getWindowID(Window w)
          Returns id of given Window and throws NoSuchElementException if not found.
 Collection getWindows()
          Returns a new collection of the Window's being watched.
protected  void removeFilteredWindows()
          Go through all Window's and remove any that are filtered.
protected  void removeWindow(Window w)
          Remove given window.
 void setFilterAllWindows(boolean b)
          If true, ignore all new windows that appear.
 void setRemoveWindows(boolean whether)
          Set whether Window's should be removed from our map as they disappear.
 void setWaitTimeout(long time)
          Set the amount of time to before a waitTillSomething() command fails.
 void updateUIs()
          Tells all windows I'm watching to update their UI.
 void waitTillWindowNotPresent(Window w)
          Uses waitTimeout varaible as timeout.
 void waitTillWindowNotPresent(Window w, long timeout)
          This method returns after the given Window has been closed or the timeout is reached.
 void waitTillWindowPresent(Window w)
          Uses waitTimeout varaible as timeout.
 void waitTillWindowPresent(Window w, long timeout)
          This method returns after the given Window has been added or the timeout is reached.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_WAIT_TIMEOUT

public static long DEFAULT_WAIT_TIMEOUT
Default timeout for waitTillWindowPresent(), waitTillWindowNotPresent() and other future wait functions. Measured in milliseconds.


id

protected Integer id
Next index to be used. Guaranteed to be incremented in consistent manner.


windows

protected HashMap windows
HashMap of Window to Integer.


filteredNames

protected HashMap filteredNames
Ignore Window's with these names.


filteredHashCodes

protected HashMap filteredHashCodes
Ignore Window's with these hash codes.


waitTimeout

protected volatile long waitTimeout
Time in milliseconds for wait functions.


filterAllWindows

protected volatile boolean filterAllWindows
Whether to ignore all incoming windows.


removeWindows

protected volatile boolean removeWindows
Whether to remove Window's when removeWindow() is called. Default is true.

Constructor Detail

WindowWatcher

public WindowWatcher()
Default constructor. Initializes id to 0, creates maps, and attaches self as window event listener.

Method Detail

getWindows

public Collection getWindows()
Returns a new collection of the Window's being watched.


setWaitTimeout

public void setWaitTimeout(long time)
Set the amount of time to before a waitTillSomething() command fails.


setRemoveWindows

public void setRemoveWindows(boolean whether)
Set whether Window's should be removed from our map as they disappear.


getRemoveWindows

public boolean getRemoveWindows()
Return whether Window's are removed from our map as they disappear.


setFilterAllWindows

public void setFilterAllWindows(boolean b)
If true, ignore all new windows that appear.


getFilterAllWindows

public boolean getFilterAllWindows()
Returns:
True if new windows are being ignored.

updateUIs

public void updateUIs()
Tells all windows I'm watching to update their UI.


getWindowCount

public int getWindowCount()
Returns number of windows being watched.


copyWindows

public HashMap copyWindows()
Returns a new copy of the windows HashMap.


clearWindows

protected void clearWindows()
Clears windows HashMap with synchronization.


disposeWindows

public void disposeWindows()
Call dispose() on all windows then clear the HashMap.


disconnect

public void disconnect(boolean dispose)
Removes as listener for window events and dispose()'s all windows if requested. Be very careful here, potential thread lock when dispose()'ing Window's.


getWindowByName

public Window getWindowByName(String name)
Return Window with given name or null if none exists. Not responsible for duplicates.


getWindowByTitle

public Window getWindowByTitle(String title)
Return Window with given title or null if none exists. Not responsible for duplicates.


getWindowID

public int getWindowID(Window w)
Returns id of given Window and throws NoSuchElementException if not found.


getWindowByID

public Window getWindowByID(int id)
Returns Window with given id or null if it's not present.


waitTillWindowPresent

public void waitTillWindowPresent(Window w)
                           throws InterruptedException,
                                  TimeLimitExceededException
Uses waitTimeout varaible as timeout.

InterruptedException
TimeLimitExceededException

waitTillWindowNotPresent

public void waitTillWindowNotPresent(Window w)
                              throws InterruptedException,
                                     TimeLimitExceededException
Uses waitTimeout varaible as timeout.

InterruptedException
TimeLimitExceededException

waitTillWindowPresent

public void waitTillWindowPresent(Window w,
                                  long timeout)
                           throws InterruptedException,
                                  TimeLimitExceededException
This method returns after the given Window has been added or the timeout is reached.

InterruptedException
TimeLimitExceededException

waitTillWindowNotPresent

public void waitTillWindowNotPresent(Window w,
                                     long timeout)
                              throws InterruptedException,
                                     TimeLimitExceededException
This method returns after the given Window has been closed or the timeout is reached.

InterruptedException
TimeLimitExceededException

containsWindow

public boolean containsWindow(Window w)
Returns true if given Window is being watched.


containsWindow

public boolean containsWindow(int id)
Returns true if given Window id is being watched.


filtered

protected boolean filtered(Window w)
Returns true if given Window should not be watched.


filterWindow

public void filterWindow(String name)
Don't let Window's with this name be added.


filterWindow

public void filterWindow(int hashCode)
Filter Window's with this hash code.


removeFilteredWindows

protected void removeFilteredWindows()
Go through all Window's and remove any that are filtered.


addWindow

protected void addWindow(Window w)
Hashes given Window at current id and increments current id by 1.


removeWindow

protected void removeWindow(Window w)
Remove given window.


eventDispatched

public void eventDispatched(AWTEvent e)
Update 'windows' based on WindowEvent.

Specified by:
eventDispatched in interface AWTEventListener


Copyright © 2002 Matthew Pekar. All Rights Reserved.