View Javadoc

1   /**
2    * This file Copyright (c) 2010-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.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.user.client.History;
49  import com.google.gwt.user.client.ui.Widget;
50  import com.google.web.bindery.event.shared.EventBus;
51  import com.google.web.bindery.event.shared.SimpleEventBus;
52  import com.vaadin.client.communication.StateChangeEvent;
53  import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
54  import com.vaadin.client.ui.AbstractComponentConnector;
55  import com.vaadin.shared.ui.Connect;
56  
57  /**
58   * Client-side connector for {@link AppLauncher} component.
59   */
60  @Connect(AppLauncher.class)
61  public class AppLauncherConnector extends AbstractComponentConnector {
62  
63      private AppLauncherView view;
64  
65      private EventBus eventBus = new SimpleEventBus();
66  
67      public AppLauncherConnector() {
68  
69          addStateChangeHandler("appGroups", new StateChangeHandler() {
70              @Override
71              public void onStateChanged(StateChangeEvent stateChangeEvent) {
72                  // set groups in specified order
73                  List<AppGroup> groups = new ArrayList<AppGroup>(getState().appGroups.values());
74                  Collections.sort(groups, new GroupComparator(getState().groupsOrder));
75  
76                  // add groups to the view
77                  view.clear();
78                  for (final AppGroup appGroup : groups) {
79                      view.addAppGroup(appGroup);
80                      for (final AppTile tile : appGroup.getAppTiles()) {
81                          view.addAppTile(tile, appGroup);
82                      }
83                  }
84  
85                  updateRunningAppTiles();
86              }
87          });
88  
89          addStateChangeHandler("runningApps", new StateChangeHandler() {
90              @Override
91              public void onStateChanged(StateChangeEvent stateChangeEvent) {
92                  updateRunningAppTiles();
93              }
94          });
95      }
96  
97      private void updateRunningAppTiles() {
98          for (final AppGroup appGroup : getState().appGroups.values()) {
99              for (final AppTile tile : appGroup.getAppTiles()) {
100                 if (getState().runningApps.contains(tile.getName())) {
101                     view.setAppActive(tile.getName(), true);
102                 } else {
103                     view.setAppActive(tile.getName(), false);
104                 }
105 
106             }
107         }
108     }
109 
110     @Override
111     public Widget getWidget() {
112         return super.getWidget();
113     }
114 
115     @Override
116     protected Widget createWidget() {
117         this.view = new AppLauncherViewImpl(eventBus);
118         this.view.setPresenter(new Presenter() {
119             @Override
120             public void activateApp(String appName) {
121                 History.newItem("app:" + appName, true);
122             }
123         });
124         return view.asWidget();
125     }
126 
127     @Override
128     public AppLauncherState getState() {
129         return (AppLauncherState) super.getState();
130     }
131 
132     @Override
133     protected AppLauncherState createState() {
134         return new AppLauncherState();
135     }
136 
137     /**
138      * This comparator helps ordering the list of groups according to the specified groupsOrder, yet accounting as well for ordering all permanent groups
139      * before all temporary groups. This ensures accurate relative depth of groups in the applauncher.
140      */
141     static class GroupComparator implements Comparator<AppGroup> {
142 
143         private List<String> groupOrder;
144 
145         public GroupComparator(List<String> groupOrder) {
146             this.groupOrder = groupOrder;
147         }
148 
149         @Override
150         public int compare(AppGroup o1, AppGroup o2) {
151             boolean perm1 = o1.isPermanent();
152             boolean perm2 = o2.isPermanent();
153             if (perm1 == perm2) {
154                 Integer idx1 = groupOrder.indexOf(o1.getName());
155                 Integer idx2 = groupOrder.indexOf(o2.getName());
156                 return idx1.compareTo(idx2);
157             }
158             return perm1 == true ? -1 : 1;
159         }
160     }
161 
162 }