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 static java.util.Comparator.comparing;
37  import static java.util.stream.Collectors.toList;
38  
39  import info.magnolia.ui.vaadin.applauncher.AppLauncher;
40  import info.magnolia.ui.vaadin.gwt.client.applauncher.rpc.AppLauncherServerRpc;
41  import info.magnolia.ui.vaadin.gwt.client.applauncher.shared.AppGroup;
42  import info.magnolia.ui.vaadin.gwt.client.applauncher.shared.AppTile;
43  import info.magnolia.ui.vaadin.gwt.client.applauncher.widget.AppLauncherView;
44  import info.magnolia.ui.vaadin.gwt.client.applauncher.widget.AppLauncherView.Presenter;
45  import info.magnolia.ui.vaadin.gwt.client.applauncher.widget.AppLauncherViewImpl;
46  
47  import java.util.HashMap;
48  import java.util.Iterator;
49  import java.util.List;
50  import java.util.Map;
51  
52  import com.google.gwt.dom.client.Element;
53  import com.google.gwt.event.shared.HandlerRegistration;
54  import com.google.gwt.user.client.Event;
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 = getState().appGroups.values()
85                          .stream()
86                          .sorted(comparing(AppGroup::isPermanent).thenComparing((AppGroup group) -> getState().groupsOrder.indexOf(group.getName())))
87                          .collect(toList());
88  
89                  // add groups to the view
90                  view.clear();
91                  for (final AppGroup appGroup : groups) {
92                      view.addAppGroup(appGroup);
93                      for (final AppTile tile : appGroup.getAppTiles()) {
94                          view.addAppTile(tile, appGroup);
95                      }
96                  }
97  
98                  updateRunningAppTiles();
99              }
100         });
101 
102         addStateChangeHandler("runningApps", new StateChangeHandler() {
103             @Override
104             public void onStateChanged(StateChangeEvent stateChangeEvent) {
105                 updateRunningAppTiles();
106             }
107         });
108     }
109 
110     @Override
111     protected void init() {
112         super.init();
113         this.eventPreviewHandle = Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
114             @Override
115             public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
116                 int eventCode = event.getTypeInt();
117                 boolean isTouchOrMouse = (eventCode & Event.MOUSEEVENTS) > 0 || (eventCode & Event.TOUCHEVENTS) > 0;
118 
119                 final Element eventTarget = event.getNativeEvent().getEventTarget().cast();
120 
121                 if (!getWidget().getElement().isOrHasChild(eventTarget)) {
122                     return;
123                 }
124 
125                 if ((isTouchOrMouse && eventCode != Event.ONMOUSEOVER && eventCode != Event.ONMOUSEOUT && getConnection().getMessageSender().hasActiveRequest())) {
126                     event.cancel();
127                 }
128             }
129         });
130     }
131 
132     private void updateRunningAppTiles() {
133         for (final AppGroup appGroup : getState().appGroups.values()) {
134             for (final AppTile tile : appGroup.getAppTiles()) {
135                 if (getState().runningApps.contains(tile.getName())) {
136                     view.setAppActive(tile.getName(), true);
137                 } else {
138                     view.setAppActive(tile.getName(), false);
139                 }
140             }
141         }
142     }
143 
144     @Override
145     public Widget getWidget() {
146         return super.getWidget();
147     }
148 
149     @Override
150     protected Widget createWidget() {
151         this.view = new AppLauncherViewImpl(eventBus, getConnection()::getIcon);
152         this.view.setPresenter(this);
153         return view.asWidget();
154     }
155 
156     @Override
157     public AppLauncherState getState() {
158         return (AppLauncherState) super.getState();
159     }
160 
161     @Override
162     protected AppLauncherState createState() {
163         return new AppLauncherState();
164     }
165 
166     @Override
167     public void onUnregister() {
168         super.onUnregister();
169         eventPreviewHandle.removeHandler();
170         final Iterator<Map.Entry<Element,ElementResizeListener>> it = resizeListeners.entrySet().iterator();
171         while (it.hasNext()) {
172             final Map.Entry<Element,ElementResizeListener> entry = it.next();
173             getLayoutManager().removeElementResizeListener(entry.getKey(), entry.getValue());
174         }
175     }
176 
177     @Override
178     public void activateApp(String appName) {
179         getRpcProxy(AppLauncherServerRpc.class).openApp(appName);
180     }
181 
182     @Override
183     public void registerElementResizeListener(Element element, ElementResizeListener elementResizeListener) {
184         resizeListeners.put(element, elementResizeListener);
185         getLayoutManager().addElementResizeListener(element, elementResizeListener);
186     }
187 }