View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.framework.app;
35  
36  import info.magnolia.ui.api.app.AppView;
37  import info.magnolia.ui.api.view.View;
38  import info.magnolia.ui.framework.overlay.ViewAdapter;
39  import info.magnolia.ui.vaadin.tabsheet.MagnoliaTab;
40  import info.magnolia.ui.vaadin.tabsheet.MagnoliaTabSheet;
41  
42  import com.vaadin.server.ClientConnector.AttachListener;
43  import com.vaadin.server.KeyMapper;
44  import com.vaadin.server.Page;
45  import com.vaadin.server.ThemeResource;
46  import com.vaadin.ui.Component;
47  
48  /**
49   * View used to give all apps a uniform look-and-feel.
50   *
51   * @see info.magnolia.ui.api.app.AppView
52   */
53  public class DefaultAppView implements AppView {
54  
55      private Listener listener;
56  
57      private KeyMapper<MagnoliaTab> mapper = new KeyMapper<MagnoliaTab>();
58  
59      private final MagnoliaTabSheet tabsheet = new MagnoliaTabSheet() {
60  
61          @Override
62          public void setActiveTab(MagnoliaTab tab) {
63              super.setActiveTab(tab);
64              String key = mapper.key(tab);
65              listener.onFocus(key);
66          }
67  
68          @Override
69          public void closeTab(MagnoliaTab tab) {
70              super.closeTab(tab);
71              String key = mapper.key(tab);
72              listener.onClose(key);
73              mapper.remove(tab);
74          }
75      };
76  
77      public DefaultAppView() {
78          super();
79          tabsheet.setSizeFull();
80          tabsheet.addStyleName("app");
81      }
82  
83      @Override
84      public String addSubAppView(View view, String caption, boolean closable) {
85          return addSubAppView(view, caption, "", closable);
86      }
87  
88      @Override
89      public String addSubAppView(View view, String caption, String icon, boolean closable) {
90          final MagnoliaTab tab = tabsheet.addTab(caption, view != null ? view.asVaadinComponent() : null);
91          tab.setIcon(icon);
92          tab.setClosable(closable);
93          if (tabsheet.getActiveTab() != tab) {
94              tabsheet.setActiveTab(tab);
95          }
96          String key = mapper.key(tab);
97          return key;
98      }
99  
100     @Override
101     public void setActiveSubAppView(String instanceId) {
102         tabsheet.setActiveTab(mapper.get(instanceId));
103     }
104 
105     @Override
106     public String getActiveSubAppView() {
107         return mapper.key(tabsheet.getActiveTab());
108     }
109 
110     @Override
111     public void updateCaption(String instanceId, String caption) {
112         mapper.get(instanceId).setCaption(caption);
113     }
114 
115     @Override
116     public void setTheme(String themeName) {
117         String stylename = String.format("app-%s", themeName);
118         final String themeUrl = String.format("../%s/styles.css", themeName);
119 
120         final Component vaadinComponent = asVaadinComponent();
121         vaadinComponent.addStyleName(stylename);
122         final ThemeResource res = new ThemeResource(themeUrl);
123 
124         if (vaadinComponent.isAttached()) {
125             Page.getCurrent().getStyles().add(res);
126         } else {
127             vaadinComponent.addAttachListener((AttachListener) event -> Page.getCurrent().getStyles().add(res));
128         }
129     }
130 
131     @Override
132     public void setListener(Listener listener) {
133         this.listener = listener;
134     }
135 
136     @Override
137     public MagnoliaTabSheet asVaadinComponent() {
138         return tabsheet;
139     }
140 
141     @Override
142     public View getSubAppViewContainer(final String instanceId) {
143         return new ViewAdapter(mapper.get(instanceId));
144     }
145 
146     @Override
147     public void closeSubAppView(String instanceId) {
148         tabsheet.closeTabFromServer(mapper.get(instanceId));
149     }
150 
151     @Override
152     public void setAppLogo(String logo) {
153         setAppLogo(logo, "");
154     }
155 
156     @Override
157     public void setAppLogo(String logo, String bgcolor) {
158         tabsheet.setLogo(logo, bgcolor);
159     }
160 
161     @Override
162     public void setAppName(String name) {
163         tabsheet.setName(name);
164     }
165 
166 }