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