1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
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
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
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 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 }