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.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2011 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. /**
  22. * Helper class for constructing a MouseEventDetails object from a
  23. * {@link NativeEvent}.
  24. *
  25. * @author Vaadin Ltd
  26. * @since 7.0.0
  27. *
  28. */
  29. public class MouseEventDetailsBuilder {
  30. /**
  31. * Construct a {@link MouseEventDetails} object from the given event
  32. *
  33. * @param evt
  34. * The event to use as a source for the details
  35. * @return a MouseEventDetails containing information from the event
  36. */
  37. public static MouseEventDetails buildMouseEventDetails(NativeEvent evt) {
  38. return buildMouseEventDetails(evt, null);
  39. }
  40. /**
  41. * Construct a {@link MouseEventDetails} object from the given event
  42. *
  43. * @param evt
  44. * The event to use as a source for the details
  45. * @param relativeToElement
  46. * The element whose position
  47. * {@link MouseEventDetails#getRelativeX()} and
  48. * {@link MouseEventDetails#getRelativeY()} are relative to.
  49. * @return a MouseEventDetails containing information from the event
  50. */
  51. public static MouseEventDetails buildMouseEventDetails(NativeEvent evt,
  52. Element relativeToElement) {
  53. MouseEventDetails mouseEventDetails = new MouseEventDetails();
  54. mouseEventDetails.setType(Event.getTypeInt(evt.getType()));
  55. mouseEventDetails.setClientX(Util.getTouchOrMouseClientX(evt));
  56. mouseEventDetails.setClientY(Util.getTouchOrMouseClientY(evt));
  57. mouseEventDetails.setButton(evt.getButton());
  58. mouseEventDetails.setAltKey(evt.getAltKey());
  59. mouseEventDetails.setCtrlKey(evt.getCtrlKey());
  60. mouseEventDetails.setMetaKey(evt.getMetaKey());
  61. mouseEventDetails.setShiftKey(evt.getShiftKey());
  62. if (relativeToElement != null) {
  63. mouseEventDetails.setRelativeX(getRelativeX(
  64. mouseEventDetails.getClientX(), relativeToElement));
  65. mouseEventDetails.setRelativeY(getRelativeY(
  66. mouseEventDetails.getClientY(), relativeToElement));
  67. }
  68. return mouseEventDetails;
  69. }
  70. private static int getRelativeX(int clientX, Element target) {
  71. return clientX - target.getAbsoluteLeft() + target.getScrollLeft()
  72. + target.getOwnerDocument().getScrollLeft();
  73. }
  74. private static int getRelativeY(int clientY, Element target) {
  75. return clientY - target.getAbsoluteTop() + target.getScrollTop()
  76. + target.getOwnerDocument().getScrollTop();
  77. }
  78. }