001: /*
002: * $Author: rahul_kumar $
003: * $Id: Bindings.java,v 1.3 2004/01/01 06:50:15 rahul_kumar Exp $
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package isql;
013:
014: import java.util.*;
015: import javax.swing.KeyStroke;
016: import javax.swing.JTable;
017: import util.*;
018:
019: /**
020: * Class documentation.
021: * @author rahul kumar <rahul_kumar@yahoo.com>
022: * @see XXX
023: */
024:
025: public class Bindings {
026: Map htBindings;
027:
028: /** ctor/constructor.
029: */
030: public Bindings() {
031: Map liBindInput = null;
032: Map liBindTable = null;
033: Map liBindFrame = null;
034: htBindings = new HashMap();
035: liBindInput = new HashMap();
036: liBindTable = new HashMap();
037: liBindFrame = new HashMap();
038: htBindings.put("input", liBindInput);
039: htBindings.put("table", liBindTable);
040: htBindings.put("frame", liBindFrame);
041: }
042:
043: /** bind a key to an action for a scope.
044: * scope may be frame or table or a regex.
045: * key is as per Keystroke.getKeyStroke(String).
046: * action is the action name in the *Actions.java files.
047: */
048: public void addBinding(String scope, String userkey, String action)
049: throws InvalidBindingException {
050: String key = userkey.replace('-', ' ');
051: // RK added on 20031226 14:23:34
052: // validate the keystroke here
053: // itself
054: if (KeyStroke.getKeyStroke(key) == null)
055: throw new InvalidBindingException(
056: "Invalid key specified for binding:[" + userkey
057: + "]");
058:
059: if (SQLForm.DEBUG)
060: System.out.println("Binding:" + key + "," + action);
061: if (PerlWrapper.isMatching(scope, "frame")) {
062: if (SQLForm.DEBUG)
063: System.out.println("Adding to frame:" + key);
064: Map liBindFrame = (Map) htBindings.get("frame");
065: //liBindFrame.put(key, action);
066: liBindFrame.put(action, key);
067: }
068: if (PerlWrapper.isMatching(scope, "table")) {
069: if (SQLForm.DEBUG)
070: System.out.println("Adding to table:" + key);
071: Map liBindTable = (Map) htBindings.get("table");
072: //liBindTable.put(key, action);
073: liBindTable.put(action, key);
074: }
075: }
076:
077: /** convenience method to return a map of bindings for the
078: * inputarea.
079: */
080: public Map getBindingsForInputArea() {
081: if (htBindings == null)
082: return null;
083: return (Map) htBindings.get("input");
084: }
085:
086: /** convenience method to return a map of bindings for the
087: * frames tab.
088: */
089: public Map getBindingsForFrame() {
090: if (htBindings == null)
091: return null;
092: return (Map) htBindings.get("frame");
093: }
094:
095: /** convenience method to return a map of bindings for the
096: * table tab.
097: */
098: public Map getBindingsForTable() {
099: if (htBindings == null)
100: return null;
101: return (Map) htBindings.get("table");
102: }
103:
104: public void bindToTable(Map bindings, JTable jt) {
105: if (bindings != null) {
106: Iterator it = bindings.keySet().iterator();
107: String keystroke;
108: String action;
109: while (it.hasNext()) {
110: action = (String) it.next();
111: keystroke = (String) bindings.get(action);
112: //System.out.println( "IWP got:"+keystroke+":"+action);
113: KeyStroke ks = KeyStroke.getKeyStroke(keystroke);
114: if (ks == null) {
115: System.err
116: .println("ERROR: Could not understand key binding for key:"
117: + keystroke);
118: System.err
119: .println(" : Please see documentation or doc of javax.swing.KeyStroke getKeyStroke(String). Use a hyphen in place of space");
120: System.err
121: .println(" e.g.: control-S , alt-shift-X , typed-a , INSERT , DELETE ");
122: continue;
123: }
124: jt.getInputMap().put(ks, action);
125: }
126: } // bindings
127: } // bindToTable
128:
129: } // class
|