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.

SpecialEvent.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2014, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. */
  14. package com.google.gwt.query.client.plugins.events;
  15. import com.google.gwt.dom.client.Element;
  16. import com.google.gwt.query.client.Function;
  17. import com.google.gwt.user.client.Event;
  18. /**
  19. * Interface used to register special events.
  20. *
  21. * Use EventsListeners.special.add(
  22. */
  23. public interface SpecialEvent {
  24. /**
  25. * Default implementation of SpecialEvents for simple cases like
  26. * creating event aliases. Extend this for creating more complex
  27. * cases.
  28. */
  29. public static class DefaultSpecialEvent implements SpecialEvent {
  30. protected final String delegateType;
  31. protected final String type;
  32. protected Function handler = new Function() {
  33. public boolean f(Event e, Object... arg) {
  34. setEvent(e);
  35. EventsListener.getInstance(getElement()).dispatchEvent(e, type);
  36. return true;
  37. };
  38. };
  39. public DefaultSpecialEvent(String type, String delegateType) {
  40. this.type = type;
  41. this.delegateType = delegateType;
  42. }
  43. protected EventsListener listener(Element e) {
  44. return EventsListener.getInstance(e);
  45. }
  46. @Override
  47. public void add(Element e, String eventType, String nameSpace, Object data, Function f) {
  48. // Nothing to do, let gQuery use default events mechanism
  49. }
  50. @Override
  51. public boolean hasHandlers(Element e) {
  52. return listener(e).hasHandlers(Event.getTypeInt(type), type, handler);
  53. }
  54. @Override
  55. public void remove(Element e, String eventType, String nameSpace, Function f) {
  56. // Nothing to do, let gQuery use default events mechanism
  57. }
  58. @Override
  59. public boolean setup(Element e) {
  60. if (!hasHandlers(e)) {
  61. listener(e).bind(delegateType, null, handler);
  62. }
  63. return false;
  64. }
  65. @Override
  66. public boolean tearDown(Element e) {
  67. if (!hasHandlers(e)) {
  68. listener(e).unbind(delegateType, handler);
  69. }
  70. return false;
  71. }
  72. }
  73. /**
  74. * For each bind call the add function is called.
  75. */
  76. void add(Element e, String eventType, String nameSpace, Object data, Function f);
  77. /**
  78. * Return true if there are handlers bound to this special event.
  79. */
  80. boolean hasHandlers(Element e);
  81. /**
  82. * For each unbind call the remove function is called.
  83. */
  84. void remove(Element e, String eventType, String nameSpace, Function f);
  85. /**
  86. * When the first event handler is bound for an EventsListener gQuery executes the setup function.
  87. *
  88. * If the method returns false means that gQuery has to run the default bind for the event before
  89. * calling add.
  90. */
  91. boolean setup(Element e);
  92. /**
  93. * The last unbind call triggers the tearDown method.
  94. *
  95. * If the method returns false means that gQuery has to run the default unbind for the event
  96. * before calling remove.
  97. */
  98. boolean tearDown(Element e);
  99. }