View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * The Actionbar widget, consisting of sections and groups of actions.
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          setImmediate(true);
67          setOpen(true);
68          registerRpc(new ActionbarServerRpc() {
69  
70              @Override
71              public void onActionTriggered(String actionToken) {
72                  fireEvent(new ActionTriggerEvent(Actionbar.this, actionToken));
73              }
74  
75              @Override
76              public void setOpen(boolean isOpen) {
77                  Actionbar.this.setOpen(isOpen);
78              }
79          });
80      }
81  
82      public void setOpen(boolean isOpen) {
83          getState().isOpen = isOpen;
84          if (isOpen && !getStyleName().contains("open")) {
85              addStyleName("open");
86          } else if (!isOpen && getStyleName().contains("open")) {
87              removeStyleName("open");
88          }
89  
90      }
91  
92      @Override
93      protected ActionbarState getState() {
94          return (ActionbarState) super.getState();
95      }
96  
97      @Override
98      protected ActionbarState getState(boolean markAsDirty) {
99          return (ActionbarState) super.getState(markAsDirty);
100     }
101 
102     // ACTION BAR API ///////////////////////////
103 
104     public void addAction(ActionbarItem action, String sectionName) {
105         ActionbarSection section = getState().sections.get(sectionName);
106         if (section != null) {
107             section.addAction(action);
108         } else {
109             log.warn("Action was not added: no section found with name '" + sectionName + "'.");
110         }
111     }
112 
113     public void removeAction(String actionName) {
114         for (ActionbarSection section : getState().sections.values()) {
115             section.removeAction(actionName);
116         }
117     }
118 
119     public void addSection(String sectionName, String caption) {
120         getState().sections.put(sectionName, new ActionbarSection(sectionName, caption));
121         getState().sectionOrder.add(sectionName);
122         setSectionVisible(sectionName, true);
123     }
124 
125     public void removeSection(String sectionName) {
126         getState().sectionOrder.remove(sectionName);
127         getState().sections.remove(sectionName);
128     }
129 
130     public void setSectionPreview(Resource previewResource, String sectionName) {
131         setResource(sectionName, previewResource);
132         setSectionVisible(sectionName, true);
133     }
134 
135     public Map<String, ActionbarSection> getSections() {
136         return getState(false).sections;
137     }
138 
139     public void setSectionVisible(String sectionName, boolean isVisible) {
140         ActionbarSection section = getState().sections.get(sectionName);
141         if (isVisible && section != null) {
142             if (!getState().visibleSections.contains(section)) {
143                 getState().visibleSections.add(section);
144             }
145         } else {
146             getState().visibleSections.remove(section);
147         }
148     }
149 
150     public boolean isSectionVisible(String sectionName) {
151         final Iterator<ActionbarSection> it = getState(false).visibleSections.iterator();
152         boolean result = false;
153         while (!result && it.hasNext()) {
154             result = it.next().getName().equals(sectionName);
155         }
156         return result;
157     }
158 
159     public void setGroupEnabled(String groupName, boolean isEnabled) {
160         for (ActionbarSection section : getState().sections.values()) {
161             doSetGroupEnabled(section, groupName, isEnabled);
162         }
163     }
164 
165     public void setGroupEnabled(String groupName, String sectionName, boolean isEnabled) {
166         doSetGroupEnabled(getState().sections.get(sectionName), groupName, isEnabled);
167     }
168 
169     private void doSetGroupEnabled(ActionbarSection section, String groupName, boolean isEnabled) {
170         for (ActionbarItem action : section.getActions().values()) {
171             if (groupName.equals(action.getGroupName())) {
172                 doSetActionEnabled(isEnabled, action);
173             }
174         }
175     }
176 
177     public void setActionEnabled(String actionName, boolean isEnabled) {
178         final Collection<ActionbarSection> sections = getState().sections.values();
179         for (ActionbarSection section : sections) {
180             setActionEnabled(section.getName(), actionName, isEnabled);
181         }
182     }
183 
184     public void setActionEnabled(String sectionName, String actionName, boolean isEnabled) {
185         ActionbarItem action = getState().sections.get(sectionName).getActions().get(actionName);
186         if (action != null) {
187             doSetActionEnabled(isEnabled, action);
188         }
189     }
190 
191     private void doSetActionEnabled(boolean isEnabled, ActionbarItem action) {
192         getState().disabledActions.remove(action);
193         if (!isEnabled) {
194             getState().disabledActions.add(action);
195         }
196     }
197 
198     public void registerActionIconResource(String actionName, ThemeResource iconResource) {
199         setResource(actionName, iconResource);
200     }
201 
202     // EVENTS AND LISTENERS
203 
204     public void addActionTriggerListener(ActionTriggerListener listener) {
205         addListener(ActionTriggerListener.EVENT_ID, ActionTriggerEvent.class, listener, ActionTriggerListener.EVENT_METHOD);
206     }
207 
208     public void removeActionTriggerListener(ActionTriggerListener listener) {
209         removeListener(ActionTriggerListener.EVENT_ID, ActionTriggerEvent.class, listener);
210     }
211 
212     /**
213      * The listener interface for triggering actions from the action bar.
214      */
215     public interface ActionTriggerListener extends ConnectorEventListener {
216 
217         public static final String EVENT_ID = "at";
218         public static final Method EVENT_METHOD = ReflectTools.findMethod(ActionTriggerListener.class, "actionTrigger", ActionTriggerEvent.class);
219 
220         public void actionTrigger(ActionTriggerEvent event);
221     }
222 
223     /**
224      * The event fired when triggering actions from the action bar.
225      */
226     public static class ActionTriggerEvent extends Component.Event {
227 
228         private final String actionName;
229 
230         public ActionTriggerEvent(Component source, String actionName) {
231             super(source);
232             this.actionName = actionName;
233         }
234 
235         public String getActionName() {
236             return actionName;
237         }
238     }
239 }