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