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().setContent(() -> field);
125 
126         if (StringUtils.isNotBlank(selectedItemId)) {
127             Object itemId = contentConnector.getItemIdByUrlFragment(selectedItemId);
128             if (itemId != null) {
129                 field.setValue(itemId);
130             }
131         }
132 
133         // Remove caption on workbench field
134         field.setCaption(null);
135         field.setSizeFull();
136 
137         getView().setCaption(definition.getLabel());
138         getView().setClosable(true);
139 
140         final OverlayCloser closer = appContext.openOverlay(getView(), getView().getModalityLevel());
141         getView().addDialogCloseHandler(dialogView -> closer.close());
142 
143         getView().getContentView().asVaadinComponent().addStyleName("choose-dialog");
144 
145         return getView();
146     }
147 
148     @Override
149     public ChooseDialogView getView() {
150         return (ChooseDialogView) super.getView();
151     }
152 
153     @Override
154     public Object[] getActionParameters(String actionName) {
155         Set<Object> selected = new HashSet<>();
156         if (chosenItemId == null) {
157             chosenItemId = contentConnector.getDefaultItemId();
158         }
159         selected.add(chosenItemId);
160         Item item = contentConnector.getItem(chosenItemId);
161         return new Object[] { actionName, item, ChooseDialogPresenterImpl.this, field, getView(), callback, selected };
162     }
163 
164     @Override
165     protected DialogActionExecutor getExecutor() {
166         return (DialogActionExecutor) super.getExecutor();
167     }
168 
169     @Override
170     protected void onCancel () {
171         if (callback != null) {
172             callback.onCancel();
173         }
174     }
175 }