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.detail;
35
36 import info.magnolia.event.EventBus;
37 import info.magnolia.i18nsystem.SimpleTranslator;
38 import info.magnolia.ui.api.app.SubAppContext;
39 import info.magnolia.ui.api.event.AdmincentralEventBus;
40 import info.magnolia.ui.api.event.ContentChangedEvent;
41 import info.magnolia.ui.api.location.Location;
42 import info.magnolia.ui.api.view.View;
43 import info.magnolia.ui.contentapp.ContentSubAppView;
44 import info.magnolia.ui.framework.app.BaseSubApp;
45 import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
46
47 import javax.inject.Inject;
48 import javax.inject.Named;
49
50 import org.apache.commons.lang3.StringUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class DetailSubApp extends BaseSubApp<ContentSubAppView> {
72
73 private static final Logger log = LoggerFactory.getLogger(DetailSubApp.class);
74
75 private final DetailEditorPresenter presenter;
76 private final EventBus adminCentralEventBus;
77 private final SimpleTranslator i18n;
78
79 private Object itemId;
80
81 private String caption;
82
83 private ContentConnector contentConnector;
84
85 @Inject
86 protected DetailSubApp(final SubAppContext subAppContext, final ContentSubAppView view, @Named(AdmincentralEventBus.NAME) EventBus adminCentralEventBus,
87 DetailEditorPresenter presenter, SimpleTranslator i18n, ContentConnector contentConnector) {
88 super(subAppContext, view);
89
90 this.adminCentralEventBus = adminCentralEventBus;
91 this.presenter = presenter;
92 this.i18n = i18n;
93 this.contentConnector = contentConnector;
94 bindHandlers();
95 }
96
97
98
99
100
101
102
103
104
105
106 @Override
107 public ContentSubAppView start(final Location location) {
108 DetailLocation detailLocation = DetailLocation.wrap(location);
109 super.start(detailLocation);
110
111 setCaption(detailLocation);
112 this.itemId = contentConnector.getItemIdByUrlFragment(detailLocation.getNodePath());
113
114 View view;
115 if (detailLocation.hasVersion()) {
116 view = presenter.start(detailLocation.getNodePath(), detailLocation.getViewType(), contentConnector, detailLocation.getVersion());
117 } else {
118 view = presenter.start(detailLocation.getNodePath(), detailLocation.getViewType(), contentConnector);
119 }
120 getView().setContentView(view);
121 return getView();
122 }
123
124
125
126
127 @Override
128 public DetailLocation getCurrentLocation() {
129 return DetailLocation.wrap(super.getCurrentLocation());
130 }
131
132 @Override
133 public boolean supportsLocation(Location location) {
134 DetailLocation itemLocation = DetailLocation.wrap(location);
135 String currentPath = getCurrentLocation().getNodePath();
136 return currentPath.equals(itemLocation.getNodePath());
137 }
138
139
140
141
142 @Override
143 public void locationChanged(Location location) {
144 DetailLocation detailLocation = DetailLocation.wrap(location);
145 if (!detailLocation.equals(getCurrentLocation())) {
146 setCaption(detailLocation);
147 View view = presenter.update(detailLocation);
148 getView().setContentView(view);
149 }
150 }
151
152 @Override
153 public String getCaption() {
154 return caption;
155 }
156
157 private void bindHandlers() {
158 adminCentralEventBus.addHandler(ContentChangedEvent.class, new ContentChangedEvent.Handler() {
159
160 @Override
161 public void onContentChanged(ContentChangedEvent event) {
162
163 if (contentConnector.canHandleItem(event.getItemId())) {
164
165 if (itemId == null) {
166
167 String currentNodePath = getCurrentLocation().getNodePath();
168
169
170 int splitIndex = currentNodePath.lastIndexOf("/");
171 if (splitIndex == 0) {
172 splitIndex = 1;
173 }
174 String parentNodePath = currentNodePath.substring(0, splitIndex);
175 Object parentItemId = contentConnector.getItemIdByUrlFragment(parentNodePath);
176 if (!contentConnector.canHandleItem(parentItemId)) {
177 getSubAppContext().close();
178 }
179
180 } else {
181
182 if (!contentConnector.canHandleItem(itemId)) {
183 getSubAppContext().close();
184 }
185
186 else {
187 String currentNodePath = getCurrentLocation().getNodePath();
188 String itemPath = contentConnector.getItemUrlFragment(itemId);
189 if (!currentNodePath.equals(itemPath)) {
190 DetailLocation location = DetailLocation.wrap(getSubAppContext().getLocation());
191 location.updateNodePath(itemPath);
192
193 getSubAppContext().setLocation(location);
194
195 setCaption(location);
196 }
197 }
198 }
199 }
200 }
201 });
202 }
203
204
205
206
207
208 protected void setCaption(DetailLocation location) {
209 String caption = getBaseCaption(location);
210
211 if (StringUtils.isNotBlank(location.getVersion())) {
212 caption = i18n.translate("subapp.versioned_page", caption, location.getVersion() );
213 }
214 this.caption = caption;
215 }
216
217
218
219
220
221 protected String getBaseCaption(DetailLocation location) {
222 return location.getNodePath();
223 }
224
225 }