View Javadoc
1   /**
2    * This file Copyright (c) 2014-2016 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.pages.app.editor.statusbar;
35  
36  import info.magnolia.pages.app.editor.PagesEditorSubAppDescriptor;
37  import info.magnolia.pages.app.editor.extension.Extension;
38  import info.magnolia.pages.app.editor.extension.ExtensionContainer;
39  import info.magnolia.pages.app.editor.extension.ExtensionFactory;
40  import info.magnolia.pages.app.editor.extension.definition.ExtensionDefinition;
41  import info.magnolia.pages.app.editor.statusbar.definition.StatusBarDefinition;
42  import info.magnolia.ui.api.app.SubAppContext;
43  import info.magnolia.ui.api.app.SubAppDescriptor;
44  import info.magnolia.ui.contentapp.detail.DetailLocation;
45  import info.magnolia.ui.workbench.StatusBarView;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.Map;
50  
51  import javax.inject.Inject;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  import com.vaadin.server.Sizeable;
57  import com.vaadin.ui.Alignment;
58  import com.vaadin.ui.Component;
59  
60  /**
61   * A presenter class for the {@link StatusBarView} displayed at the bottom of the page editor. Takes care of loading and
62   * displaying the components inside the status bar.
63   */
64  public class StatusBarPresenter implements ExtensionContainer {
65  
66      private static final Logger log = LoggerFactory.getLogger(StatusBarPresenter.class);
67  
68      private StatusBarView view;
69      private SubAppContext subAppContext;
70      private final ExtensionFactory extensionFactory;
71  
72      private List<Extension> extensions = new ArrayList<Extension>();
73  
74      @Inject
75      public StatusBarPresenter(final StatusBarView view, SubAppContext subAppContext, ExtensionFactory extensionFactory) {
76          this.view = view;
77          this.subAppContext = subAppContext;
78          this.extensionFactory = extensionFactory;
79  
80          view.asVaadinComponent().setHeight(24, Sizeable.Unit.PIXELS);
81      }
82  
83      @Override
84      public StatusBarView start(DetailLocation location) {
85          SubAppDescriptor subAppDescriptor = subAppContext.getSubAppDescriptor();
86  
87          if (subAppDescriptor instanceof PagesEditorSubAppDescriptor) {
88              StatusBarDefinition definition = ((PagesEditorSubAppDescriptor) subAppDescriptor).getStatusBar();
89              if (definition != null) {
90                  Map<String, ExtensionDefinition> extensionDefinitions = definition.getExtensions();
91                  extensions = extensionFactory.createExtensions(extensionDefinitions);
92              }
93              else {
94                  log.error("No statusBar definition defined for pages detail app, no extensions will be loaded.");
95              }
96          } else {
97              log.error("Expected an instance of {} but got {}. No extensions will be loaded.", PagesEditorSubAppDescriptor.class.getSimpleName(), subAppDescriptor.getClass().getName());
98          }
99          for (Extension extension : extensions) {
100             Component component = extension.start(location).asVaadinComponent();
101             // give auto width and fixed margin to extensions in statusbar
102             component.addStyleName("statusbar-extension");
103             view.addComponent(component, Alignment.MIDDLE_CENTER);
104         }
105         return view;
106     }
107 
108     @Override
109     public void onLocationUpdate(DetailLocation location) {
110         for (Extension extension : extensions) {
111             extension.onLocationUpdate(location);
112         }
113     }
114 
115     @Override
116     public void deactivateExtensions() {
117         for (Extension extension : extensions) {
118             extension.deactivate();
119         }
120     }
121 
122     @Override
123     public void stop() {
124         for (Extension extension : extensions) {
125             extension.stop();
126         }
127     }
128 }