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.pagebar;
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.pagebar.definition.PageBarDefinition;
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.contentapp.detail.DetailView;
46  import info.magnolia.ui.vaadin.editor.pagebar.PageBarView;
47  
48  import java.util.ArrayList;
49  import java.util.List;
50  import java.util.Map;
51  
52  import javax.inject.Inject;
53  
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  /**
58   * Presenter for the page bar displayed on top of the page editor. Acts as container for {@link Extension}s defined under
59   * the pageBar.
60   */
61  public class PageBarPresenter implements ExtensionContainer, PageBarView.Listener {
62  
63      private static final Logger log = LoggerFactory.getLogger(PageBarPresenter.class);
64  
65      private final PageBarView view;
66      private final SubAppContext subAppContext;
67      private final ExtensionFactory extensionFactory;
68  
69      private List<Extension> extensions = new ArrayList<Extension>();
70  
71      @Inject
72      public PageBarPresenter(PageBarView view, SubAppContext subAppContext, ExtensionFactory extensionFactory) {
73          this.view = view;
74          this.subAppContext = subAppContext;
75          this.extensionFactory = extensionFactory;
76      }
77  
78      @Override
79      public PageBarView start(DetailLocation location) {
80          SubAppDescriptor subAppDescriptor = subAppContext.getSubAppDescriptor();
81  
82          if (subAppDescriptor instanceof PagesEditorSubAppDescriptor) {
83              PageBarDefinition definition =  ((PagesEditorSubAppDescriptor) subAppDescriptor).getPageBar();
84              if (definition != null) {
85                  Map<String, ExtensionDefinition> extensionDefinitions = definition.getExtensions();
86                  extensions = extensionFactory.createExtensions(extensionDefinitions);
87              } else {
88                  log.error("No pageBar definition defined for pages detail app, no extensions will be loaded.");
89              }
90          } else {
91              log.error("Expected an instance of {} but got {}. No extensions will be loaded.", PagesEditorSubAppDescriptor.class.getSimpleName(), subAppDescriptor.getClass().getName());
92          }
93  
94          for (Extension extension : extensions) {
95              view.addPageBarComponent(extension.start(location));
96          }
97          view.setListener(this);
98  
99          togglePreviewMode(location);
100         return view;
101     }
102 
103     public void setPageName(String caption, String nodePath) {
104         String pageName = caption.toUpperCase() + "  -  " + nodePath;
105         view.setPageName(pageName);
106     }
107 
108     public void setPageName(String pageName) {
109         view.setPageName(pageName);
110     }
111 
112     private void togglePreviewMode(DetailLocation location) {
113         boolean isPreview = DetailView.ViewType.VIEW.equals(location.getViewType());
114         view.togglePreviewMode(isPreview);
115     }
116 
117     public PageBarView getView() {
118         return view;
119     }
120 
121     @Override
122     public void onLocationUpdate(DetailLocation location) {
123         for (Extension extension : extensions) {
124             extension.onLocationUpdate(location);
125         }
126         togglePreviewMode(location);
127     }
128 
129     @Override
130     public void deactivateExtensions() {
131         for (Extension extension : extensions) {
132             extension.deactivate();
133         }
134     }
135 
136     @Override
137     public void stop() {
138         for (Extension extension : extensions) {
139             extension.stop();
140         }
141     }
142 }