View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.dialog.actionarea;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.api.action.ActionDefinition;
38  import info.magnolia.ui.api.context.UiContext;
39  import info.magnolia.ui.api.view.View;
40  import info.magnolia.ui.dialog.actionarea.definition.ActionRendererDefinition;
41  import info.magnolia.ui.dialog.actionarea.definition.EditorActionAreaDefinition;
42  import info.magnolia.ui.dialog.actionarea.renderer.ActionRenderer;
43  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
44  import info.magnolia.ui.dialog.definition.SecondaryActionDefinition;
45  
46  import java.util.HashMap;
47  import java.util.Map;
48  
49  import javax.inject.Inject;
50  
51  /**
52   * Implementation of {@link EditorActionAreaPresenter}.
53   */
54  public class EditorActionAreaPresenterImpl implements EditorActionAreaPresenter {
55  
56      private EditorActionAreaView view;
57  
58      private ComponentProvider componentProvider;
59  
60      @Inject
61      public EditorActionAreaPresenterImpl(EditorActionAreaView view, ComponentProvider componentProvider) {
62          this.view = view;
63          this.componentProvider = componentProvider;
64      }
65  
66      @Override
67      public EditorActionAreaView start(Iterable<ActionDefinition> actions, EditorActionAreaDefinition definition, final ActionListener listener, UiContext uiContext) {
68          final Map<String, View> secondaryActions = new HashMap<>();
69          for (ActionDefinition action : actions) {
70              ActionRendererDefinition actionRendererDef = definition.getActionRenderers().get(action.getName());
71              ActionRenderer actionRenderer = actionRendererDef == null ?
72                      componentProvider.getComponent(ActionRenderer.class):
73                      componentProvider.newInstance(actionRendererDef.getRendererClass(), action, actionRendererDef, uiContext);
74              final View actionView = actionRenderer.start(action, listener);
75              // We simply compare secondary actions by name, as comparing the underlying object might not work due to a
76              // wrapped/proxied definition
77              if (definition.getSecondaryActions().stream()
78                      .anyMatch(secondaryActionDefinition -> action.getName().equals(secondaryActionDefinition.getName()))) {
79                  // Store rendered secondary action
80                  secondaryActions.put(action.getName(), actionView);
81              } else {
82                  view.addPrimaryAction(actionView, action.getName());
83              }
84          }
85  
86          // Add secondary action views according to their order in definition
87          for (final SecondaryActionDefinition secondaryActionDefinition : definition.getSecondaryActions()) {
88              final String actionName = secondaryActionDefinition.getName();
89              final View actionView = secondaryActions.get(actionName);
90              if (actionView != null) {
91                  view.addSecondaryAction(actionView, actionName);
92              }
93          }
94  
95          return view;
96      }
97  
98      protected EditorActionAreaView getView() {
99          return view;
100     }
101 }