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.

MouseEventDetailsBuilder.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2000-2016 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;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.dom.client.NativeEvent;
  19. import com.google.gwt.user.client.Event;
  20. import com.vaadin.shared.MouseEventDetails;
  21. import com.vaadin.shared.MouseEventDetails.MouseButton;
  22. /**
  23. * Helper class for constructing a MouseEventDetails object from a
  24. * {@link NativeEvent}.
  25. *
  26. * @author Vaadin Ltd
  27. * @since 7.0.0
  28. *
  29. */
  30. public class MouseEventDetailsBuilder {
  31. /**
  32. * Construct a {@link MouseEventDetails} object from the given event
  33. *
  34. * @param evt
  35. * The event to use as a source for the details
  36. * @return a MouseEventDetails containing information from the event
  37. */
  38. public static MouseEventDetails buildMouseEventDetails(NativeEvent evt) {
  39. return buildMouseEventDetails(evt, null);
  40. }
  41. /**
  42. * Construct a {@link MouseEventDetails} object from the given event
  43. *
  44. * @param evt
  45. * The event to use as a source for the details
  46. * @param relativeToElement
  47. * The element whose position
  48. * {@link MouseEventDetails#getRelativeX()} and
  49. * {@link MouseEventDetails#getRelativeY()} are relative to.
  50. * @return a MouseEventDetails containing information from the event
  51. */
  52. public static MouseEventDetails buildMouseEventDetails(NativeEvent evt,
  53. Element relativeToElement) {
  54. MouseEventDetails mouseEventDetails = new MouseEventDetails();
  55. mouseEventDetails.setType(Event.getTypeInt(evt.getType()));
  56. mouseEventDetails.setClientX(WidgetUtil.getTouchOrMouseClientX(evt));
  57. mouseEventDetails.setClientY(WidgetUtil.getTouchOrMouseClientY(evt));
  58. if (evt.getButton() == NativeEvent.BUTTON_LEFT) {
  59. mouseEventDetails.setButton(MouseButton.LEFT);
  60. } else if (evt.getButton() == NativeEvent.BUTTON_RIGHT) {
  61. mouseEventDetails.setButton(MouseButton.RIGHT);
  62. } else if (evt.getButton() == NativeEvent.BUTTON_MIDDLE) {
  63. mouseEventDetails.setButton(MouseButton.MIDDLE);
  64. } else {
  65. // IE8 does not always report a button. Assume left.
  66. mouseEventDetails.setButton(MouseButton.LEFT);
  67. }
  68. mouseEventDetails.setAltKey(evt.getAltKey());
  69. mouseEventDetails.setCtrlKey(evt.getCtrlKey());
  70. mouseEventDetails.setMetaKey(evt.getMetaKey());
  71. mouseEventDetails.setShiftKey(evt.getShiftKey());
  72. if (relativeToElement != null) {
  73. mouseEventDetails.setRelativeX(getRelativeX(
  74. mouseEventDetails.getClientX(), relativeToElement));
  75. mouseEventDetails.setRelativeY(getRelativeY(
  76. mouseEventDetails.getClientY(), relativeToElement));
  77. }
  78. return mouseEventDetails;
  79. }
  80. private static int getRelativeX(int clientX, Element target) {
  81. return clientX - target.getAbsoluteLeft() + target.getScrollLeft()
  82. + target.getOwnerDocument().getScrollLeft();
  83. }
  84. private static int getRelativeY(int clientY, Element target) {
  85. return clientY - target.getAbsoluteTop() + target.getScrollTop()
  86. + target.getOwnerDocument().getScrollTop();
  87. }
  88. }