### Eclipse Workspace Patch 1.0 #P keyListeners Index: src/net/stuff/servoy/plugins/listeners/KeyListenersPluginProvider.java =================================================================== --- src/net/stuff/servoy/plugins/listeners/KeyListenersPluginProvider.java (revision 9) +++ src/net/stuff/servoy/plugins/listeners/KeyListenersPluginProvider.java (working copy) @@ -30,6 +30,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import javax.swing.JComboBox; @@ -67,7 +69,7 @@ private static final String KEY_PRESSED = "keyPressed"; private static final String KEY_TYPED = "keyTyped"; private static final String KEY_RELEASED = "keyReleased"; - private static final String VERSION = "1.2"; + private static final String VERSION = "1.3"; private final Map functions = new ConcurrentHashMap(); private final KeyListenersPlugin plugin; @@ -78,9 +80,11 @@ protected class FunctionKeyListener extends KeyAdapter { private Object func; + private Timer timer; private final Component comp; + private int waitTime; - public FunctionKeyListener(final Function f, final Component comp) { + public FunctionKeyListener(final Function f, final Component comp, int wait) { if (functionDef != null) { this.func = new MyFunctionDefinition(f); } else { @@ -92,6 +96,7 @@ } } this.comp = comp; + this.waitTime = wait; } public Component getComponent() { @@ -99,14 +104,46 @@ } protected void callBackJSFunction(final KeyEvent e) { + if (waitTime <= 0) { + executeJSFunction(e); + } + else { + // don't fire immediately, but wait a bit + if (timer != null) { + timer.cancel(); + } + timer = new Timer(); + timer.schedule(new MethodExecutor(e), waitTime); + } + } + + class MethodExecutor extends TimerTask { + private KeyEvent event; + + public MethodExecutor(final KeyEvent e) { + this.event = e; + } + + public void run() { + timer.cancel(); + executeJSFunction(event); + event = null; + } + } + + private void executeJSFunction(final KeyEvent e) { if (func != null) { try { + if (comp instanceof com.servoy.j2db.smart.dataui.DataField) { + // in Servoy 5 the JFormattedTextField does not have the value yet when the method is fired + ((com.servoy.j2db.smart.dataui.DataField)comp).commitEdit(); + } if (func instanceof MyFunctionDefinition) { - ((MyFunctionDefinition)func).execute(plugin.getApplication(), new Object[] {e}, true); + ((MyFunctionDefinition)func).execute(plugin.getApplication(), new Object[] {e, comp.getName(), new String(new char[]{e.getKeyChar()})}, true); } else { Class[] clazz = new Class[] {IClientPluginAccess.class, Object[].class, Boolean.class}; Method method = functionDef.getMethod("execute", clazz); - method.invoke(func, new Object[] {plugin.getApplication(), new Object[] {e}, Boolean.TRUE}); + method.invoke(func, new Object[] {plugin.getApplication(), new Object[] {e, comp.getName(), new String(new char[]{e.getKeyChar()})}, Boolean.TRUE}); } } catch (final Exception ex) { Debug.error(ex); @@ -117,8 +154,8 @@ protected class KeyPressedListener extends FunctionKeyListener { - public KeyPressedListener(final Function func, final Component comp) { - super(func, comp); + public KeyPressedListener(final Function func, final Component comp, final int wait) { + super(func, comp, wait); } /* (non-Javadoc) @@ -131,8 +168,8 @@ protected class KeyReleasedListener extends FunctionKeyListener { - public KeyReleasedListener(final Function func, final Component comp) { - super(func, comp); + public KeyReleasedListener(final Function func, final Component comp, final int wait) { + super(func, comp, wait); } /* (non-Javadoc) @@ -145,8 +182,8 @@ protected class KeyTypedListener extends FunctionKeyListener { - public KeyTypedListener(final Function func, final Component comp) { - super(func, comp); + public KeyTypedListener(final Function func, final Component comp, final int wait) { + super(func, comp, wait); } /* (non-Javadoc) @@ -158,15 +195,19 @@ } public void js_addKeyListener(final Object o, final Function f, final String eventName) { + js_addKeyListener(o, f, eventName, 0); + } + + public void js_addKeyListener(final Object o, final Function f, final String eventName, final int wait) { if (!plugin.isWeb() && o != null && o instanceof Component && f != null && eventName != null) { final Component component = getTextComponent((Component)o); FunctionKeyListener kl = null; if (eventName.equalsIgnoreCase(KEY_PRESSED)) { - kl = new KeyPressedListener(f, component); + kl = new KeyPressedListener(f, component, wait); } else if (eventName.equalsIgnoreCase(KEY_RELEASED)) { - kl = new KeyReleasedListener(f, component); + kl = new KeyReleasedListener(f, component, wait); } else if (eventName.equalsIgnoreCase(KEY_TYPED)) { - kl = new KeyTypedListener(f, component); + kl = new KeyTypedListener(f, component, wait); } if (kl != null) { @@ -259,7 +300,7 @@ } else if (REMOVE_ALL_KEY_LISTENERS.equals(methodName)) { return new String[] {"element"}; } else if (ADD_KEY_LISTENER.equals(methodName)) { - return new String[] {"element", "function", "eventType"}; + return new String[] {"element", "function", "eventType", "[waitTimeMs]"}; } return null; } @@ -278,7 +319,17 @@ } public String getToolTip(final String methodName) { - return getSample(methodName); + if (methodName.equals(ADD_KEY_LISTENER)) { + return "Adds a key listener to the given element. Whenever the given key event occurs (\"" + + KEY_PRESSED + + "\", \"" + + KEY_RELEASED + + "\" or \"" + + KEY_TYPED + + "\") the method provided will be fired. The optional waitTimeMs sets a wait time where the user does not type before the method is fired."; + } else { + return getSample(methodName); + } } public boolean isDeprecated(String methodName) {