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.ui;
029
030import org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle;
031import org.opencms.ade.galleries.shared.CmsResultItemBean;
032import org.opencms.gwt.client.CmsCoreProvider;
033import org.opencms.gwt.client.dnd.I_CmsDragHandle;
034import org.opencms.gwt.client.dnd.I_CmsDraggable;
035import org.opencms.gwt.client.ui.CmsListItemWidget;
036import org.opencms.gwt.client.ui.input.CmsLabel.I_TitleGenerator;
037import org.opencms.gwt.client.util.CmsClientStringUtil;
038import org.opencms.gwt.client.util.CmsToolTipHandler;
039import org.opencms.gwt.shared.CmsAdditionalInfoBean;
040import org.opencms.gwt.shared.CmsListInfoBean;
041
042import com.google.gwt.dom.client.Element;
043import com.google.gwt.user.client.DOM;
044import com.google.gwt.user.client.ui.HTML;
045
046/**
047 * The result list item widget.<p>
048 *
049 * Enabling the image tile view.<p>
050 *
051 * @since 8.0.0
052 */
053public class CmsResultItemWidget extends CmsListItemWidget {
054
055    /**
056     * Widget containing the image(s) for a result list item..<p>
057     */
058    public class ImageTile extends HTML implements I_CmsDragHandle {
059
060        /** The draggable result list item. */
061        private I_CmsDraggable m_draggable;
062
063        /**
064         * Creates a new instance with the given content.<p>
065         *
066         * @param content the HTML content
067         */
068        public ImageTile(String content) {
069
070            super(content);
071        }
072
073        /**
074         * @see org.opencms.gwt.client.dnd.I_CmsDragHandle#getDraggable()
075         */
076        public I_CmsDraggable getDraggable() {
077
078            return m_draggable;
079        }
080
081        /**
082         * Sets the draggable widget.<p>
083         *
084         * @param draggable the draggable widget
085         */
086        public void setDraggable(I_CmsDraggable draggable) {
087
088            m_draggable = draggable;
089        }
090
091    }
092
093    /** Standard image tile scale parameter. */
094    private static final String IMAGE_SCALE_PARAM = "?__scale=t:1,c:ffffff,r:0";
095
096    /** Tile view flag. */
097    private boolean m_hasTileView;
098
099    /** The image tile. */
100    private ImageTile m_imageTile;
101
102    /** The tool tip handler. */
103    private CmsToolTipHandler m_tooltipHandler;
104
105    /**
106     * Constructor.<p>
107     *
108     * @param infoBean the resource info bean
109     * @param showPath <code>true</code> to show the resource path in sub title
110     */
111    public CmsResultItemWidget(CmsResultItemBean infoBean, boolean showPath) {
112
113        super(infoBean);
114        if (showPath) {
115            setSubtitleLabel(infoBean.getPath());
116            setSubtitleTitle(infoBean.getSubTitle());
117        } else {
118            setSubtitleTitle(infoBean.getPath());
119        }
120
121        // if resourceType=="image" prepare for tile view
122        if (CmsResultsTab.isImagelikeType(infoBean.getType())) {
123            m_hasTileView = true;
124            // add tile view marker css classes
125            String src = infoBean.getViewLink();
126            if (src == null) {
127                src = CmsCoreProvider.get().link(infoBean.getPath());
128            }
129            String timeParam = "&time=" + System.currentTimeMillis();
130            // insert tile view image div
131            ImageTile imageTile = new ImageTile("<img src=\"" + src + getBigImageScaleParam()
132            // add time stamp to override browser image caching
133                + timeParam
134                + "\" class=\""
135                + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().bigImage()
136                + "\" />"
137                // using a second image tag for the small thumbnail variant
138                + "<img src=\""
139                + src
140                + getSmallImageScaleParam(infoBean)
141                // add time stamp to override browser image caching
142                + timeParam
143                + "\" class=\""
144                + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().smallImage()
145                + "\" />"
146                + "<div class='"
147                + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().expiredImageOverlay()
148                + "' />");
149            imageTile.setStyleName(I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().imageTile());
150            if (CmsClientStringUtil.checkIsPathOrLinkToSvg(infoBean.getPath())) {
151                imageTile.addStyleName(I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().svg());
152            }
153            m_imageTile = imageTile;
154            m_tooltipHandler = new CmsToolTipHandler(imageTile, generateTooltipHtml(infoBean));
155            m_contentPanel.insert(imageTile, 0);
156        }
157
158    }
159
160    private static Element appendDom(Element parent, String name) {
161
162        Element child = DOM.createElement(name);
163        parent.appendChild(child);
164        return child;
165    }
166
167    /**
168     * Gets the image tile.<p>
169     *
170     * @return the image tile
171     */
172    public ImageTile getImageTile() {
173
174        return m_imageTile;
175    }
176
177    /**
178     * Indicates wther there is a tile view available for this widget.<p>
179     *
180     * @return <code>true</code> if a tiled view is available
181     */
182    public boolean hasTileView() {
183
184        return m_hasTileView;
185    }
186
187    /**
188     * Initializes the title attribute of the subtitle line.<p>
189     *
190     * @param subtitleTitle the value to set
191     */
192    public void setSubtitleTitle(final String subtitleTitle) {
193
194        m_subtitle.setTitle(subtitleTitle);
195        m_subtitle.setTitleGenerator(new I_TitleGenerator() {
196
197            public String getTitle(String originalText) {
198
199                return subtitleTitle;
200            }
201        });
202    }
203
204    /**
205     * @see com.google.gwt.user.client.ui.Composite#onDetach()
206     */
207    @Override
208    protected void onDetach() {
209
210        if (m_tooltipHandler != null) {
211            m_tooltipHandler.clearShowing();
212        }
213        super.onDetach();
214    }
215
216    /**
217     * Generates the HTML for the item tool-tip.<p>
218     *
219     * @param infoBean the item info
220     *
221     * @return the generated HTML
222     */
223    private String generateTooltipHtml(CmsListInfoBean infoBean) {
224
225        Element root = DOM.createElement("div");
226        appendDom(appendDom(root, "p"), "b").setInnerText(CmsClientStringUtil.shortenString(infoBean.getTitle(), 70));
227        if (infoBean.hasAdditionalInfo()) {
228            for (CmsAdditionalInfoBean additionalInfo : infoBean.getAdditionalInfo()) {
229                appendDom(root, "p").setInnerText(
230                    additionalInfo.getName()
231                        + ":\u00a0"
232                        + CmsClientStringUtil.shortenString(additionalInfo.getValue(), 45));
233            }
234        }
235        return root.getInnerHTML();
236    }
237
238    /**
239     * Returns the scale parameter for big thumbnail images.<p>
240     *
241     * @return the scale parameter
242     */
243    private String getBigImageScaleParam() {
244
245        return IMAGE_SCALE_PARAM
246            + ",w:"
247            + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().bigImageWidth()
248            + ",h:"
249            + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().bigImageHeight();
250    }
251
252    /**
253     * Returns the scale parameter for small thumbnail images.<p>
254     *
255     * @param infoBean the resource info
256     *
257     * @return the scale parameter
258     */
259    private String getSmallImageScaleParam(CmsResultItemBean infoBean) {
260
261        String result = null;
262        if (infoBean.getDimension() != null) {
263            String[] sizes = infoBean.getDimension().split("x");
264            try {
265                int width = Integer.parseInt(sizes[0].trim());
266                int height = Integer.parseInt(sizes[1].trim());
267                // only use the small image dimensions in case of dimensions smaller than the big thumbnail
268                if ((I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().bigImageWidth() > width)
269                    || (I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().bigImageHeight() > height)) {
270                    result = IMAGE_SCALE_PARAM
271                        + ",w:"
272                        + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().smallImageWidth()
273                        + ",h:"
274                        + I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().smallImageHeight();
275                }
276            } catch (Exception e) {
277                // failed parsing the dimensions, will use big image
278            }
279        }
280        if (result == null) {
281            result = getBigImageScaleParam();
282        }
283        return result;
284    }
285}