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.AppContext;
37  import info.magnolia.ui.api.app.SubApp;
38  import info.magnolia.ui.api.app.SubAppContext;
39  import info.magnolia.ui.api.location.Location;
40  import info.magnolia.ui.api.view.View;
41  import info.magnolia.ui.framework.UiFrameworkView;
42  import info.magnolia.ui.framework.ioc.UiContextReference;
43  
44  import org.apache.commons.lang3.StringUtils;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  /**
49   * Basic implementation of a subApp with default behavior suitable for most sub apps.
50   *
51   * @see info.magnolia.ui.api.app.SubApp
52   *
53   * @param <V> {@link View} implementation used by subApp.
54   */
55  public class BaseSubApp<V extends View> implements SubApp, UiFrameworkView {
56  
57      private static final Logger log = LoggerFactory.getLogger(BaseSubApp.class);
58  
59      private final SubAppContext subAppContext;
60      private final V view;
61  
62      protected BaseSubApp(final SubAppContext subAppContext, final V view) {
63          if (subAppContext == null || view == null) {
64              throw new IllegalArgumentException("Constructor does not allow for null args. Found SubAppContext = " + subAppContext + ", View = " + view);
65          }
66          this.subAppContext = subAppContext;
67          this.view = view;
68      }
69  
70      @Override
71      public V start(Location location) {
72          onSubAppStart();
73          return view;
74      }
75  
76      @Override
77      public void stop() {
78          onSubAppStop();
79      }
80  
81      @Override
82      public void locationChanged(Location location) {
83      }
84  
85      /**
86       * This method is being called by the AppController when iterating over opened subApps.
87       * The subApp itself decides whether it supports the current location based on parameters or
88       * whether the appController should launch a new instance of the subApp.
89       */
90  
91      @Override
92      public boolean supportsLocation(Location location) {
93          return true;
94      }
95  
96      @Override
97      public UiContextReference getCurrentViewReference() {
98          return UiContextReference.ofSubApp(getSubAppContext());
99      }
100 
101     /**
102      * This hook-up method is called on {@link #start(info.magnolia.ui.api.location.Location)} and enables subclasses to perform additional work before the view is displayed.
103      * The default implementation does nothing.
104      */
105     protected void onSubAppStart() {
106     }
107 
108     /**
109      * This hook-up method is called on {@link #stop()} and enables subclasses to perform additional work when stopping the subApp.
110      * The default implementation does nothing.
111      */
112     protected void onSubAppStop() {
113     }
114 
115     public SubAppContext getSubAppContext() {
116         return subAppContext;
117     }
118 
119     @Override
120     public String getSubAppId() {
121         return subAppContext.getSubAppId();
122     }
123 
124     @Override
125     public V getView() {
126         return view;
127     }
128 
129     public AppContext getAppContext() {
130         return subAppContext.getAppContext();
131     }
132 
133     /**
134      * This method will try to determine the current sub app caption, the one usually displayed in the tab where the subapp opens.
135      *
136      * @return the configured label for this subapp. If no label is found in the subapp configuration, it will try to use the label from the parent app.
137      *         If the latter is missing too, it will return an empty string.
138      */
139     @Override
140     public String getCaption() {
141         String label = subAppContext.getSubAppDescriptor().getLabel();
142         if (StringUtils.isNotBlank(label)) {
143             return label;
144         }
145         label = subAppContext.getAppContext().getLabel();
146         if (StringUtils.isNotBlank(label)) {
147             return label;
148         }
149         log.warn("No label could be found for sub app [{}] in app [{}]", subAppContext.getSubAppDescriptor().getName(), subAppContext.getAppContext().getName());
150         return "";
151     }
152 
153     protected Location getCurrentLocation() {
154         return getSubAppContext().getLocation();
155     }
156 
157     @Override
158     public boolean isCloseable() {
159         return subAppContext.getSubAppDescriptor().isClosable();
160     }
161 
162     @Override
163     public String getIcon(Location location) {
164         return subAppContext.getSubAppDescriptor().getIcon();
165     }
166 }