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.actionbar;
35  
36  import info.magnolia.ui.actionbar.definition.ActionbarSectionDefinition;
37  import info.magnolia.ui.api.action.ActionDefinition;
38  import info.magnolia.ui.api.availability.AvailabilityDefinition;
39  import info.magnolia.ui.contentapp.browser.ActionExecutionService;
40  import info.magnolia.ui.contentapp.browser.context.ActionConfigurationContext;
41  import info.magnolia.ui.contentapp.browser.context.ValueContext;
42  import info.magnolia.ui.framework.datasource.components.SelectedItems;
43  import info.magnolia.ui.vaadin.actionbar.Actionbar;
44  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
45  
46  import java.util.List;
47  import java.util.Map;
48  import java.util.Optional;
49  import java.util.function.Predicate;
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 ActionConfigurationContext actionConfiguration;
66      private final ValueContext<T> valueContext;
67  
68      @Inject
69      ActionbarPresenter(
70              ValueContext<T> valueContext,
71              ActionConfigurationContext actionConfiguration,
72              ActionExecutionService actionExecutionService) {
73          this.actionExecutionService = actionExecutionService;
74          this.actionConfiguration = actionConfiguration;
75          this.valueContext = valueContext;
76      }
77  
78      Optional<ActionbarItem> createActionItem(String actionName, String groupName) {
79          return actionConfiguration.actions().value().flatMap(actions -> {
80              final ActionDefinition actionDefinition = actions.get(actionName);
81              if (actionDefinition == null) {
82                  return Optional.empty();
83              }
84  
85              String label = actionDefinition.getLabel();
86              if (StringUtils.isBlank(label)) {
87                  label = actionName;
88              }
89              String icon = actionDefinition.getIcon();
90  
91              // only icons from icon-fonts currently work
92              return Optional.of(new ActionbarItem(actionName, label, icon, groupName));
93          });
94      }
95  
96      Optional<ActionbarSectionDefinition> getAvailableSection(List<ActionbarSectionDefinition> sectionAvailabilities) {
97          final SelectedItems<T> selectedItems = valueContext.current().value().orElseGet(SelectedItems::empty);
98  
99          final Predicate<AvailabilityDefinition> availabilityFilter = actionExecutionService.getAvailabilityFilter(selectedItems);
100         return sectionAvailabilities
101                 .stream()
102                 .filter(actionbarSection -> availabilityFilter.test(actionbarSection.getAvailability()))
103                 .findFirst();
104     }
105 
106     boolean isEnableAction(String actionName) {
107         final SelectedItems<T> selectedItems = valueContext.current().value().orElseGet(SelectedItems::empty);
108         final Predicate<AvailabilityDefinition> availabilityFilter = actionExecutionService.getAvailabilityFilter(selectedItems);
109 
110         Optional<Map<String, ActionDefinition>> actionConfigOpt = actionConfiguration.actions().value();
111         if (actionConfigOpt.isPresent() && actionConfigOpt.get().containsKey(actionName)) {
112             return availabilityFilter.test(actionConfigOpt.get().get(actionName).getAvailability());
113         }
114         return false;
115     }
116 
117     public void fireAction(Actionbar.ActionTriggerEvent actionTriggerEvent) {
118         actionExecutionService.executeAction(getActionName(actionTriggerEvent.getActionName()),
119                 valueContext.current().nullableValue());
120     }
121 
122     private String getActionName(String actionToken) {
123         return StringUtils.substringAfterLast(actionToken, ":");
124     }
125 }