com.metamatrix.console.util
Class AutoRefresher

java.lang.Object
  extended by com.metamatrix.console.util.AutoRefresher
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener

public class AutoRefresher
extends java.lang.Object
implements java.awt.event.ActionListener

Utility class to cause an automatic refresh of data controlled by the 'target' class. Intended to be used by panels representing sub applications within the Console. Could also be used by an external controller who would manage a set of these objects and the panels they control.

Clients can specify a refresh rate in seconds, and dynamically enable or disable auto refreshing.

An AutoRefresher has a true property, which controllers/targets should set to true when about to start a lengthy network operation, and then set to false when and if the operation finishes. The AutoRefresher class will ignore it's internal timer while busy, since it is in the middle of refreshing itself anyway. Note: it is a good idea to set the code that sets busy back to false in a finally block, to ensure that the AutoRefresher will be un-busied in the event of a failed network operation.

Example:

public class SomePanel extends BasePanel {

    public Collection retrieveSomeData() {
        Collection result;
        try{
            arAutoRefresher.setBusy(true);
            //HERE'S THE OPERATION THAT CAN TAKE SOME TIME, AND
            //POTENTIALLY FAIL --------------------------------
            result = remoteProxy.retrieveSomeData;
            //-------------------------------------------------
        } catch (SomeException e) {
            //do something
        } catch (OtherException e) {
            //do something
        } finally {
            arAutoRefresher.setBusy(false);
        }
    }
}


Field Summary
static int DEFAULT_DELAY
          Default refresh rate (seconds) used if none is otherwise specified.
 
Constructor Summary
AutoRefresher(AutoRefreshable refTarget, ConnectionInfo conn)
          Creates an AutoRefresher with the default refresh rate
AutoRefresher(AutoRefreshable refTarget, int refreshRateSeconds, boolean isAutoRefreshEnabled, ConnectionInfo conn)
          Creates an AutoRefresher with the specified refresh rate
AutoRefresher(AutoRefreshable refTarget, int refreshRateSeconds, ConnectionInfo conn)
          Creates an AutoRefresher with the specified refresh rate
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          This method, called by this Manager's Timer, indicates that time has run out.
 int getRefreshRate()
           
protected  javax.swing.Timer getTimer()
          Returns the Timer object used by AutoRefresher.
 void init()
          Calls super.init() and also initializes timing behavior.
 boolean isAutoRefreshEnabled()
          If the autoRefresh feature of AutoRefresher is enabled, the timer will run and the Manager will be notified each time the timer runs out, and will call it's own refresh() method.
 boolean isBusy()
          Indicates whether this AutoRefresher is in the middle of a network call or not.
 void refresh()
           
 void refresh(boolean setIsStale)
          This method calls super.refresh and, additionally, stops the timer running.
 void setAutoRefreshEnabled(boolean isAutoRefreshEnabled)
          If the autoRefresh feature of AutoRefresher is enabled, the timer will run and the Manager will be notified each time the timer runs out, and will call it's own refresh() method.
 void setBusy(boolean busy)
          Indicates whether this AutoRefresher is in the middle of a network call or not.
 void setRefreshRate(int seconds)
          Subclasses of AutoRefresher must be sure to set their refresh rate, in addition to other initialization behavior.
 void setRefreshTarget(AutoRefreshable ref)
           
 void startTimer()
          If the timer isn't already started, and autoRefresh is enabled, starts this Timed Manager's clock ticking until a Manager's data in memory is considered stale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DELAY

public static final int DEFAULT_DELAY
Default refresh rate (seconds) used if none is otherwise specified.

See Also:
Constant Field Values
Constructor Detail

AutoRefresher

public AutoRefresher(AutoRefreshable refTarget,
                     ConnectionInfo conn)
Creates an AutoRefresher with the default refresh rate

See Also:
DEFAULT_DELAY

AutoRefresher

public AutoRefresher(AutoRefreshable refTarget,
                     int refreshRateSeconds,
                     ConnectionInfo conn)
Creates an AutoRefresher with the specified refresh rate

Parameters:
refreshRateSeconds - rate at which AutoRefresher refreshes itself

AutoRefresher

public AutoRefresher(AutoRefreshable refTarget,
                     int refreshRateSeconds,
                     boolean isAutoRefreshEnabled,
                     ConnectionInfo conn)
Creates an AutoRefresher with the specified refresh rate

Parameters:
refreshRateSeconds - rate at which AutoRefresher refreshes itself
isAutoRefreshEnabled - controls whether AutoRefresher will refresh itself or not
Method Detail

init

public void init()
Calls super.init() and also initializes timing behavior.

See Also:
setRefreshRate(int)

startTimer

public void startTimer()
If the timer isn't already started, and autoRefresh is enabled, starts this Timed Manager's clock ticking until a Manager's data in memory is considered stale. Subclasses of AutoRefresher should call this method every time a request is made for its data.

Note that this method may always be called - it will only start the timer if autoRefresh is enabled.

There is no explicit stopTimer method, rather the refresh method causes the timer to stop. (The refresh method is either called by some outside code, or by this AutoRefresher when it's timer runs out.)

See Also:
refresh(boolean), AutoRefresher#setIsAutoRefreshEnabled

refresh

public void refresh(boolean setIsStale)
This method calls super.refresh and, additionally, stops the timer running. Subclasses of this AutoRefresher class must be sure to call the start() method whenever it retrieves its data. Does the work of refreshing in a background thread, because this can be slow (especially if the server becomes unavailable).

See Also:
startTimer()

refresh

public void refresh()

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
This method, called by this Manager's Timer, indicates that time has run out. In response, this manager triggers its own refresh.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
See Also:
refresh(boolean)

setRefreshTarget

public void setRefreshTarget(AutoRefreshable ref)

getRefreshRate

public int getRefreshRate()
Returns:
time, in seconds, after which data is stale

setRefreshRate

public void setRefreshRate(int seconds)
Subclasses of AutoRefresher must be sure to set their refresh rate, in addition to other initialization behavior.

Parameters:
seconds - time after which data is stale

isAutoRefreshEnabled

public boolean isAutoRefreshEnabled()
If the autoRefresh feature of AutoRefresher is enabled, the timer will run and the Manager will be notified each time the timer runs out, and will call it's own refresh() method. Disabling autoRefresh means that a AutoRefresher can only be refreshed by explicitly having its refresh() method called.

Returns:
boolean indicating if AutoRefresh is enabled or not

setAutoRefreshEnabled

public void setAutoRefreshEnabled(boolean isAutoRefreshEnabled)
If the autoRefresh feature of AutoRefresher is enabled, the timer will run and the Manager will be notified each time the timer runs out, and will call it's own refresh() method. Disabling autoRefresh means that a AutoRefresher can only be refreshed by explicitly having its refresh() method called.


getTimer

protected javax.swing.Timer getTimer()
Returns the Timer object used by AutoRefresher. It is not intended that it will ever be necessary to access this object directly.


isBusy

public boolean isBusy()
Indicates whether this AutoRefresher is in the middle of a network call or not. AutoRefresher will ignore its internal timer during this time, if auto-refresh is enabled.

Managers should set themselves to busy right before beginning a potentially long network operation, and set themselves to not busy right after the operation completes.

Returns:
boolean indicating whether AutoRefresher is in the middle of an atomic lengthy call

setBusy

public void setBusy(boolean busy)
Indicates whether this AutoRefresher is in the middle of a network call or not. AutoRefresher will ignore its internal timer during this time, if auto-refresh is enabled.

Managers should set themselves to busy right before beginning a potentially long network operation, and set themselves to not busy right after the operation completes.

Parameters:
busy - indicates AutoRefresher is in the middle of an atomic network call


Copyright © 2009. All Rights Reserved.