|
|
|||||
Evan Summers's BlogGooey Beans 2: Concrete ActionPosted by evanx on November 13, 2006 at 07:31 AM | Comments (0)The top story today is... Java is GPL'ed! The future begins today! And now for something completely unmomentous... An earlier demo (Address Form with Gbc) is being used as a vehicle to play with GroupLayout (more on that later), but more to the point, as a testbed for the following part of Gooey Beans, namely Concrete Action. In case you missed it, the first part was Resource Map.
The following abstract is a spoiler for the actual article...
Abstract
Here's the Address Form demo using actions.
As you can see we have actions that have been configured with icons and keystrokes, and we use these to configure toolbar buttons and menu items. "But Why?" So that we can disable one action object, to disable the button, menu item and keystroke associated with it. Also we ensure consistency in each screen eg. of the label and tooltip in the toolbar and menus, and also across the application, using global defaults, eg. for icons and keystrokes. Here's a code snippet of our concrete action to give you a gist.
public class QAction extends AbstractAction { String actionCommand; String keyStroke; String iconName; public QAction(QActionProperties properties) { configure(properties); } public void configure(QActionProperties properties) { setActionCommand(properties.getName()); setLabel(properties.getLabel()); setToolTip(properties.getToolTip()); setMnemonic(properties.getMnemonic()); setKeyStroke(properties.getKeyStroke()); setIconName(properties.getIconName()); } ... } As you can see, we have an action properties object, into which we can inject resources, eg. translations. We use this properties object to create our concrete action, which we can then use to create our menu items and toolbar buttons. We keep a global map of default action properties, and for each screen we can create new actions, and/or reference a global action, where we can override the defaults, eg. typically the tooltip eg. from a vague "Save data" default to a more informative "Save this Address Form." Our sample application code is as follows.
public class AddressFormPanel extends JPanel implements ActionListener { AddressFormPanelProperties properties = new AddressFormPanelProperties(); ... QAction newAction = new QAction(properties.newAction); QAction deleteAction = new QAction(properties.deleteAction); QAction editAction = new QAction(properties.editAction); QAction saveAction = new QAction(properties.saveAction); QAction cancelAction = new QAction(properties.cancelAction); JToolBar toolBar = createSmallIconToolBar( newAction, deleteAtion, editAction, saveAction, cancelAction ); ... public void actionPerformed(ActionEvent event) { if (newAction.equalsActionCommand(event)) { ... } else if (deleteAction.equalsActionCommand(event)) { ... } ... setEnabled(); } public void setEnabled() { saveAction.setEnabled(isEdited()); cancelAction.setEnabled(isEdited()); deleteAction.setEnabled(isSelected()); ... } ... } where we reference the actions in our actionPerformed() method, and also of course to enable/disable them depending on the current state.
Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment | |||||
|
|