]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/adapter/AWTKeyEventAdapter.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / events / adapter / AWTKeyEventAdapter.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 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.scenegraph.g2d.events.adapter;\r
17 \r
18 import java.awt.event.KeyEvent;\r
19 import java.awt.event.KeyListener;\r
20 \r
21 import org.simantics.scenegraph.g2d.events.EventDebugPolicy;\r
22 import org.simantics.scenegraph.g2d.events.IEventHandler;\r
23 import org.simantics.scenegraph.g2d.events.IEventQueue;\r
24 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
25 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;\r
26 \r
27 \r
28 public class AWTKeyEventAdapter extends AbstractEventAdapter implements KeyListener {\r
29 \r
30     private final boolean DEBUG                  = EventDebugPolicy.AWT_KEY_EVENT_ADAPTION;\r
31 \r
32     private final boolean IGNORE_CONSUMED_EVENTS = false;\r
33 \r
34     public AWTKeyEventAdapter(Object sender, IEventHandler delegator) {\r
35         super(sender, delegator);\r
36     }\r
37 \r
38     public AWTKeyEventAdapter(Object sender, IEventQueue queue) {\r
39         super(sender, queue);\r
40     }\r
41 \r
42     @Override\r
43     public void keyPressed(KeyEvent e) {\r
44         if (IGNORE_CONSUMED_EVENTS) {\r
45             if (e.isConsumed()) {\r
46                 if (DEBUG)\r
47                     System.out.println("AWT key pressed but already consumed: " + e);\r
48                 return;\r
49             }\r
50         }\r
51         if (DEBUG)\r
52             System.out.println("AWT key pressed: " + e);\r
53         handleEvent(\r
54                 new KeyPressedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));\r
55     }\r
56 \r
57     @Override\r
58     public void keyReleased(KeyEvent e) {\r
59         if (IGNORE_CONSUMED_EVENTS) {\r
60             if (e.isConsumed()) {\r
61                 if (DEBUG)\r
62                     System.out.println("AWT key released but already consumed: " + e);\r
63                 return;\r
64             }\r
65         }\r
66         if (DEBUG)\r
67             System.out.println("AWT key released: " + e);\r
68         handleEvent(\r
69                 new KeyReleasedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));\r
70     }\r
71 \r
72     @Override\r
73     public void keyTyped(KeyEvent e) {\r
74         if (IGNORE_CONSUMED_EVENTS) {\r
75             if (e.isConsumed()) {\r
76                 if (DEBUG)\r
77                     System.out.println("AWT key typed but already consumed: " + e);\r
78                 return;\r
79             }\r
80         }\r
81         System.out.println("AWT key typed: " + e);\r
82         handleEvent(\r
83                 new KeyPressedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));\r
84     }\r
85 \r
86 }\r