The Swing Action Manager (SAM) enhances Swing Actions and makes them easier to use.
SAM aids in the creation, usage and management of javax.swing.Actions in Swing application toolbars, menus and elsewhere.
It adds additional features to actions such as role-based security, context, a component factory and a XML format that allows
the weight-based merging of multiple action XML files.
SAM is fully-functional, with very good API documentation and decent (and improving) test coverage.
It has been deployed in production applications.
To learn more about SAM, read the online Javadoc package description.
It has full documentation including examples, usages, API details etc.
Though code complete, this project is new, so the project website is not yet fully functional.
Finish the Action XML Editor. The editor doesn't save the XML, though you could cut and paste from the preview button. See
the ActionEditor.java class for the rest of the editor todos.
Get eyes on it and feedback
Finalize the API (it should be pretty close)
A few more examples and a tutorial.
Examples
More examples are available in the
online Javadoc package description.
as well as the test and demo source code. Here's a quick one to whet the appetite:
SomeAppController.java
...
public void createFrame() {
...
JMenuBar menuBar = ActionUIFactory.getInstance().createMenuBar("main-menu");
myFrame.setJMenuBar(menuBar);
...
}
public void init() {
...
BasicAction saveDocument = (BasicAction)ActionManager.getAction("save");
createNewDocument.addActionListener() {
save();
}
createNewDocument.addShouldBeEnabledDelegate(new ShouldBeEnabledDelegate() {
public void shouldBeEnabled(javax.swing.Action action) {
return doc.isModified();
}
});
...
}
AppsActions.xml
...
<action id="new-action"
name="New"
mnemonic="N"
smicon="/toolbarButtonGraphics/general/New16.gif"
lgicon="/toolbarButtonGraphics/general/New24.gif"
accel="control N"
desc="Create a new document"
longdesc="This command creates a new document"
actionClass="com.foo.bar.OpenDialogAction"> <!-- This action opens any dialog given by the action.getValue("dialogClass") -->
<name-value-pair name="dialogClass" value="com.foo.bar.NewFileDialog"/>
<!-- Only administrator can create new docs. -->
<role name="Admin"/>
</action>
<action id="open-action"
command="OpenCommand"
name="Open..."
mnemonic="O"
smicon="/toolbarButtonGraphics/general/Open16.gif"
lgicon="/toolbarButtonGraphics/general/Open24.gif"
accel="control O"
desc="Open the specified document">
</action>
... save, etc...
<action-list id="main-menu">
<action-list id="file-menu">
<action idref="new-action"/>
<!-- This action inherits from open-action and defines a mnemonic-->
<action id="open-action-for-menu" idref="open-action" mnemonic="P"/>
<action idref="save-action"/>
<separator/>
<action idref="exit-action"/>
</action-list>
<action-list id="edit-menu" triggerActionRefId="edit-menu-action"> <!-- The popup menu's action-->
<action idref="copy-action"/>
<!-- You can define any or all the actions "inline" instead of refering to them by idref. -->
<action id="cut-action"
name="Cut"
mnemonic="T"
smicon="/toolbarButtonGraphics/general/Cut16.gif"
lgicon="/toolbarButtonGraphics/general/Cut24.gif"
accel="control X"
desc="Remove selected item"/>
<action idref="paste-action"/>
</action-list>
<action-list id="view-menu" triggerActionRefId="view-menu-action">
<!-- Creates a toggle group -->
<group id="align">
<action idref="align-left-action"/>
<action idref="align-center-action"/>
<action idref="align-right-action"/>
</group>
<!-- A JSeparator gets created for this. -->
<separator/>
<!-- Another extend and override -->
<action id="history-action-menu-overridde" idref="history-action"
mnemonic="B" desc="Shows or hides the status bar"/>
<role name="Manager"/>
</action-list>
<action-list id="help-menu" name="Help" mnemonic="H" desc="Help Operations">
<action idref="about-action"/>
</action-list>
</action-list>