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.ade.upload.client.ui;
029
030import org.opencms.ade.upload.client.Messages;
031import org.opencms.ade.upload.client.ui.css.I_CmsLayoutBundle;
032import org.opencms.gwt.client.CmsCoreProvider;
033import org.opencms.gwt.client.ui.css.I_CmsConstantsBundle;
034import org.opencms.gwt.client.ui.input.upload.CmsFileInfo;
035import org.opencms.gwt.client.ui.input.upload.CmsUploadButton;
036import org.opencms.gwt.shared.CmsListInfoBean;
037
038import java.util.ArrayList;
039import java.util.List;
040
041import com.google.gwt.core.client.JavaScriptObject;
042import com.google.gwt.core.client.JsArray;
043import com.google.gwt.user.client.ui.HTML;
044
045/**
046 * Provides the upload dialog for form data support.<p>
047 *
048 * @since 8.0.0
049 */
050public class CmsUploadDialogFormDataImpl extends A_CmsUploadDialog {
051
052    /** The highlighted state color. */
053    private String m_hightLightColor = I_CmsConstantsBundle.INSTANCE.css().backgroundColorHighlight();
054
055    /** The normal state color. */
056    private String m_normalColor = I_CmsLayoutBundle.INSTANCE.constants().css().backgroundColorDialog();
057
058    /**
059     * Default constructor.<p>
060     */
061    public CmsUploadDialogFormDataImpl() {
062
063        super();
064        addUploadZone(m_scrollPanel.getElement(), this);
065    }
066
067    /**
068     * @see org.opencms.ade.upload.client.ui.A_CmsUploadDialog#createInfoBean(org.opencms.gwt.client.ui.input.upload.CmsFileInfo)
069     */
070    @Override
071    public CmsListInfoBean createInfoBean(CmsFileInfo file) {
072
073        return new CmsListInfoBean(
074            file.getFileName(),
075            CmsUploadButton.formatBytes(file.getFileSize()) + " (" + getResourceType(file) + ")",
076            null);
077    }
078
079    /**
080     * @see org.opencms.ade.upload.client.ui.A_CmsUploadDialog#getFileSizeTooLargeMessage(org.opencms.gwt.client.ui.input.upload.CmsFileInfo)
081     */
082    @Override
083    public String getFileSizeTooLargeMessage(CmsFileInfo file) {
084
085        return Messages.get().key(
086            Messages.GUI_UPLOAD_FILE_TOO_LARGE_2,
087            CmsUploadButton.formatBytes(file.getFileSize()),
088            CmsUploadButton.formatBytes(Long.valueOf(CmsCoreProvider.get().getUploadFileSizeLimit()).intValue()));
089    }
090
091    /**
092     * @see org.opencms.ade.upload.client.ui.A_CmsUploadDialog#isTooLarge(org.opencms.gwt.client.ui.input.upload.CmsFileInfo)
093     */
094    @Override
095    public boolean isTooLarge(CmsFileInfo cmsFileInfo) {
096
097        long maxFileSize = CmsCoreProvider.get().getUploadFileSizeLimit();
098        if (maxFileSize < 0) {
099            return false;
100        }
101        return cmsFileInfo.getFileSize() > maxFileSize;
102    }
103
104    /**
105     * @see org.opencms.ade.upload.client.ui.A_CmsUploadDialog#updateSummary()
106     */
107    @Override
108    public void updateSummary() {
109
110        setContentLength(calculateContentLength());
111        StringBuffer buffer = new StringBuffer(64);
112        buffer.append("<p class=\"").append(
113            org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.uploadButton().dialogMessage()).append("\">");
114        buffer.append("<b>" + Messages.get().key(Messages.GUI_UPLOAD_SUMMARY_FILES_0) + "</b> ");
115        buffer.append(
116            Messages.get().key(
117                Messages.GUI_UPLOAD_SUMMARY_FILES_VALUE_3,
118                Integer.valueOf(getFilesToUpload().size()),
119                getFileText(),
120                CmsUploadButton.formatBytes(Long.valueOf(getContentLength()).intValue())));
121        buffer.append("</p>");
122        setSummaryHTML(buffer.toString());
123    }
124
125    /**
126     * Adds a javascript file array to the list of files to upload.<p>
127     *
128     * @param files a javascript file array
129     */
130    protected void addJsFiles(JavaScriptObject files) {
131
132        JsArray<CmsFileInfo> cmsFiles = files.cast();
133        List<CmsFileInfo> fileObjects = new ArrayList<CmsFileInfo>();
134        for (int i = 0; i < cmsFiles.length(); ++i) {
135            fileObjects.add(cmsFiles.get(i));
136        }
137        addFiles(fileObjects);
138    }
139
140    /**
141     * Returns the content length.<p>
142     *
143     * @return the content length
144     */
145    protected long calculateContentLength() {
146
147        int result = 0;
148        for (CmsFileInfo file : getFilesToUpload().values()) {
149            result += file.getFileSize();
150        }
151        return Long.valueOf(result).longValue();
152    }
153
154    /**
155     * @see org.opencms.ade.upload.client.ui.A_CmsUploadDialog#removeDragAndDropMessage()
156     */
157    @Override
158    protected void removeDragAndDropMessage() {
159
160        if (m_dragAndDropMessage != null) {
161            m_dragAndDropMessage.removeFromParent();
162            m_dragAndDropMessage = null;
163            m_normalColor = I_CmsLayoutBundle.INSTANCE.constants().css().backgroundColorDialog();
164            m_scrollPanel.getElement().getStyle().setBackgroundColor(m_normalColor);
165            doResize();
166        }
167    }
168
169    /**
170     * @see org.opencms.ade.upload.client.ui.A_CmsUploadDialog#setDragAndDropMessage()
171     */
172    @Override
173    protected void setDragAndDropMessage() {
174
175        if (m_dragAndDropMessage == null) {
176            m_dragAndDropMessage = new HTML();
177            m_dragAndDropMessage.setStyleName(I_CmsLayoutBundle.INSTANCE.uploadCss().dragAndDropMessage());
178            m_dragAndDropMessage.setText(Messages.get().key(Messages.GUI_UPLOAD_DRAG_AND_DROP_ENABLED_0));
179        }
180        getContentWrapper().add(m_dragAndDropMessage);
181        m_normalColor = I_CmsConstantsBundle.INSTANCE.css().notificationNormalBg();
182        m_scrollPanel.getElement().getStyle().setBackgroundColor(m_normalColor);
183        doResize();
184    }
185
186    /**
187     * Adds a upload drop zone to the given element.<p>
188     *
189     * @param element the element to add the upload zone
190     * @param dialog this dialog
191     */
192    private native void addUploadZone(JavaScriptObject element, CmsUploadDialogFormDataImpl dialog)/*-{
193
194        function dragover(event) {
195            event.stopPropagation();
196            event.preventDefault();
197            element.style.backgroundColor = dialog.@org.opencms.ade.upload.client.ui.CmsUploadDialogFormDataImpl::m_hightLightColor;
198        }
199
200        function dragleave(event) {
201            event.stopPropagation();
202            event.preventDefault();
203            element.style.backgroundColor = dialog.@org.opencms.ade.upload.client.ui.CmsUploadDialogFormDataImpl::m_normalColor;
204        }
205
206        function drop(event) {
207            event.preventDefault();
208            var dt = event.dataTransfer;
209            var files = dt.files;
210            element.style.backgroundColor = dialog.@org.opencms.ade.upload.client.ui.CmsUploadDialogFormDataImpl::m_normalColor;
211            dialog.@org.opencms.ade.upload.client.ui.CmsUploadDialogFormDataImpl::addJsFiles(Lcom/google/gwt/core/client/JavaScriptObject;)(files);
212        }
213
214        element.addEventListener("dragover", dragover, false);
215        element.addEventListener("dragexit", dragleave, false);
216        element.addEventListener("dragleave", dragleave, false);
217        element.addEventListener("dragend", dragleave, false);
218        element.addEventListener("drop", drop, false);
219
220    }-*/;
221}