]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyToCommand.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / KeyToCommand.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyToCommand.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyToCommand.java
new file mode 100644 (file)
index 0000000..5093f1e
--- /dev/null
@@ -0,0 +1,83 @@
+/*******************************************************************************\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