View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.sample.app.main;
35  
36  import static java.util.stream.Collectors.toList;
37  
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.sample.app.main.chooser.BirdAttributeChooser;
42  import info.magnolia.ui.api.app.AppController;
43  import info.magnolia.ui.api.app.registry.AppDescriptorRegistry;
44  import info.magnolia.ui.api.context.UiContext;
45  import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;
46  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
47  import info.magnolia.ui.datasource.jcr.SessionReattachingNodeWrapper;
48  import info.magnolia.ui.framework.chooser.definition.AppAwareWorkbenchChooserDefinition;
49  import info.magnolia.ui.framework.chooser.definition.ChooserDefinition;
50  import info.magnolia.ui.framework.chooser.definition.SingleItemWorkbenchChooserDefinition;
51  import info.magnolia.ui.framework.overlay.ChooserController;
52  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
53  
54  import java.util.Collections;
55  
56  import javax.inject.Inject;
57  import javax.jcr.Node;
58  import javax.jcr.RepositoryException;
59  
60  import com.vaadin.data.ValueProvider;
61  import com.vaadin.ui.Button;
62  import com.vaadin.ui.Component;
63  import com.vaadin.v7.ui.Label;
64  import com.vaadin.v7.ui.VerticalLayout;
65  
66  /**
67   * View implementation of navigation view.
68   */
69  public class NavigationViewImpl implements NavigationView {
70  
71      private Listener listener;
72  
73      private VerticalLayout layout;
74  
75      private final AppController appController;
76      private final AppDescriptorRegistry appDescriptorRegistry;
77  
78      private final SimpleTranslator i18n;
79  
80      private final UiContext context;
81      private final ChooserController chooserController;
82  
83      @Inject
84      public NavigationViewImpl(AppController appController, AppDescriptorRegistry appDescriptorRegistry, SimpleTranslator i18n, UiContext context, ChooserController chooserController) {
85          this.appController = appController;
86          this.appDescriptorRegistry = appDescriptorRegistry;
87          this.i18n = i18n;
88          this.context = context;
89          this.chooserController = chooserController;
90      }
91  
92      @Override
93      public void setListener(Listener listener) {
94          this.listener = listener;
95      }
96  
97      @Override
98      public Component asVaadinComponent() {
99          if (layout == null) {
100             layout = new VerticalLayout();
101             layout.setMargin(true);
102             layout.setSpacing(true);
103             layout.addComponent(new Label(i18n.translate("sample.app.navigation.label")));
104 
105             layout.addComponent(createButton("Alpha"));
106             layout.addComponent(createButton("Bravo"));
107             layout.addComponent(createButton("Charlie"));
108             layout.addComponent(createButton("Delta"));
109             layout.addComponent(createButton("Echo"));
110 
111             layout.addComponent(new Button("Choose bird", e -> {
112                 ChooserDefinition<String, BirdAttributeChooser> chooserDefinition = new ChooserDefinition<>();
113                 chooserDefinition.setTitle("Booze a chird");
114                 chooserDefinition.setImplementationClass(BirdAttributeChooser.class);
115 
116                 chooserController.openChooser(chooserDefinition).whenCompleteAsync((result, err) -> {
117                     if (err != null) {
118                         throw new RuntimeException(err);
119                     }
120 
121                     context.openNotification(MessageStyleTypeEnum.INFO, true, "Your bird: " + result.getChoice().orElse("(nada)"));
122                 });
123             }));
124 
125             layout.addComponent(new Button("Definition-based workbench chooser", e -> {
126                 AppAwareWorkbenchChooserDefinition<JcrDatasourceDefinition, Node> chooserDefinition = new AppAwareWorkbenchChooserDefinition<>(appDescriptorRegistry);
127                 chooserDefinition.setTitle("Choose a thing™ ❤");
128                 chooserDefinition.setAppName("content-app");
129 
130                 ConfiguredColumnDefinition<Node> columnDefinition = new ConfiguredColumnDefinition<>();
131                 columnDefinition.setName("name");
132                 columnDefinition.setCaption("Name");
133                 columnDefinition.setValueProvider(NameValueProvider.class);
134                 chooserDefinition.setColumns(Collections.singletonList(columnDefinition));
135 
136                 chooserController.openChooser(chooserDefinition, Collections.emptySet())
137                         .whenComplete((result, err) -> {
138                             if (err != null) {
139                                 throw new RuntimeException(err);
140                             }
141 
142                             context.openNotification(MessageStyleTypeEnum.INFO, true,
143                                     "Your selection: " + (result.isChosen() ?
144                                             result.getChoice()
145                                                     .map(x -> String.join(", ", x.stream().map(NodeUtil::getPathIfPossible).collect(toList())))
146                                                     .orElse("(nothing)") :
147                                             "(cancelled)"));
148                         });
149             }));
150 
151             layout.addComponent(new Button("Definition-based single-item workbench chooser", e -> {
152                 JcrDatasourceDefinition datasourceDef = new JcrDatasourceDefinition();
153                 datasourceDef.setWorkspace("config");
154                 datasourceDef.getAllowedNodeTypes().add("nt:base");
155 
156                 final ConfiguredColumnDefinition<Node> nameColDef = new ConfiguredColumnDefinition<>();
157                 nameColDef.setCaption("Name");
158                 nameColDef.setName("name");
159                 nameColDef.setValueProvider(NameValueProvider.class);
160 
161                 final AppAwareWorkbenchChooserDefinition<JcrDatasourceDefinition, Node> workbenchChooserDefinition = new AppAwareWorkbenchChooserDefinition<>(appDescriptorRegistry);
162                 workbenchChooserDefinition.setTitle("Choose only one thing");
163                 workbenchChooserDefinition.setDatasource(datasourceDef);
164                 workbenchChooserDefinition.setColumns(Collections.singletonList(nameColDef));
165 
166                 SingleItemWorkbenchChooserDefinition<JcrDatasourceDefinition, Node> chooserDefinition = new SingleItemWorkbenchChooserDefinition<>();
167                 chooserDefinition.setWorkbenchChooser(workbenchChooserDefinition);
168 
169                 try {
170                     Node coreModuleNode = new SessionReattachingNodeWrapper(
171                             MgnlContext.getJCRSession("config").getNode("/modules/core"));
172 
173                     chooserController.openChooser(chooserDefinition, coreModuleNode)
174                             .whenComplete((result, err) -> {
175                                 if (err != null) {
176                                     throw new RuntimeException(err);
177                                 }
178 
179                                 context.openNotification(MessageStyleTypeEnum.INFO, true,
180                                         "Your selection: " + (result.isChosen() && result.getChoice().isPresent() ?
181                                                 NodeUtil.getPathIfPossible(result.getChoice().get()) :
182                                                 "(none)"));
183                             });
184                 } catch (RepositoryException repex) {
185                     throw new RuntimeException(repex);
186                 }
187             }));
188         }
189         return layout;
190     }
191 
192     private Button createButton(final String name) {
193         return new Button(i18n.translate("sample.app.navigation.button.select") + " " + name, new Button.ClickListener() {
194 
195             @Override
196             public void buttonClick(Button.ClickEvent event) {
197                 listener.onItemSelected(name);
198             }
199         });
200     }
201 
202     private static class NameValueProvider implements ValueProvider<Node, String> {
203 
204         @Inject
205         public NameValueProvider() {
206         }
207 
208         @Override
209         public String apply(Node node) {
210             return NodeUtil.getName(node);
211         }
212     }
213 }