View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 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.actionbar;
35  
36  import info.magnolia.ui.actionbar.definition.ActionbarDefinition;
37  import info.magnolia.ui.actionbar.definition.ActionbarGroupDefinition;
38  import info.magnolia.ui.actionbar.definition.ActionbarItemDefinition;
39  import info.magnolia.ui.actionbar.definition.ActionbarSectionDefinition;
40  import info.magnolia.ui.api.action.ActionDefinition;
41  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Map;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.lang.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import com.vaadin.server.Resource;
54  
55  /**
56   * Default presenter for an action bar.
57   */
58  public class ActionbarPresenter implements ActionbarView.Listener {
59  
60      /**
61       * Listener interface for the Actionbar.
62       */
63      public interface Listener {
64  
65          void onActionbarItemClicked(String itemName);
66      }
67  
68      private static final Logger log = LoggerFactory.getLogger(ActionbarPresenter.class);
69  
70      private ActionbarDefinition definition;
71  
72      private Map<String, ActionDefinition> actions;
73  
74      private ActionbarView view;
75  
76      private Listener listener;
77  
78      @Inject
79      public ActionbarPresenter(ActionbarView view) {
80          this.view = view;
81          view.setListener(this);
82      }
83  
84      public void setListener(Listener listener) {
85          this.listener = listener;
86      }
87  
88      /**
89       * Initializes an action bar with the given definition and returns the view for the parent to add it.
90       */
91      public ActionbarView start(ActionbarDefinition definition, Map<String, ActionDefinition> actions) {
92          this.definition = definition;
93          this.actions = actions;
94  
95          if (definition != null) {
96  
97              // build action bar structure from definition
98              for (ActionbarSectionDefinition section : definition.getSections()) {
99                  view.addSection(section.getName(), section.getLabel());
100                 List<String> actionNames = new ArrayList<String>();
101 
102                 for (ActionbarGroupDefinition group : section.getGroups()) {
103                     // standalone groups make no sense
104                     log.debug("Group actions: " + group.getItems());
105 
106                     for (ActionbarItemDefinition action : group.getItems()) {
107                         if (actionNames.contains(action.getName())) {
108                             log.warn("Action was not added: an action with name " + action + "': was already added to the section" + section.getName() + ".");
109                             continue;
110                         }
111                         actionNames.add(action.getName());
112                         addActionItem(action.getName(), group.getName(), section.getName());
113                     }
114                 }
115             }
116         } else {
117             log.debug("No actionbar definition found. This will result in an empty action bar. Is that intended?");
118         }
119         return view;
120     }
121 
122     private void addActionItem(String actionName, String groupName, String sectionName) {
123 
124         ActionDefinition actionDefinition = actions.get(actionName);
125         if (actionDefinition != null) {
126             String label = actionDefinition.getLabel();
127             if (StringUtils.isBlank(label)) {
128                 label = actionName;
129             }
130             String icon = actionDefinition.getIcon();
131 
132             // only icons from icon-fonts currently work
133             ActionbarItem item = new ActionbarItem(actionName, label, icon, groupName);
134             view.addAction(item, sectionName);
135         }
136     }
137 
138     public void setPreview(final Resource previewResource) {
139         view.setPreview(previewResource);
140     }
141 
142     // METHODS DELEGATING TO THE VIEW
143 
144     public void enable(String... actionNames) {
145         if (view != null) {
146             for (String action : actionNames) {
147                 view.setActionEnabled(action, true);
148             }
149         }
150     }
151 
152     public void disable(String... actionNames) {
153         if (view != null) {
154             for (String action : actionNames) {
155                 view.setActionEnabled(action, false);
156             }
157         }
158     }
159 
160     public void enableGroup(String groupName) {
161         if (view != null) {
162             view.setGroupEnabled(groupName, true);
163         }
164     }
165 
166     public void disableGroup(String groupName) {
167         if (view != null) {
168             view.setGroupEnabled(groupName, false);
169         }
170     }
171 
172     public void enableGroup(String groupName, String sectionName) {
173         if (view != null) {
174             view.setGroupEnabled(groupName, sectionName, true);
175         }
176     }
177 
178     public void disableGroup(String groupName, String sectionName) {
179         if (view != null) {
180             view.setGroupEnabled(groupName, sectionName, false);
181         }
182     }
183 
184     public void showSection(String... sectionNames) {
185         if (view != null) {
186             for (String section : sectionNames) {
187                 view.setSectionVisible(section, true);
188             }
189         }
190     }
191 
192     public void hideSection(String... sectionNames) {
193         if (view != null) {
194             for (String section : sectionNames) {
195                 view.setSectionVisible(section, false);
196             }
197         }
198     }
199 
200     // VIEW LISTENER
201 
202     @Override
203     public void onActionbarItemClicked(String actionToken) {
204         String actionName = getActionName(actionToken);
205         listener.onActionbarItemClicked(actionName);
206     }
207 
208     private String getActionName(String actionToken) {
209         final String[] chunks = actionToken.split(":");
210         if (chunks.length != 2) {
211             log.warn("Invalid actionToken [{}]: it is expected to be in the form sectionName:actionName. Action name cannot be resolved. Please check actionbar definition.", actionToken);
212             return null;
213         }
214         final String sectionName = chunks[0];
215         final String actionName = chunks[1];
216 
217         return actionName;
218     }
219 }