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.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
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
104
105 public static class RootView extends CssLayout implements UIComponent {
106
107 public RootView() {
108 setSizeFull();
109 }
110 }
111
112
113
114
115 public interface LocationContext extends UiComponentContext {
116 ContextProperty<BrowserLocation> location();
117 }
118
119
120
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
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
171 int i = parameter.indexOf(':');
172 if (i != -1) {
173
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
185 int i = fragment.indexOf(':');
186 if (i == -1) {
187 return "";
188 }
189
190 int j = fragment.indexOf(':', i + 1);
191 if (j == -1) {
192 return "";
193 }
194
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 }