com.pietschy.command.delegate
Class FocusTrackingDelegateMediator

java.lang.Object
  extended by com.pietschy.command.delegate.AbstractDelegateMediator
      extended by com.pietschy.command.delegate.FocusTrackingDelegateMediator
All Implemented Interfaces:
DelegateMediator

public class FocusTrackingDelegateMediator
extends AbstractDelegateMediator

A DelegateMediator that finds its ActionCommandExecutor set by traversing up the swing component hierarchy from the focused component. The final set consists of all executors in all DelegateProviders found in the focus heirarchy along with any that have been statically added using setBaselineDelegateProviders(DelegateProvider[]) or setBaselineDelegates(ActionCommandExecutor, ActionCommandExecutor...). Those found from the focus hierarchy are given precedence over the baseline providers.

The delegate provider heirarchy is only updated on changes to the permenant focus owner.

The following examples show a panel the implements DelegateProvider utilising an internal CommandContainer.

   class MyPanel extends JPanel implements DelegateProvider
   {
      private CommandContainer commandContainer = new CommandContainer();

      public MyPanel()
      {
         ...
      }

      public ActionCommandExecutor getActionCommandExecutor(String id)
      {
         commandContainer.getActionCommandExecutor(id);
      }
   }
 


Field Summary
 
Fields inherited from class com.pietschy.command.delegate.AbstractDelegateMediator
EMPTY_PROVIDER_ARRAY
 
Constructor Summary
FocusTrackingDelegateMediator(java.awt.Window context)
          Creates a new DelegateManager that tracks focus in the same window as the specified component.
FocusTrackingDelegateMediator(java.awt.Window context, RelatedWindowDiscriminator discriminator)
          Creates a new DelegateManager that uses the specified RelatedWindowDiscriminator to determine focus events related to the main window.
 
Method Summary
protected  void extractProviders(java.awt.Component component, java.util.ArrayList<DelegateProvider> providers)
          Extracts all the provider from the heirarchy of the specified component.
 ActionCommandExecutor getExecutor(java.lang.String id)
          Searches the container hierarchy and gets the first executor it finds.
 java.awt.Window getWindow()
          Gets the window associated with this mediator.
protected  boolean isEligableForTracking(java.awt.Component component)
          Checks if the specified component is eligable for focus tracking.
protected  void recomputeProvidersFromComponent(java.awt.Component component)
          Invoked on a focus change to update the current provider list and notify all listeners
 void setBaselineDelegateProvider(DelegateProvider staticProvider)
          Sets the current static provider list to be the specified provider.
 void setBaselineDelegateProviders(DelegateProvider[] providers)
          Sets the current static provider list to the specified list.
 void setBaselineDelegates(ActionCommandExecutor delegate, ActionCommandExecutor... otherDelegates)
          Sets the current list of baseline delegates to those specified.
 void setBaselineDelegates(java.util.Collection<? extends ActionCommandExecutor> delegates)
          Sets the current list of baseline delegates to those specified.
 void setRelatedWindowDiscriminator(RelatedWindowDiscriminator relatedWindowDiscriminator)
          Sets the discriminator to use when tracking focus events.
 
Methods inherited from class com.pietschy.command.delegate.AbstractDelegateMediator
addDelegateMediatorListener, fireDelegatesChanged, getExecutor, nullSafeArray, nullSafeArray, removeDelegateMediatorListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FocusTrackingDelegateMediator

public FocusTrackingDelegateMediator(java.awt.Window context)
Creates a new DelegateManager that tracks focus in the same window as the specified component.

Parameters:
context - the window of interest.

FocusTrackingDelegateMediator

public FocusTrackingDelegateMediator(java.awt.Window context,
                                     RelatedWindowDiscriminator discriminator)
Creates a new DelegateManager that uses the specified RelatedWindowDiscriminator to determine focus events related to the main window.

Parameters:
context - the window of interest for this mediator.
Method Detail

getWindow

public java.awt.Window getWindow()
Gets the window associated with this mediator.

Returns:
the window associated with this mediator.

setBaselineDelegateProvider

public void setBaselineDelegateProvider(DelegateProvider staticProvider)
Sets the current static provider list to be the specified provider. This method will removed all focus specific providers until the next focus change. If the provider is null then the static provider list will be emptied, thus clearing all delegates until the next focus event.

Parameters:
staticProvider - the provider to use or null to clear the static provider list.

setBaselineDelegateProviders

public void setBaselineDelegateProviders(DelegateProvider[] providers)
Sets the current static provider list to the specified list. This method will removed all focus specific providers until the next focus change. If providers is null then the provider list will be emptied, thus clearing all delegates until the next focus event.

Parameters:
providers - the providers to use or null to clear the provider list.

setBaselineDelegates

public void setBaselineDelegates(ActionCommandExecutor delegate,
                                 ActionCommandExecutor... otherDelegates)
Sets the current list of baseline delegates to those specified. This method clears all existing baseline providers and adds a new provider containing the specified delegates.

Parameters:
delegate - the first delegate.
otherDelegates - any additional delegates.

setBaselineDelegates

public void setBaselineDelegates(java.util.Collection<? extends ActionCommandExecutor> delegates)
Sets the current list of baseline delegates to those specified. This method clears all existing baseline providers and adds a new provider containing the specified delegates.

Parameters:
delegates - the new delegates to use.

getExecutor

public ActionCommandExecutor getExecutor(java.lang.String id)
Description copied from interface: DelegateMediator
Searches the container hierarchy and gets the first executor it finds.

Parameters:
id - the id to search for.
Returns:
the first executor in the container hierarchy or null if none exists.

setRelatedWindowDiscriminator

public void setRelatedWindowDiscriminator(RelatedWindowDiscriminator relatedWindowDiscriminator)
Sets the discriminator to use when tracking focus events. The discriminator is used to determine if the currently focused window should be tracked for delegates associated with this mediators window.

The default discriminator only tracks delegates in the same window as the DelegatingCommand.

Parameters:
relatedWindowDiscriminator - the discriminator to use.
Throws:
java.lang.NullPointerException - if the discriminator is null.

extractProviders

protected void extractProviders(java.awt.Component component,
                                java.util.ArrayList<DelegateProvider> providers)
Extracts all the provider from the heirarchy of the specified component.

Parameters:
component - the component to traverse.
providers - the array in which to store any providers that are found.

recomputeProvidersFromComponent

protected void recomputeProvidersFromComponent(java.awt.Component component)
Invoked on a focus change to update the current provider list and notify all listeners

Parameters:
component - the currently focused component.

isEligableForTracking

protected boolean isEligableForTracking(java.awt.Component component)
Checks if the specified component is eligable for focus tracking. This method returns true but can be overridden by subclasses to prevent certain focus events from re-computing the delegate provider heirarchy.

Parameters:
component - the component that has been focused.
Returns:
true if the component should result in recalculation of the delegate chain. If false the delegate chain will remain as it was for the last focus event.


Copyright © 2006 - 2007 Andrew Pietsch