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.gwt.client.ui.input.upload;
029
030import org.opencms.gwt.client.Messages;
031import org.opencms.gwt.client.ui.CmsProgressBar;
032import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
033import org.opencms.gwt.shared.CmsUploadProgessInfo;
034
035import java.util.List;
036
037import com.google.gwt.user.client.ui.FlexTable;
038import com.google.gwt.user.client.ui.FlowPanel;
039
040/**
041 * Provides the upload progress information.<p>
042 *
043 * Has a progressbar and a table for showing details.<p>
044 */
045public class CmsUploadProgressInfo extends FlowPanel {
046
047    /** The progress bar. */
048    private CmsProgressBar m_bar;
049
050    /** The table for showing upload details. */
051    private FlexTable m_fileinfo;
052
053    /** A sorted list of the filenames to upload. */
054    private List<String> m_orderedFilenamesToUpload;
055
056    /** Signals if the progress was set at least one time. */
057    private boolean m_started;
058
059    /** The upload content length. */
060    private long m_contentLength;
061
062    /**
063     * Default constructor.<p>
064     *
065     * @param orderedFilenamesToUpload the files
066     */
067    public CmsUploadProgressInfo(List<String> orderedFilenamesToUpload) {
068
069        // get a ordered list of filenames
070        m_orderedFilenamesToUpload = orderedFilenamesToUpload;
071
072        // create the progress bar
073        m_bar = new CmsProgressBar();
074
075        // create the file info table
076        m_fileinfo = new FlexTable();
077        m_fileinfo.addStyleName(I_CmsLayoutBundle.INSTANCE.uploadButton().fileInfoTable());
078
079        // arrange the progress info
080        addStyleName(I_CmsLayoutBundle.INSTANCE.uploadButton().progressInfo());
081        add(m_bar);
082        add(m_fileinfo);
083    }
084
085    /**
086     * Sets the upload content length.<p>
087     *
088     * @param contentLength the upload content length
089     */
090    public void setContentLength(long contentLength) {
091
092        m_contentLength = contentLength;
093    }
094
095    /**
096     * Finishes the state of the progress bar.<p>
097     */
098    public void finish() {
099
100        String length = CmsUploadButton.formatBytes(m_contentLength);
101        int fileCount = m_orderedFilenamesToUpload.size();
102        m_bar.setValue(100);
103        m_fileinfo.removeAllRows();
104        m_fileinfo.setHTML(0, 0, "<b>" + Messages.get().key(Messages.GUI_UPLOAD_FINISH_UPLOADED_0) + "</b>");
105        m_fileinfo.setText(
106            0,
107            1,
108            Messages.get().key(
109                Messages.GUI_UPLOAD_FINISH_UPLOADED_VALUE_4,
110                Integer.valueOf(fileCount),
111                Integer.valueOf(fileCount),
112                getFileText(),
113                length));
114    }
115
116    /**
117     * Sets the progress information.<p>
118     *
119     * @param info the progress info bean
120     */
121    public void setProgress(CmsUploadProgessInfo info) {
122
123        int currFile = info.getCurrentFile();
124
125        int currFileIndex = 0;
126        if (currFile == 0) {
127            // no files read so far
128        } else {
129            currFileIndex = currFile - 1;
130            if (currFileIndex >= m_orderedFilenamesToUpload.size()) {
131                currFileIndex = m_orderedFilenamesToUpload.size() - 1;
132            }
133        }
134
135        if (m_contentLength == 0) {
136            m_contentLength = info.getContentLength();
137        }
138
139        String currFilename = m_orderedFilenamesToUpload.get(currFileIndex);
140        String contentLength = CmsUploadButton.formatBytes(m_contentLength);
141        int fileCount = m_orderedFilenamesToUpload.size();
142        String readBytes = CmsUploadButton.formatBytes(getBytesRead(info.getPercent()));
143
144        m_bar.setValue(info.getPercent());
145
146        if (!m_started) {
147            m_started = true;
148            m_fileinfo.setHTML(0, 0, "<b>" + Messages.get().key(Messages.GUI_UPLOAD_PROGRESS_CURRENT_FILE_0) + "</b>");
149            m_fileinfo.setHTML(1, 0, "<b>" + Messages.get().key(Messages.GUI_UPLOAD_PROGRESS_UPLOADING_0) + "</b>");
150            m_fileinfo.setHTML(2, 0, "");
151
152            m_fileinfo.setText(0, 1, "");
153            m_fileinfo.setText(1, 1, "");
154            m_fileinfo.setText(2, 1, "");
155
156            m_fileinfo.getColumnFormatter().setWidth(0, "100px");
157        }
158
159        m_fileinfo.setText(0, 1, currFilename);
160        m_fileinfo.setText(
161            1,
162            1,
163            Messages.get().key(
164                Messages.GUI_UPLOAD_PROGRESS_CURRENT_VALUE_3,
165                Integer.valueOf(currFileIndex + 1),
166                Integer.valueOf(fileCount),
167                getFileText()));
168        m_fileinfo.setText(
169            2,
170            1,
171            Messages.get().key(Messages.GUI_UPLOAD_PROGRESS_UPLOADING_VALUE_2, readBytes, contentLength));
172    }
173
174    /**
175     * Returns the bytes that are read so far.<p>
176     *
177     * The total request size is larger than the sum of all file sizes that are uploaded.
178     * Because boundaries and the target folder or even some other information than only
179     * the plain file contents are submited to the server.<p>
180     *
181     * This method calculates the bytes that are read with the help of the file sizes.<p>
182     *
183     * @param percent the server side determined percentage
184     *
185     * @return the bytes that are read so far
186     */
187    private long getBytesRead(long percent) {
188
189        return percent != 0 ? (m_contentLength * percent) / 100 : 0;
190    }
191
192    /**
193     * Returns the file text.<p>
194     *
195     * @return the file text
196     */
197    private String getFileText() {
198
199        if (m_orderedFilenamesToUpload.size() > 1) {
200            return Messages.get().key(Messages.GUI_UPLOAD_FILES_PLURAL_0);
201        } else {
202            return Messages.get().key(Messages.GUI_UPLOAD_FILES_SINGULAR_0);
203        }
204    }
205}