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.UIComponent;
37  import info.magnolia.ui.api.app.AppContext;
38  import info.magnolia.ui.api.app.SubApp;
39  import info.magnolia.ui.api.app.SubAppContext;
40  import info.magnolia.ui.api.location.Location;
41  import info.magnolia.ui.api.view.View;
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, UIComponent {
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 StringUtils.equals(location.getAppName(), getAppContext().getAppDescriptor().getName()) &&
94                  StringUtils.equals(location.getSubAppId(), getSubAppId());
95      }
96  
97      @Override
98      public UiContextReference getCurrentViewReference() {
99          return UiContextReference.ofSubApp(getSubAppContext());
100     }
101 
102     /**
103      * 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.
104      * The default implementation does nothing.
105      */
106     protected void onSubAppStart() {
107     }
108 
109     /**
110      * This hook-up method is called on {@link #stop()} and enables subclasses to perform additional work when stopping the subApp.
111      * The default implementation does nothing.
112      */
113     protected void onSubAppStop() {
114     }
115 
116     public SubAppContext getSubAppContext() {
117         return subAppContext;
118     }
119 
120     @Override
121     public String getSubAppId() {
122         return subAppContext.getSubAppId();
123     }
124 
125     @Override
126     public V getView() {
127         return view;
128     }
129 
130     public AppContext getAppContext() {
131         return subAppContext.getAppContext();
132     }
133 
134     /**
135      * This method will try to determine the current sub app caption, the one usually displayed in the tab where the subapp opens.
136      *
137      * @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.
138      *         If the latter is missing too, it will return an empty string.
139      */
140     @Override
141     public String getCaption() {
142         String label = subAppContext.getSubAppDescriptor().getLabel();
143         if (StringUtils.isNotBlank(label)) {
144             return label;
145         }
146         label = subAppContext.getAppContext().getLabel();
147         if (StringUtils.isNotBlank(label)) {
148             return label;
149         }
150         log.warn("No label could be found for sub app [{}] in app [{}]", subAppContext.getSubAppDescriptor().getName(), subAppContext.getAppContext().getName());
151         return "";
152     }
153 
154     protected Location getCurrentLocation() {
155         return getSubAppContext().getLocation();
156     }
157 
158     @Override
159     public boolean isCloseable() {
160         return subAppContext.getSubAppDescriptor().isClosable();
161     }
162 
163     @Override
164     public String getIcon(Location location) {
165         return subAppContext.getSubAppDescriptor().getIcon();
166     }
167 
168     @Override
169     public void destroy() {
170     }
171 }