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.ui.dialog.choosedialog;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.i18nsystem.I18nizer;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.ComponentProvider;
40  import info.magnolia.ui.api.app.ChooseDialogCallback;
41  import info.magnolia.ui.api.context.UiContext;
42  import info.magnolia.ui.api.overlay.OverlayCloser;
43  import info.magnolia.ui.dialog.BaseDialogPresenter;
44  import info.magnolia.ui.dialog.actionarea.DialogActionExecutor;
45  import info.magnolia.ui.dialog.definition.ChooseDialogDefinition;
46  import info.magnolia.ui.dialog.definition.DialogDefinition;
47  import info.magnolia.ui.form.field.factory.FieldFactory;
48  import info.magnolia.ui.form.field.factory.FieldFactoryFactory;
49  import info.magnolia.ui.vaadin.integration.NullItem;
50  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
51  
52  import java.util.HashSet;
53  import java.util.Set;
54  
55  import javax.inject.Inject;
56  
57  import org.apache.commons.lang3.StringUtils;
58  
59  import com.vaadin.v7.data.Item;
60  import com.vaadin.v7.data.Property.ValueChangeListener;
61  import com.vaadin.v7.ui.Field;
62  
63  /**
64   * Factory for creating workbench choose dialog presenters.
65   */
66  public class ChooseDialogPresenterImpl extends BaseDialogPresenter implements ChooseDialogPresenter {
67  
68      private FieldFactoryFactory fieldFactoryFactory;
69  
70      private ContentConnector contentConnector;
71  
72      private Object chosenItemId;
73  
74      private ChooseDialogCallback callback;
75  
76      private Field<Object> field;
77  
78      @Inject
79      public ChooseDialogPresenterImpl(
80              FieldFactoryFactory fieldFactoryFactory,
81              ComponentProvider componentProvider,
82              DialogActionExecutor executor,
83              ChooseDialogView view,
84              I18nizer i18nizer,
85              SimpleTranslator i18n,
86              ContentConnector contentConnector) {
87          super(componentProvider, executor, view, i18nizer, i18n);
88          this.fieldFactoryFactory = fieldFactoryFactory;
89          this.contentConnector = contentConnector;
90          this.componentProvider = componentProvider;
91      }
92  
93      /**
94       * @deprecated since 5.5.5 - use {@link #ChooseDialogPresenterImpl(FieldFactoryFactory, ComponentProvider, DialogActionExecutor, ChooseDialogView, I18nizer, SimpleTranslator, ContentConnector)} instead.
95       */
96      @Deprecated
97      public ChooseDialogPresenterImpl(
98              FieldFactoryFactory fieldFactoryFactory,
99              ComponentProvider componentProvider,
100             I18nContentSupport i18nContentSupport,
101             DialogActionExecutor executor,
102             ChooseDialogView view,
103             I18nizer i18nizer,
104             SimpleTranslator i18n,
105             ContentConnector contentConnector) {
106         this(fieldFactoryFactory, componentProvider, executor, view, i18nizer, i18n, contentConnector);
107     }
108 
109     @Override
110     public ChooseDialogView start(DialogDefinition definition, UiContext uiContext) {
111         getExecutor().setDialogDefinition(definition);
112         return (ChooseDialogView) super.start(definition, uiContext);
113     }
114 
115     @Override
116     public ChooseDialogView start(ChooseDialogCallback callback, ChooseDialogDefinition definition, UiContext appContext, String selectedItemId) {
117         start(definition, appContext);
118         this.callback = callback;
119 
120         final FieldFactory formField = fieldFactoryFactory.createFieldFactory(definition.getField(), new NullItem());
121         formField.setComponentProvider(componentProvider);
122         this.field = (Field<Object>) formField.createField();
123         field.addValueChangeListener((ValueChangeListener) event -> chosenItemId = event.getProperty().getValue());
124         getView().setCaption(definition.getLabel());
125         getView().setContent(() -> field);
126 
127         if (StringUtils.isNotBlank(selectedItemId)) {
128             Object itemId = contentConnector.getItemIdByUrlFragment(selectedItemId);
129             if (itemId != null) {
130                 field.setValue(itemId);
131             }
132         }
133 
134         final OverlayCloser closer = appContext.openOverlay(getView(), getView().getModalityLevel());
135         getView().setCaption(definition.getLabel());
136         getView().addDialogCloseHandler(dialogView -> closer.close());
137         getView().setClosable(true);
138 
139         return getView();
140     }
141 
142     @Override
143     public ChooseDialogView getView() {
144         return (ChooseDialogView) super.getView();
145     }
146 
147     @Override
148     public Object[] getActionParameters(String actionName) {
149         Set<Object> selected = new HashSet<>();
150         if (chosenItemId == null) {
151             chosenItemId = contentConnector.getDefaultItemId();
152         }
153         selected.add(chosenItemId);
154         Item item = contentConnector.getItem(chosenItemId);
155         return new Object[] { actionName, item, ChooseDialogPresenterImpl.this, field, getView(), callback, selected };
156     }
157 
158     @Override
159     protected DialogActionExecutor getExecutor() {
160         return (DialogActionExecutor) super.getExecutor();
161     }
162 
163     @Override
164     protected void onCancel () {
165         if (callback != null) {
166             callback.onCancel();
167         }
168     }
169 }