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.framework.overlay;
35  
36  import static java.util.stream.Collectors.toList;
37  
38  import info.magnolia.ui.CloseHandler;
39  import info.magnolia.ui.actionbar.definition.ConfiguredActionbarDefinition;
40  import info.magnolia.ui.api.ioc.UiContextScoped;
41  import info.magnolia.ui.availability.AvailabilityChecker;
42  import info.magnolia.ui.chooser.Chooser;
43  import info.magnolia.ui.chooser.definition.ChooserDefinition;
44  import info.magnolia.ui.contentapp.browser.actions.ActionbarPresenter;
45  import info.magnolia.ui.contentapp.browser.actions.ActionbarView;
46  import info.magnolia.ui.datasource.DatasourceDefinition;
47  import info.magnolia.ui.dialog.ActionExecution;
48  import info.magnolia.ui.dialog.DialogBuilder;
49  import info.magnolia.ui.dialog.EditorActionBar;
50  import info.magnolia.ui.framework.ioc.SessionStore;
51  import info.magnolia.ui.framework.ioc.UiComponentProvider;
52  import info.magnolia.ui.framework.layout.LayoutDefinition;
53  
54  import java.util.HashMap;
55  import java.util.List;
56  import java.util.Optional;
57  import java.util.concurrent.CompletableFuture;
58  
59  import javax.inject.Inject;
60  
61  import com.vaadin.ui.Component;
62  import com.vaadin.ui.HorizontalLayout;
63  import com.vaadin.ui.Window;
64  
65  import lombok.AccessLevel;
66  import lombok.AllArgsConstructor;
67  
68  /**
69   * Simple utility for handling the lifecycle of {@link Chooser}s.
70   */
71  @UiContextScoped
72  public class ChooserController {
73  
74      //TODO MGNLUI-5927 The window for some reason has fixed size.
75      //TODO MGNLUI-5926 Making setSizeFull for actionbarview removes it, it needs some height, 445 is accurate so as not
76      // to create scroll bar for grid.
77      private static final String SAME_AS_GRID_HEIGHT = "445px";
78      private final UiComponentProvider componentProvider;
79  
80      @Inject
81      public ChooserController(UiComponentProvider componentProvider) {
82          this.componentProvider = componentProvider;
83      }
84  
85      public <T, C extends Chooser<T>> OnItemChosen<T> openChooser(ChooserDefinition<T, C> definition, T initialChoice) {
86          Chooser<T> chooser = componentProvider.inChildContext(definition).newInstance(definition.getImplementationClass());
87          chooser.setChoice(initialChoice);
88  
89          OnItemChosen<T> result = new OnItemChosen<>();
90          Window chooserWindow = buildWindow(definition, chooser, result);
91  
92          chooser.bindInstance(CloseHandler.class, chooserWindow::close);
93          chooser.bindInstance(ChooseHandler.class, (action) -> result
94                  .action(action)
95                  .complete(ChooseResult.ofChoice(chooser.getChoice())));
96  
97          return result;
98      }
99  
100     private <T, C extends Chooser<T>> Window buildWindow(ChooserDefinition<T, C> definition, Chooser<T> chooser, OnItemChosen<T> result) {
101         return DialogBuilder.dialog()
102                     .withTitle(definition.getLabel())
103                     .light(definition.isLight())
104                     .wide(definition.isWide())
105                     .withContent(prepareChooserLayout(definition, chooser))
106                     .withFooter(prepareFooter(definition, chooser))
107                     .withStyles("choose-dialog")
108                     .withCloseListener(e -> {
109                         result.complete(ChooseResult.ofChoice(null));
110                         SessionStore.access().releaseBeanStore(chooser.getCurrentViewReference());
111                     })
112                     .buildAndOpen();
113     }
114 
115     //TODO MGNLUI-5928 Only provide preview where necessary, currently added to all choosers.
116     //TODO MGNLUI-5926 Using ActionbarView to provide a preview is likely an overkill here.
117     private <T, C extends Chooser<T>> HorizontalLayout prepareChooserLayout(ChooserDefinition<T, C> definition, Chooser<T> chooser) {
118         final HorizontalLayout layout = new HorizontalLayout(chooser.asVaadinComponent());
119         layout.setSizeFull();
120         layout.setMargin(false);
121         layout.setSpacing(false);
122         layout.setExpandRatio(chooser.asVaadinComponent(), 1f);
123 
124         Optional.ofNullable(definition.getDatasource())
125                 .map(DatasourceDefinition::getPreview).ifPresent(preview -> {
126             final ConfiguredActionbarDefinitionnition.html#ConfiguredActionbarDefinition">ConfiguredActionbarDefinition actionbarDefinition = new ConfiguredActionbarDefinition();
127             final ActionbarPresenter<T> actionbarPresenter = chooser.getComponentProvider().newInstance(ActionbarPresenter.class, actionbarDefinition, new HashMap<>());
128             final ActionbarView<T> previewBar = chooser.getComponentProvider().newInstance(ActionbarView.class, actionbarPresenter, actionbarDefinition);
129             previewBar.setHeight(SAME_AS_GRID_HEIGHT);
130             layout.addComponent(previewBar);
131             layout.setExpandRatio(previewBar, 0f);
132         });
133         return layout;
134     }
135 
136     private <T, C extends Chooser<T>> Component prepareFooter(ChooserDefinition<T, C> definition, Chooser<T> chooser) {
137         final UiComponentProvider chooserComponentProvider = chooser.getComponentProvider();
138         List<ActionExecution<T>> actionExecutions = ActionExecution.<T>fromDefinitions(definition.getActions().values(), chooserComponentProvider).collect(toList());
139         EditorActionBar<T> editorActionBar = chooser.create(EditorActionBar.class, chooserComponentProvider.getComponent(AvailabilityChecker.class));
140         LayoutDefinition<?> layoutDefinition = definition.getFooterLayout();
141         editorActionBar
142                 .withActions(actionExecutions)
143                 .withLayoutDefinition(layoutDefinition);
144         return editorActionBar.layout();
145     }
146 
147     public <T, C extends Chooser<T>> OnItemChosen<T> openChooser(ChooserDefinition<T, C> definition) {
148         return this.openChooser(definition, null);
149     }
150 
151     public static class OnItemChosen<T> extends CompletableFuture<ChooseResult<T>> {
152         private String action;
153 
154         public OnItemChosen<T> action(String action) {
155             this.action = action;
156             return this;
157         }
158 
159         public String getAction() {
160             return action;
161         }
162     }
163 
164     public interface ChooseHandler {
165         void action(String name);
166     }
167 
168     /**
169      * Holds the choice.
170      *
171      * @param <T>
172      *     choice item type.
173      */
174     @AllArgsConstructor(access = AccessLevel.PACKAGE)
175     public static class ChooseResult<T> {
176         private boolean isChosen;
177         private T choice;
178 
179         public static <T> ChooseResult<T> ofChoice(T choice) {
180             return new ChooseResult<>(choice != null, choice);
181         }
182 
183         public boolean isChosen() {
184             return this.isChosen;
185         }
186 
187         public Optional<T> getChoice() {
188             return Optional.ofNullable(choice);
189         }
190     }
191 }