1 14 package org.wings.plaf.css; 15 16 import org.wings.SComponent; 17 import org.wings.script.JavaScriptListener; 18 import org.wings.script.ScriptListener; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import javax.swing.*; 23 import java.awt.event.InputEvent ; 24 import java.awt.event.KeyEvent ; 25 26 30 public class InputMapScriptListener 31 extends JavaScriptListener { 32 private final static transient Log log = LogFactory.getLog(InputMapScriptListener.class); 33 34 public InputMapScriptListener(String event, String code, String script) { 35 super(event, code, script); 36 } 37 38 public static void install(SComponent component) { 39 ScriptListener[] scriptListeners = component.getScriptListeners(); 40 41 for (int i = 0; i < scriptListeners.length; i++) { 42 ScriptListener scriptListener = scriptListeners[i]; 43 if (scriptListener instanceof InputMapScriptListener) 44 component.removeScriptListener(scriptListener); 45 } 46 47 InputMap inputMap = component.getInputMap(); 48 49 StringBuffer pressed = new StringBuffer (); 50 StringBuffer typed = new StringBuffer (); 51 StringBuffer released = new StringBuffer (); 52 KeyStroke[] keyStrokes = inputMap.keys(); 53 54 for (int i = 0; i < keyStrokes.length; i++) { 55 KeyStroke keyStroke = keyStrokes[i]; 56 Object binding = inputMap.get(keyStroke); 57 58 62 63 switch (keyStroke.getKeyEventType()) { 64 case KeyEvent.KEY_PRESSED: 65 writeMatch(pressed, keyStroke); 66 writeRequest(pressed, binding); 67 break; 68 case KeyEvent.KEY_TYPED: 69 writeMatch(typed, keyStroke); 70 writeRequest(typed, binding); 71 log.debug("typed binding = " + binding); 72 break; 73 case KeyEvent.KEY_RELEASED: 74 writeMatch(released, keyStroke); 75 writeRequest(released, binding); 76 log.debug("released binding = " + binding); 77 break; 78 } 79 } 80 81 if (pressed.length() > 0) 82 component.addScriptListener(new InputMapScriptListener("onkeydown", "return pressed_" + component.getName() + "(event)", 83 "function pressed_" + component.getName() + "(event) {\n " + 84 "event = getEvent(event);\n " + 85 pressed.toString() + " return true;\n}\n")); 86 if (typed.length() > 0) 87 component.addScriptListener(new InputMapScriptListener("onkeypress", "return typed_" + component.getName() + "(event)", 88 "function typed_" + component.getName() + "(event) {\n " + 89 "event = getEvent(event);\n " + 90 typed.toString() + " return true;\n}\n")); 91 if (released.length() > 0) 92 component.addScriptListener(new InputMapScriptListener("onkeyup", "return released_" + component.getName() + "(event)", 93 "function released_" + component.getName() + "(event) {\n" + 94 "event = getEvent(event);\n " + 95 released.toString() + " return true;\n}\n")); 96 } 97 98 private static void writeMatch(StringBuffer buffer, KeyStroke keyStroke) { 99 buffer.append("if (event.keyCode == " + keyStroke.getKeyCode()); 100 if ((keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0) 101 buffer.append(" && event.shiftKey"); 102 else 103 buffer.append(" && !event.shiftKey"); 104 if ((keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0) 105 buffer.append(" && event.ctrlKey"); 106 else 107 buffer.append(" && !event.ctrlKey"); 108 if ((keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0) 109 buffer.append(" && event.altKey"); 110 else 111 buffer.append(" && !event.altKey"); 112 buffer.append(")"); 113 } 114 115 private static void writeRequest(StringBuffer buffer, Object binding) { 116 buffer.append(" { sendEvent(event, \"").append(binding).append("\"); return false; }\n"); 117 } 118 } 119 | Popular Tags |