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.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.actionbar.definition.ActionbarDefinition;
38  import info.magnolia.ui.ValueContext;
39  import info.magnolia.ui.contentapp.browser.preview.PreviewProvider;
40  import info.magnolia.ui.UIComponent;
41  import info.magnolia.ui.vaadin.actionbar.Actionbar;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  import javax.inject.Inject;
47  
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.server.Resource;
52  import com.vaadin.ui.CssLayout;
53  
54  /**
55   * Actionbar view, lays out the sub-app actions according the {@link ActionbarDefinition},
56   * triggers their execution against the current value of the {@link ValueContext}.
57   *
58   * @param <T>
59   *     data type actionbar executes actions against
60   */
61  public class ActionbarView<T> extends CssLayout implements UIComponent {
62  
63      private static final String PREVIEW_SECTION_NAME = "preview";
64  
65      private static final Logger log = LoggerFactory.getLogger(ActionbarView.class);
66  
67      private final Actionbar actionbar = new Actionbar();
68      private final ActionbarPresenter<T> presenter;
69      private final ActionbarDefinition definition;
70      private final SimpleTranslator simpleTranslator;
71  
72      @Inject
73      @SuppressWarnings("unchecked")
74      public ActionbarView(ValueContext<T> valueContext, ActionbarDefinition definition, SimpleTranslator simpleTranslator, PreviewProvider<T> previewProvider,
75                           ActionbarPresenter presenter) {
76          this.definition = definition;
77          this.simpleTranslator = simpleTranslator;
78          this.presenter = presenter;
79  
80          setHeight(100, Unit.PERCENTAGE);
81          addStyleName("actionbar");
82  
83          initialiseActionbar(definition);
84  
85          valueContext.observe(items -> {
86              this.update();
87              setPreview(items.stream().findFirst()
88                      .flatMap(previewProvider::getResource)
89                      .orElse(null));
90          });
91          addComponent(actionbar);
92      }
93  
94      private void setPreview(Resource previewResource) {
95          if (previewResource != null) {
96              if (!actionbar.getSections().containsKey(PREVIEW_SECTION_NAME)) {
97                  actionbar.addSection(PREVIEW_SECTION_NAME, simpleTranslator.translate("actionbar.preview"));
98              }
99              actionbar.setSectionPreview(previewResource, PREVIEW_SECTION_NAME);
100         } else {
101             if (actionbar.getSections().containsKey(PREVIEW_SECTION_NAME)) {
102                 actionbar.removeSection(PREVIEW_SECTION_NAME);
103             }
104         }
105     }
106 
107     private void initialiseActionbar(ActionbarDefinition definition) {
108         // build action bar structure from definition
109         definition.getSections().forEach(section -> {
110             actionbar.addSection(section.getName(), section.getLabel());
111             final List<String> usedActions = new ArrayList<>();
112             section.getGroups().forEach(group -> {
113                 // standalone groups make no sense
114                 log.debug("Group actions: {}", group.getItems());
115 
116                 group.getItems().forEach(action -> {
117                     if (usedActions.contains(action.getName())) {
118                         log.warn("Action was not added: an action with name '{}' was already added to the section '{}'.", action.getName(), section.getName());
119                         return;
120                     }
121 
122                     usedActions.add(action.getName());
123                     this.presenter
124                             .createActionItem(action.getName(), group.getName())
125                             .ifPresent(item -> {
126                                 actionbar.addAction(item, section.getName());
127                                 actionbar.setActionEnabled(action.getName(), this.presenter.isEnabledAction(action.getName()));
128                             });
129                 });
130             });
131         });
132 
133         this.actionbar.addActionTriggerListener(event -> presenter.fireAction(event.getActionName()));
134     }
135 
136     private void update() {
137         this.definition.getSections().forEach(section -> actionbar.setSectionVisible(section.getName(), false));
138         this.presenter.getAvailableSection(definition.getSections()).ifPresent(section ->
139                 actionbar.setSectionVisible(section.getName(), true));
140         this.presenter.getAvailableActions().forEach(actionbarItem ->
141                 actionbar.setActionEnabled(actionbarItem.getName(), this.presenter.isEnabledAction(actionbarItem.getName())));
142     }
143 }