KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: InputMapScriptListener.java,v 1.10 2005/03/03 13:18:25 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25
26 /**
27  * @author hengels
28  * @version $Revision: 1.10 $
29  */

30 public class InputMapScriptListener
31         extends JavaScriptListener {
32     private final static transient Log log = LogFactory.getLog(InputMapScriptListener.class);
33
34     public InputMapScriptListener(String JavaDoc event, String JavaDoc code, String JavaDoc 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 JavaDoc pressed = new StringBuffer JavaDoc();
50         StringBuffer JavaDoc typed = new StringBuffer JavaDoc();
51         StringBuffer JavaDoc released = new StringBuffer JavaDoc();
52         KeyStroke[] keyStrokes = inputMap.keys();
53
54         for (int i = 0; i < keyStrokes.length; i++) {
55             KeyStroke keyStroke = keyStrokes[i];
56             Object JavaDoc binding = inputMap.get(keyStroke);
57
58             /*
59             writeMatch(typed, keyStroke);
60             writeRequest(typed, binding);
61             */

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 JavaDoc 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 JavaDoc buffer, Object JavaDoc binding) {
116         buffer.append(" { sendEvent(event, \"").append(binding).append("\"); return false; }\n");
117     }
118 }
119
Popular Tags