View Javadoc

1   /**
2    * This file Copyright (c) 2010-2012 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.dialoglayout;
35  
36  import java.util.Iterator;
37  import java.util.Set;
38  
39  import org.vaadin.rpc.client.ClientSideHandler;
40  import org.vaadin.rpc.client.ClientSideProxy;
41  import org.vaadin.rpc.client.Method;
42  
43  import com.google.gwt.user.client.ui.Composite;
44  import com.google.gwt.user.client.ui.HasWidgets;
45  import com.google.gwt.user.client.ui.Widget;
46  import com.vaadin.terminal.gwt.client.ApplicationConnection;
47  import com.vaadin.terminal.gwt.client.Container;
48  import com.vaadin.terminal.gwt.client.Paintable;
49  import com.vaadin.terminal.gwt.client.RenderSpace;
50  import com.vaadin.terminal.gwt.client.UIDL;
51  import com.vaadin.terminal.gwt.client.VConsole;
52  
53  /**
54   * VBaseDialog.
55   */
56  public class VBaseDialog extends Composite implements Container, ClientSideHandler, HasWidgets, VBaseDialogView.Presenter {
57  
58      private final VBaseDialogView view = createView();
59      
60      private final ClientSideProxy proxy = new ClientSideProxy(this) {{
61          register("addAction", new Method() {
62              @Override
63              public void invoke(String methodName, Object[] params) {
64                  final String name = String.valueOf(params[0]);
65                  final String label = String.valueOf(params[1]);
66                  getView().addAction(name, label);
67              }
68          });
69  
70          register("setDescription", new Method() {
71              @Override
72              public void invoke(String methodName, Object[] params) {
73                  final String description = String.valueOf(params[0]);
74                  getView().setDescription(description);
75              }
76          });
77  
78          register("setCaption", new Method() {
79              @Override
80              public void invoke(String methodName, Object[] params) {
81                  final String caption = String.valueOf(params[0]);
82                  getView().setCaption(caption);
83              }
84          }); 
85          
86          register("setActionLabel", new Method() {
87              @Override
88              public void invoke(String methodName, Object[] params) {
89                  final String actionName = String.valueOf(params[0]);
90                  final String label = String.valueOf(params[1]);
91                  getView().setActionLabel(actionName, label);
92              }
93          }); 
94      }};
95      
96      
97  
98      public VBaseDialog() {
99          initWidget(view.asWidget());
100         view.setPresenter(this);
101     }
102     
103     public VBaseDialogView getView() {
104         return view;
105     }
106     
107     protected VBaseDialogView createView() {
108         return new VBaseDialogViewImpl();
109     }
110     
111     protected ClientSideProxy getProxy() {
112         return proxy;
113     }
114     
115     @Override
116     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
117         if (client.updateComponent(this, uidl, true)) {
118             return;
119         }
120         final Paintable contentPaintable = client.getPaintable(uidl.getChildUIDL(0));
121         final Widget contentWidget = (Widget)contentPaintable;
122         view.setContent(contentWidget);
123         contentPaintable.updateFromUIDL(uidl.getChildUIDL(0), client);
124         proxy.update(this, uidl, client);
125     }
126 
127     @Override
128     public void setHeight(String height) {
129         super.setHeight(height);
130         if (view != null) {
131             view.asWidget().setHeight(height);
132         }
133     }
134     
135     @Override
136     public void setWidth(String width) {
137         super.setWidth(width);
138         view.asWidget().setWidth(width);
139     }
140 
141     @Override
142     public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
143 
144     }
145 
146     @Override
147     public boolean hasChildComponent(Widget component) {
148         return view.getContent() == component;
149     }
150 
151     @Override
152     public void updateCaption(Paintable component, UIDL uidl) {
153         if (uidl.hasAttribute("caption")) {
154             view.setCaption(uidl.getStringAttribute("caption"));
155         }
156     }
157 
158     @Override
159     public boolean requestLayout(Set<Paintable> children) {
160         return false;
161     }
162 
163     @Override
164     public RenderSpace getAllocatedSpace(Widget child) {
165         return new RenderSpace(view.getContentWidth(), view.getContentHeight());
166     }
167 
168     @Override
169     public boolean initWidget(Object[] params) {
170         return false;
171     }
172 
173     @Override
174     public void handleCallFromServer(String method, Object[] params) {
175         VConsole.error("Unknown call from server " + method);
176     }
177 
178     @Override
179     public void add(Widget w) {
180         view.add(w);
181     }
182 
183     @Override
184     public void clear() {
185         view.clear();
186     }
187 
188     @Override
189     public Iterator<Widget> iterator() {
190         return view.iterator();
191     }
192 
193     @Override
194     public boolean remove(Widget w) {
195         return view.remove(w);
196     }
197 
198     @Override
199     public void fireAction(String action) {
200         getProxy().call("fireAction", action);
201         
202     }
203 
204     @Override
205     public void closeDialog() {
206         getProxy().call("closeDialog");
207     }
208 
209 }