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