In previous postings we’ve looked at how to store action names or keys in a database, and how to associate those with keystrokes. Today we’ll look at an Action routine more closely.

Action

Action

Here’s a very simple Action. It remembers an instance of an OrderForm from its (the Action’s) instantiation and, when the Action is invoked, it in turn invokes OrderForm.turnRed().

[java]
package com.sscorp.core.actions;

import com.sscorp.core.OrderForm;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

/**
* A sample
*/
public class TurnRed implements javax.swing.Action {

private OrderForm orderForm;

public TurnRed(OrderForm orderForm) {
this.orderForm = orderForm;
}

@Override
public Object getValue(String key) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void putValue(String key, Object value) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void setEnabled(boolean b) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean isEnabled() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void actionPerformed(ActionEvent e) {
orderForm.turnRed();
}
}
[/java]

This use of actions to implement commands in Java has generally been accepted here as a standard design pattern in our order entry application.

We’ve implemented a Swing view of the function keys and the commands that are mapped to each key. In the Swing view, using a mouse of finger to click or tap the key in the view has the same effect as typing the key on the keyboard. Drawing the Swing view is very easy because we store the commands in a database. Making the Swing view react to mouse or screen taps is very easy because we’ve implemented the commands using Swing Actions.

This is an example of how we typically use a Swing Action. In a future posting I’d like to show how we make the live Swing view of the keys and the commands bound to each key.

Image credit: Action Heroes by Sabine Sauermaul

Leave a Reply

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

 characters available