View Javadoc

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