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 info.magnolia.ui.DialogBuilder;
37  import info.magnolia.ui.api.ioc.UiContextScoped;
38  import info.magnolia.ui.framework.chooser.Chooser;
39  import info.magnolia.ui.framework.chooser.definition.ChooserDefinition;
40  import info.magnolia.ui.framework.ioc.UiContextBoundComponentProvider;
41  
42  import java.util.Arrays;
43  import java.util.Optional;
44  import java.util.concurrent.CompletableFuture;
45  
46  import javax.inject.Inject;
47  
48  import org.apache.commons.lang3.StringUtils;
49  
50  import com.vaadin.ui.Button;
51  import com.vaadin.ui.Window;
52  
53  import lombok.AccessLevel;
54  import lombok.AllArgsConstructor;
55  
56  /**
57   * Simple utility for handling the lifecycle of {@link Chooser}s.
58   */
59  @UiContextScoped
60  public class ChooserController {
61  
62      private final UiContextBoundComponentProvider parentComponentProvider;
63  
64      @Inject
65      public ChooserController(UiContextBoundComponentProvider parentComponentProvider) {
66          this.parentComponentProvider = parentComponentProvider;
67      }
68  
69      private <T> CompletableFuture<ChooseResult<T>> openChooser(Chooser<T> chooser) {
70          CompletableFuture<ChooseResult<T>> result = new CompletableFuture<>();
71  
72          Button cancel = new Button("Cancel");
73          Button choose = new Button("Select");
74  
75          cancel.addStyleName("v-button-secondary");
76          choose.addStyleName("v-button-commit");
77  
78          Window chooserWindow = DialogBuilder.dialog()
79                  .withTitle(StringUtils.defaultIfBlank(chooser.getTitle(), "Choose"))
80                  .withContent(chooser.asVaadinComponent())
81                  .withActions(Arrays.asList(cancel, choose))
82                  .buildAndOpen();
83  
84          chooserWindow.setWidth("500px");
85          chooserWindow.setHeight("500px");
86          chooserWindow.addCloseListener(e -> chooser.destroy());
87  
88          cancel.addClickListener(e -> {
89              result.complete(ChooseResult.ofNoChoice());
90              chooserWindow.close();
91          });
92  
93          choose.addClickListener(event -> {
94              result.complete(ChooseResult.ofChoice(chooser.getChoice()));
95              chooserWindow.close();
96          });
97  
98          return result;
99      }
100 
101     public <T, C extends Chooser<T>> CompletableFuture<ChooseResult<T>> openChooser(ChooserDefinition<T, C> definition) {
102         return this.openChooser(definition, null);
103     }
104 
105     public <T, C extends Chooser<T>> CompletableFuture<ChooseResult<T>> openChooser(ChooserDefinition<T, C> definition, T initialChoice) {
106         Chooser<T> chooser = this.parentComponentProvider.inChildContext(definition).newInstance(definition.getImplementationClass());
107         chooser.setChoice(initialChoice);
108         return openChooser(chooser);
109     }
110 
111     /**
112      * Holds the choice.
113      *
114      * @param <T>
115      *     choice item type.
116      */
117     @AllArgsConstructor(access = AccessLevel.PRIVATE)
118     public static class ChooseResult<T> {
119         private boolean isChosen;
120         private T choice;
121 
122         static <T> ChooseResult<T> ofNoChoice() {
123             return new ChooseResult<>(false, null);
124         }
125 
126         static <T> ChooseResult<T> ofChoice(T choice) {
127             return new ChooseResult<>(true, choice);
128         }
129 
130         public boolean isChosen() {
131             return this.isChosen;
132         }
133 
134         public Optional<T> getChoice() {
135             return Optional.ofNullable(choice);
136         }
137     }
138 }