com.levigo.util.swing.action
Class Context

java.lang.Object
  extended by java.util.AbstractCollection<Object>
      extended by com.levigo.util.swing.action.Context
All Implemented Interfaces:
Iterable<Object>, Collection<Object>

public class Context
extends AbstractCollection<Object>

The Context is the class in the middle of the context model. It contains context objects which serve as arguments for Commands.

The Context has just one owner, a JComponent. A reference to this owner is held inside the context. The Context is stored as ClientProperty of the owner.

Both, a FocusListener and a ContainerListener will be created for and registered with the owner. The context contains references on its own child contexts on which it is registered as a ContextListener.

The context contains the collection of the components from the child contexts as cachedActiveChildsContextObjects. The context objects contains information for the commands. (For example: items, session, viewer...).

The contexts form a hierarchy reflecting the Component hierarchy of the application's UI.

See Also:
Command, CommandAction, ContextListener

Nested Class Summary
static class Context.Ancestors
          Enum used to determine whether the aggregated view includes elements of ancestor (parent, grand-parent etc.) contexts.
static class Context.Children
          Enum used to determine whether the aggregated view includes elements of child contexts.
 
Field Summary
static String PROPERTY_KEY
          The ClientProperty key used by the GUI Component to store its context object in.
 
Method Summary
 boolean add(Object o)
          Ensures that this collection contains the specified element (optional operation).
 void addChildContext(Context child)
          Adds the context as child context and registers itself as ContextListener.
 void addContextChangeListener(ContextListener contextListener)
          Adds ContextListener to the collection of the listeners.
static boolean changed(JComponent c)
          Propagate a context change for the Context associated with the given component.
 void clear()
          Removes all of the elements from this collection (optional operation).
 void contextChanged()
          Propagate context changes to all parent contexts and actions that are registered as ContextListener.
static Context create(Context.Children ca, Context.Ancestors aa)
          Create an owner-less context with the given aggregation modes.
static Context get(JComponent component)
          Get the context for a given component by looking at the client properties.
protected  Collection<Context> getChildren()
          Returns the child contexts.
 JComponent getOwner()
          Returns the owner.
 Context getParent()
          Return the parent context.
static Context getPrivateContext(JComponent component)
          Get the context for a given component by looking at the client properties.
protected  Collection<ContextListener> getRegisteredListeners()
          Method getRegisteredListeners -ContextListener.
static Context install(JComponent owner, Context.Children ca, Context.Ancestors aa)
          Install a context with the given aggregation mode on the given owner.
 boolean isActive()
          Returns the info if the context is active.
 Iterator<Object> iterator()
          Returns an iterator over the elements in this context.
 boolean remove(Object o)
          Removes a single instance of the specified element from this collection, if it is present (optional operation).
 void removeChildContext(Context child)
          Removes the context from the collection of child contexts and unregisters itself as ContextListener.
 void removeContextChangeListener(ContextListener contextListener)
          Removes ContextListener from the collection of listeners so that it doesn't receive notifications any longer.
 void setActive(boolean active)
          Sets the active.
 int size()
          Returns the number of elements in this collection.
 String toString()
           
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

PROPERTY_KEY

public static String PROPERTY_KEY
The ClientProperty key used by the GUI Component to store its context object in.

Method Detail

get

public static Context get(JComponent component)
Get the context for a given component by looking at the client properties. If the component does have a context, it is returned. Otherwise the component hierarchy is searched upwards until a context is found or the root is reached.

Parameters:
component -
Returns:
the context or null if no component in the hierarchy had a context.

changed

public static boolean changed(JComponent c)
Propagate a context change for the Context associated with the given component. Don't propagate a change, if the component doesn't have a context.

Parameters:
c - the changed component
Returns:
true if there was a context to which the change could be propagated, false otherwise.

getPrivateContext

public static Context getPrivateContext(JComponent component)
Get the context for a given component by looking at the client properties. Return the context or null if it doesn't have one. In contrast to get(JComponent),this method doesn't look at ancestors in the component hierarchy.

Parameters:
component -
Returns:
the context or null if no component in the hierarchy had a context.

install

public static Context install(JComponent owner,
                              Context.Children ca,
                              Context.Ancestors aa)
Install a context with the given aggregation mode on the given owner. Wire it to its parent, if the owner already has one, otherwise postpone the wiring until the owner is added to one.

Parameters:
owner -
Returns:
The newly installed context.

create

public static Context create(Context.Children ca,
                             Context.Ancestors aa)
Create an owner-less context with the given aggregation modes.

Parameters:
ca -
aa -
Returns:

iterator

public Iterator<Object> iterator()
Returns an iterator over the elements in this context. There are no guarantees concerning the order in which the elements are returned. depending on the Returns an iterator over the elements in this context as well as all of the parent contexts up to the root context. The Elements are returned by context with the local (this) context's first, then the parent's Objects etc.

Specified by:
iterator in interface Iterable<Object>
Specified by:
iterator in interface Collection<Object>
Specified by:
iterator in class AbstractCollection<Object>
Returns:
an Iterator over the elements in this context and its parent contexts.

addChildContext

public void addChildContext(Context child)
Adds the context as child context and registers itself as ContextListener.

Parameters:
child - which should be registered in the context

removeChildContext

public void removeChildContext(Context child)
Removes the context from the collection of child contexts and unregisters itself as ContextListener.

Parameters:
child - which should be removed from the list of the context

addContextChangeListener

public void addContextChangeListener(ContextListener contextListener)
Adds ContextListener to the collection of the listeners. Information about context changes is sent to listeners registered here.

Parameters:
contextListener - to add to the collection of listeners.
See Also:
ContextListener

removeContextChangeListener

public void removeContextChangeListener(ContextListener contextListener)
Removes ContextListener from the collection of listeners so that it doesn't receive notifications any longer.

Parameters:
contextListener - to remove from the collection of listeners.
See Also:
ContextListener

contextChanged

public void contextChanged()
Propagate context changes to all parent contexts and actions that are registered as ContextListener.

See Also:
ContextListener.contextChanged(Context)

add

public boolean add(Object o)
Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false ). This preserves the invariant that a collection always contains the specified element after this call returns.

Specified by:
add in interface Collection<Object>
Overrides:
add in class AbstractCollection<Object>
Parameters:
o - element whose presence in this collection is to be ensured.
Returns:
true if this collection changed as a result of the call
Throws:
UnsupportedOperationException - add is not supported by this collection.
ClassCastException - class of the specified element prevents it from being added to this collection.
NullPointerException - if the specified element is null and this collection does not support null elements.
IllegalArgumentException - some aspect of this element prevents it from being added to this collection.

clear

public void clear()
Removes all of the elements from this collection (optional operation). This collection will be empty after this method returns unless it throws an exception.

Specified by:
clear in interface Collection<Object>
Overrides:
clear in class AbstractCollection<Object>
Throws:
UnsupportedOperationException - if the clear method is not supported by this collection.

remove

public boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

Specified by:
remove in interface Collection<Object>
Overrides:
remove in class AbstractCollection<Object>
Parameters:
o - element to be removed from this collection, if present.
Returns:
true if this collection changed as a result of the call
Throws:
ClassCastException - if the type of the specified element is incompatible with this collection (optional).
NullPointerException - if the specified element is null and this collection does not support null elements (optional).
UnsupportedOperationException - remove is not supported by this collection.

size

public int size()
Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface Collection<Object>
Specified by:
size in class AbstractCollection<Object>
Returns:
the number of elements in this collection

isActive

public boolean isActive()
Returns the info if the context is active.

Returns:
boolean

setActive

public void setActive(boolean active)
Sets the active. This property is important to read the context objects just from the active child context.

Parameters:
active - true- active, false- not active.

getChildren

protected Collection<Context> getChildren()
Returns the child contexts.

Returns:
Collection of the child contexts

getRegisteredListeners

protected Collection<ContextListener> getRegisteredListeners()
Method getRegisteredListeners -ContextListener.

Returns:
Collection of the ContextListeners

getOwner

public JComponent getOwner()
Returns the owner.

Returns:
JComponent

toString

public String toString()
Overrides:
toString in class AbstractCollection<Object>
See Also:
Object.toString()

getParent

public Context getParent()
Return the parent context.

Returns:
Context


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.