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