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.preview.ui.CmsImagePreviewDialog;
031import org.opencms.ade.galleries.client.ui.CmsGalleryDialog;
032import org.opencms.ade.galleries.shared.CmsImageInfoBean;
033import org.opencms.ade.galleries.shared.I_CmsImagePreviewProvider;
034import org.opencms.gwt.client.CmsCoreProvider;
035import org.opencms.gwt.client.rpc.CmsRpcAction;
036import org.opencms.gwt.client.util.I_CmsSimpleCallback;
037import org.opencms.util.CmsUUID;
038
039import java.util.HashMap;
040import java.util.Map;
041
042import com.google.gwt.user.client.Command;
043import com.google.gwt.user.client.ui.FlowPanel;
044
045/**
046 * The image resource preview.<p>
047 *
048 * @since 8.0.0
049 */
050public final class CmsImageResourcePreview extends A_CmsResourcePreview<CmsImageInfoBean> {
051
052    /** The image preview handler. */
053    private CmsImagePreviewHandler m_handler;
054
055    /** The preview dialog widget. */
056    private CmsImagePreviewDialog m_previewDialog;
057
058    /**
059     * Constructor.<p>
060     *
061     * @param galleryDialog the gallery dialog
062     */
063    public CmsImageResourcePreview(CmsGalleryDialog galleryDialog) {
064
065        super(galleryDialog);
066    }
067
068    /**
069     * @see org.opencms.ade.galleries.client.preview.A_CmsResourcePreview#getHandler()
070     */
071    @Override
072    public I_CmsPreviewHandler<CmsImageInfoBean> getHandler() {
073
074        if (m_handler == null) {
075            throw new UnsupportedOperationException("Preview handler not initialized");
076        }
077        return m_handler;
078    }
079
080    /**
081     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getPreviewDialog()
082     */
083    public CmsImagePreviewDialog getPreviewDialog() {
084
085        return m_previewDialog;
086    }
087
088    /**
089     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#getPreviewName()
090     */
091    public String getPreviewName() {
092
093        return I_CmsImagePreviewProvider.PREVIEW_NAME;
094    }
095
096    /**
097     * @see org.opencms.ade.galleries.client.preview.A_CmsResourcePreview#getViewLink()
098     */
099    @Override
100    public String getViewLink() {
101
102        return m_infoBean.getViewLink();
103    }
104
105    /**
106     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#loadResourceInfo(java.lang.String)
107     */
108    public void loadResourceInfo(final String resourcePath) {
109
110        CmsRpcAction<CmsImageInfoBean> action = new CmsRpcAction<CmsImageInfoBean>() {
111
112            /**
113             * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
114             */
115            @Override
116            public void execute() {
117
118                A_CmsResourcePreview.getService().getImageInfo(resourcePath, getLocale(), this);
119            }
120
121            /**
122             * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
123             */
124            @Override
125            protected void onResponse(CmsImageInfoBean result) {
126
127                result.setSelectedPath(resourcePath);
128                result.setViewLink(result.getViewLink());
129                showData(result);
130            }
131        };
132        action.execute();
133
134    }
135
136    /**
137     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#openPreview(java.lang.String, boolean)
138     */
139    public void openPreview(String resourcePath, boolean disableSelection) {
140
141        if (m_previewDialog != null) {
142            m_previewDialog.removeFromParent();
143        }
144        FlowPanel parentPanel = getGalleryDialog().getParentPanel();
145        m_previewDialog = new CmsImagePreviewDialog(
146            getGalleryDialog().getController().getDialogMode(),
147            parentPanel.getOffsetHeight(),
148            parentPanel.getOffsetWidth(),
149            disableSelection);
150        // initialize the controller and controller handler
151        m_handler = new CmsImagePreviewHandler(this);
152        m_previewDialog.init(m_handler);
153        CmsPreviewUtil.exportFunctions(getPreviewName(), this);
154        parentPanel.add(m_previewDialog);
155        m_handler.getGalleryDialog().setPreviewVisible(true);
156        //load preview data
157        loadResourceInfo(resourcePath);
158    }
159
160    /**
161     * @see org.opencms.ade.galleries.client.preview.A_CmsResourcePreview#removePreview()
162     */
163    @Override
164    public void removePreview() {
165
166        super.removePreview();
167        m_handler = null;
168    }
169
170    /**
171     * @see org.opencms.ade.galleries.client.preview.I_CmsResourcePreview#saveProperties(java.util.Map, com.google.gwt.user.client.Command)
172     */
173    public void saveProperties(final Map<String, String> properties, final Command afterSaveCallback) {
174
175        CmsRpcAction<CmsImageInfoBean> action = new CmsRpcAction<CmsImageInfoBean>() {
176
177            /**
178             * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
179             */
180            @Override
181            public void execute() {
182
183                getService().updateImageProperties(getResourcePath(), getLocale(), properties, this);
184            }
185
186            /**
187             * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
188             */
189            @Override
190            protected void onResponse(CmsImageInfoBean result) {
191
192                showData(result);
193                if (afterSaveCallback != null) {
194                    afterSaveCallback.execute();
195                }
196            }
197        };
198        action.execute();
199
200    }
201
202    /**
203     * @see org.opencms.ade.galleries.client.preview.A_CmsResourcePreview#selectResource(java.lang.String, org.opencms.util.CmsUUID, java.lang.String)
204     */
205    @Override
206    public void selectResource(final String resourcePath, final CmsUUID structureId, final String title) {
207
208        switch (getGalleryMode()) {
209            case widget:
210                getInitialCroppingParameter(resourcePath, new I_CmsSimpleCallback<CmsCroppingParamBean>() {
211
212                    public void execute(CmsCroppingParamBean param) {
213
214                        if (getGalleryDialog().getWidgetHandler() != null) {
215                            getGalleryDialog().getWidgetHandler().setWidgetValue(resourcePath, structureId, param);
216                        } else {
217                            if (CmsPreviewUtil.isAdvancedWidget()) {
218                                CmsPreviewUtil.setVfsImage(
219                                    resourcePath,
220                                    param.getScaleParam(),
221                                    param.getFormatName(),
222                                    param.getRatio() + "");
223                            } else {
224                                CmsPreviewUtil.setResourcePath(
225                                    resourcePath
226                                        + ((param.isCropped() || param.isScaled()) ? "?" + param.toString() : ""));
227                            }
228                        }
229                    }
230                });
231
232                break;
233            case editor:
234                getInitialCroppingParameter(resourcePath, new I_CmsSimpleCallback<CmsCroppingParamBean>() {
235
236                    public void execute(CmsCroppingParamBean param) {
237
238                        Map<String, String> attributes = new HashMap<String, String>();
239                        attributes.put("title", title);
240                        attributes.put("width", String.valueOf(param.getResultingWidth()));
241                        attributes.put("height", String.valueOf(param.getResultingHeight()));
242                        CmsPreviewUtil.setImage(
243                            CmsCoreProvider.get().link(resourcePath)
244                                + (param.isScaled() ? "?" + param.toString() + ",c:transparent" : ""),
245                            attributes);
246                        CmsPreviewUtil.closeDialog();
247                    }
248                });
249                break;
250            case ade:
251            case view:
252            case adeView:
253            default:
254                //nothing to do here, should not be called
255                break;
256        }
257    }
258
259    /**
260     * @see org.opencms.ade.galleries.client.preview.A_CmsResourcePreview#setResource()
261     */
262    @Override
263    public void setResource() {
264
265        if (m_handler == null) {
266            throw new UnsupportedOperationException("Preview handler not initialized");
267        }
268        final CmsCroppingParamBean croppingParam = m_handler.getCroppingParam();
269
270        switch (getGalleryMode()) {
271            case widget:
272                if (getGalleryDialog().getWidgetHandler() != null) {
273                    getGalleryDialog().getWidgetHandler().setWidgetValue(
274                        m_infoBean.getResourcePath(),
275                        m_infoBean.getStructureId(),
276                        croppingParam);
277                } else {
278                    if (CmsPreviewUtil.isAdvancedWidget()) {
279                        CmsPreviewUtil.setVfsImage(
280                            m_infoBean.getResourcePath(),
281                            croppingParam.getScaleParam(),
282                            croppingParam.getFormatName(),
283                            croppingParam.getRatio() + "");
284                    } else {
285                        CmsPreviewUtil.setResourcePath(
286                            m_infoBean.getResourcePath()
287                                + ((croppingParam.isCropped() || croppingParam.isScaled())
288                                ? "?" + croppingParam.toString()
289                                : ""));
290                    }
291                }
292                break;
293            case editor:
294                m_handler.getImageAttributes(new I_CmsSimpleCallback<Map<String, String>>() {
295
296                    public void execute(Map<String, String> attributes) {
297
298                        CmsPreviewUtil.setImage(
299                            CmsCoreProvider.get().link(
300                                m_infoBean.getResourcePath()
301                                    + ((croppingParam.isCropped() || croppingParam.isScaled())
302                                    ? "?" + croppingParam.toString() + ",c:transparent"
303                                    : "")),
304                            attributes);
305                    }
306                });
307
308                break;
309            case ade:
310            case view:
311            case adeView:
312            default:
313                //nothing to do here, should not be called
314                break;
315        }
316    }
317
318    /**
319     * Returns the image info bean for the given resource.<p>
320     *
321     * @param resourcePath the resource path
322     * @param callback the calback to execute
323     */
324    private void getImageInfo(final String resourcePath, final I_CmsSimpleCallback<CmsImageInfoBean> callback) {
325
326        CmsRpcAction<CmsImageInfoBean> action = new CmsRpcAction<CmsImageInfoBean>() {
327
328            /**
329             * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
330             */
331            @Override
332            public void execute() {
333
334                start(0, true);
335                A_CmsResourcePreview.getService().getImageInfo(resourcePath, getLocale(), this);
336
337            }
338
339            /**
340             * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
341             */
342            @Override
343            protected void onResponse(CmsImageInfoBean result) {
344
345                stop(false);
346                callback.execute(result);
347            }
348        };
349        action.execute();
350    }
351
352    /**
353     * Returns the initial cropping parameter bean for a given resource.<p>
354     *
355     * @param resourcePath the resource path
356     * @param callback the callback to execute
357     */
358    private void getInitialCroppingParameter(
359        final String resourcePath,
360        final I_CmsSimpleCallback<CmsCroppingParamBean> callback) {
361
362        getImageInfo(resourcePath, new I_CmsSimpleCallback<CmsImageInfoBean>() {
363
364            public void execute(CmsImageInfoBean imageInfo) {
365
366                CmsImageFormatHandler formatHandler = new CmsImageFormatHandler(
367                    getGalleryMode(),
368                    getGalleryDialog(),
369                    resourcePath,
370                    imageInfo.getHeight(),
371                    imageInfo.getWidth());
372                CmsCroppingParamBean param = formatHandler.getCroppingParam();
373                if (formatHandler.isUseFormats()) {
374                    formatHandler.getFormats().values().iterator().next().adjustCroppingParam(param);
375                }
376                callback.execute(param);
377            }
378        });
379
380    }
381}