001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.acacia.client.widgets.complex;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031
032import com.google.gwt.core.client.GWT;
033import com.google.gwt.uibinder.client.UiBinder;
034import com.google.gwt.uibinder.client.UiField;
035import com.google.gwt.user.client.rpc.AsyncCallback;
036import com.google.gwt.user.client.ui.Composite;
037import com.google.gwt.user.client.ui.FlowPanel;
038import com.google.gwt.user.client.ui.HTML;
039import com.google.gwt.user.client.ui.Image;
040import com.google.gwt.user.client.ui.Label;
041import com.google.gwt.user.client.ui.Widget;
042
043/**
044 * Data view widget which fetches a preview image for the selected item from the server.<p>
045 */
046public class CmsDataViewPreviewWidget extends Composite {
047
048    /**
049     * Loads image from data source.<p>
050     */
051    public static class ContentImageLoader implements I_ImageProvider {
052
053        /**
054         * @see org.opencms.acacia.client.widgets.complex.CmsDataViewPreviewWidget.I_ImageProvider#loadImage(java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
055         */
056        public void loadImage(String config, String id, AsyncCallback<String> callback) {
057
058            CmsCoreProvider.getVfsService().getDataViewThumbnail(config, id, callback);
059        }
060    }
061
062    /**
063     * Interface to load the thumbnail (potentially asynchronously).
064     */
065    public static interface I_ImageProvider {
066
067        /**
068         * Loads the thumbnail.<p>
069         *
070         * @param config the widget configuration
071         * @param id the data id
072         * @param callback the callback to call with the result (the image URL)
073         */
074        void loadImage(String config, String id, AsyncCallback<String> callback);
075    }
076
077    /**
078     * Loads image from a fixed URL.<p>
079     */
080    public static class SimpleImageLoader implements I_ImageProvider {
081
082        /** The thumbnail url. */
083        private String m_url;
084
085        /**
086         * Creates a new URL.<p>
087         *
088         * @param url the URL of the image to load
089         */
090        public SimpleImageLoader(String url) {
091
092            m_url = url;
093        }
094
095        /**
096         * @see org.opencms.acacia.client.widgets.complex.CmsDataViewPreviewWidget.I_ImageProvider#loadImage(java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
097         */
098        public void loadImage(String config, String id, AsyncCallback<String> callback) {
099
100            callback.onSuccess(m_url);
101        }
102
103    }
104
105    /** The ui-binder interface for this widget. */
106    interface I_CmsPreviewUiBinder extends UiBinder<Widget, CmsDataViewPreviewWidget> {
107        // GWT interface, nothing to do here
108    }
109
110    /** The widget configuration. */
111    private String m_config;
112
113    /** The object used to access the editor value. */
114    private CmsDataViewValueAccessor m_accessor;
115
116    /** The container for the image. */
117    @UiField
118    protected FlowPanel m_imageContainer;
119
120    /** The preview image. */
121    @UiField
122    protected Image m_image;
123
124    /** The label to the right of the preview image. */
125    @UiField
126    protected Label m_label;
127
128    /** The label with the description text. */
129    @UiField
130    protected HTML m_descriptionLabel;
131
132    /** The image provider. */
133    private I_ImageProvider m_imageProvider;
134
135    /**
136     * Creates a new instance.<p>
137     *
138     * @param config the widget configuration
139     * @param accessor the accessor for the editor values
140     * @param provider the image provider
141     */
142    public CmsDataViewPreviewWidget(String config, CmsDataViewValueAccessor accessor, I_ImageProvider provider) {
143
144        I_CmsPreviewUiBinder binder = GWT.create(I_CmsPreviewUiBinder.class);
145        initWidget(binder.createAndBindUi(this));
146        m_config = config;
147        m_accessor = accessor;
148        m_label.setText(m_accessor.getValue().getTitle());
149        m_imageProvider = provider;
150        if (provider == null) {
151            m_imageContainer.setVisible(false);
152        }
153        m_descriptionLabel.setHTML(m_accessor.getValue().getDescription());
154    }
155
156    /**
157     * @see com.google.gwt.user.client.ui.Widget#onLoad()
158     */
159    @Override
160    protected void onLoad() {
161
162        super.onLoad();
163        if (m_imageProvider != null) {
164            m_imageProvider.loadImage(m_config, m_accessor.getValue().getId(), new AsyncCallback<String>() {
165
166                public void onFailure(Throwable caught) {
167                    // do nothing
168
169                }
170
171                public void onSuccess(String imageUrl) {
172
173                    if (imageUrl == null) {
174                        imageUrl = "";
175                    }
176                    m_image.setUrl(imageUrl);
177                }
178            });
179        }
180    }
181
182}