Project

General

Profile

Download (4.11 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.gef;
12

    
13
import java.util.HashMap;
14
import java.util.Map;
15

    
16
import org.eclipse.swt.events.KeyEvent;
17

    
18
import org.eclipse.jface.action.IAction;
19

    
20
/**
21
 * The KeyHandler should handle all normal keystrokes on an
22
 * <code>EditPartViewer</code>. <I>Normal</i> is simply defined as keystrokes
23
 * which are not associated with an Accelerator on the Menu. The KeyHandler will
24
 * be forwarded KeyEvents by the active Tool, which is usually the
25
 * SelectionTool. The Tool may be in a state where keystrokes should not be
26
 * processed, in which case it will not forward the keystrokes. For this reason,
27
 * it is important to always handle KeyEvents by using a KeyHandler.
28
 * <P>
29
 * KeyHandlers can be chained by calling {@link #setParent(KeyHandler)}. If this
30
 * KeyHandler does not handle the keystroke, it will pass the keystroke to its
31
 * <i>parent</i> KeyHandler.
32
 * <P>
33
 * KeyHandlers can be implemented using two stragegies. One is to map
34
 * {@link KeyStroke KeyStrokes} to {@link org.eclipse.jface.action.IAction
35
 * Actions} using the {@link #put(KeyStroke, IAction)} and
36
 * {@link #remove(KeyStroke)} API. The other is to subclass KeyHandler, and
37
 * override various methods. A combination of the two is also useful.
38
 * 
39
 * @since 2.0
40
 */
41
public class KeyHandler {
42

    
43
	private Map actions;
44
	private KeyHandler parent;
45

    
46
	/**
47
	 * Processes a <i>key pressed</i> event. This method is called by the Tool
48
	 * whenever a key is pressed, and the Tool is in the proper state.
49
	 * 
50
	 * @param event
51
	 *            the KeyEvent
52
	 * @return <code>true</code> if KeyEvent was handled in some way
53
	 */
54
	public boolean keyPressed(KeyEvent event) {
55
		if (performStroke(new KeyStroke(event, true))) {
56
			event.doit = false;
57
			return true;
58
		}
59
		return parent != null && parent.keyPressed(event);
60
	}
61

    
62
	/**
63
	 * Processes a <i>key released</i> event. This method is called by the Tool
64
	 * whenever a key is released, and the Tool is in the proper state.
65
	 * 
66
	 * @param event
67
	 *            the KeyEvent
68
	 * @return <code>true</code> if KeyEvent was handled in some way
69
	 */
70
	public boolean keyReleased(KeyEvent event) {
71
		if (performStroke(new KeyStroke(event, false)))
72
			return true;
73
		return parent != null && parent.keyReleased(event);
74
	}
75

    
76
	private boolean performStroke(KeyStroke key) {
77
		if (actions == null)
78
			return false;
79
		IAction action = (IAction) actions.get(key);
80
		if (action == null)
81
			return false;
82
		if (action.isEnabled())
83
			action.run();
84
		return true;
85
	}
86

    
87
	/**
88
	 * Maps a specified <code>KeyStroke</code> to an <code>IAction</code>. When
89
	 * a KeyEvent occurs matching the given KeyStroke, the Action will be
90
	 * <code>run()</code> iff it is enabled.
91
	 * 
92
	 * @param keystroke
93
	 *            the KeyStroke
94
	 * @param action
95
	 *            the Action to run
96
	 */
97
	public void put(KeyStroke keystroke, IAction action) {
98
		if (actions == null)
99
			actions = new HashMap();
100
		actions.put(keystroke, action);
101
	}
102

    
103
	/**
104
	 * Removed a mapped <code>IAction</code> for the specified
105
	 * <code>KeyStroke</code>.
106
	 * 
107
	 * @param keystroke
108
	 *            the KeyStroke to be unmapped
109
	 */
110
	public void remove(KeyStroke keystroke) {
111
		if (actions != null)
112
			actions.remove(keystroke);
113
	}
114

    
115
	/**
116
	 * Sets a <i>parent</i> <code>KeyHandler</code> to which this KeyHandler
117
	 * will forward un-consumed KeyEvents. This KeyHandler will first attempt to
118
	 * handle KeyEvents. If it does not recognize a given KeyEvent, that event
119
	 * is passed to its <i>parent</i>
120
	 * 
121
	 * @param parent
122
	 *            the <i>parent</i> KeyHandler
123
	 * @return <code>this</code> for convenience
124
	 */
125
	public KeyHandler setParent(KeyHandler parent) {
126
		this.parent = parent;
127
		return this;
128
	}
129

    
130
}
(23-23/44)