Task.ProgressMonitor
Class SwingUIHookAdapter

java.lang.Object
  extended by Task.ProgressMonitor.UIHookAdapter
      extended by Task.ProgressMonitor.SwingUIHookAdapter

public class SwingUIHookAdapter
extends UIHookAdapter

SwingUIHookAdapter is an EDT friendly adapter that uses the UIHookIF facility of the networking layer to provide feedback to the UI, and get cancel-signal from the UI as well.

This class can also function as a hybrid adapter - The whole idea being that you can cancel a task by invoking cancel() on this object, OR the provided SwingWorker/Future can be cancelled as well, and this will interrupt the underlying IO, and any task that is checking isCancelled(). Note that when used in this manner, resetting the cancel flag only resets the flag controlled by this class and NOT the isCancelled method on the SwingWorker/Future.

The following abstract methods that you have to implement to tie this into your UI:

  1. updateRecieveStatusInUI(int,int)
  2. updateSendStatusInUI(int,int)
  3. closeInUI()

In your GUI, if you want to interrupt the IO operation of the underlying HTTP client, then just call cancel(). This will interrupt the underlying IO stream. Please note that the cancel flag will cause any subsequent IO operations with this SwingUIHookAdapter to cancel as well. If you do not want this to happen, then call resetCancelFlag(). AbstractTask automatically does this for you, so you don't have to do any of this yourself.

This ProgressMonitor may be used by more than 1 objects. So be sure to clearAllStatusListeners() once you are done using this ProgressMonitor, so that there are no ties to the UI once you are done using this monitor.

Since:
Oct 1, 2007, 2:57:56 PM
Version:
1.0
Author:
Nazmul Idris

Nested Class Summary
static class SwingUIHookAdapter.PropertyList
           
 
Field Summary
protected  java.beans.PropertyChangeSupport _boundProperties
          property change listener support for status property
 
Fields inherited from class Task.ProgressMonitor.UIHookAdapter
_uiHook
 
Constructor Summary
SwingUIHookAdapter()
          simply creates an adapter class that you have to override with the implementations of the callback methods: updateRecieveStatusInUI(int,int) updateSendStatusInUI(int,int) (int, int)} closeInUI()
SwingUIHookAdapter(java.util.concurrent.Future<?> future)
          The Future object is used to determine if the cancel has occured.
 
Method Summary
 void addRecieveStatusListener(java.beans.PropertyChangeListener l)
          The property change event that's fired to the listener has the following parts: public void propertyChange(PropertyChangeEvent evt) { String type = evt.getPropertyName(); String progressStr = evt.getOldValue().toString(); int progress = Integer.parseInt(progressStr); String msg = evt.getNewValue().toString(); }
 void addSendStatusListener(java.beans.PropertyChangeListener l)
          The property change event that's fired to the listener has the following parts: public void propertyChange(PropertyChangeEvent evt) { String type = evt.getPropertyName(); String progressStr = evt.getOldValue().toString(); int progress = Integer.parseInt(progressStr); String msg = evt.getNewValue().toString(); }
 void addUnderlyingIOStreamInterruptedOrClosed(java.beans.PropertyChangeListener l)
          The property change event that's fired to the listener has the following parts: public void propertyChange(PropertyChangeEvent evt) { String type = evt.getPropertyName(); }
 void cancel()
          Call this method to interrupt the underlying IO stream.
 void clearAllStatusListeners()
          this runs in the EDT. why?
 void closeInUI()
          this is called in EDT, in your code. this is called if the underlying IO stream was closed for some reason.
 void enableRecieveStatusNotification(boolean enabled)
          you can put this UIHook in silent mode - will not update UI with status updates on RECV
 void enableSendStatusNotification(boolean enabled)
          you can put this UIHook in silent mode - will not update UI with status updates on SEND
 java.lang.String getProgressMessage()
           
 java.util.concurrent.Future<?> getSwingWorker()
          Returns a reference to the underlying SwingWorker providing the execution context/thread, if one has been copyFrom
 void interruptedIOInUI()
          this is called in EDT, in your code. this is called if the underlying IO stream was interrupted for some reason (user cancellation via UIHookIF, or SwingWorker (which belongs to a task) cancellation for some reason).
 boolean isCancelled()
          This is used to check if cancel() was called already.
 boolean isRecieveStatusNotificationEnabled()
          see if silent mode is activated for RECV updates
 boolean isSendStatusNotificationEnabled()
          see if silent mode is activated for SEND updates
 void resetCancelFlag()
          If cancel() is called, then all subsequent IO operations will be cancelled, until this method is called to reset the cancel condition.
 void restoreNotificationSet()
           
 void saveNotificationSet()
          this saves the current state of enableRecieveStatusNotification(boolean) and enableSendStatusNotification(boolean)
 void setProgressMessage(java.lang.String msg)
           
 void setSwingWorker(java.util.concurrent.Future<?> future)
          sets a reference to the underlying SwingWorker providing the execution context/thread. this is used to evaluate cancellation.
 void updateRecieveStatusInUI(int progress, int total)
          this is called in EDT, in your code. provides a RECV status update (progress bytes of total bytes)
 void updateSendStatusInUI(int progress, int total)
          this is called in EDT, in your code. provides a SEND status update (progress bytes of total bytes)
 
Methods inherited from class Task.ProgressMonitor.UIHookAdapter
getUIHook
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_boundProperties

protected java.beans.PropertyChangeSupport _boundProperties
property change listener support for status property

Constructor Detail

SwingUIHookAdapter

public SwingUIHookAdapter()
simply creates an adapter class that you have to override with the implementations of the callback methods:
  1. updateRecieveStatusInUI(int,int)
  2. updateSendStatusInUI(int,int) (int, int)}
  3. closeInUI()


SwingUIHookAdapter

public SwingUIHookAdapter(java.util.concurrent.Future<?> future)
                   throws java.lang.IllegalArgumentException
The Future object is used to determine if the cancel has occured.

Parameters:
future - SwingWorker that is provided by the Task API
Throws:
java.lang.IllegalArgumentException
Method Detail

clearAllStatusListeners

public void clearAllStatusListeners()
this runs in the EDT. why? it's common for tasks to release the listeners in the background while the ui hook is still being updated by the edt (an property change event update)... to avoid this loss of status info, it's best to run this in the EDT.


addSendStatusListener

public void addSendStatusListener(java.beans.PropertyChangeListener l)
                           throws java.lang.IllegalArgumentException
The property change event that's fired to the listener has the following parts:
 public void propertyChange(PropertyChangeEvent evt) {
   String type = evt.getPropertyName();
   String progressStr = evt.getOldValue().toString();
   int progress = Integer.parseInt(progressStr);
   String msg = evt.getNewValue().toString();
 }
 

Throws:
java.lang.IllegalArgumentException
See Also:
SwingUIHookAdapter.PropertyList.Send

addRecieveStatusListener

public void addRecieveStatusListener(java.beans.PropertyChangeListener l)
                              throws java.lang.IllegalArgumentException
The property change event that's fired to the listener has the following parts:
 public void propertyChange(PropertyChangeEvent evt) {
   String type = evt.getPropertyName();
   String progressStr = evt.getOldValue().toString();
   int progress = Integer.parseInt(progressStr);
   String msg = evt.getNewValue().toString();
 }
 

Throws:
java.lang.IllegalArgumentException
See Also:
SwingUIHookAdapter.PropertyList.Recieve

addUnderlyingIOStreamInterruptedOrClosed

public void addUnderlyingIOStreamInterruptedOrClosed(java.beans.PropertyChangeListener l)
                                              throws java.lang.IllegalArgumentException
The property change event that's fired to the listener has the following parts:
 public void propertyChange(PropertyChangeEvent evt) {
   String type = evt.getPropertyName();
 }
 

Throws:
java.lang.IllegalArgumentException
See Also:
SwingUIHookAdapter.PropertyList.UnderlyingIOStreamClosed, SwingUIHookAdapter.PropertyList.UnderlyingIOStreamInterruped

updateSendStatusInUI

public void updateSendStatusInUI(int progress,
                                 int total)
this is called in EDT, in your code. provides a SEND status update (progress bytes of total bytes)


updateRecieveStatusInUI

public void updateRecieveStatusInUI(int progress,
                                    int total)
this is called in EDT, in your code. provides a RECV status update (progress bytes of total bytes)


closeInUI

public void closeInUI()
this is called in EDT, in your code. this is called if the underlying IO stream was closed for some reason.


interruptedIOInUI

public void interruptedIOInUI()
this is called in EDT, in your code. this is called if the underlying IO stream was interrupted for some reason (user cancellation via UIHookIF, or SwingWorker (which belongs to a task) cancellation for some reason).


enableSendStatusNotification

public void enableSendStatusNotification(boolean enabled)
you can put this UIHook in silent mode - will not update UI with status updates on SEND

Parameters:
enabled - false activates silent mode. default is true (non silent mode)

enableRecieveStatusNotification

public void enableRecieveStatusNotification(boolean enabled)
you can put this UIHook in silent mode - will not update UI with status updates on RECV

Parameters:
enabled - false activates silent mode. default is true (non silent mode)

saveNotificationSet

public void saveNotificationSet()
this saves the current state of enableRecieveStatusNotification(boolean) and enableSendStatusNotification(boolean)


restoreNotificationSet

public void restoreNotificationSet()

isSendStatusNotificationEnabled

public boolean isSendStatusNotificationEnabled()
see if silent mode is activated for SEND updates

Returns:
false means silent mode is active, true is default

isRecieveStatusNotificationEnabled

public boolean isRecieveStatusNotificationEnabled()
see if silent mode is activated for RECV updates

Returns:
false means silent mode is active, true is default

getSwingWorker

public java.util.concurrent.Future<?> getSwingWorker()
Returns a reference to the underlying SwingWorker providing the execution context/thread, if one has been copyFrom

Returns:
may return null

setSwingWorker

public void setSwingWorker(java.util.concurrent.Future<?> future)
sets a reference to the underlying SwingWorker providing the execution context/thread. this is used to evaluate cancellation.


cancel

public void cancel()
Call this method to interrupt the underlying IO stream. This will case SEND or RECV to abort with InterruptedIOException. If you want to resume IO operations in the future, be sure to call resetCancelFlag().


isCancelled

public boolean isCancelled()
This is used to check if cancel() was called already.


resetCancelFlag

public void resetCancelFlag()
If cancel() is called, then all subsequent IO operations will be cancelled, until this method is called to reset the cancel condition.


setProgressMessage

public void setProgressMessage(java.lang.String msg)

getProgressMessage

public java.lang.String getProgressMessage()


Copyright © 2011. All Rights Reserved.