01: package isql;
02:
03: import java.awt.*;
04:
05: import java.awt.event.*;
06: import javax.swing.*;
07: import javax.swing.text.*;
08:
09: public class KeyMap {
10: // the bindings would go into another class file which could be a
11: // dynamic name like jdbc driver name: emacs/vi bindings/mutt
12: static final JTextComponent.KeyBinding[] defaultBindings = {
13: new JTextComponent.KeyBinding(KeyStroke
14: .getKeyStroke("control Z"), "Ctrl-Z"),
15: new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(
16: KeyEvent.VK_C, InputEvent.CTRL_MASK),
17: DefaultEditorKit.beepAction) };
18:
19: public static void main(String args[]) {
20: JFrame frame = new JFrame("Keymap demo");
21: frame.addWindowListener(new WindowAdapter() {
22: public void windowClosing(WindowEvent e) {
23: System.exit(0);
24: }
25: });
26:
27: final JTextPane pane = new JTextPane();
28: pane.setPreferredSize(new Dimension(600, 400));
29: Keymap km = pane.getKeymap();
30: /*
31: KeyStroke ks = KeyStroke.getKeyStroke(
32: "control Z");
33: //KeyEvent.VK_Z, Event.CTRL_MASK);
34: */
35: // define all actions in one file -applications actions in one
36: // place? and editing actions separately or all together ?
37: // with a getActions
38: // Can the file itself be dynamic or pluggable ?
39: Action act = new TextAction("Ctrl-Z") {
40: public void actionPerformed(ActionEvent e) {
41: pane.replaceSelection("ZZZ");
42: }
43: };
44: //km.addActionForKeyStroke(ks, act);
45: Keymap k = pane.getKeymap();
46: // main program does the following, tying in the bindings with
47: // the actions.
48: JTextComponent.loadKeymap(k, defaultBindings, TextAction
49: .augmentList(pane.getActions(), new Action[] { act }));
50: // we would use class.getActions() above, rather than new
51: // Action[]
52:
53: JPanel panel = new JPanel();
54: panel.add("Center", pane);
55: frame.getContentPane().add(panel);
56: frame.pack();
57: frame.setVisible(true);
58: Action[] acts = pane.getActions();
59: for (int i = 0; i < acts.length; i++) {
60: System.out.println("Act:" + acts[i].getValue(Action.NAME));
61: }
62: }
63: }
|