-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g2d.participant;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.List;\r
-\r
-import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
-import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
-import org.simantics.scenegraph.g2d.events.KeyEvent;\r
-import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
-import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
-import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
-import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;\r
-import org.simantics.scenegraph.g2d.events.command.Commands;\r
-\r
-/**\r
- * Converts key presses to commands. \r
- * Do not use this in eclipse environment, use XXXTODO instead which\r
- * converts eclipse commands into g2d commands.\r
- * \r
- * CommandKeyBinding.DEFAULT_BINDINGS\r
- * \r
- * @See {@link CommandKeyBinding} command bindings\r
- * @See {@link Commands} commands\r
- * @author Toni Kalajainen\r
- */\r
-public class KeyToCommand extends AbstractCanvasParticipant {\r
-\r
- @Dependency KeyUtil keyUtil;\r
- List<CommandKeyBinding> binds = new ArrayList<CommandKeyBinding>();\r
- \r
- public KeyToCommand(CommandKeyBinding... binds) {\r
- assert(binds!=null);\r
- for (CommandKeyBinding b : binds)\r
- this.binds.add(b);\r
- }\r
- \r
- public KeyToCommand(Collection<CommandKeyBinding> binds) {\r
- assert(binds!=null);\r
- this.binds.addAll(binds);\r
- }\r
- \r
- public void addBinding(CommandKeyBinding binding) {\r
- if (!this.binds.contains(binding))\r
- this.binds.add(binding);\r
- }\r
-\r
- @EventHandler(priority = Integer.MIN_VALUE)\r
- public boolean handleKeyEvent(KeyEvent e) {\r
- //System.out.println("keytocommand " + e);\r
- boolean pressed = (e instanceof KeyPressedEvent);\r
- nextBinding: \r
- for (CommandKeyBinding ckb : binds) {\r
- int keyCode = ckb.keyCode[0];\r
- boolean _down = keyCode>0;\r
- if (_down != pressed) continue;\r
- if (!_down) keyCode = -keyCode; \r
- if (e.keyCode != keyCode) continue;\r
- for (int i=1; i<ckb.keyCode.length; i++) {\r
- int code = ckb.keyCode[i];\r
- boolean down = code>0;\r
- if (!down) code = -code; \r
- if (keyUtil.isKeyPressed(code) ^ down)\r
- continue nextBinding;\r
- }\r
- CommandEvent ce = new CommandEvent(getContext(), e.time, ckb.command);\r
- getContext().getEventQueue().queueFirst(ce);\r
- }\r
- return false;\r
- } \r
- \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.participant;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
+import org.simantics.scenegraph.g2d.events.KeyEvent;
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
+import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;
+import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;
+import org.simantics.scenegraph.g2d.events.command.Commands;
+
+/**
+ * Converts key presses to commands.
+ * Do not use this in eclipse environment, use XXXTODO instead which
+ * converts eclipse commands into g2d commands.
+ *
+ * CommandKeyBinding.DEFAULT_BINDINGS
+ *
+ * @See {@link CommandKeyBinding} command bindings
+ * @See {@link Commands} commands
+ * @author Toni Kalajainen
+ */
+public class KeyToCommand extends AbstractCanvasParticipant {
+
+ @Dependency KeyUtil keyUtil;
+ List<CommandKeyBinding> binds = new ArrayList<CommandKeyBinding>();
+
+ public KeyToCommand(CommandKeyBinding... binds) {
+ assert(binds!=null);
+ for (CommandKeyBinding b : binds)
+ this.binds.add(b);
+ }
+
+ public KeyToCommand(Collection<CommandKeyBinding> binds) {
+ assert(binds!=null);
+ this.binds.addAll(binds);
+ }
+
+ public void addBinding(CommandKeyBinding binding) {
+ if (!this.binds.contains(binding))
+ this.binds.add(binding);
+ }
+
+ @EventHandler(priority = Integer.MIN_VALUE)
+ public boolean handleKeyEvent(KeyEvent e) {
+ //System.out.println("keytocommand " + e);
+ boolean pressed = (e instanceof KeyPressedEvent);
+ nextBinding:
+ for (CommandKeyBinding ckb : binds) {
+ int keyCode = ckb.keyCode[0];
+ boolean _down = keyCode>0;
+ if (_down != pressed) continue;
+ if (!_down) keyCode = -keyCode;
+ if (e.keyCode != keyCode) continue;
+ for (int i=1; i<ckb.keyCode.length; i++) {
+ int code = ckb.keyCode[i];
+ boolean down = code>0;
+ if (!down) code = -code;
+ if (keyUtil.isKeyPressed(code) ^ down)
+ continue nextBinding;
+ }
+ CommandEvent ce = new CommandEvent(getContext(), e.time, ckb.command);
+ getContext().getEventQueue().queueFirst(ce);
+ }
+ return false;
+ }
+
+}