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