123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /*
- * Copyright 2000-2016 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.client.ui.popupview;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import com.google.gwt.event.shared.HandlerRegistration;
- import com.vaadin.client.ComponentConnector;
- import com.vaadin.client.ConnectorHierarchyChangeEvent;
- import com.vaadin.client.VCaption;
- import com.vaadin.client.VCaptionWrapper;
- import com.vaadin.client.communication.StateChangeEvent;
- import com.vaadin.client.ui.AbstractHasComponentsConnector;
- import com.vaadin.client.ui.PostLayoutListener;
- import com.vaadin.client.ui.VOverlay;
- import com.vaadin.client.ui.VPopupView;
- import com.vaadin.shared.ui.ComponentStateUtil;
- import com.vaadin.shared.ui.Connect;
- import com.vaadin.shared.ui.popupview.PopupViewServerRpc;
- import com.vaadin.shared.ui.popupview.PopupViewState;
- import com.vaadin.ui.PopupView;
-
- @Connect(PopupView.class)
- public class PopupViewConnector extends AbstractHasComponentsConnector
- implements PostLayoutListener, VisibilityChangeHandler {
-
- private boolean centerAfterLayout = false;
-
- private final List<HandlerRegistration> handlerRegistration = new ArrayList<>();
-
- @Override
- protected void init() {
- super.init();
-
- handlerRegistration.add(getWidget().addVisibilityChangeHandler(this));
- }
-
- @Override
- public void onStateChanged(StateChangeEvent stateChangeEvent) {
- super.onStateChanged(stateChangeEvent);
-
- getWidget().setHTML(getState().html);
- getWidget().popup.setHideOnMouseOut(getState().hideOnMouseOut);
- }
-
- @Override
- public PopupViewState getState() {
- return (PopupViewState) super.getState();
- }
-
- @Override
- public void updateCaption(ComponentConnector childConnector) {
- if (VCaption.isNeeded(childConnector)) {
- if (getWidget().popup.captionWrapper != null) {
- getWidget().popup.captionWrapper.updateCaption();
- } else {
- getWidget().popup.captionWrapper = new VCaptionWrapper(
- childConnector, getConnection());
- getWidget().popup.setWidget(getWidget().popup.captionWrapper);
- getWidget().popup.captionWrapper.updateCaption();
- }
- } else {
- if (getWidget().popup.captionWrapper != null) {
- getWidget().popup
- .setWidget(getWidget().popup.popupComponentWidget);
- }
- }
- }
-
- @Override
- public VPopupView getWidget() {
- return (VPopupView) super.getWidget();
- }
-
- @Override
- public void postLayout() {
- if (centerAfterLayout) {
- centerAfterLayout = false;
- getWidget().center();
- }
- }
-
- @Override
- public void onConnectorHierarchyChange(
- ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {
- // Render the popup if visible and show it.
- if (!getChildComponents().isEmpty()) {
- getWidget().preparePopup(getWidget().popup);
- getWidget().popup.setPopupConnector(getChildComponents().get(0));
-
- final StringBuffer styleBuf = new StringBuffer();
- final String primaryName = getWidget().popup.getStylePrimaryName();
- styleBuf.append(primaryName);
-
- // Add "animate-in" class back if already present
- boolean isAnimatingIn = getWidget().popup.getStyleName()
- .contains(VOverlay.ADDITIONAL_CLASSNAME_ANIMATE_IN);
-
- if (isAnimatingIn) {
- styleBuf.append(" ");
- styleBuf.append(primaryName);
- styleBuf.append("-");
- styleBuf.append(VOverlay.ADDITIONAL_CLASSNAME_ANIMATE_IN);
- }
-
- if (ComponentStateUtil.hasStyles(getState())) {
- for (String style : getState().styles) {
- styleBuf.append(" ");
- styleBuf.append(primaryName);
- styleBuf.append("-");
- styleBuf.append(style);
- }
- }
-
- getWidget().popup.setStyleName(styleBuf.toString());
- getWidget().showPopup(getWidget().popup);
- centerAfterLayout = true;
-
- } else {
- // The popup shouldn't be visible, try to hide it.
- getWidget().popup.hide();
- }
- }
-
- @Override
- public void onVisibilityChange(VisibilityChangeEvent event) {
- getRpcProxy(PopupViewServerRpc.class)
- .setPopupVisibility(event.isVisible());
- }
-
- }
|