View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.applauncher.connector;
35  
36  import info.magnolia.ui.vaadin.applauncher.AppLauncher;
37  import info.magnolia.ui.vaadin.gwt.client.applauncher.shared.AppGroup;
38  import info.magnolia.ui.vaadin.gwt.client.applauncher.shared.AppTile;
39  import info.magnolia.ui.vaadin.gwt.client.applauncher.widget.AppLauncherView;
40  import info.magnolia.ui.vaadin.gwt.client.applauncher.widget.AppLauncherView.Presenter;
41  import info.magnolia.ui.vaadin.gwt.client.applauncher.widget.AppLauncherViewImpl;
42  
43  import java.util.ArrayList;
44  import java.util.Collections;
45  import java.util.Comparator;
46  import java.util.HashMap;
47  import java.util.Iterator;
48  import java.util.List;
49  import java.util.Map;
50  
51  import com.google.gwt.dom.client.Element;
52  import com.google.gwt.event.shared.HandlerRegistration;
53  import com.google.gwt.user.client.Event;
54  import com.google.gwt.user.client.History;
55  import com.google.gwt.user.client.ui.Widget;
56  import com.google.web.bindery.event.shared.EventBus;
57  import com.google.web.bindery.event.shared.SimpleEventBus;
58  import com.vaadin.client.communication.StateChangeEvent;
59  import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
60  import com.vaadin.client.ui.AbstractComponentConnector;
61  import com.vaadin.client.ui.layout.ElementResizeListener;
62  import com.vaadin.shared.ui.Connect;
63  
64  /**
65   * Client-side connector for {@link AppLauncher} component.
66   */
67  @Connect(AppLauncher.class)
68  public class AppLauncherConnector extends AbstractComponentConnector implements Presenter {
69  
70      private AppLauncherView view;
71  
72      private EventBus eventBus = new SimpleEventBus();
73  
74      private HandlerRegistration eventPreviewHandle;
75  
76      private Map<Element, ElementResizeListener> resizeListeners = new HashMap<Element, ElementResizeListener>();
77  
78      public AppLauncherConnector() {
79  
80          addStateChangeHandler("appGroups", new StateChangeHandler() {
81              @Override
82              public void onStateChanged(StateChangeEvent stateChangeEvent) {
83                  // set groups in specified order
84                  List<AppGroup> groups = new ArrayList<AppGroup>(getState().appGroups.values());
85                  Collections.sort(groups, new GroupComparator(getState().groupsOrder));
86  
87                  // add groups to the view
88                  view.clear();
89                  for (final AppGroup appGroup : groups) {
90                      view.addAppGroup(appGroup);
91                      for (final AppTile tile : appGroup.getAppTiles()) {
92                          view.addAppTile(tile, appGroup);
93                      }
94                  }
95  
96                  updateRunningAppTiles();
97              }
98          });
99  
100         addStateChangeHandler("runningApps", new StateChangeHandler() {
101             @Override
102             public void onStateChanged(StateChangeEvent stateChangeEvent) {
103                 updateRunningAppTiles();
104             }
105         });
106     }
107 
108     @Override
109     protected void init() {
110         super.init();
111         this.eventPreviewHandle = Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
112             @Override
113             public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
114                 int eventCode = event.getTypeInt();
115                 boolean isTouchOrMouse = (eventCode & Event.MOUSEEVENTS) > 0 || (eventCode & Event.TOUCHEVENTS) > 0;
116 
117                 final Element eventTarget = event.getNativeEvent().getEventTarget().cast();
118 
119                 if (!getWidget().getElement().isOrHasChild(eventTarget)) {
120                     return;
121                 }
122 
123                 if ((isTouchOrMouse && eventCode != Event.ONMOUSEOVER && eventCode != Event.ONMOUSEOUT && getConnection().getMessageSender().hasActiveRequest())) {
124                     event.cancel();
125                 }
126             }
127         });
128     }
129 
130     private void updateRunningAppTiles() {
131         for (final AppGroup appGroup : getState().appGroups.values()) {
132             for (final AppTile tile : appGroup.getAppTiles()) {
133                 if (getState().runningApps.contains(tile.getName())) {
134                     view.setAppActive(tile.getName(), true);
135                 } else {
136                     view.setAppActive(tile.getName(), false);
137                 }
138             }
139         }
140     }
141 
142     @Override
143     public Widget getWidget() {
144         return super.getWidget();
145     }
146 
147     @Override
148     protected Widget createWidget() {
149         this.view = new AppLauncherViewImpl(eventBus);
150         this.view.setPresenter(this);
151         return view.asWidget();
152     }
153 
154     @Override
155     public AppLauncherState getState() {
156         return (AppLauncherState) super.getState();
157     }
158 
159     @Override
160     protected AppLauncherState createState() {
161         return new AppLauncherState();
162     }
163 
164     @Override
165     public void onUnregister() {
166         super.onUnregister();
167         eventPreviewHandle.removeHandler();
168         final Iterator<Map.Entry<Element,ElementResizeListener>> it = resizeListeners.entrySet().iterator();
169         while (it.hasNext()) {
170             final Map.Entry<Element,ElementResizeListener> entry = it.next();
171             getLayoutManager().removeElementResizeListener(entry.getKey(), entry.getValue());
172         }
173     }
174 
175     @Override
176     public void activateApp(String appName) {
177         History.newItem("app:" + appName, true);
178     }
179 
180     @Override
181     public void registerElementResizeListener(Element element, ElementResizeListener elementResizeListener) {
182         resizeListeners.put(element, elementResizeListener);
183         getLayoutManager().addElementResizeListener(element, elementResizeListener);
184     }
185 
186     /**
187      * This comparator helps ordering the list of groups according to the specified groupsOrder, yet accounting as well for ordering all permanent groups
188      * before all temporary groups. This ensures accurate relative depth of groups in the applauncher.
189      */
190     static class GroupComparator implements Comparator<AppGroup> {
191 
192         private List<String> groupOrder;
193 
194         public GroupComparator(List<String> groupOrder) {
195             this.groupOrder = groupOrder;
196         }
197 
198         @Override
199         public int compare(AppGroup o1, AppGroup o2) {
200             boolean perm1 = o1.isPermanent();
201             boolean perm2 = o2.isPermanent();
202             if (perm1 == perm2) {
203                 Integer idx1 = groupOrder.indexOf(o1.getName());
204                 Integer idx2 = groupOrder.indexOf(o2.getName());
205                 return idx1.compareTo(idx2);
206             }
207             return perm1 == true ? -1 : 1;
208         }
209     }
210 
211 }