View Javadoc
1   /**
2    * This file Copyright (c) 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.contentapp.browser.actions;
35  
36  import info.magnolia.ui.actionbar.definition.ActionbarDefinition;
37  import info.magnolia.ui.actionbar.definition.ActionbarSectionDefinition;
38  import info.magnolia.ui.api.action.ActionDefinition;
39  import info.magnolia.ui.api.availability.AvailabilityDefinition;
40  import info.magnolia.ui.contentapp.browser.ActionExecutionService;
41  import info.magnolia.ui.contentapp.browser.context.ValueContext;
42  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.Optional;
48  import java.util.function.Predicate;
49  import java.util.stream.Stream;
50  
51  import javax.inject.Inject;
52  
53  import org.apache.commons.lang3.StringUtils;
54  
55  /**
56   * Presenter bound to {@link ActionbarView}. Provides means of section/group/action lookup,
57   * constructs DTO's for the client-side, delegates action execution calls to
58   * {@link ActionExecutionService}.
59   *
60   * @param <T> item type.
61   */
62  public class ActionbarPresenter<T> {
63  
64      private final ActionExecutionService actionExecutionService;
65      private final Map<String, ActionDefinition> actions;
66      private final ActionbarDefinition actionbarDefinition;
67      private final ValueContext<T> valueContext;
68  
69      @Inject
70      ActionbarPresenter(
71              ValueContext<T> valueContext,
72              Map<String, ActionDefinition> actionConfiguration,
73              ActionbarDefinition actionbarDefinition,
74              ActionExecutionService actionExecutionService) {
75          this.actionExecutionService = actionExecutionService;
76          this.actions = actionConfiguration;
77          this.actionbarDefinition = actionbarDefinition;
78          this.valueContext = valueContext;
79      }
80  
81      Optional<ActionbarItem> createActionItem(String actionName, String groupName) {
82          final ActionDefinition actionDefinition = actions.get(actionName);
83          if (actionDefinition == null) {
84              return Optional.empty();
85          }
86  
87          String label = actionDefinition.getLabel();
88          if (StringUtils.isBlank(label)) {
89              label = actionName;
90          }
91          String icon = actionDefinition.getIcon();
92  
93          // only icons from icon-fonts currently work
94          return Optional.of(new ActionbarItem(actionName, label, icon, groupName));
95      }
96  
97      Optional<ActionbarSectionDefinition> getAvailableSection(List<ActionbarSectionDefinition> sectionAvailabilities) {
98          return sectionAvailabilities
99                  .stream()
100                 .filter(actionbarSection ->
101                         actionExecutionService
102                                 .getAvailabilityFilter(valueContext.get())
103                                 .test(actionbarSection.getAvailability()))
104                 .findFirst();
105     }
106 
107     List<ActionbarItem> getAvailableActions() {
108         List<ActionbarItem> actions = new ArrayList<>();
109 
110         getAvailableSection(actionbarDefinition.getSections())
111                 .ifPresent(section ->
112                         section.getGroups().forEach(group ->
113                                 group.getItems().forEach(action -> {
114                                     createActionItem(action.getName(), group.getName()).ifPresent(actions::add);
115                                 })));
116         return actions;
117     }
118 
119     boolean isEnableAction(String actionName) {
120         final Stream<T> selectedItems = valueContext.get();
121         final Predicate<AvailabilityDefinition> availabilityFilter = actionExecutionService.getAvailabilityFilter(selectedItems);
122         if (actions.containsKey(actionName)) {
123             return availabilityFilter.test(actions.get(actionName).getAvailability());
124         }
125         return false;
126     }
127 
128     void fireAction(String actionName) {
129         actionExecutionService.executeAction(getActionName(actionName));
130     }
131 
132     void fireDefaultAction() {
133         if (isEnableAction(actionbarDefinition.getDefaultAction())) {
134             fireAction(actionbarDefinition.getDefaultAction());
135         }
136     }
137 
138     private String getActionName(String actionToken) {
139         return actionToken.contains(":") ? StringUtils.substringAfterLast(actionToken, ":") : actionToken;
140     }
141 }