View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  /*
19   * Ext GWT 2.2.4 - Ext for GWT
20   * Copyright(c) 2007-2010, Ext JS, LLC.
21   * licensing@extjs.com
22   * 
23   * http://extjs.com/license
24   */
25  package org.vectomatic.svg.edit.client.gxt.widget;
26  
27  import com.extjs.gxt.ui.client.GXT;
28  import com.extjs.gxt.ui.client.event.BaseEvent;
29  import com.extjs.gxt.ui.client.event.ComponentEvent;
30  import com.extjs.gxt.ui.client.event.EventType;
31  import com.extjs.gxt.ui.client.event.Events;
32  import com.extjs.gxt.ui.client.event.Listener;
33  import com.extjs.gxt.ui.client.widget.Component;
34  import com.google.gwt.dom.client.Element;
35  
36  /**
37   * Provides a convenient wrapper for normalized keyboard navigation. Provides an
38   * easy way to implement custom navigation schemes for any UI component.
39   */
40  public class KeyNavExt<E extends ComponentEvent> implements Listener<E> {
41  	public static final int KEY_F2 = 113;
42  	private static EventType keyPressEvent;
43  	private Component component;
44  
45  	static {
46  		if (GXT.isIE || GXT.isGecko || GXT.isWebKit) {
47  			keyPressEvent = Events.OnKeyDown;
48  		} else {
49  			keyPressEvent = Events.OnKeyPress;
50  		}
51  	}
52  
53  	/**
54  	 * Returns the key event type.
55  	 * 
56  	 * @return the key event type
57  	 */
58  	public static EventType getKeyEvent() {
59  		return keyPressEvent;
60  	}
61  
62  	/**
63  	 * Creates a new KeyNav without a target component. Events must be passed to
64  	 * the {@link #handleEvent(BaseEvent)} method.
65  	 */
66  	public KeyNavExt() {
67  
68  	}
69  
70  	/**
71  	 * Creates a new key nav for the specified target. The KeyNav will listen
72  	 * for the key events.
73  	 * @param target the target component
74  	 */
75  	public KeyNavExt(Component target) {
76  		bind(target);
77  	}
78  
79  	/**
80  	 * Binds the key nav to the component.
81  	 * @param target the target component
82  	 */
83  	public void bind(final Component target) {
84  		if (this.component != null) {
85  			this.component.removeListener(keyPressEvent, this);
86  			this.component.removeListener(Events.OnKeyUp, this);
87  		}
88  		if (target != null) {
89  			target.addListener(keyPressEvent, this);
90  			target.addListener(Events.OnKeyUp, this);
91  			target.sinkEvents(keyPressEvent.getEventCode());
92  			target.sinkEvents(Events.OnKeyUp.getEventCode());
93  		}
94  		this.component = target;
95  	}
96  
97  	/**
98  	 * Returns the target component.
99  	 * 
100 	 * @return the target component
101 	 */
102 	public Component getComponent() {
103 		return component;
104 	}
105 
106 	@SuppressWarnings("unchecked")
107 	public void handleEvent(ComponentEvent ce) {
108 		EventType type = ce.getType();
109 		if (type == keyPressEvent || type == Events.OnKeyUp) {
110 			if (component.getElement() != (Element) ce.getEvent().getCurrentEventTarget().cast()) {
111 				return;
112 			}
113 
114 			E e = (E) ce;
115 
116 			if (type == keyPressEvent) {
117 				onKeyPress(e);
118 			} else {
119 				onKeyUp(e);
120 			}
121 		}
122 	}
123 
124 	public void onKeyPress(E ce) {
125 	}
126 
127 	public void onKeyUp(E ce) {
128 	}
129 }