KeyMap.java :  » Database-Client » SQLMinus » isql » Java Open Source

Java Open Source » Database Client » SQLMinus 
SQLMinus » isql » KeyMap.java
package isql;
import java.awt.*;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*; 

public class KeyMap {
    // the bindings would go into another class file which could be a
    // dynamic name like jdbc driver name: emacs/vi bindings/mutt
    static final JTextComponent.KeyBinding[] defaultBindings = {
        new JTextComponent.KeyBinding(
                KeyStroke.getKeyStroke("control Z"),
                "Ctrl-Z"),
        new JTextComponent.KeyBinding(
                KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
                DefaultEditorKit.beepAction)
    };
    public static void main(String args[]) {
        JFrame frame = new JFrame("Keymap demo");
        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                System.exit(0);
                }
                });     

        final JTextPane pane = new JTextPane();
        pane.setPreferredSize(new Dimension(600, 400));     
        Keymap km = pane.getKeymap();
        /*
           KeyStroke ks = KeyStroke.getKeyStroke(
           "control Z");
        //KeyEvent.VK_Z, Event.CTRL_MASK);
         */
        // define all actions in one file -applications actions in one
        // place? and editing actions separately or all together ?
        // with a getActions
        // Can the file itself be dynamic or pluggable ?
        Action act = new TextAction("Ctrl-Z") {
            public void actionPerformed(ActionEvent e) {
                pane.replaceSelection("ZZZ");
            }
        };
        //km.addActionForKeyStroke(ks, act);     
        Keymap k = pane.getKeymap();
        // main program does the following, tying in the bindings with
        // the actions.
        JTextComponent.loadKeymap(k, defaultBindings, TextAction.augmentList(pane.getActions(), new Action[]{act} ));
        // we would use class.getActions() above, rather than new
        // Action[]

        JPanel panel = new JPanel();
        panel.add("Center", pane);     frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
        Action[] acts = pane.getActions();
        for( int i = 0; i < acts.length; i++ ){ 
            System.out.println( "Act:"+acts[i].getValue(Action.NAME));
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.