]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.participant;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
19 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
20 import org.simantics.scenegraph.g2d.events.KeyEvent;\r
21 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
22 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
23 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
24 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;\r
25 import org.simantics.scenegraph.g2d.events.command.Commands;\r
26 \r
27 /**\r
28  * Converts key presses to commands. \r
29  * Do not use this in eclipse environment, use XXXTODO instead which\r
30  * converts eclipse commands into g2d commands.\r
31  * \r
32  * CommandKeyBinding.DEFAULT_BINDINGS\r
33  * \r
34  * @See {@link CommandKeyBinding} command bindings\r
35  * @See {@link Commands} commands\r
36  * @author Toni Kalajainen\r
37  */\r
38 public class KeyToCommand extends AbstractCanvasParticipant {\r
39 \r
40         @Dependency KeyUtil keyUtil;\r
41         List<CommandKeyBinding> binds = new ArrayList<CommandKeyBinding>();\r
42         \r
43         public KeyToCommand(CommandKeyBinding... binds) {\r
44                 assert(binds!=null);\r
45                 for (CommandKeyBinding b : binds)\r
46                         this.binds.add(b);\r
47         }\r
48         \r
49         public KeyToCommand(Collection<CommandKeyBinding> binds) {\r
50                 assert(binds!=null);\r
51                 this.binds.addAll(binds);\r
52         }\r
53         \r
54         public void addBinding(CommandKeyBinding binding) {\r
55                 if (!this.binds.contains(binding))\r
56                         this.binds.add(binding);\r
57         }\r
58 \r
59         @EventHandler(priority = Integer.MIN_VALUE)\r
60         public boolean handleKeyEvent(KeyEvent e) {\r
61             //System.out.println("keytocommand " + e);\r
62                 boolean pressed = (e instanceof KeyPressedEvent);\r
63                 nextBinding:            \r
64                 for (CommandKeyBinding ckb : binds) {\r
65                         int keyCode = ckb.keyCode[0];\r
66                         boolean _down = keyCode>0;\r
67                         if (_down != pressed) continue;\r
68                         if (!_down) keyCode = -keyCode;                 \r
69                         if (e.keyCode != keyCode) continue;\r
70                         for (int i=1; i<ckb.keyCode.length; i++) {\r
71                                 int code = ckb.keyCode[i];\r
72                                 boolean down = code>0;\r
73                                 if (!down) code = -code;                                \r
74                                 if (keyUtil.isKeyPressed(code) ^ down)\r
75                                         continue nextBinding;\r
76                         }\r
77                         CommandEvent ce = new CommandEvent(getContext(), e.time, ckb.command);\r
78                         getContext().getEventQueue().queueFirst(ce);\r
79                 }\r
80                 return false;\r
81         }       \r
82         \r
83 }\r