You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultEditorEventHandler.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.widget.grid;
  17. import com.google.gwt.core.client.Duration;
  18. import com.google.gwt.dom.client.BrowserEvents;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.event.dom.client.KeyCodes;
  21. import com.google.gwt.user.client.Event;
  22. import com.google.gwt.user.client.ui.Widget;
  23. import com.vaadin.client.ComponentConnector;
  24. import com.vaadin.client.Util;
  25. import com.vaadin.client.WidgetUtil;
  26. import com.vaadin.client.ui.AbstractFieldConnector;
  27. import com.vaadin.client.ui.FocusUtil;
  28. import com.vaadin.client.widgets.Grid;
  29. import com.vaadin.client.widgets.Grid.Editor;
  30. import com.vaadin.client.widgets.Grid.EditorDomEvent;
  31. import java.util.List;
  32. /**
  33. * The default handler for Grid editor events. Offers several overridable
  34. * protected methods for easier customization.
  35. *
  36. * @since 7.6
  37. * @author Vaadin Ltd
  38. */
  39. public class DefaultEditorEventHandler<T> implements Editor.EventHandler<T> {
  40. public static final int KEYCODE_OPEN = KeyCodes.KEY_ENTER;
  41. public static final int KEYCODE_MOVE_VERTICAL = KeyCodes.KEY_ENTER;
  42. public static final int KEYCODE_CLOSE = KeyCodes.KEY_ESCAPE;
  43. public static final int KEYCODE_MOVE_HORIZONTAL = KeyCodes.KEY_TAB;
  44. public static final int KEYCODE_BUFFERED_SAVE = KeyCodes.KEY_ENTER;
  45. private double lastTouchEventTime = 0;
  46. private int lastTouchEventX = -1;
  47. private int lastTouchEventY = -1;
  48. private int lastTouchEventRow = -1;
  49. /**
  50. * Returns whether the given event is a touch event that should open the
  51. * editor.
  52. *
  53. * @param event
  54. * the received event
  55. * @return whether the event is a touch open event
  56. */
  57. protected boolean isTouchOpenEvent(EditorDomEvent<T> event) {
  58. final Event e = event.getDomEvent();
  59. final int type = e.getTypeInt();
  60. final double now = Duration.currentTimeMillis();
  61. final int currentX = WidgetUtil.getTouchOrMouseClientX(e);
  62. final int currentY = WidgetUtil.getTouchOrMouseClientY(e);
  63. final boolean validTouchOpenEvent = type == Event.ONTOUCHEND
  64. && now - lastTouchEventTime < 500
  65. && lastTouchEventRow == event.getCell().getRowIndex()
  66. && Math.abs(lastTouchEventX - currentX) < 20
  67. && Math.abs(lastTouchEventY - currentY) < 20;
  68. if (type == Event.ONTOUCHSTART) {
  69. lastTouchEventX = currentX;
  70. lastTouchEventY = currentY;
  71. }
  72. if (type == Event.ONTOUCHEND) {
  73. lastTouchEventTime = now;
  74. lastTouchEventRow = event.getCell().getRowIndex();
  75. }
  76. return validTouchOpenEvent;
  77. }
  78. /**
  79. * Returns whether the given event should open the editor. The default
  80. * implementation returns true if and only if the event is a doubleclick or
  81. * if it is a keydown event and the keycode is {@link #KEYCODE_OPEN}.
  82. *
  83. * @param event
  84. * the received event
  85. * @return true if the event is an open event, false otherwise
  86. */
  87. protected boolean isOpenEvent(EditorDomEvent<T> event) {
  88. final Event e = event.getDomEvent();
  89. return e.getTypeInt() == Event.ONDBLCLICK
  90. || (e.getTypeInt() == Event.ONKEYDOWN
  91. && e.getKeyCode() == KEYCODE_OPEN)
  92. || isTouchOpenEvent(event);
  93. }
  94. /**
  95. * Opens the editor on the appropriate row if the received event is an open
  96. * event. The default implementation uses
  97. * {@link #isOpenEvent(EditorDomEvent) isOpenEvent}.
  98. *
  99. * @param event
  100. * the received event
  101. * @return true if this method handled the event and nothing else should be
  102. * done, false otherwise
  103. */
  104. protected boolean handleOpenEvent(EditorDomEvent<T> event) {
  105. if (isOpenEvent(event)) {
  106. final EventCellReference<T> cell = event.getCell();
  107. editRow(event, cell.getRowIndex(), cell.getColumnIndexDOM());
  108. event.getDomEvent().preventDefault();
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * Specifies the direction at which the focus should move.
  115. */
  116. public enum CursorMoveDelta {
  117. UP(-1, 0), RIGHT(0, 1), DOWN(1, 0), LEFT(0, -1);
  118. public final int rowDelta;
  119. public final int colDelta;
  120. CursorMoveDelta(int rowDelta, int colDelta) {
  121. this.rowDelta = rowDelta;
  122. this.colDelta = colDelta;
  123. }
  124. public boolean isChanged() {
  125. return rowDelta != 0 || colDelta != 0;
  126. }
  127. }
  128. /**
  129. * Returns the direction to which the cursor should move.
  130. *
  131. * @param event
  132. * the mouse event, not null.
  133. * @return the direction. May return null if the cursor should not move.
  134. */
  135. protected CursorMoveDelta getDeltaFromKeyDownEvent(
  136. EditorDomEvent<T> event) {
  137. Event e = event.getDomEvent();
  138. if (e.getKeyCode() == KEYCODE_MOVE_VERTICAL) {
  139. return e.getShiftKey() ? CursorMoveDelta.UP : CursorMoveDelta.DOWN;
  140. } else if (e.getKeyCode() == KEYCODE_MOVE_HORIZONTAL) {
  141. // Prevent tab out of Grid Editor
  142. event.getDomEvent().preventDefault();
  143. return e.getShiftKey() ? CursorMoveDelta.LEFT
  144. : CursorMoveDelta.RIGHT;
  145. }
  146. return null;
  147. }
  148. /**
  149. * Moves the editor to another row or another column if the received event
  150. * is a move event. The default implementation moves the editor to the
  151. * clicked row if the event is a click; otherwise, if the event is a keydown
  152. * and the keycode is {@link #KEYCODE_MOVE_VERTICAL}, moves the editor one
  153. * row up or down if the shift key is pressed or not, respectively. Keydown
  154. * event with keycode {@link #KEYCODE_MOVE_HORIZONTAL} moves the editor left
  155. * or right if shift key is pressed or not, respectively.
  156. *
  157. * @param event
  158. * the received event
  159. * @return true if this method handled the event and nothing else should be
  160. * done, false otherwise
  161. */
  162. protected boolean handleMoveEvent(EditorDomEvent<T> event) {
  163. Event e = event.getDomEvent();
  164. final EventCellReference<T> cell = event.getCell();
  165. // TODO: Move on touch events
  166. if (e.getTypeInt() == Event.ONCLICK) {
  167. editRow(event, cell.getRowIndex(), cell.getColumnIndexDOM());
  168. return true;
  169. } else if (e.getTypeInt() == Event.ONKEYDOWN) {
  170. CursorMoveDelta delta = getDeltaFromKeyDownEvent(event);
  171. final boolean changed = delta != null;
  172. if (changed) {
  173. int columnCount = event.getGrid().getVisibleColumns().size();
  174. int colIndex = delta.colDelta > 0
  175. ? findNextEditableColumnIndex(event.getGrid(),
  176. event.getFocusedColumnIndex() + delta.colDelta)
  177. : findPrevEditableColumnIndex(event.getGrid(),
  178. event.getFocusedColumnIndex() + delta.colDelta);
  179. int rowIndex = event.getRowIndex();
  180. // Handle row change with horizontal move when column goes out
  181. // of range.
  182. if (delta.rowDelta == 0 && colIndex < 0) {
  183. if (delta.colDelta > 0
  184. && rowIndex < event.getGrid().getDataSource().size()
  185. - 1) {
  186. delta = CursorMoveDelta.DOWN;
  187. colIndex = findNextEditableColumnIndex(event.getGrid(),
  188. 0);
  189. } else if (delta.colDelta < 0 && rowIndex > 0) {
  190. delta = CursorMoveDelta.UP;
  191. colIndex = findPrevEditableColumnIndex(event.getGrid(),
  192. columnCount - 1);
  193. }
  194. }
  195. editRow(event, rowIndex + delta.rowDelta, colIndex);
  196. }
  197. return changed;
  198. }
  199. return false;
  200. }
  201. /**
  202. * Finds index of the first editable column, starting at the specified
  203. * index.
  204. *
  205. * @param grid
  206. * the current grid, not null.
  207. * @param startingWith
  208. * start with this column. Index into the
  209. * {@link Grid#getVisibleColumns()}.
  210. * @return the index of the nearest visible column; may return the
  211. * <code>startingWith</code> itself. Returns -1 if there is no such
  212. * column.
  213. */
  214. protected int findNextEditableColumnIndex(Grid<T> grid, int startingWith) {
  215. final List<Grid.Column<?, T>> columns = grid.getVisibleColumns();
  216. for (int i = startingWith; i < columns.size(); i++) {
  217. if (isEditable(grid, columns.get(i))) {
  218. return i;
  219. }
  220. }
  221. return -1;
  222. }
  223. protected boolean isEditable(Grid<T> grid, Grid.Column<?, T> column) {
  224. if (!column.isEditable()) {
  225. return false;
  226. }
  227. // figure out whether the widget nested in the editor cell is editable.
  228. // if it is disabled or read-only then it is not editable.
  229. final Widget editorCell = grid.getEditorWidget(column);
  230. final ComponentConnector connector = Util.findConnectorFor(editorCell);
  231. if (connector == null) {
  232. // not a Vaadin Connector, perhaps something generated by the
  233. // renderer? Assume it's enabled.
  234. return true;
  235. }
  236. if (!connector.isEnabled()) {
  237. return false;
  238. }
  239. if (connector instanceof AbstractFieldConnector) {
  240. final AbstractFieldConnector field = (AbstractFieldConnector) connector;
  241. if (field.isReadOnly()) {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. /**
  248. * Finds index of the last editable column, searching backwards starting at
  249. * the specified index.
  250. *
  251. * @param grid
  252. * the current grid, not null.
  253. * @param startingWith
  254. * start with this column. Index into the
  255. * {@link Grid#getVisibleColumns()}.
  256. * @return the index of the nearest visible column; may return the
  257. * <code>startingWith</code> itself. Returns -1 if there is no such
  258. * column.
  259. */
  260. protected int findPrevEditableColumnIndex(Grid<T> grid, int startingWith) {
  261. final List<Grid.Column<?, T>> columns = grid.getVisibleColumns();
  262. for (int i = startingWith; i >= 0; i--) {
  263. if (isEditable(grid, columns.get(i))) {
  264. return i;
  265. }
  266. }
  267. return -1;
  268. }
  269. /**
  270. * Moves the editor to another column if the received event is a move event.
  271. * By default the editor is moved on a keydown event with keycode
  272. * {@link #KEYCODE_MOVE_HORIZONTAL}. This moves the editor left or right if
  273. * shift key is pressed or not, respectively.
  274. *
  275. * @param event
  276. * the received event
  277. * @return true if this method handled the event and nothing else should be
  278. * done, false otherwise
  279. */
  280. protected boolean handleBufferedMoveEvent(EditorDomEvent<T> event) {
  281. Event e = event.getDomEvent();
  282. if (e.getType().equals(BrowserEvents.CLICK)
  283. && event.getRowIndex() == event.getCell().getRowIndex()) {
  284. editRow(event, event.getRowIndex(),
  285. event.getCell().getColumnIndexDOM());
  286. return true;
  287. } else if (e.getType().equals(BrowserEvents.KEYDOWN)
  288. && e.getKeyCode() == KEYCODE_MOVE_HORIZONTAL) {
  289. // Prevent tab out of Grid Editor
  290. event.getDomEvent().preventDefault();
  291. final int newColIndex = e.getShiftKey()
  292. ? findPrevEditableColumnIndex(event.getGrid(),
  293. event.getFocusedColumnIndex() - 1)
  294. : findNextEditableColumnIndex(event.getGrid(),
  295. event.getFocusedColumnIndex() + 1);
  296. if (newColIndex >= 0) {
  297. editRow(event, event.getRowIndex(), newColIndex);
  298. }
  299. return true;
  300. } else if (e.getType().equals(BrowserEvents.KEYDOWN)
  301. && e.getKeyCode() == KEYCODE_BUFFERED_SAVE) {
  302. triggerValueChangeEvent(event);
  303. // Save and close.
  304. event.getGrid().getEditor().save();
  305. FocusUtil.setFocus(event.getGrid(), true);
  306. return true;
  307. }
  308. return false;
  309. }
  310. /**
  311. * Returns whether the given event should close the editor. The default
  312. * implementation returns true if and only if the event is a keydown event
  313. * and the keycode is {@link #KEYCODE_CLOSE}.
  314. *
  315. * @param event
  316. * the received event
  317. * @return true if the event is a close event, false otherwise
  318. */
  319. protected boolean isCloseEvent(EditorDomEvent<T> event) {
  320. final Event e = event.getDomEvent();
  321. return e.getTypeInt() == Event.ONKEYDOWN
  322. && e.getKeyCode() == KEYCODE_CLOSE;
  323. }
  324. /**
  325. * Closes the editor if the received event is a close event. The default
  326. * implementation uses {@link #isCloseEvent(EditorDomEvent) isCloseEvent}.
  327. *
  328. * @param event
  329. * the received event
  330. * @return true if this method handled the event and nothing else should be
  331. * done, false otherwise
  332. */
  333. protected boolean handleCloseEvent(EditorDomEvent<T> event) {
  334. if (isCloseEvent(event)) {
  335. event.getEditor().cancel();
  336. FocusUtil.setFocus(event.getGrid(), true);
  337. return true;
  338. }
  339. return false;
  340. }
  341. protected void editRow(EditorDomEvent<T> event, int rowIndex,
  342. int colIndex) {
  343. int rowCount = event.getGrid().getDataSource().size();
  344. // Limit rowIndex between 0 and rowCount - 1
  345. rowIndex = Math.max(0, Math.min(rowCount - 1, rowIndex));
  346. int colCount = event.getGrid().getVisibleColumns().size();
  347. // Limit colIndex between 0 and colCount - 1
  348. colIndex = Math.max(0, Math.min(colCount - 1, colIndex));
  349. if (rowIndex != event.getRowIndex()) {
  350. triggerValueChangeEvent(event);
  351. }
  352. event.getEditor().editRow(rowIndex, colIndex);
  353. }
  354. /**
  355. * Triggers a value change event from the editor field if it has focus. This
  356. * is based on the assumption that editor field will fire the value change
  357. * when a blur event occurs.
  358. *
  359. * @param event
  360. * the editor DOM event
  361. */
  362. private void triggerValueChangeEvent(EditorDomEvent<T> event) {
  363. // Force a blur to cause a value change event
  364. Widget editorWidget = event.getEditorWidget();
  365. if (editorWidget != null) {
  366. Element focusedElement = WidgetUtil.getFocusedElement();
  367. if (editorWidget.getElement().isOrHasChild(focusedElement)) {
  368. focusedElement.blur();
  369. focusedElement.focus();
  370. }
  371. }
  372. }
  373. @Override
  374. public boolean handleEvent(EditorDomEvent<T> event) {
  375. final Editor<T> editor = event.getEditor();
  376. final boolean isBody = event.getCell().isBody();
  377. final boolean handled;
  378. if (event.getGrid().isEditorActive()) {
  379. handled = handleCloseEvent(event)
  380. || (!editor.isBuffered() && isBody
  381. && handleMoveEvent(event))
  382. || (editor.isBuffered() && isBody
  383. && handleBufferedMoveEvent(event));
  384. } else {
  385. handled = event.getGrid().isEnabled() && isBody
  386. && handleOpenEvent(event);
  387. }
  388. // Buffered mode should swallow all events, if not already handled.
  389. boolean swallowEvent = event.getGrid().isEditorActive()
  390. && editor.isBuffered();
  391. return handled || swallowEvent;
  392. }
  393. }