001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (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.ui.client;
029
030import org.opencms.ade.upload.client.I_CmsUploadContext;
031import org.opencms.ade.upload.client.Messages;
032import org.opencms.gwt.client.ui.input.upload.CmsFileInput;
033import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton;
034import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler;
035import org.opencms.util.CmsStringUtil;
036
037import com.google.common.base.Supplier;
038import com.google.gwt.dom.client.Style.Unit;
039import com.google.gwt.event.logical.shared.CloseHandler;
040import com.google.gwt.user.client.ui.PopupPanel;
041
042/**
043 * Handles single file uploads. Allows to specify the target file name.<p>
044 */
045public class CmsSingleFileUploadHandler implements I_CmsUploadButtonHandler {
046
047    /** The dialog close handler. */
048    private CloseHandler<PopupPanel> m_closeHandler;
049
050    /** Factory for creating upload contexts. */
051    private Supplier<I_CmsUploadContext> m_contextFactory;
052
053    /** The replace dialog. */
054    private CmsSingleFileUploadDialog m_dialog;
055
056    /** The target file name. */
057    private String m_targetFileName;
058
059    /** The target file name prefix. */
060    private String m_targetFileNamePrefix;
061
062    /** The upload folder path. */
063    private String m_targetFolderPath;
064
065    /** The upload button. */
066    private I_CmsUploadButton m_uploadButton;
067
068    /** The dialog title. */
069    private String m_dialogTitle;
070
071    /**
072     * Constructor.<p>
073     *
074     * @param contextFactory the upload context factory
075     * @param dialogTitle the dialog title
076     */
077    public CmsSingleFileUploadHandler(Supplier<I_CmsUploadContext> contextFactory, String dialogTitle) {
078
079        m_contextFactory = contextFactory;
080        m_dialogTitle = dialogTitle;
081    }
082
083    /**
084     * Returns the new file name.<p>
085     *
086     * @param originalFileName the original file name
087     *
088     * @return the new file name
089     */
090    public String getFileName(String originalFileName) {
091
092        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_targetFileName)) {
093            return m_targetFileName;
094        } else if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_targetFileNamePrefix)) {
095            return m_targetFileNamePrefix + originalFileName;
096        } else {
097            return originalFileName;
098        }
099    }
100
101    /**
102     * Returns the targetFolderPath.<p>
103     *
104     * @return the targetFolderPath
105     */
106    public String getTargetFolderPath() {
107
108        return m_targetFolderPath;
109    }
110
111    /**
112     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler#initializeFileInput(org.opencms.gwt.client.ui.input.upload.CmsFileInput)
113     */
114    public void initializeFileInput(CmsFileInput fileInput) {
115
116        // important to set font-size as inline style, as IE7 and IE8 will not accept it otherwise
117        fileInput.getElement().getStyle().setFontSize(200, Unit.PX);
118        fileInput.getElement().getStyle().setProperty("minHeight", "200px");
119        fileInput.setAllowMultipleFiles(false);
120        fileInput.setName("replace");
121        fileInput.addStyleName(
122            org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.uploadButton().uploadFileInput());
123    }
124
125    /**
126     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler#onChange(org.opencms.gwt.client.ui.input.upload.CmsFileInput)
127     */
128    public void onChange(CmsFileInput fileInput) {
129
130        if (m_dialog == null) {
131            String dialogTitle = CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_dialogTitle)
132            ? m_dialogTitle
133            : Messages.get().key(Messages.GUI_UPLOAD_DIALOG_TITLE_1, getTargetFolderPath());
134            m_dialog = new CmsSingleFileUploadDialog(this, dialogTitle);
135            m_dialog.setContext(m_contextFactory.get());
136            m_dialog.center();
137            if (m_closeHandler != null) {
138                m_dialog.addCloseHandler(m_closeHandler);
139            }
140        } else if (m_uploadButton != null) {
141            m_uploadButton.createFileInput();
142        }
143        if (fileInput.getFiles().length == 1) {
144            m_dialog.setFileInput(fileInput);
145        }
146    }
147
148    /**
149     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler#setButton(org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton)
150     */
151    public void setButton(I_CmsUploadButton button) {
152
153        m_uploadButton = button;
154    }
155
156    /**
157     * Sets the dialog close handler.<p>
158     *
159     * @param closeHandler the close handler
160     */
161    public void setCloseHandler(CloseHandler<PopupPanel> closeHandler) {
162
163        m_closeHandler = closeHandler;
164        if (m_dialog != null) {
165            m_dialog.addCloseHandler(closeHandler);
166        }
167    }
168
169    /**
170     * Sets the target file name.<p>
171     *
172     * @param fileName the target file name
173     */
174    public void setTargetFileName(String fileName) {
175
176        m_targetFileName = fileName;
177    }
178
179    /**
180     * Sets the target file name prefix.<p>
181     *
182     * @param targetFileNamePrefix the target file name prefix to set
183     */
184    public void setTargetFileNamePrefix(String targetFileNamePrefix) {
185
186        m_targetFileNamePrefix = targetFileNamePrefix;
187    }
188
189    /**
190     * Sets the target folder path.<p>
191     *
192     * @param folderPath the target folder path
193     */
194    public void setTargetFolderPath(String folderPath) {
195
196        m_targetFolderPath = folderPath;
197    }
198}