I'd like to show some concepts of creating simple, lightweight application framework based on some design patterns used in J2EE and Java Swing Application Framework. Main concept is to extend well known command pattern to make tasks and actions.
This "framework" is now a bunch of ideas and some proof of concept. I'd be happy to hear some comments from you.
Lets start with an example and see the source.
Actions framework solves some common tasks and problems that exist during rich application development:
- multiple access methods to functionalities (by button, menu item, context menu etc.),
- building multilingual interfaces,
- building sequences of tasks (for example to show dialog and save document before closing it ),
- organizing business components.
Basically it's an implementation of command pattern. Task is a part of code encapsulation single functionality. You can build sequences of both synchronous and asynchronous tasks. An Action is a task too, but has some more information such as name, label, description (tooltip text) and graphical icon. Action can be also saved after execution to build actions history to undo/redo them or even reuse as a macro.

Actions are designed to support multilingual interfaces. Action name is a key, and only property which is defined in the code. Label, icon and tooltip are automatically loaded from resources file. For example (ActionsFramework.properties file):
addAction.label=Add
addAction.tooltipText=Add task
addAction.icon=Embed('../../icons/add.png')
Those properties are then used in UI elements such as buttons, menus and context menus.
You can use UI elements from actionui package or use ActionsUIBinder to bind to existing UI elements. Example of ActionButton:
<actionui:actionbutton id="button1" action="{someAction}">
</actionui:actionbutton>
A button will display label, icon and toolTip from someAction and execute it on click. Building a toolbar from array of actions is also very easy. You can use ActionsUIBinder to build context menu as in the example :
for(var i:uint = 0; i < this.allActions.length; i++)
{
var item:ContextMenuItem = ActionsUIBinder.createBindedContextMenuItem(
AbstractAction(this.allActions[i]));
this.contextMenu.customItems.push(item);
}
Using tasks and actions makes user interface development much faster and actions are easy to manage. Even if you use every action in toolbar, drop down menu and context menu, you always have the same labels and icons with no effort – you only change properties file.
Generic support for cancel and undo actions is now in plans, but it's easy to implement in specific project because actions before execution are copied and saved in TaskManager history.

No comments:
Post a Comment