1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.events.command;
14 import java.awt.event.KeyEvent;
15 import java.util.Arrays;
18 * Describes a key binding of a command
20 * @See {@link Command} Command class
21 * @See {@link Commands} Default commands
22 * @See KeyToCommand Participant that forwards key presses to commands
24 * @author Toni Kalajainen
26 public class CommandKeyBinding {
28 public static final CommandKeyBinding[] DEFAULT_BINDINGS = {
29 new CommandKeyBinding(Commands.CANCEL, "Press Escape to cancel the current operation.", KeyEvent.VK_ESCAPE),
30 new CommandKeyBinding(Commands.PAN_LEFT, null, KeyEvent.VK_LEFT),
31 new CommandKeyBinding(Commands.PAN_RIGHT, null, KeyEvent.VK_RIGHT),
32 new CommandKeyBinding(Commands.PAN_UP, null, KeyEvent.VK_UP),
33 new CommandKeyBinding(Commands.PAN_DOWN, null, KeyEvent.VK_DOWN),
34 // new CommandKeyBinding(Commands.ZOOM_TO_FIT, null, KeyEvent.VK_1),
35 // new CommandKeyBinding(Commands.ZOOM_TO_FIT_HORIZ, null, KeyEvent.VK_2),
36 // new CommandKeyBinding(Commands.ZOOM_TO_FIT_VERT, null, KeyEvent.VK_3),
37 // new CommandKeyBinding(Commands.AUTOSCALE, null, KeyEvent.VK_4),
38 new CommandKeyBinding(Commands.ZOOM_TO_SELECTION, null, KeyEvent.VK_2),
39 new CommandKeyBinding(Commands.ZOOM_TO_PAGE, null, KeyEvent.VK_3),
40 new CommandKeyBinding(Commands.ZOOM_IN, null, KeyEvent.VK_PLUS),
41 new CommandKeyBinding(Commands.ZOOM_IN, null, KeyEvent.VK_ADD),
42 new CommandKeyBinding(Commands.ZOOM_OUT, null, KeyEvent.VK_MINUS),
43 new CommandKeyBinding(Commands.ZOOM_OUT, null, KeyEvent.VK_SUBTRACT),
44 new CommandKeyBinding(Commands.ZOOM_TO_AREA, null, KeyEvent.VK_F3),
45 new CommandKeyBinding(Commands.REFRESH, null, KeyEvent.VK_F5),
46 new CommandKeyBinding(Commands.RENAME, null, KeyEvent.VK_F2),
47 new CommandKeyBinding(Commands.DELETE, null, KeyEvent.VK_DELETE),
48 new CommandKeyBinding(Commands.SELECT_ALL, null, -KeyEvent.VK_A, KeyEvent.VK_CONTROL),
49 new CommandKeyBinding(Commands.SELECT_ALL, null, KeyEvent.VK_A, KeyEvent.VK_CONTROL),
50 new CommandKeyBinding(Commands.CUT, null, KeyEvent.VK_X, KeyEvent.VK_CONTROL),
51 new CommandKeyBinding(Commands.COPY, null, KeyEvent.VK_C, KeyEvent.VK_CONTROL),
52 new CommandKeyBinding(Commands.PASTE, null, KeyEvent.VK_V, KeyEvent.VK_CONTROL),
53 new CommandKeyBinding(Commands.PRINT, null, KeyEvent.VK_P, KeyEvent.VK_CONTROL),
54 new CommandKeyBinding(Commands.PDFPRINT, null, KeyEvent.VK_P, KeyEvent.VK_ALT),
55 new CommandKeyBinding(Commands.BRING_UP, null, KeyEvent.VK_PAGE_UP),
56 new CommandKeyBinding(Commands.SEND_DOWN, null, KeyEvent.VK_PAGE_DOWN),
57 new CommandKeyBinding(Commands.BRING_TO_TOP, null, KeyEvent.VK_HOME),
58 new CommandKeyBinding(Commands.SEND_TO_BOTTOM, null, KeyEvent.VK_END),
59 new CommandKeyBinding(Commands.ROTATE_ELEMENT_CW, null, KeyEvent.VK_PERIOD),
60 new CommandKeyBinding(Commands.ROTATE_ELEMENT_CCW, null, KeyEvent.VK_COMMA),
61 new CommandKeyBinding(Commands.FLIP_ELEMENT_HORIZONTAL, null, KeyEvent.VK_F, -KeyEvent.VK_CONTROL),
62 new CommandKeyBinding(Commands.FLIP_ELEMENT_VERTICAL, null, KeyEvent.VK_V, -KeyEvent.VK_CONTROL),
63 // new CommandKeyBinding(Commands.ROTATE_CANVAS_CW_GRAB, null, KeyEvent.VK_PERIOD),
64 // new CommandKeyBinding(Commands.ROTATE_CANVAS_CW_RELEASE, null, -KeyEvent.VK_PERIOD),
65 // new CommandKeyBinding(Commands.ROTATE_CANVAS_CCW_GRAB, null, KeyEvent.VK_COMMA),
66 // new CommandKeyBinding(Commands.ROTATE_CANVAS_CCW_RELEASE, null, -KeyEvent.VK_COMMA),
67 new CommandKeyBinding(Commands.GRID_TOGGLE, null, KeyEvent.VK_G),
68 new CommandKeyBinding(Commands.RULER_TOGGLE, null, KeyEvent.VK_R),
69 new CommandKeyBinding(Commands.FOCUS_TOOLTIP, null, KeyEvent.VK_F9)
73 * Required keys including modifiers, virtual codes.
74 * The code at first index is the trigger code, and other codes are
75 * modifiers (ctrl, shift, etc).
76 * Negative code is release (index=0) or modifier is up (index>0)
78 public final int [] keyCode;
79 /** Command to trigger */
80 public final Command command;
81 /** Guide shown in UI about how to use the binding */
82 public final String guide;
86 public CommandKeyBinding(Command command, String guide, int...keyCode) {
87 assert(keyCode.length>0);
88 this.command = command;
89 this.keyCode = keyCode;
91 this.hash = Arrays.hashCode(keyCode) + 13*command.hashCode();
96 public int hashCode() {
101 public boolean equals(Object obj) {
102 if (!(obj instanceof CommandKeyBinding)) return false;
103 CommandKeyBinding other = (CommandKeyBinding) obj;
104 return other.command.equals(command) && Arrays.equals(keyCode, other.keyCode);