In this post we’ll start looking at how we use Java Actions to implement commands here. My examples will be taken from our order entry system. I used Actions in the order entry software with better results than I expected.

Action

Action

We’ll start in this post by looking at how I stored the actions. I have table in a database which binds an action label to a key within a context:

[sql]
CREATE TABLE [dbo].[OrderEntryKeyBindings](
[KeySet] [char](40) NOT NULL,
[KeyStroke] [char](40) NOT NULL,
[MimeType] [char](40) NOT NULL,
[Action] [varchar](max) NOT NULL,
[KeyLabel] [char](40) NOT NULL,
CONSTRAINT [PK_OrderEntryKeyBindings] PRIMARY KEY CLUSTERED
(
[KeySet] ASC,
[KeyStroke] ASC
));
[/sql]

Which looks like this when populated:

Table Contents

Table Contents

The KeySet column contains an arbitrary name I made up so that I could activate different action maps in different contexts. For example, when entering order information there’s a default key map that is active; when navigating a pop-up menu there’s a different key map active.

The KeyStroke is what the action is bound to; for example, “F1″ or “control B.” See documentation of javax.swing.KeyStroke.getKeyStroke() for more syntax information.

The MimeType for our purposes in this series of blogs will always be “text/plain.” Our software also handles short JavaScript scripts in the action map but that’s outside the scope of this post.

The Action column corresponds to an action label in the Java code, as we’ll see later.

Finally, the KeyLabel column is used to draw a map of the function keys and their actions, which turns out something like this, leaving out most of the blank keys for simplicity:

KeyMap

The idea is that if the user types the F1 key, something turns red, etc. We also hook the action up to the key map shown above, so that another way of triggering the TurnRed action is by tapping or clicking the “Turn Red” box when the key map is displayed.

That’s an overview, and a look at how the database stores the mappings between keys and actions. In a later post we’ll see how to use these in Java.

Image credit: Action Heroes by Sabine Sauermaul

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available