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;
029
030import org.opencms.ade.upload.client.ui.A_CmsUploadDialog;
031import org.opencms.ade.upload.client.ui.CmsUploadDialogImpl;
032import org.opencms.gwt.client.A_CmsEntryPoint;
033import org.opencms.gwt.client.CmsCoreProvider;
034import org.opencms.gwt.client.ui.CmsErrorDialog;
035
036import java.util.List;
037
038import com.google.gwt.core.client.GWT;
039import com.google.gwt.user.client.Window;
040
041/**
042 * Upload dialog entry class.<p>
043 *
044 * @since 8.0.0
045 */
046public class CmsUpload extends A_CmsEntryPoint {
047
048    /** Name of exported dialog close function. */
049    private static final String FUNCTION_OPEN_UPLOAD_DIALOG = "cms_ade_openUploadDialog";
050
051    /**
052     * Exports the open dialog method.<p>
053     */
054    public static native void exportOpenUploadDialog() /*-{
055
056        $wnd[@org.opencms.ade.upload.client.CmsUpload::FUNCTION_OPEN_UPLOAD_DIALOG] = function(
057                uploadTarget) {
058            @org.opencms.ade.upload.client.CmsUpload::openDialog(Ljava/lang/String;)(uploadTarget);
059        };
060
061    }-*/;
062
063    /**
064     * Opens an empty upload dialog.<p>
065     *
066     * @param uploadTarget the target folder
067     */
068    private static void openDialog(String uploadTarget) {
069
070        try {
071            A_CmsUploadDialog dialog = GWT.create(CmsUploadDialogImpl.class);
072            dialog.setContext(new I_CmsUploadContext() {
073
074                public void onUploadFinished(List<String> uploadedFiles) {
075
076                    Window.Location.reload();
077                }
078            });
079            dialog.setTargetFolder(uploadTarget);
080            dialog.loadAndShow();
081        } catch (Exception e) {
082            CmsErrorDialog.handleException(
083                new Exception(
084                    "Deserialization of dialog data failed. This may be caused by expired java-script resources, please clear your browser cache and try again.",
085                    e));
086        }
087    }
088
089    /**
090     * @see org.opencms.gwt.client.A_CmsEntryPoint#onModuleLoad()
091     */
092    @Override
093    public void onModuleLoad() {
094
095        super.onModuleLoad();
096        if ((getDialogMode() != null) && getDialogMode().equals("button")) {
097            exportOpenUploadDialog();
098        } else {
099            try {
100                A_CmsUploadDialog dialog = GWT.create(CmsUploadDialogImpl.class);
101                I_CmsUploadContext context = new I_CmsUploadContext() {
102
103                    public void onUploadFinished(List<String> uploadedFiles) {
104
105                        String closeLink = getCloseLink() + "?resource=";
106                        Window.Location.assign(CmsCoreProvider.get().link(closeLink));
107                    }
108                };
109                dialog.setContext(context);
110                dialog.setTargetFolder(getTargetFolder());
111                dialog.loadAndShow();
112            } catch (Exception e) {
113                CmsErrorDialog.handleException(
114                    new Exception(
115                        "Deserialization of dialog data failed. This may be caused by expired java-script resources, please clear your browser cache and try again.",
116                        e));
117            }
118        }
119    }
120
121    /**
122     * Retrieves the close link global variable as a string.<p>
123     *
124     * @return the close link
125     */
126    protected native String getCloseLink() /*-{
127
128        return $wnd[@org.opencms.gwt.shared.I_CmsUploadConstants::ATTR_CLOSE_LINK];
129
130    }-*/;
131
132    /**
133     * Retrieves the dialog mode global variable as a string.<p>
134     *
135     * @return the dialog mode
136     */
137    protected native String getDialogMode() /*-{
138
139        return $wnd[@org.opencms.gwt.shared.I_CmsUploadConstants::ATTR_DIALOG_MODE];
140
141    }-*/;
142
143    /**
144     * Retrieves the target folder global variable as a string.<p>
145     *
146     * @return the target folder
147     */
148    private native String getTargetFolder() /*-{
149
150        return $wnd[@org.opencms.gwt.shared.I_CmsUploadConstants::VAR_TARGET_FOLDER];
151
152    }-*/;
153}