]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/command/CommandKeyBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / events / command / CommandKeyBinding.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.scenegraph.g2d.events.command;\r
13 \r
14 import java.awt.event.KeyEvent;\r
15 import java.util.Arrays;\r
16 \r
17 /**\r
18  * Describes a key binding of a command\r
19  * \r
20  * @See {@link Command} Command class\r
21  * @See {@link Commands} Default commands\r
22  * @See KeyToCommand Participant that forwards key presses to commands\r
23  * \r
24  * @author Toni Kalajainen\r
25  */\r
26 public class CommandKeyBinding {\r
27 \r
28         public static final CommandKeyBinding[] DEFAULT_BINDINGS = {\r
29                 new CommandKeyBinding(Commands.CANCEL, "Press Escape to cancel the current operation.", KeyEvent.VK_ESCAPE),\r
30                 new CommandKeyBinding(Commands.PAN_LEFT, null, KeyEvent.VK_LEFT),\r
31                 new CommandKeyBinding(Commands.PAN_RIGHT, null, KeyEvent.VK_RIGHT),\r
32                 new CommandKeyBinding(Commands.PAN_UP, null, KeyEvent.VK_UP),\r
33                 new CommandKeyBinding(Commands.PAN_DOWN, null, KeyEvent.VK_DOWN),\r
34 //              new CommandKeyBinding(Commands.ZOOM_TO_FIT, null, KeyEvent.VK_1),\r
35 //        new CommandKeyBinding(Commands.ZOOM_TO_FIT_HORIZ, null, KeyEvent.VK_2),\r
36 //        new CommandKeyBinding(Commands.ZOOM_TO_FIT_VERT, null, KeyEvent.VK_3),\r
37 //        new CommandKeyBinding(Commands.AUTOSCALE, null, KeyEvent.VK_4),\r
38         new CommandKeyBinding(Commands.ZOOM_TO_SELECTION, null, KeyEvent.VK_2),\r
39         new CommandKeyBinding(Commands.ZOOM_TO_PAGE, null, KeyEvent.VK_3),\r
40                 new CommandKeyBinding(Commands.ZOOM_IN, null, KeyEvent.VK_PLUS),\r
41                 new CommandKeyBinding(Commands.ZOOM_IN, null, KeyEvent.VK_ADD),\r
42                 new CommandKeyBinding(Commands.ZOOM_OUT, null, KeyEvent.VK_MINUS),\r
43                 new CommandKeyBinding(Commands.ZOOM_OUT, null, KeyEvent.VK_SUBTRACT),\r
44                 new CommandKeyBinding(Commands.ZOOM_TO_AREA, null, KeyEvent.VK_F3),\r
45                 new CommandKeyBinding(Commands.REFRESH, null, KeyEvent.VK_F5),\r
46                 new CommandKeyBinding(Commands.RENAME, null, KeyEvent.VK_F2),\r
47                 new CommandKeyBinding(Commands.DELETE, null, KeyEvent.VK_DELETE),\r
48                 new CommandKeyBinding(Commands.SELECT_ALL, null, -KeyEvent.VK_A, KeyEvent.VK_CONTROL),\r
49                 new CommandKeyBinding(Commands.SELECT_ALL, null, KeyEvent.VK_A, KeyEvent.VK_CONTROL),\r
50                 new CommandKeyBinding(Commands.CUT, null, KeyEvent.VK_X, KeyEvent.VK_CONTROL),\r
51                 new CommandKeyBinding(Commands.COPY, null, KeyEvent.VK_C, KeyEvent.VK_CONTROL),\r
52                 new CommandKeyBinding(Commands.PASTE, null, KeyEvent.VK_V, KeyEvent.VK_CONTROL),\r
53                 new CommandKeyBinding(Commands.PRINT, null, KeyEvent.VK_P, KeyEvent.VK_CONTROL),\r
54                 new CommandKeyBinding(Commands.PDFPRINT, null, KeyEvent.VK_P, KeyEvent.VK_ALT),\r
55                 new CommandKeyBinding(Commands.BRING_UP, null, KeyEvent.VK_PAGE_UP),\r
56                 new CommandKeyBinding(Commands.SEND_DOWN, null, KeyEvent.VK_PAGE_DOWN),         \r
57                 new CommandKeyBinding(Commands.BRING_TO_TOP, null, KeyEvent.VK_HOME),\r
58                 new CommandKeyBinding(Commands.SEND_TO_BOTTOM, null, KeyEvent.VK_END),\r
59                 new CommandKeyBinding(Commands.ROTATE_ELEMENT_CW, null, KeyEvent.VK_PERIOD),\r
60                 new CommandKeyBinding(Commands.ROTATE_ELEMENT_CCW, null, KeyEvent.VK_COMMA),\r
61                 new CommandKeyBinding(Commands.FLIP_ELEMENT_HORIZONTAL, null, KeyEvent.VK_F, -KeyEvent.VK_CONTROL),\r
62                 new CommandKeyBinding(Commands.FLIP_ELEMENT_VERTICAL, null, KeyEvent.VK_V, -KeyEvent.VK_CONTROL),\r
63 //              new CommandKeyBinding(Commands.ROTATE_CANVAS_CW_GRAB, null, KeyEvent.VK_PERIOD),\r
64 //              new CommandKeyBinding(Commands.ROTATE_CANVAS_CW_RELEASE, null, -KeyEvent.VK_PERIOD),\r
65 //              new CommandKeyBinding(Commands.ROTATE_CANVAS_CCW_GRAB, null, KeyEvent.VK_COMMA),\r
66 //              new CommandKeyBinding(Commands.ROTATE_CANVAS_CCW_RELEASE, null, -KeyEvent.VK_COMMA),\r
67                 new CommandKeyBinding(Commands.GRID_TOGGLE, null, KeyEvent.VK_G),\r
68                 new CommandKeyBinding(Commands.RULER_TOGGLE, null, KeyEvent.VK_R),\r
69                 new CommandKeyBinding(Commands.FOCUS_TOOLTIP, null, KeyEvent.VK_F9)\r
70                 };\r
71         \r
72         /** \r
73          * Required keys including modifiers, virtual codes.\r
74          * The code at first index is the trigger code, and other codes are \r
75          * modifiers (ctrl, shift, etc).\r
76          * Negative code is release (index=0) or modifier is up (index>0) \r
77          */\r
78         public final int [] keyCode;\r
79         /** Command to trigger */\r
80         public final Command command;\r
81         /** Guide shown in UI about how to use the binding */\r
82         public final String guide;\r
83         \r
84         private int hash;\r
85         \r
86         public CommandKeyBinding(Command command, String guide, int...keyCode) {\r
87                 assert(keyCode.length>0);\r
88                 this.command = command;\r
89                 this.keyCode = keyCode;\r
90                 this.guide = guide;\r
91                 this.hash = Arrays.hashCode(keyCode) + 13*command.hashCode();\r
92         }\r
93                 \r
94         \r
95         @Override\r
96         public int hashCode() {\r
97                 return hash;\r
98         }\r
99         \r
100         @Override\r
101         public boolean equals(Object obj) {\r
102                 if (!(obj instanceof CommandKeyBinding)) return false;\r
103                 CommandKeyBinding other = (CommandKeyBinding) obj;              \r
104                 return other.command.equals(command) && Arrays.equals(keyCode, other.keyCode);\r
105         }\r
106         \r
107 }\r