GUI Commands User GuideVersion 2.1 |
||
Container PropertiesContainer properties are a mechanism that allows you to pass objects around your commands and groups in a loosely coupled manner. Once a command or group is bound to a container it has access to all the properties defined within the container hierarchy. Containers will request the property from their parent if it's not locally defined. Standard PropertiesThere are a number of standard properties are used to provide common infrastructure required by the library. These are:
CommandContainer container = new CommandContainer(); // set the delegate mediator container.setDelegateMediator(new FocusTrackingDelegateMediator(myFrame)); // set the glass pane strategy container.setGlassPaneStrategy(new DefaultGlassPaneStrategy()); // set the undo context container.setUndoContext(new UndoContext()); User Defined PropertiesYou can also define your own properties using
container.putProperty("my-custom-component", myCustomComponent);
Accessing Container PropertiesContainer properties can be accessed from commands using the convenience The following example shows accessing a container property during command execution.
public void handleExecute()
{
// get the property
Object propertyValue = getContainerProperty("myProperty");
// and use it...
...
}
Monitoring Container PropertiesThe situation is a little more complex if you need to add listeners to a container property to monitor its state. Container properties are only available once commands are bound and as such listeners must be updated in any of the following cases:
To simplify monitoring of container properties both commands and groups allow you to
add Unlike PropertyChangeListeners, ContainerPropertyListeners:
The following shows a typical example of monitoring a container property:
public class MyCommand extends ActionCommand
{
// Our model which is stored as a container property.
private Model model;
// Our model listener.
private ModelListener modelListener = ...;
/**
* Create our command and add our property listener
*/
public MyCommand()
{
super("my-command");
// Add a container property listener to monitor the
// "model" property of our container.
addContainerPropertyListener("model",
new ContainerPropertyListener()
{
public void propertyChanged(ContainerPropertyEvent e)
{
// update our model reference
setModel((Model) e.getNewValue())
}
}
updateState();
}
/**
* Configure our model
*/
public void setModel(Model model)
{
if (this.model != null)
{
// remove the listener from the old model
this.model.removeModelListener(modelListener);
}
this.model = model;
if (this.model != null)
{
// and listen to the new model
this.model.addModelListener(modelListener);
}
// update our state to reflect the new model
updateState();
}
private void updateState()
{
// we're only enabled if the model is configured and ok
setEnabled(model != null && model.isInSuitableState());
}
}
|
||