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.pages.app.editor.pagebar;
35
36 import info.magnolia.pages.app.editor.PagesEditorSubAppDescriptor;
37 import info.magnolia.pages.app.editor.extension.Extension;
38 import info.magnolia.pages.app.editor.extension.ExtensionContainer;
39 import info.magnolia.pages.app.editor.extension.ExtensionFactory;
40 import info.magnolia.pages.app.editor.extension.definition.ExtensionDefinition;
41 import info.magnolia.pages.app.editor.pagebar.definition.PageBarDefinition;
42 import info.magnolia.ui.api.app.SubAppContext;
43 import info.magnolia.ui.api.app.SubAppDescriptor;
44 import info.magnolia.ui.contentapp.detail.DetailLocation;
45 import info.magnolia.ui.contentapp.detail.DetailView;
46 import info.magnolia.ui.vaadin.editor.pagebar.PageBarView;
47
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.Map;
51
52 import javax.inject.Inject;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61 public class PageBarPresenter implements ExtensionContainer, PageBarView.Listener {
62
63 private static final Logger log = LoggerFactory.getLogger(PageBarPresenter.class);
64
65 private final PageBarView view;
66 private final SubAppContext subAppContext;
67 private final ExtensionFactory extensionFactory;
68
69 private List<Extension> extensions = new ArrayList<Extension>();
70
71 @Inject
72 public PageBarPresenter(PageBarView view, SubAppContext subAppContext, ExtensionFactory extensionFactory) {
73 this.view = view;
74 this.subAppContext = subAppContext;
75 this.extensionFactory = extensionFactory;
76 }
77
78 @Override
79 public PageBarView start(DetailLocation location) {
80 SubAppDescriptor subAppDescriptor = subAppContext.getSubAppDescriptor();
81
82 if (subAppDescriptor instanceof PagesEditorSubAppDescriptor) {
83 PageBarDefinition definition = ((PagesEditorSubAppDescriptor) subAppDescriptor).getPageBar();
84 if (definition != null) {
85 Map<String, ExtensionDefinition> extensionDefinitions = definition.getExtensions();
86 extensions = extensionFactory.createExtensions(extensionDefinitions);
87 } else {
88 log.error("No pageBar definition defined for pages detail app, no extensions will be loaded.");
89 }
90 } else {
91 log.error("Expected an instance of {} but got {}. No extensions will be loaded.", PagesEditorSubAppDescriptor.class.getSimpleName(), subAppDescriptor.getClass().getName());
92 }
93
94 for (Extension extension : extensions) {
95 view.addPageBarComponent(extension.start(location));
96 }
97 view.setListener(this);
98
99 togglePreviewMode(location);
100 return view;
101 }
102
103 public void setPageName(String caption, String nodePath) {
104 String pageName = caption.toUpperCase() + " - " + nodePath;
105 view.setPageName(pageName);
106 }
107
108 public void setPageName(String pageName) {
109 view.setPageName(pageName);
110 }
111
112 private void togglePreviewMode(DetailLocation location) {
113 boolean isPreview = DetailView.ViewType.VIEW.equals(location.getViewType());
114 view.togglePreviewMode(isPreview);
115 }
116
117 public PageBarView getView() {
118 return view;
119 }
120
121 @Override
122 public void onLocationUpdate(DetailLocation location) {
123 for (Extension extension : extensions) {
124 extension.onLocationUpdate(location);
125 }
126 togglePreviewMode(location);
127 }
128
129 @Override
130 public void deactivateExtensions() {
131 for (Extension extension : extensions) {
132 extension.deactivate();
133 }
134 }
135
136 @Override
137 public void stop() {
138 for (Extension extension : extensions) {
139 extension.stop();
140 }
141 }
142 }