1 4 package org.wings.plaf.css; 5 6 import java.awt.event.InputEvent ; 7 import java.util.StringTokenizer ; 8 9 import javax.swing.KeyStroke ; 10 11 import org.wings.SComponent; 12 import org.wings.script.JavaScriptListener; 13 import org.wings.script.ScriptListener; 14 15 19 class CaptureDefaultBindingsScriptListener 20 extends JavaScriptListener 21 { 22 public CaptureDefaultBindingsScriptListener(String event, String code, String script) { 23 super(event, code, script); 24 } 25 26 static void install(SComponent component) { 27 ScriptListener[] scriptListeners = component.getScriptListeners(); 28 29 for (int i = 0; i < scriptListeners.length; i++) { 30 ScriptListener scriptListener = scriptListeners[i]; 31 if (scriptListener instanceof CaptureDefaultBindingsScriptListener) 32 component.removeScriptListener(scriptListener); 33 } 34 35 String string = (String )component.getSession().getProperty("wings.capture.default.bindings", ""); 36 37 StringBuffer typed = new StringBuffer (); 38 for (StringTokenizer tokenizer = new StringTokenizer (string, ","); tokenizer.hasMoreTokens();) { 39 String token = tokenizer.nextToken(); 40 KeyStroke keyStroke = KeyStroke.getKeyStroke(token); 41 42 writeMatch(typed, keyStroke); 43 writeCapture(typed); 44 } 45 46 if (typed.length() > 0) 47 component.addScriptListener(new CaptureDefaultBindingsScriptListener("onkeypress", "return typed_" + component.getName() + "(event)", 48 "function typed_" + component.getName() + "(event) {\n " + 49 "event = getEvent(event);\n " + 50 typed.toString() + " return true;\n}\n")); 51 } 52 53 private static void writeMatch(StringBuffer buffer, KeyStroke keyStroke) { 54 buffer.append("if (event.keyCode == " + keyStroke.getKeyCode()); 55 if ((keyStroke.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0) 56 buffer.append(" && event.shiftKey"); 57 else 58 buffer.append(" && !event.shiftKey"); 59 if ((keyStroke.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0) 60 buffer.append(" && event.ctrlKey"); 61 else 62 buffer.append(" && !event.ctrlKey"); 63 if ((keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0) 64 buffer.append(" && event.altKey"); 65 else 66 buffer.append(" && !event.altKey"); 67 buffer.append(")"); 68 } 69 70 private static void writeCapture(StringBuffer buffer) { 71 buffer.append(" { event.preventDefault(); return true; }\n"); 72 } 73 } 74 | Popular Tags |