001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.upload.client.ui;
029
030import org.opencms.ade.upload.client.I_CmsUploadContext;
031import org.opencms.gwt.client.CmsCoreProvider;
032import org.opencms.gwt.client.ui.CmsErrorDialog;
033import org.opencms.gwt.client.ui.input.upload.CmsFileInfo;
034import org.opencms.gwt.client.ui.input.upload.CmsFileInput;
035import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton;
036import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler;
037import org.opencms.gwt.shared.CmsUploadRestrictionInfo;
038
039import java.util.List;
040
041import com.google.common.base.Supplier;
042import com.google.gwt.core.client.GWT;
043import com.google.gwt.dom.client.Style.Unit;
044import com.google.gwt.event.logical.shared.CloseHandler;
045import com.google.gwt.event.shared.HandlerRegistration;
046import com.google.gwt.user.client.ui.PopupPanel;
047
048/**
049 * Default upload button handler which is used for the upload dialog.<p>
050 */
051public class CmsDialogUploadButtonHandler implements I_CmsUploadButtonHandler {
052
053    /** The upload button instance. */
054    private I_CmsUploadButton m_button;
055
056    /** The close handler for the upload dialog. */
057    private CloseHandler<PopupPanel> m_closeHandler;
058
059    /** The handler registration for the close handler of the dialog. */
060    private HandlerRegistration m_closeHandlerRegistration;
061
062    /** Factory for creating upload contexts. */
063    private Supplier<I_CmsUploadContext> m_contextFactory;
064
065    /** True if the the target folder should be treated as a root path. */
066    private boolean m_isTargetRootPath;
067
068    /** The post-create handler. **/
069    private String m_postCreateHandler;
070
071    /** The target folder for the upload dialog. */
072    private String m_targetFolder;
073
074    /** The upload dialog instance. */
075    private A_CmsUploadDialog m_uploadDialog;
076
077    /**
078     * Creates a new upload button handler.<p>
079     *
080     * @param contextFactory the context factory to use for upload contexts
081     */
082    public CmsDialogUploadButtonHandler(Supplier<I_CmsUploadContext> contextFactory) {
083
084        m_contextFactory = contextFactory;
085    }
086
087    /**
088     * Creates a new upload button handler.<p>
089     *
090     * @param contextFactory the context factory to use for upload contexts
091     * @param targetFolder the target folder
092     * @param isRootPath true fi the target folder is a root path
093     */
094    public CmsDialogUploadButtonHandler(
095        Supplier<I_CmsUploadContext> contextFactory,
096        String targetFolder,
097        boolean isRootPath) {
098
099        m_contextFactory = contextFactory;
100        m_targetFolder = targetFolder;
101        m_isTargetRootPath = isRootPath;
102    }
103
104    /**
105     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler#initializeFileInput(org.opencms.gwt.client.ui.input.upload.CmsFileInput)
106     */
107    public void initializeFileInput(CmsFileInput fileInput) {
108
109        CmsUploadRestrictionInfo restriction = CmsCoreProvider.get().getUploadRestriction();
110
111        if (m_targetFolder != null) {
112
113            String realTargetFolder = m_targetFolder;
114            if (!m_isTargetRootPath) {
115                realTargetFolder = CmsCoreProvider.get().addSiteRoot(m_targetFolder);
116            }
117
118            boolean enabled = restriction.isUploadEnabled(realTargetFolder);
119            m_button.setEnabled(enabled, "");
120            fileInput.setDisabled(!enabled);
121            String accept = restriction.getAcceptAttribute(realTargetFolder);
122            fileInput.setAccept(accept);
123        } else {
124            // CmsGwtLog.log("target folder is null");
125        }
126        // important to set font-size as inline style, as IE7 and IE8 will not accept it otherwise
127        fileInput.getElement().getStyle().setFontSize(200, Unit.PX);
128        fileInput.getElement().getStyle().setProperty("minHeight", "200px");
129        fileInput.setAllowMultipleFiles(true);
130        fileInput.setName("upload");
131        fileInput.addStyleName(
132            org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.uploadButton().uploadFileInput());
133    }
134
135    /**
136     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler#onChange(org.opencms.gwt.client.ui.input.upload.CmsFileInput)
137     */
138    public void onChange(CmsFileInput fileInput) {
139
140        if (m_uploadDialog == null) {
141            try {
142                m_uploadDialog = GWT.create(CmsUploadDialogImpl.class);
143                I_CmsUploadContext context = m_contextFactory.get();
144                m_uploadDialog.setContext(context);
145                updateDialog();
146                // the current upload button is located outside the dialog, reinitialize it with a new button handler instance
147                m_button.reinitButton(
148                    new CmsDialogUploadButtonHandler(m_contextFactory, m_targetFolder, m_isTargetRootPath));
149            } catch (Exception e) {
150                CmsErrorDialog.handleException(
151                    new Exception(
152                        "Deserialization of dialog data failed. This may be caused by expired java-script resources, please clear your browser cache and try again.",
153                        e));
154                return;
155            }
156        }
157        m_uploadDialog.addFileInput(fileInput);
158        m_button.createFileInput();
159    }
160
161    /**
162     * Opens the upload dialog for the given file references.<p>
163     *
164     * @param files the file references
165     */
166    public void openDialogWithFiles(List<CmsFileInfo> files) {
167
168        if (m_uploadDialog == null) {
169            try {
170                m_uploadDialog = GWT.create(CmsUploadDialogImpl.class);
171                I_CmsUploadContext context = m_contextFactory.get();
172                m_uploadDialog.setContext(context);
173                updateDialog();
174                if (m_button != null) {
175                    // the current upload button is located outside the dialog, reinitialize it with a new button handler instance
176                    m_button.reinitButton(
177                        new CmsDialogUploadButtonHandler(m_contextFactory, m_targetFolder, m_isTargetRootPath));
178                }
179            } catch (Exception e) {
180                CmsErrorDialog.handleException(
181                    new Exception(
182                        "Deserialization of dialog data failed. This may be caused by expired java-script resources, please clear your browser cache and try again.",
183                        e));
184                return;
185            }
186        }
187        m_uploadDialog.addFiles(files);
188        if (m_button != null) {
189            m_button.createFileInput();
190        }
191    }
192
193    /**
194     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler#setButton(org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton)
195     */
196    public void setButton(I_CmsUploadButton button) {
197
198        m_button = button;
199    }
200
201    /**
202     * Sets the close handler for the dialog.<p>
203     *
204     * @param closeHandler the close handler
205     */
206    public void setCloseHandler(CloseHandler<PopupPanel> closeHandler) {
207
208        m_closeHandler = closeHandler;
209        updateDialog();
210
211    }
212
213    /**
214     * Chooses whether the target folder should be interpreted as a root path.<p>
215     *
216     * @param isTargetRootPath true if the target folder should be treated as a root path
217     */
218    public void setIsTargetRootPath(boolean isTargetRootPath) {
219
220        m_isTargetRootPath = isTargetRootPath;
221    }
222
223    /**
224     * Sets the post-create handler.
225     *
226     * @param postCreateHandler the post-create handler
227     */
228    public void setPostCreateHandler(String postCreateHandler) {
229
230        m_postCreateHandler = postCreateHandler;
231    }
232
233    /**
234     * Sets the upload target folder.<p>
235     *
236     * @param targetFolder the upload target folder
237     */
238    public void setTargetFolder(String targetFolder) {
239
240        m_targetFolder = targetFolder;
241        updateDialog();
242    }
243
244    /**
245     * Sets the upload dialog instance.<p>
246     *
247     * @param uploadDialog the upload dialog instance
248     */
249    public void setUploadDialog(A_CmsUploadDialog uploadDialog) {
250
251        m_uploadDialog = uploadDialog;
252    }
253
254    /**
255     * Updates the dialog with the current close handler and target folder.<p>
256     */
257    protected void updateDialog() {
258
259        if (m_uploadDialog != null) {
260            if ((m_closeHandler != null)) {
261                if (m_closeHandlerRegistration != null) {
262                    m_closeHandlerRegistration.removeHandler();
263                }
264                m_closeHandlerRegistration = m_uploadDialog.addCloseHandler(m_closeHandler);
265            }
266            if ((m_targetFolder != null)) {
267                m_uploadDialog.setTargetFolder(m_targetFolder);
268            }
269            m_uploadDialog.setPostCreateHandler(m_postCreateHandler);
270            m_uploadDialog.setIsTargetRootPath(m_isTargetRootPath);
271            m_uploadDialog.updateHandler();
272        }
273    }
274}