View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 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.actionbar.connector;
35  
36  import info.magnolia.ui.vaadin.actionbar.Actionbar;
37  import info.magnolia.ui.vaadin.gwt.client.actionbar.rpc.ActionbarServerRpc;
38  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarSection;
39  import info.magnolia.ui.vaadin.gwt.client.actionbar.widget.ActionbarWidgetView;
40  import info.magnolia.ui.vaadin.gwt.client.actionbar.widget.ActionbarWidgetViewImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Comparator;
45  import java.util.List;
46  
47  import com.google.gwt.core.client.Scheduler;
48  import com.google.gwt.core.client.Scheduler.ScheduledCommand;
49  import com.google.gwt.event.shared.SimpleEventBus;
50  import com.google.gwt.user.client.Window;
51  import com.google.gwt.user.client.ui.Widget;
52  import com.google.web.bindery.event.shared.EventBus;
53  import com.googlecode.mgwt.ui.client.MGWT;
54  import com.vaadin.client.communication.RpcProxy;
55  import com.vaadin.client.communication.StateChangeEvent;
56  import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
57  import com.vaadin.client.ui.AbstractComponentConnector;
58  import com.vaadin.shared.ui.Connect;
59  
60  /**
61   * {@link ActionbarConnector}.
62   */
63  @Connect(Actionbar.class)
64  public class ActionbarConnector extends AbstractComponentConnector implements ActionbarWidgetView.Presenter {
65  
66      private ActionbarWidgetView view;
67  
68      private final EventBus eventBus = new SimpleEventBus();
69  
70      private final ActionbarServerRpc rpc = RpcProxy.create(ActionbarServerRpc.class, this);
71  
72      private final boolean isTablet = !(MGWT.getOsDetection().isDesktop() || Window.Location.getQueryString().indexOf("tablet=true") >= 0);
73  
74      private final StateChangeHandler sectionRearrangementHandler = new StateChangeHandler() {
75          @Override
76          public void onStateChanged(StateChangeEvent stateChangeEvent) {
77              List<ActionbarSection> sections = new ArrayList<ActionbarSection>(getState().sections.values());
78              Collections.sort(sections, new Comparator<ActionbarSection>() {
79                  @Override
80                  public int compare(ActionbarSection o1, ActionbarSection o2) {
81                      Integer idx1 = getState().sectionOrder.indexOf(o1.getName());
82                      Integer idx2 = getState().sectionOrder.indexOf(o2.getName());
83                      return idx1.compareTo(idx2);
84                  }
85              });
86              view.setSections(sections);
87              view.setDisabledActions(getState().disabledActions);
88              view.setVisibleSections(getState().visibleSections);
89          }
90      };
91  
92      private final StateChangeHandler previewChangeHandler = new StateChangeHandler() {
93          @Override
94          public void onStateChanged(StateChangeEvent stateChangeEvent) {
95              Scheduler.get().scheduleDeferred(new ScheduledCommand() {
96                  @Override
97                  public void execute() {
98                      for (String sectionName : getState().sections.keySet()) {
99                          String previewUrl = getResourceUrl(sectionName);
100                         if (previewUrl != null) {
101                             view.setSectionPreview(sectionName, previewUrl);
102                         }
103                     }
104                 }
105             });
106         }
107     };
108 
109     private final StateChangeHandler visibleSectionSetChangeHandler = new StateChangeHandler() {
110         @Override
111         public void onStateChanged(StateChangeEvent stateChangeEvent) {
112             Scheduler.get().scheduleDeferred(new ScheduledCommand() {
113                 @Override
114                 public void execute() {
115                     view.setVisibleSections(getState().visibleSections);
116                 }
117             });
118         }
119     };
120 
121     private final StateChangeHandler enabledActionSetChangeHandler = new StateChangeHandler() {
122         @Override
123         public void onStateChanged(StateChangeEvent stateChangeEvent) {
124             Scheduler.get().scheduleDeferred(new ScheduledCommand() {
125                 @Override
126                 public void execute() {
127                     view.setDisabledActions(getState().disabledActions);
128                 }
129             });
130         }
131     };
132 
133     private final StateChangeHandler collapseChangeHandler = new StateChangeHandler() {
134         @Override
135         public void onStateChanged(StateChangeEvent stateChangeEvent) {
136             view.setOpen(getState().isOpen);
137         }
138     };
139 
140     @Override
141     protected void init() {
142         super.init();
143         addStateChangeHandler(previewChangeHandler);
144         addStateChangeHandler("sections", sectionRearrangementHandler);
145         addStateChangeHandler("visibleSections", visibleSectionSetChangeHandler);
146         addStateChangeHandler("disabledActions", enabledActionSetChangeHandler);
147         addStateChangeHandler("isOpen", collapseChangeHandler);
148         if (isDeviceTablet()) {
149             setOpened(true);
150         }
151     }
152 
153     @Override
154     public Widget getWidget() {
155         return super.getWidget();
156     }
157 
158     @Override
159     protected Widget createWidget() {
160         this.view = new ActionbarWidgetViewImpl(eventBus, this);
161         return this.view.asWidget();
162     }
163 
164     @Override
165     public ActionbarState getState() {
166         return (ActionbarState) super.getState();
167     }
168 
169     @Override
170     public void triggerAction(String actionToken) {
171         rpc.onActionTriggered(actionToken);
172     }
173 
174     @Override
175     public void setOpened(boolean isOpen) {
176         rpc.setOpen(isOpen);
177     }
178 
179     @Override
180     public void forceLayout() {
181         getLayoutManager().setNeedsMeasure(this);
182     }
183 
184     @Override
185     public String getIconResourceURL(String actionName) {
186         return getResourceUrl(actionName);
187     }
188 
189     /**
190      * Determine if device is tablet. Allows option to add a querystring parameter of tablet=true
191      * for testing.
192      *
193      * @return Whether device is tablet.
194      */
195     @Override
196     public boolean isDeviceTablet() {
197         return isTablet;
198     }
199 }