View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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      }
91  
92      /**
93       * Calculates and sets the max height of form view.
94       */
95      @Override
96      public void onResize(ResizeEvent event) {
97          doResize();
98      }
99  
100     // 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.
101     private static int LIGHT_DIALOG_BOTTOM_MARGIN = (40-2);
102 
103     private void doResize() {
104         Widget content = getContent().getWidget();
105         if (content instanceof FormViewImpl) {
106             FormViewImpl formView = (FormViewImpl) content;
107             Element element = view.asWidget().getElement();
108             NodeList<Node> childNodes = element.getChild(0).getChildNodes();
109             int footerHeight = 0, headerHeight = 0, marginHeight = 0;
110             List<String> marginElements = Arrays.asList("dialog-description", "dialog-error", "dialog-content", "dialog-footer");
111             for (int i = 0; i < childNodes.getLength(); i++) {
112                 Node item = childNodes.getItem(i);
113                 if (item.getNodeType() == Node.ELEMENT_NODE) {
114                     Element child = (Element) item;
115                     if (child.getClassName().equalsIgnoreCase("dialog-footer")) {
116                         footerHeight = child.getOffsetHeight();
117                     } else if (child.getClassName().isEmpty()) {
118                         headerHeight = child.getOffsetHeight();
119                     }
120                     if (marginElements.contains(child.getClassName())) {
121                         marginHeight += 2;
122                     }
123                 }
124             }
125             int topMargin = element.getOffsetTop();
126             int bottomMargin = 0;
127             if ("light".equals(getState().modalityLevel)){
128                 bottomMargin = LIGHT_DIALOG_BOTTOM_MARGIN;
129             }
130             formView.setMaxHeight(view.asWidget().getElement().getOffsetHeight() - footerHeight - headerHeight - marginHeight - topMargin - bottomMargin);
131         }
132     }
133 }