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.

ViewChangeListener.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.navigator;
  17. import java.io.Serializable;
  18. import java.util.EventObject;
  19. import java.util.Map;
  20. /**
  21. * Interface for listening to View changes before and after they occur.
  22. * <p>
  23. * Implementations of this interface can also block navigation between views
  24. * before it is performed (using {@link #beforeViewChange(ViewChangeEvent)}).
  25. * <p>
  26. * The interface contains two methods {@link #beforeViewChange(ViewChangeEvent)}
  27. * and {@link #afterViewChange(ViewChangeEvent)}. The latter one has default
  28. * empty implementation.
  29. *
  30. * @author Vaadin Ltd
  31. * @since 7.0
  32. */
  33. @FunctionalInterface
  34. public interface ViewChangeListener extends Serializable {
  35. /**
  36. * Event received by the listener for attempted and executed view changes.
  37. */
  38. public static class ViewChangeEvent extends EventObject {
  39. private final View oldView;
  40. private final View newView;
  41. private final String viewName;
  42. private final String parameters;
  43. /**
  44. * Create a new view change event.
  45. *
  46. * @param navigator
  47. * Navigator that triggered the event, not null
  48. */
  49. public ViewChangeEvent(Navigator navigator, View oldView, View newView,
  50. String viewName, String parameters) {
  51. super(navigator);
  52. this.oldView = oldView;
  53. this.newView = newView;
  54. this.viewName = viewName;
  55. this.parameters = parameters;
  56. }
  57. /**
  58. * Returns the navigator that triggered this event.
  59. *
  60. * @return Navigator (not null)
  61. */
  62. public Navigator getNavigator() {
  63. return (Navigator) getSource();
  64. }
  65. /**
  66. * Returns the view being deactivated.
  67. *
  68. * @return old View
  69. */
  70. public View getOldView() {
  71. return oldView;
  72. }
  73. /**
  74. * Returns the view being activated.
  75. *
  76. * @return new View
  77. */
  78. public View getNewView() {
  79. return newView;
  80. }
  81. /**
  82. * Returns the view name of the view being activated.
  83. *
  84. * @return view name of the new View
  85. */
  86. public String getViewName() {
  87. return viewName;
  88. }
  89. /**
  90. * Returns the parameters for the view being activated.
  91. *
  92. * @return navigation parameters (potentially bookmarkable) for the new
  93. * view
  94. */
  95. public String getParameters() {
  96. return parameters;
  97. }
  98. /**
  99. * Returns the parameters for the view being activated parsed to a map,
  100. * using {@literal &} as the parameter separator character.
  101. *
  102. * @return navigation parameters (potentially bookmarkable) for the new
  103. * view
  104. * @since 8.1
  105. */
  106. public Map<String, String> getParameterMap() {
  107. return getParameterMap("&");
  108. }
  109. /**
  110. * Returns the parameters for the view being activated parsed to a map,
  111. * using the given string as the parameter separator character.
  112. *
  113. * @param separator
  114. * the parameter separator string to use
  115. * @return navigation parameters (potentially bookmarkable) for the new
  116. * view
  117. * @since 8.1
  118. */
  119. public Map<String, String> getParameterMap(String separator) {
  120. return getNavigator().parseParameterStringToMap(getParameters(),
  121. separator);
  122. }
  123. }
  124. /**
  125. * Invoked before the view is changed.
  126. * <p>
  127. * This method may e.g. open a "save" dialog or question about the change,
  128. * which may re-initiate the navigation operation after user action.
  129. * <p>
  130. * If this listener does not want to block the view change (e.g. does not
  131. * know the view in question), it should return true. If any listener
  132. * returns false, the view change is not allowed and
  133. * <code>afterViewChange()</code> methods are not called.
  134. *
  135. * @param event
  136. * view change event
  137. * @return true if the view change should be allowed or this listener does
  138. * not care about the view change, false to block the change
  139. */
  140. public boolean beforeViewChange(ViewChangeEvent event);
  141. /**
  142. * Invoked after the view is changed. If a <code>beforeViewChange</code>
  143. * method blocked the view change, this method is not called. Be careful of
  144. * unbounded recursion if you decide to change the view again in the
  145. * listener.
  146. * <p>
  147. * By default it does nothing. Override it in your listener if you need this
  148. * functionality.
  149. *
  150. * @param event
  151. * view change event
  152. */
  153. public default void afterViewChange(ViewChangeEvent event) {
  154. }
  155. }