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.api.ioc.UiContextScoped;
40  import info.magnolia.ui.availability.AvailabilityChecker;
41  import info.magnolia.ui.chooser.Chooser;
42  import info.magnolia.ui.chooser.definition.ChooserDefinition;
43  import info.magnolia.ui.dialog.ActionExecution;
44  import info.magnolia.ui.dialog.DialogBuilder;
45  import info.magnolia.ui.dialog.EditorActionBar;
46  import info.magnolia.ui.framework.ioc.SessionStore;
47  import info.magnolia.ui.framework.ioc.UiComponentProvider;
48  import info.magnolia.ui.framework.layout.LayoutDefinition;
49  
50  import java.util.List;
51  import java.util.Optional;
52  import java.util.concurrent.CompletableFuture;
53  
54  import javax.inject.Inject;
55  
56  import com.vaadin.ui.Window;
57  
58  import lombok.AccessLevel;
59  import lombok.AllArgsConstructor;
60  
61  /**
62   * Simple utility for handling the lifecycle of {@link Chooser}s.
63   */
64  @UiContextScoped
65  public class ChooserController {
66  
67      private final UiComponentProvider componentProvider;
68  
69      @Inject
70      public ChooserController(UiComponentProvider componentProvider) {
71          this.componentProvider = componentProvider;
72      }
73  
74      public <T, C extends Chooser<T>> OnItemChosen<T> openChooser(ChooserDefinition<T, C> definition, T initialChoice) {
75          Chooser<T> chooser = this.componentProvider.inChildContext(definition).newInstance(definition.getImplementationClass());
76          chooser.setChoice(initialChoice);
77  
78          OnItemChosen<T> result = new OnItemChosen<>();
79  
80          UiComponentProvider chooserComponentProvider = chooser.getComponentProvider();
81          List<ActionExecution<T>> actionExecutions = ActionExecution.<T>fromDefinitions(definition.getActions().values(), chooserComponentProvider).collect(toList());
82          EditorActionBar<T> editorActionBar = chooser.create(EditorActionBar.class, chooserComponentProvider.getComponent(AvailabilityChecker.class));
83          LayoutDefinition<?> layoutDefinition = definition.getFooterLayout();
84          editorActionBar
85                  .withActions(actionExecutions)
86                  .withLayoutDefinition(layoutDefinition);
87  
88          Window chooserWindow = DialogBuilder.dialog()
89                  .withTitle(definition.getLabel())
90                  .light(definition.isLight())
91                  .withContent(chooser.asVaadinComponent())
92                  .withFooter(editorActionBar.layout())
93                  .buildAndOpen();
94  
95          chooser.bindInstance(CloseHandler.class, chooserWindow::close);
96          chooser.bindInstance(ChooseHandler.class, (action) -> result
97                  .action(action)
98                  .complete(ChooseResult.ofChoice(chooser.getChoice())));
99  
100         chooserWindow.addStyleName("choose-dialog");
101         chooserWindow.addCloseListener(e -> {
102             result.complete(ChooseResult.ofChoice(null));
103             SessionStore.access().releaseBeanStore(chooser.getCurrentViewReference());
104         });
105         return result;
106     }
107 
108     public <T, C extends Chooser<T>> OnItemChosen<T> openChooser(ChooserDefinition<T, C> definition) {
109         return this.openChooser(definition, null);
110     }
111 
112     public static class OnItemChosen<T> extends CompletableFuture<ChooseResult<T>> {
113         private String action;
114 
115         public OnItemChosen<T> action(String action) {
116             this.action = action;
117             return this;
118         }
119 
120         public String getAction() {
121             return action;
122         }
123     }
124 
125     public interface ChooseHandler {
126         void action(String name);
127     }
128 
129     /**
130      * Holds the choice.
131      *
132      * @param <T>
133      *     choice item type.
134      */
135     @AllArgsConstructor(access = AccessLevel.PACKAGE)
136     public static class ChooseResult<T> {
137         private boolean isChosen;
138         private T choice;
139 
140         public static <T> ChooseResult<T> ofChoice(T choice) {
141             return new ChooseResult<>(choice != null, choice);
142         }
143 
144         public boolean isChosen() {
145             return this.isChosen;
146         }
147 
148         public Optional<T> getChoice() {
149             return Optional.ofNullable(choice);
150         }
151     }
152 }