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.vaadin.actionbar;
35
36 import info.magnolia.ui.vaadin.gwt.client.actionbar.connector.ActionbarState;
37 import info.magnolia.ui.vaadin.gwt.client.actionbar.rpc.ActionbarServerRpc;
38 import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
39 import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarSection;
40
41 import java.lang.reflect.Method;
42 import java.util.Collection;
43 import java.util.Iterator;
44 import java.util.Map;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.vaadin.event.ConnectorEventListener;
50 import com.vaadin.server.Resource;
51 import com.vaadin.server.ThemeResource;
52 import com.vaadin.ui.AbstractComponent;
53 import com.vaadin.ui.Component;
54 import com.vaadin.util.ReflectTools;
55
56
57
58
59 public class Actionbar extends AbstractComponent {
60
61 private static final Logger log = LoggerFactory.getLogger(Actionbar.class);
62
63 public Actionbar() {
64 setSizeFull();
65 setWidth(null);
66 setOpen(true);
67 registerRpc(new ActionbarServerRpc() {
68
69 @Override
70 public void onActionTriggered(String actionToken) {
71 fireEvent(new ActionTriggerEvent(Actionbar.this, actionToken));
72 }
73
74 @Override
75 public void setOpen(boolean isOpen) {
76 Actionbar.this.setOpen(isOpen);
77 }
78 });
79 }
80
81 public void setOpen(boolean isOpen) {
82 getState().isOpen = isOpen;
83 if (isOpen && !getStyleName().contains("open")) {
84 addStyleName("open");
85 } else if (!isOpen && getStyleName().contains("open")) {
86 removeStyleName("open");
87 }
88
89 }
90
91 @Override
92 protected ActionbarState getState() {
93 return (ActionbarState) super.getState();
94 }
95
96 @Override
97 protected ActionbarState getState(boolean markAsDirty) {
98 return (ActionbarState) super.getState(markAsDirty);
99 }
100
101
102
103 public void addAction(ActionbarItem action, String sectionName) {
104 ActionbarSection section = getState().sections.get(sectionName);
105 if (section != null) {
106 section.addAction(action);
107 } else {
108 log.warn("Action was not added: no section found with name '" + sectionName + "'.");
109 }
110 }
111
112 public void removeAction(String actionName) {
113 for (ActionbarSection section : getState().sections.values()) {
114 section.removeAction(actionName);
115 }
116 }
117
118 public void addSection(String sectionName, String caption) {
119 getState().sections.put(sectionName, new ActionbarSection(sectionName, caption));
120 getState().sectionOrder.add(sectionName);
121 setSectionVisible(sectionName, true);
122 }
123
124 public void removeSection(String sectionName) {
125 getState().sectionOrder.remove(sectionName);
126 getState().sections.remove(sectionName);
127 }
128
129 public void setSectionPreview(Resource previewResource, String sectionName) {
130 setResource(sectionName, previewResource);
131 setSectionVisible(sectionName, true);
132 }
133
134 public Map<String, ActionbarSection> getSections() {
135 return getState(false).sections;
136 }
137
138 public void setSectionVisible(String sectionName, boolean isVisible) {
139 ActionbarSection section = getState().sections.get(sectionName);
140 if (isVisible && section != null) {
141 if (!getState().visibleSections.contains(section)) {
142 getState().visibleSections.add(section);
143 }
144 } else {
145 getState().visibleSections.remove(section);
146 }
147 }
148
149 public boolean isSectionVisible(String sectionName) {
150 final Iterator<ActionbarSection> it = getState(false).visibleSections.iterator();
151 boolean result = false;
152 while (!result && it.hasNext()) {
153 result = it.next().getName().equals(sectionName);
154 }
155 return result;
156 }
157
158 public void setGroupEnabled(String groupName, boolean isEnabled) {
159 for (ActionbarSection section : getState().sections.values()) {
160 doSetGroupEnabled(section, groupName, isEnabled);
161 }
162 }
163
164 public void setGroupEnabled(String groupName, String sectionName, boolean isEnabled) {
165 doSetGroupEnabled(getState().sections.get(sectionName), groupName, isEnabled);
166 }
167
168 private void doSetGroupEnabled(ActionbarSection section, String groupName, boolean isEnabled) {
169 for (ActionbarItem action : section.getActions().values()) {
170 if (groupName.equals(action.getGroupName())) {
171 doSetActionEnabled(isEnabled, action);
172 }
173 }
174 }
175
176 public void setActionEnabled(String actionName, boolean isEnabled) {
177 final Collection<ActionbarSection> sections = getState().sections.values();
178 for (ActionbarSection section : sections) {
179 setActionEnabled(section.getName(), actionName, isEnabled);
180 }
181 }
182
183 public void setActionEnabled(String sectionName, String actionName, boolean isEnabled) {
184 ActionbarItem action = getState().sections.get(sectionName).getActions().get(actionName);
185 if (action != null) {
186 doSetActionEnabled(isEnabled, action);
187 }
188 }
189
190 private void doSetActionEnabled(boolean isEnabled, ActionbarItem action) {
191 getState().disabledActions.remove(action);
192 if (!isEnabled) {
193 getState().disabledActions.add(action);
194 }
195 }
196
197 public void registerActionIconResource(String actionName, ThemeResource iconResource) {
198 setResource(actionName, iconResource);
199 }
200
201
202
203 public void addActionTriggerListener(ActionTriggerListener listener) {
204 addListener(ActionTriggerListener.EVENT_ID, ActionTriggerEvent.class, listener, ActionTriggerListener.EVENT_METHOD);
205 }
206
207 public void removeActionTriggerListener(ActionTriggerListener listener) {
208 removeListener(ActionTriggerListener.EVENT_ID, ActionTriggerEvent.class, listener);
209 }
210
211
212
213
214 public interface ActionTriggerListener extends ConnectorEventListener {
215
216 public static final String EVENT_ID = "at";
217 public static final Method EVENT_METHOD = ReflectTools.findMethod(ActionTriggerListener.class, "actionTrigger", ActionTriggerEvent.class);
218
219 public void actionTrigger(ActionTriggerEvent event);
220 }
221
222
223
224
225 public static class ActionTriggerEvent extends Component.Event {
226
227 private final String actionName;
228
229 public ActionTriggerEvent(Component source, String actionName) {
230 super(source);
231 this.actionName = actionName;
232 }
233
234 public String getActionName() {
235 return actionName;
236 }
237 }
238 }