Project

General

Profile

Patch #379 ยป KeyListenersPluginProvider_patch.txt

Patrick Talbot, 09/29/2011 07:19 PM

 
1
### Eclipse Workspace Patch 1.0
2
#P keyListeners
3
Index: src/net/stuff/servoy/plugins/listeners/KeyListenersPluginProvider.java
4
===================================================================
5
--- src/net/stuff/servoy/plugins/listeners/KeyListenersPluginProvider.java	(revision 9)
6
+++ src/net/stuff/servoy/plugins/listeners/KeyListenersPluginProvider.java	(working copy)
7
@@ -30,6 +30,8 @@
8
 import java.util.Iterator;
9
 import java.util.List;
10
 import java.util.Map;
11
+import java.util.Timer;
12
+import java.util.TimerTask;
13
 import java.util.concurrent.ConcurrentHashMap;
14
 
15
 import javax.swing.JComboBox;
16
@@ -67,7 +69,7 @@
17
 	private static final String KEY_PRESSED = "keyPressed";
18
 	private static final String KEY_TYPED = "keyTyped";
19
 	private static final String KEY_RELEASED = "keyReleased";
20
-	private static final String VERSION = "1.2";
21
+	private static final String VERSION = "1.3";
22
 	
23
 	private final Map functions = new ConcurrentHashMap();
24
 	private final KeyListenersPlugin plugin;
25
@@ -78,9 +80,11 @@
26
 	
27
 	protected class FunctionKeyListener extends KeyAdapter {
28
 		private Object func;
29
+		private Timer timer;
30
 		private final Component comp;
31
+		private int waitTime;
32
 		
33
-		public FunctionKeyListener(final Function f, final Component comp) {
34
+		public FunctionKeyListener(final Function f, final Component comp, int wait) {
35
 			if (functionDef != null) {
36
 				this.func = new MyFunctionDefinition(f);
37
 			} else {
38
@@ -92,6 +96,7 @@
39
 				}
40
 			}
41
 			this.comp = comp;
42
+			this.waitTime = wait;
43
 		}
44
 		
45
 		public Component getComponent() {
46
@@ -99,14 +104,46 @@
47
 		}
48
 		
49
 		protected void callBackJSFunction(final KeyEvent e) {
50
+			if (waitTime <= 0) {
51
+				executeJSFunction(e);
52
+			}
53
+			else {
54
+				// don't fire immediately, but wait a bit
55
+				if (timer != null) {
56
+					timer.cancel();
57
+				}
58
+				timer = new Timer();
59
+				timer.schedule(new MethodExecutor(e), waitTime);
60
+			}
61
+		}
62
+		
63
+		class MethodExecutor extends TimerTask {
64
+			private KeyEvent event;
65
+
66
+			public MethodExecutor(final KeyEvent e) {
67
+				this.event = e;
68
+			}
69
+
70
+			public void run() {
71
+				timer.cancel();
72
+				executeJSFunction(event);
73
+				event = null;
74
+			}
75
+		}
76
+		
77
+		private void executeJSFunction(final KeyEvent e) {
78
 			if (func != null) {
79
 				try {
80
+					if (comp instanceof com.servoy.j2db.smart.dataui.DataField) {
81
+						// in Servoy 5 the JFormattedTextField does not have the value yet when the method is fired
82
+						((com.servoy.j2db.smart.dataui.DataField)comp).commitEdit();
83
+					}
84
 					if (func instanceof MyFunctionDefinition) {
85
-						((MyFunctionDefinition)func).execute(plugin.getApplication(), new Object[] {e}, true);
86
+						((MyFunctionDefinition)func).execute(plugin.getApplication(), new Object[] {e, comp.getName(), new String(new char[]{e.getKeyChar()})}, true);
87
 					} else {
88
 						Class[] clazz = new Class[] {IClientPluginAccess.class, Object[].class, Boolean.class};
89
 						Method method = functionDef.getMethod("execute", clazz);
90
-						method.invoke(func, new Object[] {plugin.getApplication(), new Object[] {e}, Boolean.TRUE});
91
+						method.invoke(func, new Object[] {plugin.getApplication(), new Object[] {e, comp.getName(), new String(new char[]{e.getKeyChar()})}, Boolean.TRUE});
92
 					}
93
 				} catch (final Exception ex) {
94
 					Debug.error(ex);
95
@@ -117,8 +154,8 @@
96
 	
97
 	protected class KeyPressedListener extends FunctionKeyListener {
98
 		
99
-		public KeyPressedListener(final Function func, final Component comp) {
100
-			super(func, comp);
101
+		public KeyPressedListener(final Function func, final Component comp, final int wait) {
102
+			super(func, comp, wait);
103
 		}
104
 		
105
 		/* (non-Javadoc)
106
@@ -131,8 +168,8 @@
107
 	
108
 	protected class KeyReleasedListener extends FunctionKeyListener {
109
 		
110
-		public KeyReleasedListener(final Function func, final Component comp) {
111
-			super(func, comp);
112
+		public KeyReleasedListener(final Function func, final Component comp, final int wait) {
113
+			super(func, comp, wait);
114
 		}
115
 		
116
 		/* (non-Javadoc)
117
@@ -145,8 +182,8 @@
118
 	
119
 	protected class KeyTypedListener extends FunctionKeyListener {
120
 		
121
-		public KeyTypedListener(final Function func, final Component comp) {
122
-			super(func, comp);
123
+		public KeyTypedListener(final Function func, final Component comp, final int wait) {
124
+			super(func, comp, wait);
125
 		}
126
 		
127
 		/* (non-Javadoc)
128
@@ -158,15 +195,19 @@
129
 	}
130
 	
131
 	public void js_addKeyListener(final Object o, final Function f, final String eventName) {
132
+		js_addKeyListener(o, f, eventName, 0);
133
+	}
134
+	
135
+	public void js_addKeyListener(final Object o, final Function f, final String eventName, final int wait) {
136
 		if (!plugin.isWeb() && o != null && o instanceof Component &&  f != null && eventName != null) {
137
 			final Component component = getTextComponent((Component)o);
138
 			FunctionKeyListener kl = null; 
139
 			if (eventName.equalsIgnoreCase(KEY_PRESSED)) {
140
-				kl = new KeyPressedListener(f, component);
141
+				kl = new KeyPressedListener(f, component, wait);
142
 			} else if (eventName.equalsIgnoreCase(KEY_RELEASED)) {
143
-				kl = new KeyReleasedListener(f, component);
144
+				kl = new KeyReleasedListener(f, component, wait);
145
 			} else if (eventName.equalsIgnoreCase(KEY_TYPED)) {
146
-				kl = new KeyTypedListener(f, component);
147
+				kl = new KeyTypedListener(f, component, wait);
148
 			}
149
 			
150
 			if (kl != null) {
151
@@ -259,7 +300,7 @@
152
 		} else if (REMOVE_ALL_KEY_LISTENERS.equals(methodName)) {
153
 			return  new String[] {"element"};
154
 		} else if (ADD_KEY_LISTENER.equals(methodName)) {
155
-			return new String[] {"element", "function", "eventType"};
156
+			return new String[] {"element", "function", "eventType", "[waitTimeMs]"};
157
 		}
158
 		return null;
159
 	}
160
@@ -278,7 +319,17 @@
161
 	}
162
 
163
 	public String getToolTip(final String methodName) {
164
-		return getSample(methodName);
165
+		if (methodName.equals(ADD_KEY_LISTENER)) {
166
+			return "Adds a key listener to the given element. Whenever the given key event occurs (\""
167
+					+ KEY_PRESSED
168
+					+ "\", \""
169
+					+ KEY_RELEASED
170
+					+ "\" or \""
171
+					+ KEY_TYPED
172
+					+ "\") the method provided will be fired. The optional waitTimeMs sets a wait time where the user does not type before the method is fired.";
173
+		} else {
174
+			return getSample(methodName);
175
+		}
176
 	}
177
 
178
 	public boolean isDeprecated(String methodName) {
179

    
    (1-1/1)