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.actionbar;
35
36 import info.magnolia.i18nsystem.SimpleTranslator;
37 import info.magnolia.objectfactory.Components;
38 import info.magnolia.ui.actionbar.builder.ActionbarFactory;
39 import info.magnolia.ui.actionbar.definition.ActionbarDefinition;
40 import info.magnolia.ui.vaadin.actionbar.Actionbar;
41 import info.magnolia.ui.vaadin.actionbar.ActionbarView;
42
43 import javax.inject.Inject;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.vaadin.server.Resource;
49
50
51
52
53 public class ActionbarPresenter implements ActionbarView.Listener {
54
55
56
57
58 public interface Listener {
59
60 void onActionbarItemClicked(String itemName);
61
62 String getLabel(String itemName);
63
64 String getIcon(String itemName);
65
66 }
67
68 private static final Logger log = LoggerFactory.getLogger(ActionbarPresenter.class);
69
70 private static final String PREVIEW_SECTION_NAME = "preview";
71
72 private ActionbarDefinition definition;
73
74 private ActionbarView actionbar;
75
76 private Listener listener;
77
78 private SimpleTranslator translator;
79
80 @Inject
81 public ActionbarPresenter(SimpleTranslator translator) {
82 this.translator = translator;
83 }
84
85
86
87
88 public ActionbarPresenter() {
89 this.translator = Components.getComponent(SimpleTranslator.class);
90 }
91
92 public void setListener(Listener listener) {
93 this.listener = listener;
94 }
95
96
97
98
99
100 public ActionbarView start(final ActionbarDefinition definition) {
101 this.definition = definition;
102 actionbar = ActionbarFactory.build(definition, listener);
103 actionbar.setListener(this);
104 return actionbar;
105 }
106
107 public void setPreview(final Resource previewResource) {
108 if (previewResource != null) {
109 if (!((Actionbar) actionbar).getSections().containsKey(PREVIEW_SECTION_NAME)) {
110 actionbar.addSection(PREVIEW_SECTION_NAME, translator.translate("actionbar.preview"));
111 }
112 actionbar.setSectionPreview(previewResource, PREVIEW_SECTION_NAME);
113 } else {
114 if (((Actionbar) actionbar).getSections().containsKey(PREVIEW_SECTION_NAME)) {
115 actionbar.removeSection(PREVIEW_SECTION_NAME);
116 }
117 }
118 }
119
120
121
122 public void enable(String... actionNames) {
123 if (actionbar != null) {
124 for (String action : actionNames) {
125 actionbar.setActionEnabled(action, true);
126 }
127 }
128 }
129
130 public void disable(String... actionNames) {
131 if (actionbar != null) {
132 for (String action : actionNames) {
133 actionbar.setActionEnabled(action, false);
134 }
135 }
136 }
137
138 public void enableGroup(String groupName) {
139 if (actionbar != null) {
140 actionbar.setGroupEnabled(groupName, true);
141 }
142 }
143
144 public void disableGroup(String groupName) {
145 if (actionbar != null) {
146 actionbar.setGroupEnabled(groupName, false);
147 }
148 }
149
150 public void enableGroup(String groupName, String sectionName) {
151 if (actionbar != null) {
152 actionbar.setGroupEnabled(groupName, sectionName, true);
153 }
154 }
155
156 public void disableGroup(String groupName, String sectionName) {
157 if (actionbar != null) {
158 actionbar.setGroupEnabled(groupName, sectionName, false);
159 }
160 }
161
162 public void showSection(String... sectionNames) {
163 if (actionbar != null) {
164 for (String section : sectionNames) {
165 actionbar.setSectionVisible(section, true);
166 }
167 }
168 }
169
170 public void hideSection(String... sectionNames) {
171 if (actionbar != null) {
172 for (String section : sectionNames) {
173 actionbar.setSectionVisible(section, false);
174 }
175 }
176 }
177
178
179
180 @Override
181 public void onActionbarItemClicked(String actionToken) {
182 String actionName = getActionName(actionToken);
183 listener.onActionbarItemClicked(actionName);
184 }
185
186 private String getActionName(String actionToken) {
187 final String[] chunks = actionToken.split(":");
188 if (chunks.length != 2) {
189 log.warn("Invalid actionToken [{}]: it is expected to be in the form sectionName:actionName. Action name cannot be resolved. Please check actionbar definition.", actionToken);
190 return null;
191 }
192 final String sectionName = chunks[0];
193 final String actionName = chunks[1];
194
195 return actionName;
196 }
197 }