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