In a previous post we saw one way to store Actions and their associated key bindings in a database. In this post we’ll start looking at how to use those Actions in Java.

Action

Action

First I create an ActionMap in Java with keys (labels) that match the labels in the database. The ActionMap will associate those keys/labels with Java methods.

[java]
/**
* Initialize action map with standard actions
*/
private void initActionMap(javax.swing.ActionMap am) {
am.put("TurnRed", new com.sscorp.core.actions.turnRed(getOrderForm()));
am.put("TurnGreen", new com.sscorp.core.actions.turnGreen(getOrderForm()));
am.put("TurnBlue", new com.sscorp.core.actions.turnBlue(getOrderForm()));
}
[/java]

The I modify an InputMap to associate key strokes with the Actions defined above.

[java]
/**
* Initialize input map from database
*/
private void initInputMap(javax.swing.InputMap im, javax.swing.ActionMap am) {
{
final java.sql.Connection conn = getConn(); // Retrieve a previously opened connection
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
keyBindingLabels.clear();
try {
stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT KeyStroke, MimeType, Action, KeyLabel FROM OrderEntryKeyBindings WHERE KeySet=’Default’ ORDER BY KeyStroke, MimeType");
while (rs.next()) {
String ksDesc = rs.getString("KeyStroke").trim();
javax.swing.KeyStroke ks = javax.swing.KeyStroke.getKeyStroke(ksDesc);
mimeType = rs.getString("MimeType").trim();
String action = rs.getString("Action").trim();
String keyLabel = rs.getString("KeyLabel").trim();
im.put(ks, action);
keyBindingLabels.put(ksDesc, keyLabel);
}
rs.close();
}
catch (java.sql.SQLException e) {
Util.logException(e, "SQL exception encountered when configuring keyboard.");
}
catch (javax.activation.MimeTypeParseException e) {
Util.logException(e, "Mime type exception encountered when configuring keyboard: Mime Type is " + mimeType);
}
finally {
if (rs != null)
try {rs.close();} catch (java.sql.SQLException e) {}
if (stmt != null)
try {stmt.close();} catch (java.sql.SQLException e) {}
}
}
}
[/java]

And if you recall, the database table that we’re querying above looked like this:

Table Contents

Table Contents

The InputMap and ActionMap that are passed to the two methods above come from the JComponent onto which the user will type the order.

[java]
public class OrderForm extends javax.swing.JComponent {
// …
public OrderForm() {
super();
initActionMap(this.getActionMap());
initInputMap(this.getInputMap(), this.getActionMap());
}
// …
}
[/java]

Similarly, I set up other maps for other JComponent-based forms within the application. I just query the database for a different value in the KeySet column, and modify the ActionMap and InputMap for the appropriate form.

So now we’ve seen the setup and initialization. In a future post we’ll look more closely at the Action objects.

Image credit: Action Heroes by Sabine Sauermaul

Leave a Reply

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

 characters available