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