diff options
author | Matti Tahvonen <matti.tahvonen@itmill.com> | 2007-03-28 15:50:42 +0000 |
---|---|---|
committer | Matti Tahvonen <matti.tahvonen@itmill.com> | 2007-03-28 15:50:42 +0000 |
commit | aa893c5486ef5deb101a010c22b7a6b3d1066b69 (patch) | |
tree | 8a4a2910f31c414290e2f995664f5565c81cbc5a /src/com/itmill/toolkit/ui/Button.java | |
parent | 6cb3cb7068fa20b6b515ee472cceb3e49bed44c1 (diff) | |
download | vaadin-framework-aa893c5486ef5deb101a010c22b7a6b3d1066b69.tar.gz vaadin-framework-aa893c5486ef5deb101a010c22b7a6b3d1066b69.zip |
Initial commit to suppor shortcut keys see #231
svn changeset:1040/svn branch:trunk
Diffstat (limited to 'src/com/itmill/toolkit/ui/Button.java')
-rw-r--r-- | src/com/itmill/toolkit/ui/Button.java | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/src/com/itmill/toolkit/ui/Button.java b/src/com/itmill/toolkit/ui/Button.java index 20bcd4fc93..f649eb8e71 100644 --- a/src/com/itmill/toolkit/ui/Button.java +++ b/src/com/itmill/toolkit/ui/Button.java @@ -28,12 +28,22 @@ package com.itmill.toolkit.ui; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; import java.lang.reflect.Method; import com.itmill.toolkit.data.Property; +import com.itmill.toolkit.terminal.KeyMapper; import com.itmill.toolkit.terminal.PaintException; import com.itmill.toolkit.terminal.PaintTarget; +import com.itmill.toolkit.event.Action; +import com.itmill.toolkit.event.ShortcutAction; +import com.itmill.toolkit.event.Action.Handler; + /** A generic button component. * @@ -41,11 +51,18 @@ import com.itmill.toolkit.terminal.PaintTarget; * @version @VERSION@ * @since 3.0 */ -public class Button extends AbstractField { +public class Button extends AbstractField implements Action.Container { /* Private members ************************************************* */ boolean switchMode = false; + + /** List of action handlers */ + private LinkedList actionHandlers = null; + + /** Action mapper */ + private KeyMapper actionMapper = null; + /** Creates a new push button. * @@ -133,6 +150,49 @@ public class Button extends AbstractField { state = false; } target.addVariable(this, "state", state); + + // Actions + if (actionHandlers != null) { + Set actionSet = new LinkedHashSet(); + for (Iterator ahi = actionHandlers.iterator(); + ahi.hasNext(); + ) { + Action[] aa = + ((Action.Handler) ahi.next()).getActions( + this, + this); + if (aa != null) + for (int ai = 0; ai < aa.length; ai++) { + actionSet.add(aa[ai]); + } + } + + target.startTag("actions"); + target.addVariable(this, "action", ""); + for (Iterator i = actionSet.iterator(); i.hasNext();) { + try { + ShortcutAction a = (ShortcutAction) i.next(); + target.startTag("action"); + if (a.getCaption() != null) + target.addAttribute("caption", a.getCaption()); + if (a.getIcon() != null) + target.addAttribute("icon", a.getIcon()); + target.addAttribute("key", actionMapper.key(a)); + target.addAttribute("keycode", a.getKeyCode()); + if(a.getModifiers() != null) { + int[] modifiers = a.getModifiers(); + target.addAttribute("modifiers", modifiers.length); + for(int j = 0; j < modifiers.length; j++) { + target.addAttribute("modifier" + j, modifiers[j]); + } + } + target.endTag("action"); + } catch( Exception e ){ + // ignore non-shorcut actions for button + } + } + target.endTag("actions"); + } } /** Invoked when the value of a variable has changed. Button @@ -166,6 +226,26 @@ public class Button extends AbstractField { setValue(new Boolean(false)); } } + // Actions + // TODO this is pretty much copy-pasted from tree, may be simplified + if (variables.containsKey("action")) { + + StringTokenizer st = + new StringTokenizer((String) variables.get("action"), ","); + if (st.countTokens() == 2) { + Action action = (Action) actionMapper.get(st.nextToken()); + if (action != null + && actionHandlers != null) + for (Iterator i = actionHandlers.iterator(); + i.hasNext(); + ) + ((Action.Handler) i.next()).handleAction( + action, + this, + this); + } + } + } /** @@ -278,4 +358,41 @@ public class Button extends AbstractField { protected void fireClick() { fireEvent(new Button.ClickEvent(this)); } + + /** Adds an action handler. + * @see com.itmill.toolkit.event.Action.Container#addActionHandler(Action.Handler) + */ + public void addActionHandler(Action.Handler actionHandler) { + + if (actionHandler != null) { + + if (actionHandlers == null) { + actionHandlers = new LinkedList(); + actionMapper = new KeyMapper(); + } + + if(!actionHandlers.contains(actionHandler)){ + actionHandlers.add(actionHandler); + requestRepaint(); + } + } + } + + /** Removes an action handler. + * @see com.itmill.toolkit.event.Action.Container#removeActionHandler(Action.Handler) + */ + public void removeActionHandler(Action.Handler actionHandler) { + + if (actionHandlers != null && actionHandlers.contains(actionHandler)) { + + actionHandlers.remove(actionHandler); + + if (actionHandlers.isEmpty()) { + actionHandlers = null; + actionMapper = null; + } + + requestRepaint(); + } + } } |