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.

PopupViewConnector.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.ui.popupview;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.google.gwt.event.shared.HandlerRegistration;
  20. import com.vaadin.client.ComponentConnector;
  21. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  22. import com.vaadin.client.VCaption;
  23. import com.vaadin.client.VCaptionWrapper;
  24. import com.vaadin.client.communication.StateChangeEvent;
  25. import com.vaadin.client.ui.AbstractHasComponentsConnector;
  26. import com.vaadin.client.ui.PostLayoutListener;
  27. import com.vaadin.client.ui.VOverlay;
  28. import com.vaadin.client.ui.VPopupView;
  29. import com.vaadin.shared.ui.ComponentStateUtil;
  30. import com.vaadin.shared.ui.Connect;
  31. import com.vaadin.shared.ui.popupview.PopupViewServerRpc;
  32. import com.vaadin.shared.ui.popupview.PopupViewState;
  33. import com.vaadin.ui.PopupView;
  34. @Connect(PopupView.class)
  35. public class PopupViewConnector extends AbstractHasComponentsConnector
  36. implements PostLayoutListener, VisibilityChangeHandler {
  37. private boolean centerAfterLayout = false;
  38. private final List<HandlerRegistration> handlerRegistration = new ArrayList<>();
  39. @Override
  40. protected void init() {
  41. super.init();
  42. handlerRegistration.add(getWidget().addVisibilityChangeHandler(this));
  43. }
  44. @Override
  45. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  46. super.onStateChanged(stateChangeEvent);
  47. getWidget().setHTML(getState().html);
  48. getWidget().popup.setHideOnMouseOut(getState().hideOnMouseOut);
  49. }
  50. @Override
  51. public PopupViewState getState() {
  52. return (PopupViewState) super.getState();
  53. }
  54. @Override
  55. public void updateCaption(ComponentConnector childConnector) {
  56. if (VCaption.isNeeded(childConnector)) {
  57. if (getWidget().popup.captionWrapper != null) {
  58. getWidget().popup.captionWrapper.updateCaption();
  59. } else {
  60. getWidget().popup.captionWrapper = new VCaptionWrapper(
  61. childConnector, getConnection());
  62. getWidget().popup.setWidget(getWidget().popup.captionWrapper);
  63. getWidget().popup.captionWrapper.updateCaption();
  64. }
  65. } else {
  66. if (getWidget().popup.captionWrapper != null) {
  67. getWidget().popup
  68. .setWidget(getWidget().popup.popupComponentWidget);
  69. }
  70. }
  71. }
  72. @Override
  73. public VPopupView getWidget() {
  74. return (VPopupView) super.getWidget();
  75. }
  76. @Override
  77. public void postLayout() {
  78. if (centerAfterLayout) {
  79. centerAfterLayout = false;
  80. getWidget().center();
  81. }
  82. }
  83. @Override
  84. public void onConnectorHierarchyChange(
  85. ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {
  86. // Render the popup if visible and show it.
  87. if (!getChildComponents().isEmpty()) {
  88. getWidget().preparePopup(getWidget().popup);
  89. getWidget().popup.setPopupConnector(getChildComponents().get(0));
  90. final StringBuffer styleBuf = new StringBuffer();
  91. final String primaryName = getWidget().popup.getStylePrimaryName();
  92. styleBuf.append(primaryName);
  93. // Add "animate-in" class back if already present
  94. boolean isAnimatingIn = getWidget().popup.getStyleName()
  95. .contains(VOverlay.ADDITIONAL_CLASSNAME_ANIMATE_IN);
  96. if (isAnimatingIn) {
  97. styleBuf.append(" ");
  98. styleBuf.append(primaryName);
  99. styleBuf.append("-");
  100. styleBuf.append(VOverlay.ADDITIONAL_CLASSNAME_ANIMATE_IN);
  101. }
  102. if (ComponentStateUtil.hasStyles(getState())) {
  103. for (String style : getState().styles) {
  104. styleBuf.append(" ");
  105. styleBuf.append(primaryName);
  106. styleBuf.append("-");
  107. styleBuf.append(style);
  108. }
  109. }
  110. getWidget().popup.setStyleName(styleBuf.toString());
  111. getWidget().showPopup(getWidget().popup);
  112. centerAfterLayout = true;
  113. } else {
  114. // The popup shouldn't be visible, try to hide it.
  115. getWidget().popup.hide();
  116. }
  117. }
  118. @Override
  119. public void onVisibilityChange(VisibilityChangeEvent event) {
  120. getRpcProxy(PopupViewServerRpc.class)
  121. .setPopupVisibility(event.isVisible());
  122. }
  123. }