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.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
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
102
103 public static class RootView extends CssLayout implements UiFrameworkView {
104
105 public RootView() {
106 setSizeFull();
107 }
108 }
109
110
111
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
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
162 int i = parameter.indexOf(':');
163 if (i != -1) {
164
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
176 int i = fragment.indexOf(':');
177 if (i == -1) {
178 return "";
179 }
180
181 int j = fragment.indexOf(':', i + 1);
182 if (j == -1) {
183 return "";
184 }
185
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 }