View Javadoc
1   package org.vaadin.peter.contextmenu.client;
2   
3   import com.google.gwt.event.dom.client.ClickEvent;
4   import com.google.gwt.event.dom.client.ClickHandler;
5   import com.google.gwt.event.dom.client.KeyCodes;
6   import com.google.gwt.event.dom.client.KeyUpEvent;
7   import com.google.gwt.event.dom.client.KeyUpHandler;
8   import com.google.gwt.event.dom.client.MouseOutEvent;
9   import com.google.gwt.event.dom.client.MouseOutHandler;
10  import com.google.gwt.event.dom.client.MouseOverEvent;
11  import com.google.gwt.event.dom.client.MouseOverHandler;
12  import com.google.gwt.user.client.Timer;
13  import com.vaadin.client.ServerConnector;
14  import com.vaadin.client.communication.RpcProxy;
15  
16  /**
17   * ContextMenuItemWidgetHandler is context menu item specific object that
18   * handles the server communication when item is interacted with.
19   * 
20   * @author Peter Lehto / Vaadin Ltd
21   * 
22   */
23  public class ContextMenuItemWidgetHandler implements ClickHandler,
24  		MouseOverHandler, MouseOutHandler, KeyUpHandler {
25  
26  	private ContextMenuServerRpc contextMenuRpc;
27  
28  	private final Timer openTimer = new Timer() {
29  		@Override
30  		public void run() {
31  			onItemClicked();
32  		}
33  	};
34  
35  	private ContextMenuItemWidget widget;
36  
37  	public ContextMenuItemWidgetHandler(ContextMenuItemWidget widget,
38  			ServerConnector connector) {
39  		this.widget = widget;
40  
41  		contextMenuRpc = RpcProxy.create(ContextMenuServerRpc.class, connector);
42  	}
43  
44  	@Override
45  	public void onKeyUp(KeyUpEvent event) {
46  		int keycode = event.getNativeEvent().getKeyCode();
47  
48  		if (keycode == KeyCodes.KEY_LEFT) {
49  			onLeftPressed();
50  		} else if (keycode == KeyCodes.KEY_RIGHT) {
51  			onRightPressed();
52  		} else if (keycode == KeyCodes.KEY_UP) {
53  			onUpPressed();
54  		} else if (keycode == KeyCodes.KEY_DOWN) {
55  			onDownPressed();
56  		} else if (keycode == KeyCodes.KEY_ENTER) {
57  			onEnterPressed();
58  		}
59  	}
60  
61  	@Override
62  	public void onMouseOut(MouseOutEvent event) {
63  		openTimer.cancel();
64  		widget.setFocus(false);
65  	}
66  
67  	@Override
68  	public void onMouseOver(MouseOverEvent event) {
69  		openTimer.cancel();
70  
71  		if (isEnabled()) {
72  			if (!widget.isSubmenuOpen()) {
73  				widget.closeSiblingMenus();
74  			}
75  			widget.setFocus(true);
76  
77  			if (widget.hasSubMenu() && !widget.isSubmenuOpen()) {
78  				openTimer.schedule(500);
79  			}
80  		}
81  	}
82  
83  	@Override
84  	public void onClick(ClickEvent event) {
85  		if (isEnabled()) {
86  			openTimer.cancel();
87  
88  			if (widget.hasSubMenu()) {
89  				if (!widget.isSubmenuOpen()) {
90  					widget.onItemClicked();
91  					contextMenuRpc.itemClicked(widget.getId(), false);
92  				}
93  			} else {
94  				boolean menuClosed = widget.onItemClicked();
95  				contextMenuRpc.itemClicked(widget.getId(), menuClosed);
96  			}
97  		}
98  	}
99  
100 	private boolean isEnabled() {
101 		return widget.isEnabled();
102 	}
103 
104 	private void onLeftPressed() {
105 		if (isEnabled()) {
106 			widget.closeThisAndSelectParent();
107 		}
108 	}
109 
110 	private void onRightPressed() {
111 		if (isEnabled()) {
112 			if (widget.hasSubMenu()) {
113 				onItemClicked();
114 			}
115 		}
116 	}
117 
118 	private void onUpPressed() {
119 		if (isEnabled()) {
120 			widget.selectUpperSibling();
121 		}
122 	}
123 
124 	private void onDownPressed() {
125 		if (isEnabled()) {
126 			widget.selectLowerSibling();
127 		}
128 	}
129 
130 	private void onEnterPressed() {
131 		if (isEnabled()) {
132 			if (widget.hasSubMenu()) {
133 				if (!widget.isSubmenuOpen()) {
134 					widget.onItemClicked();
135 					contextMenuRpc.itemClicked(widget.getId(), false);
136 				}
137 			} else {
138 				boolean menuClosed = widget.onItemClicked();
139 				contextMenuRpc.itemClicked(widget.getId(), menuClosed);
140 			}
141 		}
142 	}
143 
144 	private void onItemClicked() {
145 		boolean menuClosed = widget.onItemClicked();
146 		contextMenuRpc.itemClicked(widget.getId(), menuClosed);
147 	}
148 
149 }