1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
104
105 public static class RootView extends CssLayout implements UiFrameworkView {
106
107 public RootView() {
108 setSizeFull();
109 }
110 }
111
112
113
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
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
164 int i = parameter.indexOf(':');
165 if (i != -1) {
166
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
178 int i = fragment.indexOf(':');
179 if (i == -1) {
180 return "";
181 }
182
183 int j = fragment.indexOf(':', i + 1);
184 if (j == -1) {
185 return "";
186 }
187
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 }