View Javadoc
1   /**
2    * This file Copyright (c) 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.contentapp;
35  
36  import info.magnolia.ui.api.app.SubAppContext;
37  import info.magnolia.ui.api.location.DefaultLocation;
38  import info.magnolia.ui.api.location.Location;
39  import info.magnolia.ui.contentapp.configuration.BrowserDescriptor;
40  import info.magnolia.ui.contentapp.browser.Browser;
41  import info.magnolia.ui.contentapp.browser.context.ActionConfigurationContext;
42  import info.magnolia.ui.framework.UiFrameworkView;
43  import info.magnolia.ui.framework.ViewProvider;
44  import info.magnolia.ui.framework.app.BaseSubApp;
45  
46  import javax.inject.Inject;
47  
48  import org.apache.commons.lang3.StringUtils;
49  
50  import com.vaadin.ui.CssLayout;
51  
52  /**
53   * Browser sub-app.
54   */
55  public class ContentBrowserSubApp extends BaseSubApp<ContentBrowserSubApp.RootView> {
56  
57      private Browser browser;
58  
59      @Inject
60      public ContentBrowserSubApp(SubAppContext subAppContext, ViewProvider viewProvider) {
61          super(subAppContext, viewProvider.create(subAppContext.getSubAppDescriptor().getLabel(), RootView.class));
62      }
63  
64      @Override
65      @SuppressWarnings("unchecked")
66      public RootView start(Location location) {
67          final SubAppContext subAppContext = getSubAppContext();
68          final BrowserDescriptor browserDescriptor = (BrowserDescriptor) subAppContext.getSubAppDescriptor();
69  
70          getView().bindContext(ActionConfigurationContext.class).actions().set(browserDescriptor.getActions());
71          getView().bindDatasourceDefinition(browserDescriptor.getDatasource());
72  
73          this.browser = getView().getViewProvider().create("browser", Browser.class, browserDescriptor);
74          this.browser.setLocation(BrowserLocation.wrap(location));
75  
76          getView().addComponent(browser.asVaadinComponent());
77  
78          return super.start(location);
79      }
80  
81      @Override
82      public void locationChanged(Location location) {
83          super.locationChanged(location);
84          browser.setLocation(BrowserLocation.wrap(location));
85      }
86  
87      @Override
88      public void stop() {
89          getView().destroy();
90      }
91  
92      @Override
93      public String getCaption() {
94          return getSubAppContext().getSubAppDescriptor().getLabel();
95      }
96  
97      @Override
98      public String getIcon(Location location) {
99          return getSubAppContext().getSubAppDescriptor().getIcon();
100     }
101 
102     /**
103      * Root view.
104      */
105     public static class RootView extends CssLayout implements UiFrameworkView {
106 
107         public RootView() {
108             setSizeFull();
109         }
110     }
111 
112     /**
113      * Location used in ContentSubApps. Extends the Default Location by adding fields for the nodePath, viewType and query.
114      */
115     public static class BrowserLocation extends DefaultLocation {
116 
117         private String nodePath;
118         private String viewType;
119         private String query;
120 
121         public BrowserLocation(String appName, String subAppId, String parameter) {
122             super(LOCATION_TYPE_APP, appName, subAppId, parameter);
123 
124             parameter = StringUtils.defaultString(parameter);
125             setNodePath(extractNodePath(parameter));
126             setViewType(extractView(parameter));
127             setQuery(extractQuery(parameter));
128         }
129 
130         public String getNodePath() {
131             return unescapeSpecialCharacters(nodePath);
132         }
133 
134         /**
135          * If the node path is empty, assume root path.
136          */
137         private void setNodePath(String nodePath) {
138             this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : escapeSpecialCharacters(nodePath);
139         }
140 
141         public String getViewType() {
142             return unescapeSpecialCharacters(viewType);
143         }
144 
145         private void setViewType(String viewType) {
146             this.viewType = escapeSpecialCharacters(viewType);
147         }
148 
149         public String getQuery() {
150             return unescapeSpecialCharacters(query);
151         }
152 
153         private void setQuery(String query) {
154             this.query = escapeSpecialCharacters(query);
155         }
156 
157         private String extractNodePath(String parameter) {
158             int i = parameter.indexOf(':');
159             return i != -1 ? parameter.substring(0, i) : parameter;
160         }
161 
162         private String extractView(String parameter) {
163             // first param is path
164             int i = parameter.indexOf(':');
165             if (i != -1) {
166                 // isolate view type parameter, there can be more
167                 int j = parameter.indexOf(':', i + 1);
168                 String view = (j != -1) ? parameter.substring(i + 1, j) : parameter.substring(i + 1);
169                 if (StringUtils.isNotBlank(view)) {
170                     return view;
171                 }
172             }
173             return null;
174         }
175 
176         public static String extractQuery(String fragment) {
177             // nodePath
178             int i = fragment.indexOf(':');
179             if (i == -1) {
180                 return "";
181             }
182             // view
183             int j = fragment.indexOf(':', i + 1);
184             if (j == -1) {
185                 return "";
186             }
187             // query
188             return fragment.substring(j + 1);
189         }
190 
191         protected void updateParameter() {
192             StringBuilder sb = new StringBuilder();
193             sb.append(nodePath);
194             sb.append(":");
195             sb.append(viewType);
196             sb.append(":");
197             sb.append(query);
198             super.setParameter(sb.toString());
199         }
200 
201         public static BrowserLocation wrap(Location location) {
202             return new BrowserLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
203         }
204 
205         public void updateNodePath(String newNodePath) {
206             setNodePath(newNodePath);
207             updateParameter();
208         }
209 
210         public void updateViewType(String newViewType) {
211             setViewType(newViewType);
212             updateParameter();
213         }
214 
215         public void updateQuery(String newQuery) {
216             setQuery(newQuery);
217             updateParameter();
218         }
219 
220     }
221 }