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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.participant;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.List;
\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
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
32 * CommandKeyBinding.DEFAULT_BINDINGS
\r
34 * @See {@link CommandKeyBinding} command bindings
\r
35 * @See {@link Commands} commands
\r
36 * @author Toni Kalajainen
\r
38 public class KeyToCommand extends AbstractCanvasParticipant {
\r
40 @Dependency KeyUtil keyUtil;
\r
41 List<CommandKeyBinding> binds = new ArrayList<CommandKeyBinding>();
\r
43 public KeyToCommand(CommandKeyBinding... binds) {
\r
44 assert(binds!=null);
\r
45 for (CommandKeyBinding b : binds)
\r
49 public KeyToCommand(Collection<CommandKeyBinding> binds) {
\r
50 assert(binds!=null);
\r
51 this.binds.addAll(binds);
\r
54 public void addBinding(CommandKeyBinding binding) {
\r
55 if (!this.binds.contains(binding))
\r
56 this.binds.add(binding);
\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
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
77 CommandEvent ce = new CommandEvent(getContext(), e.time, ckb.command);
\r
78 getContext().getEventQueue().queueFirst(ce);
\r