View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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          addStateChangeHandler("appGroups", new StateChangeHandler() {
69              @Override
70              public void onStateChanged(StateChangeEvent stateChangeEvent) {
71              }
72          });
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 Comparator<AppGroup>() {
80                      @Override
81                      public int compare(AppGroup o1, AppGroup o2) {
82                          Integer idx1 = getState().groupsOrder.indexOf(o1.getName());
83                          Integer idx2 = getState().groupsOrder.indexOf(o2.getName());
84                          return idx1.compareTo(idx2);
85                      }
86                  });
87  
88                  // add groups to the view
89                  view.clear();
90                  for (final AppGroup appGroup : groups) {
91                      view.addAppGroup(appGroup);
92                      for (final AppTile tile : appGroup.getAppTiles()) {
93                          view.addAppTile(tile, appGroup);
94                      }
95                  }
96  
97                  updateRunningAppTiles();
98              }
99          });
100 
101         addStateChangeHandler("runningApps", new StateChangeHandler() {
102             @Override
103             public void onStateChanged(StateChangeEvent stateChangeEvent) {
104                 updateRunningAppTiles();
105             }
106         });
107     }
108 
109     private void updateRunningAppTiles() {
110         for (final AppGroup appGroup : getState().appGroups.values()) {
111             for (final AppTile tile : appGroup.getAppTiles()) {
112                 if (getState().runningApps.contains(tile.getName())) {
113                     view.setAppActive(tile.getName(), true);
114                 } else {
115                     view.setAppActive(tile.getName(), false);
116                 }
117 
118             }
119         }
120     }
121 
122     @Override
123     public Widget getWidget() {
124         return super.getWidget();
125     }
126 
127     @Override
128     protected Widget createWidget() {
129         this.view = new AppLauncherViewImpl(eventBus);
130         this.view.setPresenter(new Presenter() {
131             @Override
132             public void activateApp(String appName) {
133                 History.newItem("app:" + appName, true);
134             }
135         });
136         return view.asWidget();
137     }
138 
139     @Override
140     public AppLauncherState getState() {
141         return (AppLauncherState) super.getState();
142     }
143 
144     @Override
145     protected AppLauncherState createState() {
146         return new AppLauncherState();
147     }
148 
149 }