GUI Commands User GuideVersion 2.1 |
||
Containers and Binding Part IBinding is the mechanism by which commands and groups are associated with a particular container. Groups can then query their container to locate their members. The following diagram shows the basic principle.
Containers can also be combined into a hierarchy allowing you to segregate commands into individual names spaces. This is covered in depth in Part II. There are three basic binding styles for commands and groups as follows: 1. Container BindingIn container binding commands and groups are bound directly to a specific container. The following is an example of a group and command being bound to the same container: # A simple command. my-command@face.text=My Command # ..and a group that uses it group!my-group@face.text=My Group group!my-group@members=my-command // now create the container.. CommandContainer container = new CommandContainer(); // create and bind the group.. CommandGroup myGroup = new CommandGroup("my-group"); myGroup.bind(container); // create and bind the command.. ActionCommand myCommand = new ActionCommand("my-command") {...}; myCommand.bind(container); Once the command is bound the group will automatically discover and include it. The order of binding doesn't matter, groups can be bound before the commands they contain and vice versa. 2. Component BindingComponent binding uses the Swing component hierarchy to associate commands and groups with their container. In this you must also bind the container, along with the commands and groups directly to Swing components. Commands and Groups will automatically traverse the Swing hierarchy locating their nearest container. Using this approach you would typically start by binding a container to your main frame. Then all commands and groups within the frame will automatically find each other as they are added to the component hierarchy.
public class MainFrame extends JFrame
{
public MainFrame()
{
CommandContainer container = new CommandContainer();
container.bind(this);
CommandGroup myGroup = new CommandGroup("my-group");
myGroup.bind(this);
getContentPane().add(new MyCommandPanel());
}
}
public class MyCommandPanel extends JPanel
{
public MyCommandPanel()
{
ActionCommand myCommand = new ActionCommand("my-command") {...};
mycommand.bind(this);
}
}
In this case, the order of binding does matter. The CommandContainer must be bound before the groups and commands are bound and/or added to the component hierarchy. This is because groups and commands will only search for a container when they are first bound, or when their component hierarchy changes. Groups and commands can still be bound in any order relative to each other. The following diagram depicts the concept of component binding:
Please Note: This example only supports the case where there is only one instance of
MyCommandPanel in the frame at any given time. If there are to be multiple concurrent
instances you'll need to use a container hierarchy.
3. Global Container BindingGlobal binding is essentially the same as Container binding except that commands
and groups are bound to the global container. The global container instance is defined
by // create and bind the group.. CommandGroup myGroup = new CommandGroup("my-group"); myGroup.bind(); // create and bind the command.. ActionCommand myCommand = new ActionCommand("my-command") {...}; myCommand.bind(); |
||