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.postupload.client.ui;
029
030import org.opencms.ade.postupload.client.Messages;
031import org.opencms.gwt.client.CmsJsFunctions;
032
033import com.google.gwt.core.client.GWT;
034import com.google.gwt.dom.client.Style.Visibility;
035import com.google.gwt.uibinder.client.UiBinder;
036import com.google.gwt.uibinder.client.UiField;
037import com.google.gwt.user.client.ui.Composite;
038import com.google.gwt.user.client.ui.FlowPanel;
039import com.google.gwt.user.client.ui.Image;
040import com.google.gwt.user.client.ui.Label;
041
042import jsinterop.base.Js;
043
044/**
045 * Widget to display an image preview in the upload property dialog.
046 */
047public class CmsImagePreview extends Composite {
048
049    /**
050     * The UI binder interface.<p>
051     */
052    interface I_UiBinder extends UiBinder<FlowPanel, CmsImagePreview> {
053        // nothing to do
054    }
055
056    private static I_UiBinder uiBinder = GWT.create(I_UiBinder.class);
057
058    /** The preview image. */
059    @UiField
060    protected Image m_image;
061
062    /** The first info label. */
063    @UiField
064    protected Label m_info1;
065
066    /** The second info label. */
067    @UiField
068    protected Label m_info2;
069
070    @UiField
071    protected Label m_label;
072
073    /** Link which opens the preview in a separate window. */
074    @UiField
075    protected CmsPreviewLink m_link;
076
077    /**
078     * Creates a new instance.
079     */
080    public CmsImagePreview() {
081
082        initWidget(uiBinder.createAndBindUi(this));
083        m_label.setText(Messages.get().key(Messages.GUI_DIALOG_PREVIEW_LABEL_0));
084        m_image.addErrorHandler(event -> CmsJsFunctions.INSTANCE.handleBrokenImage(Js.cast(m_image.getElement())));
085    }
086
087    /**
088     * Hides the widget using the CSS 'visibility' property (so it will still occupy space).
089     */
090    public void hide() {
091
092        getElement().getStyle().setVisibility(Visibility.HIDDEN);
093    }
094
095    /**
096     * Sets the full preview URL.
097     *
098     * @param fullPreviewUrl the full preview URL
099     */
100    public void setFullPreview(String fullPreviewUrl) {
101
102        m_link.setHref(fullPreviewUrl);
103    }
104
105    /**
106     * Sets the high resolution preview URL.
107     *
108     * @param highResPreviewLink the high resolution preview URL
109     */
110    public void setHighResImageUrl(String highResPreviewLink) {
111
112        if (highResPreviewLink != null) {
113            m_image.getElement().setAttribute("srcset", highResPreviewLink + " 2x");
114        } else {
115            m_image.getElement().removeAttribute("srcset");
116        }
117
118    }
119
120    /**
121     * Sets the preview image URL.
122     *
123     * @param imageUrl the preview image URL
124     */
125    public void setImageUrl(String imageUrl) {
126
127        if (imageUrl == null) {
128            imageUrl = "";
129        }
130        m_image.setUrl(imageUrl);
131    }
132
133    /**
134     * Sets the first info label text.
135     *
136     * @param text the text
137     */
138    public void setInfo1(String text) {
139
140        m_info1.setText(text);
141    }
142
143    /**
144     * Sets the second info label text.
145     *
146     * @param text the text
147     */
148    public void setInfo2(String text) {
149
150        m_info2.setText(text);
151    }
152
153}