View Javadoc
1   /**
2    * This file Copyright (c) 2010-2014 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.dam.app.assets.field;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.dam.app.ui.field.upload.AssetUploadReceiver;
38  import info.magnolia.event.EventBus;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.NodeUtil;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.ui.api.action.ActionDefinition;
44  import info.magnolia.ui.api.context.UiContext;
45  import info.magnolia.ui.api.event.ChooseDialogEventBus;
46  import info.magnolia.ui.api.overlay.OverlayCloser;
47  import info.magnolia.ui.api.overlay.OverlayLayer.ModalityLevel;
48  import info.magnolia.ui.api.view.View;
49  import info.magnolia.ui.dialog.actionarea.ActionListener;
50  import info.magnolia.ui.dialog.actionarea.renderer.ActionRenderer;
51  import info.magnolia.ui.form.field.upload.UploadReceiver;
52  import info.magnolia.ui.form.field.upload.basic.BasicUploadProgressIndicator;
53  import info.magnolia.ui.framework.overlay.ViewAdapter;
54  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeItemId;
55  import info.magnolia.ui.workbench.event.SelectionChangedEvent;
56  
57  import java.util.Set;
58  
59  import javax.inject.Inject;
60  import javax.inject.Named;
61  import javax.jcr.RepositoryException;
62  
63  import com.vaadin.ui.Button;
64  import com.vaadin.ui.Button.ClickEvent;
65  import com.vaadin.ui.CssLayout;
66  import com.vaadin.ui.NativeButton;
67  import com.vaadin.ui.UI;
68  import com.vaadin.ui.Upload;
69  import com.vaadin.ui.Upload.FailedEvent;
70  import com.vaadin.ui.Upload.FailedListener;
71  import com.vaadin.ui.Upload.FinishedEvent;
72  import com.vaadin.ui.Upload.FinishedListener;
73  import com.vaadin.ui.Upload.ProgressListener;
74  import com.vaadin.ui.Upload.StartedEvent;
75  import com.vaadin.ui.Upload.StartedListener;
76  import com.vaadin.ui.Upload.SucceededEvent;
77  import com.vaadin.ui.Upload.SucceededListener;
78  
79  /**
80   * Renders and upload instead of a mere button.
81   */
82  public class UploadAssetActionRenderer implements ActionRenderer {
83  
84      private UiContext layer;
85  
86      private Upload upload;
87  
88      private UploadReceiver receiver;
89  
90      private ProgressPopup progressIndicator;
91  
92      private OverlayCloser progressIndicatorCloseHandle;
93  
94      private final SimpleTranslator i18n;
95  
96      /**
97       * @deprecated since version 5.2.2, more detailed c-tor should be used
98       * in order to handle upload button availability properly.
99       */
100     @Deprecated
101     public UploadAssetActionRenderer(UiContext layer, SimpleTranslator i18n) {
102         this.layer = layer;
103         this.i18n = i18n;
104         receiver = Components.newInstance(AssetUploadReceiver.class, i18n);
105     }
106 
107     @Inject
108     public UploadAssetActionRenderer(UiContext layer, SimpleTranslator i18n, @Named(ChooseDialogEventBus.NAME)EventBus eventBus) {
109         this.layer = layer;
110         this.i18n = i18n;
111         receiver = Components.newInstance(AssetUploadReceiver.class, i18n);
112         eventBus.addHandler(SelectionChangedEvent.class, new SelectionChangedEvent.Handler() {
113             @Override
114             public void onSelectionChanged(SelectionChangedEvent event) {
115                 Set<Object> itemIds = event.getItemIds();
116                 if (itemIds == null || itemIds.isEmpty()) {
117                     return;
118                 }
119                 try {
120                     JcrNodeItemId nodeItemId = (JcrNodeItemId) itemIds.iterator().next();
121                     javax.jcr.Node node = MgnlContext.getJCRSession(nodeItemId.getWorkspace()).getNodeByIdentifier(nodeItemId.getUuid());
122                     boolean uploadAllowed = itemIds.size() == 1;
123                     if (uploadAllowed) {
124                         uploadAllowed &= NodeUtil.isNodeType(node, NodeTypes.Folder.NAME) || NodeUtil.getAncestors(node).isEmpty();
125                     }
126                     upload.setEnabled(uploadAllowed);
127                 } catch (RepositoryException e) {
128                     upload.setEnabled(false);
129                 }
130             }
131         });
132     }
133 
134     @Override
135     public View start(final ActionDefinition action, final ActionListener listener) {
136         this.upload = new Upload(action.getLabel(), receiver) {
137             @Override
138             public void setCaption(String caption) {
139                 setButtonCaption(caption);
140             }
141         };
142 
143         this.upload.addStartedListener(new StartedListener() {
144             @Override
145             public void uploadStarted(StartedEvent event) {
146                 UI.getCurrent().setPollInterval(1000);
147                 progressIndicator = new ProgressPopup(upload, i18n);
148                 progressIndicatorCloseHandle = layer.openOverlay(new ViewAdapter(progressIndicator), ModalityLevel.NON_MODAL);
149                 progressIndicator.progressIndicator.setProgress(0);
150                 progressIndicator.progressIndicator.setVisible(true);
151             }
152         });
153 
154         this.upload.addFinishedListener(new FinishedListener() {
155             @Override
156             public void uploadFinished(FinishedEvent event) {
157                 UI.getCurrent().setPollInterval(-1);
158                 progressIndicatorCloseHandle.close();
159             }
160         });
161 
162         this.upload.addProgressListener(new ProgressListener() {
163             @Override
164             public void updateProgress(long readBytes, long contentLength) {
165                 progressIndicator.progressIndicator.refreshLayout(readBytes, contentLength, receiver.getFileName());
166             }
167         });
168 
169 
170         this.upload.addFailedListener(new FailedListener() {
171             @Override
172             public void uploadFailed(FailedEvent event) {
173                 progressIndicatorCloseHandle.close();
174             }
175         });
176 
177         this.upload.addSucceededListener(new SucceededListener() {
178             @Override
179             public void uploadSucceeded(SucceededEvent event) {
180                 listener.onActionFired(action.getName(), upload.getReceiver());
181             }
182         });
183 
184         this.upload.setImmediate(true);
185         return new ViewAdapter(upload);
186     }
187 
188 
189     private static class ProgressPopup extends CssLayout {
190 
191         public BasicUploadProgressIndicator progressIndicator;
192 
193         private ProgressPopup(final Upload upload, final SimpleTranslator i18n) {
194 
195             addStyleName("direct-upload-progress-indicator");
196             addStyleName("upload-image-field");
197             CssLayout indicatorWrapper = new CssLayout();
198             indicatorWrapper.addStyleName("in-progress");
199             addComponent(indicatorWrapper);
200             String inProgressCaption = "field.upload.basic.uploading.file";
201             String inProgressRatioCaption = "field.upload.basic.uploaded.file";
202             this.progressIndicator = new BasicUploadProgressIndicator(inProgressCaption, inProgressRatioCaption, i18n);
203             indicatorWrapper.addComponent(progressIndicator);
204             Button cancelButton = new NativeButton(null, new Button.ClickListener() {
205                 @Override
206                 public void buttonClick(ClickEvent event) {
207                     if (upload.isUploading()) {
208                         upload.interruptUpload();
209                     }
210                 }
211             });
212             cancelButton.addStyleName("cancel");
213             indicatorWrapper.addComponent(cancelButton);
214         }
215     }
216 }