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.galleries.client.preview;
029
030import org.opencms.ade.galleries.client.ui.CmsGalleryDialog;
031import org.opencms.ade.galleries.shared.CmsResourceInfoBean;
032import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode;
033import org.opencms.ade.galleries.shared.rpc.I_CmsPreviewService;
034import org.opencms.ade.galleries.shared.rpc.I_CmsPreviewServiceAsync;
035import org.opencms.gwt.client.CmsCoreProvider;
036import org.opencms.gwt.shared.property.CmsClientProperty;
037import org.opencms.util.CmsUUID;
038
039import com.google.gwt.core.client.GWT;
040import com.google.gwt.user.client.rpc.ServiceDefTarget;
041
042/**
043 * Preview dialog controller.<p>
044 *
045 * This class handles the communication between preview dialog and the server.
046 *
047 * @param <T> the resource info bean type
048 *
049 * @since 8.0.0
050 */
051public abstract class A_CmsResourcePreview<T extends CmsResourceInfoBean> implements I_CmsResourcePreview<T> {
052
053    /** The preview service. */
054    private static I_CmsPreviewServiceAsync m_previewService;
055
056    /** The info bean of the binary preview dialog. */
057    protected T m_infoBean;
058
059    /** The gallery dialog in which this preview is displayed. */
060    private CmsGalleryDialog m_galleryDialog;
061
062    /**
063     * Constructor.<p>
064     *
065     * @param galleryDialog the gallery dialog instance
066     */
067    protected A_CmsResourcePreview(CmsGalleryDialog galleryDialog) {
068
069        m_galleryDialog = galleryDialog;
070    }
071
072    /**
073     * Returns the preview service.<p>
074     *
075     * @return the preview service
076     */
077    protected static I_CmsPreviewServiceAsync getService() {
078
079        if (m_previewService == null) {
080            m_previewService = GWT.create(I_CmsPreviewService.class);
081            String serviceUrl = CmsCoreProvider.get().link("org.opencms.ade.galleries.CmsPreviewService.gwt");
082            ((ServiceDefTarget)m_previewService).setServiceEntryPoint(serviceUrl);
083        }
084        return m_previewService;
085    }
086
087    /**
088     * Removes the preview service reference.<p>
089     */
090    private static void clearService() {
091
092        m_previewService = null;
093    }
094
095    /**
096     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getGalleryDialog()
097     */
098    public CmsGalleryDialog getGalleryDialog() {
099
100        return m_galleryDialog;
101    }
102
103    /**
104     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getGalleryMode()
105     */
106    public GalleryMode getGalleryMode() {
107
108        return m_galleryDialog.getController().getDialogMode();
109    }
110
111    /**
112     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getHandler()
113     */
114    public abstract I_CmsPreviewHandler<T> getHandler();
115
116    /**
117     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getLocale()
118     */
119    public String getLocale() {
120
121        return m_galleryDialog.getController().getSearchLocale();
122    }
123
124    /**
125     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getResourcePath()
126     */
127    public String getResourcePath() {
128
129        return m_infoBean.getResourcePath();
130
131    }
132
133    /**
134     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getViewLink()
135     */
136    public String getViewLink() {
137
138        return getResourcePath();
139    }
140
141    /**
142     * @see org.opencms.ade.galleries.client.preview.I_CmsPreviewController#removePreview()
143     */
144    public void removePreview() {
145
146        getPreviewDialog().removeFromParent();
147        m_infoBean = null;
148        clearService();
149    }
150
151    /**
152     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#selectResource(java.lang.String, org.opencms.util.CmsUUID, java.lang.String)
153     */
154    public void selectResource(String resourcePath, CmsUUID structureId, String title) {
155
156        switch (getGalleryMode()) {
157            case widget:
158                if (getGalleryDialog().getWidgetHandler() != null) {
159                    getGalleryDialog().getWidgetHandler().setWidgetValue(resourcePath, structureId, null);
160                } else {
161                    CmsPreviewUtil.setResourcePath(resourcePath);
162                }
163                break;
164            case editor:
165                CmsPreviewUtil.setLink(resourcePath, title, null);
166                CmsPreviewUtil.closeDialog();
167                break;
168            case ade:
169            case view:
170            case adeView:
171            default:
172                //nothing to do here, should not be called
173                break;
174        }
175    }
176
177    /**
178     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#setDataInEditor()
179     */
180    public boolean setDataInEditor() {
181
182        return getHandler().setDataInEditor();
183    }
184
185    /**
186     * Sets the current resource within the editor or xml-content.<p>
187     */
188    public void setResource() {
189
190        selectResource(
191            m_infoBean.getResourcePath(),
192            m_infoBean.getStructureId(),
193            m_infoBean.getProperties().get(CmsClientProperty.PROPERTY_TITLE));
194    }
195
196    /**
197     * Calls the preview handler to display the given data.<p>
198     *
199     * @param resourceInfo the resource info data
200     */
201    public void showData(T resourceInfo) {
202
203        m_infoBean = resourceInfo;
204        getHandler().showData(resourceInfo);
205    }
206}