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.

LayoutEvents.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.event;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import com.vaadin.event.MouseEvents.ClickEvent;
  20. import com.vaadin.shared.Connector;
  21. import com.vaadin.shared.MouseEventDetails;
  22. import com.vaadin.ui.Component;
  23. import com.vaadin.ui.ComponentContainer;
  24. import com.vaadin.util.ReflectTools;
  25. public interface LayoutEvents {
  26. public interface LayoutClickListener extends ComponentEventListener {
  27. public static final Method clickMethod = ReflectTools.findMethod(
  28. LayoutClickListener.class, "layoutClick",
  29. LayoutClickEvent.class);
  30. /**
  31. * Layout has been clicked
  32. *
  33. * @param event
  34. * Component click event.
  35. */
  36. public void layoutClick(LayoutClickEvent event);
  37. }
  38. /**
  39. * The interface for adding and removing <code>LayoutClickEvent</code>
  40. * listeners. By implementing this interface a class explicitly announces
  41. * that it will generate a <code>LayoutClickEvent</code> when a component
  42. * inside it is clicked and a <code>LayoutClickListener</code> is
  43. * registered.
  44. * <p>
  45. * Note: The general Java convention is not to explicitly declare that a
  46. * class generates events, but to directly define the
  47. * <code>addListener</code> and <code>removeListener</code> methods. That
  48. * way the caller of these methods has no real way of finding out if the
  49. * class really will send the events, or if it just defines the methods to
  50. * be able to implement an interface.
  51. * </p>
  52. *
  53. * @since 6.5.2
  54. * @see LayoutClickListener
  55. * @see LayoutClickEvent
  56. */
  57. public interface LayoutClickNotifier extends Serializable {
  58. /**
  59. * Add a click listener to the layout. The listener is called whenever
  60. * the user clicks inside the layout. An event is also triggered when
  61. * the click targets a component inside a nested layout or Panel,
  62. * provided the targeted component does not prevent the click event from
  63. * propagating. A caption is not considered part of a component.
  64. *
  65. * The child component that was clicked is included in the
  66. * {@link LayoutClickEvent}.
  67. *
  68. * Use {@link #removeListener(LayoutClickListener)} to remove the
  69. * listener.
  70. *
  71. * @param listener
  72. * The listener to add
  73. */
  74. public void addLayoutClickListener(LayoutClickListener listener);
  75. /**
  76. * @deprecated As of 7.0, replaced by
  77. * {@link #addLayoutClickListener(LayoutClickListener)}
  78. **/
  79. @Deprecated
  80. public void addListener(LayoutClickListener listener);
  81. /**
  82. * Removes an LayoutClickListener.
  83. *
  84. * @param listener
  85. * LayoutClickListener to be removed
  86. */
  87. public void removeLayoutClickListener(LayoutClickListener listener);
  88. /**
  89. * @deprecated As of 7.0, replaced by
  90. * {@link #removeLayoutClickListener(LayoutClickListener)}
  91. **/
  92. @Deprecated
  93. public void removeListener(LayoutClickListener listener);
  94. }
  95. /**
  96. * An event fired when the layout has been clicked. The event contains
  97. * information about the target layout (component) and the child component
  98. * that was clicked. If no child component was found it is set to null.
  99. */
  100. public static class LayoutClickEvent extends ClickEvent {
  101. private final Component clickedComponent;
  102. private final Component childComponent;
  103. public LayoutClickEvent(Component source,
  104. MouseEventDetails mouseEventDetails,
  105. Component clickedComponent, Component childComponent) {
  106. super(source, mouseEventDetails);
  107. this.clickedComponent = clickedComponent;
  108. this.childComponent = childComponent;
  109. }
  110. /**
  111. * Returns the component that was clicked, which is somewhere inside the
  112. * parent layout on which the listener was registered.
  113. *
  114. * For the direct child component of the layout, see
  115. * {@link #getChildComponent()}.
  116. *
  117. * @return clicked {@link Component}, null if none found
  118. */
  119. public Component getClickedComponent() {
  120. return clickedComponent;
  121. }
  122. /**
  123. * Returns the direct child component of the layout which contains the
  124. * clicked component.
  125. *
  126. * For the clicked component inside that child component of the layout,
  127. * see {@link #getClickedComponent()}.
  128. *
  129. * @return direct child {@link Component} of the layout which contains
  130. * the clicked Component, null if none found
  131. */
  132. public Component getChildComponent() {
  133. return childComponent;
  134. }
  135. public static LayoutClickEvent createEvent(ComponentContainer layout,
  136. MouseEventDetails mouseDetails, Connector clickedConnector) {
  137. Component clickedComponent = (Component) clickedConnector;
  138. Component childComponent = clickedComponent;
  139. while (childComponent != null
  140. && childComponent.getParent() != layout) {
  141. childComponent = childComponent.getParent();
  142. }
  143. return new LayoutClickEvent(layout, mouseDetails, clickedComponent,
  144. childComponent);
  145. }
  146. }
  147. }