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      private final DatasourceDefinition datasourceDefinition;
80  
81      @Inject
82      public ChooserController(UiComponentProvider componentProvider, DatasourceDefinition datasourceDefinition) {
83          this.componentProvider = componentProvider;
84          this.datasourceDefinition = datasourceDefinition;
85      }
86  
87      public ChooserController(UiComponentProvider componentProvider) {
88          this(componentProvider, null);
89      }
90  
91      public <T, C extends Chooser<T>> OnItemChosen<T> openChooser(ChooserDefinition<T, C> definition, T initialChoice) {
92          Chooser<T> chooser = componentProvider.inChildContext(definition).newInstance(definition.getImplementationClass());
93          chooser.setChoice(initialChoice);
94  
95          OnItemChosen<T> result = new OnItemChosen<>();
96          Window chooserWindow = buildWindow(definition, chooser, result);
97          // we need to force chooser window's z-index to a value higher than ckeditor image dialog, so that it goes above the latter.
98          // see RichTextFieldFactory#CKEDITOR_BASE_Z_INDEX and VAADIN/themes/resurface/dialogs.scss
99          chooserWindow.addStyleName("choose-dialog-always-on-top");
100 
101         chooser.bindInstance(CloseHandler.class, chooserWindow::close);
102         chooser.bindInstance(ChooseHandler.class, (action) -> result
103                 .action(action)
104                 .complete(ChooseResult.ofChoice(chooser.getChoice())));
105 
106         return result;
107     }
108 
109     private <T, C extends Chooser<T>> Window buildWindow(ChooserDefinition<T, C> definition, Chooser<T> chooser, OnItemChosen<T> result) {
110         return DialogBuilder.dialog()
111                     .withTitle(definition.getLabel())
112                     .light(definition.isLight())
113                     .width(definition.getWidth())
114                     .withContent(prepareChooserLayout(chooser))
115                     .withFooter(prepareFooter(definition, chooser))
116                     .withStyles("choose-dialog")
117                     .withCloseListener(e -> {
118                         result.complete(ChooseResult.ofChoice(null));
119                         SessionStore.access().releaseBeanStore(chooser.getCurrentViewReference());
120                     })
121                     .buildAndOpen();
122     }
123 
124     //TODO MGNLUI-5926 Using ActionbarView to provide a preview is likely an overkill here.
125     private <T, C extends Chooser<T>> HorizontalLayout prepareChooserLayout(Chooser<T> chooser) {
126         final HorizontalLayout layout = new HorizontalLayout(chooser.asVaadinComponent());
127         layout.setSizeFull();
128         layout.setMargin(false);
129         layout.setSpacing(false);
130         layout.setExpandRatio(chooser.asVaadinComponent(), 1f);
131 
132         Optional.ofNullable(datasourceDefinition)
133                 .map(DatasourceDefinition::getPreview).ifPresent(preview -> {
134             final ConfiguredActionbarDefinitionnition.html#ConfiguredActionbarDefinition">ConfiguredActionbarDefinition actionbarDefinition = new ConfiguredActionbarDefinition();
135             final ActionbarPresenter<T> actionbarPresenter = chooser.getComponentProvider().newInstance(ActionbarPresenter.class, actionbarDefinition, new HashMap<>());
136             final ActionbarView<T> previewBar = chooser.getComponentProvider().newInstance(ActionbarView.class, actionbarPresenter, actionbarDefinition);
137             previewBar.setHeight(SAME_AS_GRID_HEIGHT);
138             layout.addComponent(previewBar);
139             layout.setExpandRatio(previewBar, 0f);
140         });
141         return layout;
142     }
143 
144     private <T, C extends Chooser<T>> Component prepareFooter(ChooserDefinition<T, C> definition, Chooser<T> chooser) {
145         final UiComponentProvider chooserComponentProvider = chooser.getComponentProvider();
146         List<ActionExecution<T>> actionExecutions = ActionExecution.<T>fromDefinitions(definition.getActions().values(), chooserComponentProvider).collect(toList());
147         EditorActionBar<T> editorActionBar = chooser.create(EditorActionBar.class, chooserComponentProvider.getComponent(AvailabilityChecker.class));
148         LayoutDefinition<?> layoutDefinition = definition.getFooterLayout();
149         editorActionBar
150                 .withActions(actionExecutions)
151                 .withLayoutDefinition(layoutDefinition);
152         return editorActionBar.layout();
153     }
154 
155     public <T, C extends Chooser<T>> OnItemChosen<T> openChooser(ChooserDefinition<T, C> definition) {
156         return this.openChooser(definition, null);
157     }
158 
159     public static class OnItemChosen<T> extends CompletableFuture<ChooseResult<T>> {
160         private String action;
161 
162         public OnItemChosen<T> action(String action) {
163             this.action = action;
164             return this;
165         }
166 
167         public String getAction() {
168             return action;
169         }
170     }
171 
172     public interface ChooseHandler {
173         void action(String name);
174     }
175 
176     /**
177      * Holds the choice.
178      *
179      * @param <T>
180      *     choice item type.
181      */
182     @AllArgsConstructor(access = AccessLevel.PACKAGE)
183     public static class ChooseResult<T> {
184         private boolean isChosen;
185         private T choice;
186 
187         public static <T> ChooseResult<T> ofChoice(T choice) {
188             return new ChooseResult<>(choice != null, choice);
189         }
190 
191         public boolean isChosen() {
192             return this.isChosen;
193         }
194 
195         public Optional<T> getChoice() {
196             return Optional.ofNullable(choice);
197         }
198     }
199 }