KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > CaptureDefaultBindingsScriptListener


1 /*
2  * Copyright (c) 2004 Your Corporation. All Rights Reserved.
3  */

4 package org.wings.plaf.css;
5
6 import java.awt.event.InputEvent JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import javax.swing.KeyStroke JavaDoc;
10
11 import org.wings.SComponent;
12 import org.wings.script.JavaScriptListener;
13 import org.wings.script.ScriptListener;
14
15 /**
16  * @author hengels
17  * @version $Revision: 1.2 $
18  */

19 class CaptureDefaultBindingsScriptListener
20         extends JavaScriptListener
21 {
22     public CaptureDefaultBindingsScriptListener(String JavaDoc event, String JavaDoc code, String JavaDoc 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 JavaDoc string = (String JavaDoc)component.getSession().getProperty("wings.capture.default.bindings", "");
36
37         StringBuffer JavaDoc typed = new StringBuffer JavaDoc();
38         for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(string, ","); tokenizer.hasMoreTokens();) {
39             String JavaDoc token = tokenizer.nextToken();
40             KeyStroke JavaDoc 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 JavaDoc buffer, KeyStroke JavaDoc 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 JavaDoc buffer) {
71         buffer.append(" { event.preventDefault(); return true; }\n");
72     }
73 }
74
Popular Tags