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