View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.dialog.connector;
35  
36  import info.magnolia.ui.vaadin.dialog.FormDialog;
37  import info.magnolia.ui.vaadin.gwt.client.dialog.widget.BaseDialogView;
38  import info.magnolia.ui.vaadin.gwt.client.form.widget.FormViewImpl;
39  
40  import java.util.Arrays;
41  import java.util.List;
42  
43  import com.google.gwt.dom.client.Node;
44  import com.google.gwt.dom.client.NodeList;
45  import com.google.gwt.event.logical.shared.ResizeEvent;
46  import com.google.gwt.event.logical.shared.ResizeHandler;
47  import com.google.gwt.event.shared.HandlerRegistration;
48  import com.google.gwt.user.client.Element;
49  import com.google.gwt.user.client.Window;
50  import com.google.gwt.user.client.ui.Widget;
51  import com.vaadin.client.ui.layout.ElementResizeEvent;
52  import com.vaadin.client.ui.layout.ElementResizeListener;
53  import com.vaadin.shared.ui.Connect;
54  
55  /**
56   * DialogContainingForm assumes that content of dialog is
57   * FormView. This connector will set the height of form view
58   * based on how much space this dialog can provide to the form.
59   */
60  @Connect(FormDialog.class)
61  public class DialogContainingFormConnector extends BaseDialogConnector implements ResizeHandler {
62  
63      private BaseDialogView view;
64      private HandlerRegistration registration;
65  
66      @Override
67      protected BaseDialogView createView() {
68          this.view = super.createView();
69          return this.view;
70      }
71  
72      @Override
73      protected void init() {
74          super.init();
75          getLayoutManager().addElementResizeListener(getWidget().getElement(), listener);
76          registration = Window.addResizeHandler(this);
77      }
78  
79      private final ElementResizeListener listener = new ElementResizeListener() {
80          @Override
81          public void onElementResize(ElementResizeEvent e) {
82              doResize();
83          }
84      };
85  
86      @Override
87      public void onUnregister() {
88          registration.removeHandler();
89          getLayoutManager().removeElementResizeListener(getWidget().getElement(), listener);
90          if (getContent() != null) {
91              getLayoutManager().removeElementResizeListener(getContent().getWidget().getElement(), listener);
92          }
93      }
94  
95      /**
96       * Calculates and sets the max height of form view.
97       */
98      @Override
99      public void onResize(ResizeEvent event) {
100         doResize();
101     }
102 
103     @Override
104     public void setDescriptionVisibility(boolean isVisible) {
105         super.setDescriptionVisibility(isVisible);
106         doResize();
107     }
108 
109     @Override
110     protected void updateContent() {
111         getLayoutManager().removeElementResizeListener(getContent().getWidget().getElement(), listener);
112         super.updateContent();
113         getLayoutManager().addElementResizeListener(getContent().getWidget().getElement(), listener);
114     }
115 
116     // The light dialog should have a bottom margin equivalent to the UI bottom margin, which would be 40. Subtracted 2 so that it lines up nicely in practice.
117     private static int LIGHT_DIALOG_BOTTOM_MARGIN = (40-2);
118 
119     private void doResize() {
120         Widget content = getContent().getWidget();
121         if (content instanceof FormViewImpl) {
122             FormViewImpl/../../../../../info/magnolia/ui/vaadin/gwt/client/form/widget/FormViewImpl.html#FormViewImpl">FormViewImpl formView = (FormViewImpl) content;
123             Element element = view.asWidget().getElement();
124             NodeList<Node> childNodes = element.getChild(0).getChildNodes();
125             int footerHeight = 0, headerHeight = 0, marginHeight = 0;
126             List<String> marginElements = Arrays.asList("dialog-description", "dialog-error", "dialog-content", "dialog-footer");
127             for (int i = 0; i < childNodes.getLength(); i++) {
128                 Node item = childNodes.getItem(i);
129                 if (item.getNodeType() == Node.ELEMENT_NODE) {
130                     Element child = (Element) item;
131                     if (child.getClassName().equalsIgnoreCase("dialog-footer")) {
132                         footerHeight = child.getOffsetHeight();
133                     } else if (child.getClassName().isEmpty()) {
134                         headerHeight = child.getOffsetHeight();
135                     }
136                     if (marginElements.contains(child.getClassName())) {
137                         marginHeight += 2;
138                     }
139                 }
140             }
141             int topMargin = element.getOffsetTop();
142             int bottomMargin = 0;
143             if ("light".equals(getState().modalityLevel)){
144                 bottomMargin = LIGHT_DIALOG_BOTTOM_MARGIN;
145             }
146             formView.setMaxHeight(view.asWidget().getElement().getOffsetHeight() - footerHeight - headerHeight - marginHeight - topMargin - bottomMargin);
147         }
148     }
149 }