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.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            m_url = url;
092        }
093
094        /**
095         * @see org.opencms.acacia.client.widgets.complex.CmsDataViewPreviewWidget.I_ImageProvider#loadImage(java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
096         */
097        public void loadImage(String config, String id, AsyncCallback<String> callback) {
098
099            callback.onSuccess(m_url);
100        }
101
102    }
103
104    /** The ui-binder interface for this widget. */
105    interface I_CmsPreviewUiBinder extends UiBinder<Widget, CmsDataViewPreviewWidget> {
106        // GWT interface, nothing to do here
107    }
108
109    /** The widget configuration. */
110    private String m_config;
111
112    /** The object used to access the editor value. */
113    private CmsDataViewValueAccessor m_accessor;
114
115    /** The container for the image. */
116    @UiField
117    protected FlowPanel m_imageContainer;
118
119    /** The preview image. */
120    @UiField
121    protected Image m_image;
122
123    /** The label to the right of the preview image. */
124    @UiField
125    protected Label m_label;
126
127    /** The label with the description text. */
128    @UiField
129    protected HTML m_descriptionLabel;
130
131    /** The image provider. */
132    private I_ImageProvider m_imageProvider;
133
134    /**
135     * Creates a new instance.<p>
136     *
137     * @param config the widget configuration
138     * @param accessor the accessor for the editor values
139     * @param provider the image provider
140     */
141    public CmsDataViewPreviewWidget(String config, CmsDataViewValueAccessor accessor, I_ImageProvider provider) {
142        I_CmsPreviewUiBinder binder = GWT.create(I_CmsPreviewUiBinder.class);
143        initWidget(binder.createAndBindUi(this));
144        m_config = config;
145        m_accessor = accessor;
146        m_label.setText(m_accessor.getValue().getTitle());
147        m_imageProvider = provider;
148        if (provider == null) {
149            m_imageContainer.setVisible(false);
150        }
151        m_descriptionLabel.setHTML(m_accessor.getValue().getDescription());
152    }
153
154    /**
155     * @see com.google.gwt.user.client.ui.Widget#onLoad()
156     */
157    @Override
158    protected void onLoad() {
159
160        super.onLoad();
161        if (m_imageProvider != null) {
162            m_imageProvider.loadImage(m_config, m_accessor.getValue().getId(), new AsyncCallback<String>() {
163
164                public void onFailure(Throwable caught) {
165                    // do nothing
166
167                }
168
169                public void onSuccess(String imageUrl) {
170
171                    if (imageUrl == null) {
172                        imageUrl = "";
173                    }
174                    m_image.setUrl(imageUrl);
175                }
176            });
177        }
178    }
179
180}