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.ui;
029
030import org.opencms.ade.galleries.client.Messages;
031import org.opencms.ade.galleries.shared.CmsImageInfoBean;
032import org.opencms.ade.galleries.shared.CmsResourceInfoBean;
033import org.opencms.gwt.client.ui.CmsPushButton;
034import org.opencms.gwt.client.ui.I_CmsButton.Size;
035import org.opencms.util.CmsStringUtil;
036
037import com.google.gwt.core.client.GWT;
038import com.google.gwt.uibinder.client.UiBinder;
039import com.google.gwt.uibinder.client.UiField;
040import com.google.gwt.user.client.ui.Composite;
041import com.google.gwt.user.client.ui.Label;
042import com.google.gwt.user.client.ui.Widget;
043
044/**
045 * Widget for displaying image information.<p>
046 */
047public class CmsImageInfoDisplay extends Composite {
048
049    /**
050     * @see com.google.gwt.uibinder.client.UiBinder
051     */
052    protected interface I_CmsImageInfosTabUiBinder extends UiBinder<Widget, CmsImageInfoDisplay> {
053        // GWT interface, nothing to do here
054    }
055
056    /** The ui-binder instance for this class. */
057    private static I_CmsImageInfosTabUiBinder m_uiBinder = GWT.create(I_CmsImageInfosTabUiBinder.class);
058
059    /** Label. */
060    @UiField
061    protected Label m_displayCropFormat;
062
063    /** The format label. */
064    @UiField
065    protected Label m_displayFormat;
066
067    /** The path label. */
068    @UiField
069    protected Label m_displayPath;
070
071    /** Label. */
072    @UiField
073    protected Label m_displayPoint;
074
075    /** The type label. */
076    @UiField
077    protected Label m_displayType;
078
079    /** Label. */
080    @UiField
081    protected Label m_labelCropFormat;
082
083    /** The format label. */
084    @UiField
085    protected Label m_labelFormat;
086
087    /** The path label. */
088    @UiField
089    protected Label m_labelPath;
090
091    /** Label. */
092    @UiField
093    protected Label m_labelPoint;
094
095    /** The type label. */
096    @UiField
097    protected Label m_labelType;
098
099    /** Button for removing crop. */
100    @UiField
101    protected CmsPushButton m_removeCrop;
102
103    /** Button for removing focal point. */
104    @UiField
105    protected CmsPushButton m_removePoint;
106
107    /**
108     * The constructor.<p>
109     *
110     * @param removeCropAction action to remove the cropping
111     * @param removePointAction action to remove the image point
112    */
113    public CmsImageInfoDisplay(Runnable removeCropAction, Runnable removePointAction) {
114
115        initWidget(m_uiBinder.createAndBindUi(this));
116
117        m_labelPath.setText(Messages.get().key(Messages.GUI_PREVIEW_LABEL_PATH_0));
118        m_labelFormat.setText(Messages.get().key(Messages.GUI_PREVIEW_LABEL_FORMAT_0));
119        m_labelType.setText(Messages.get().key(Messages.GUI_PREVIEW_LABEL_TYPE_0));
120        m_labelPoint.setText(Messages.get().key(Messages.GUI_PREVIEW_LABEL_FOCALPOINT_0));
121        m_labelCropFormat.setText(Messages.get().key(Messages.GUI_PREVIEW_LABEL_CROPFORMAT_0));
122        m_removeCrop.addClickHandler(e -> removeCropAction.run());
123        m_removePoint.addClickHandler(e -> removePointAction.run());
124        setButtonStyle(m_removeCrop);
125        setButtonStyle(m_removePoint);
126    }
127
128    /**
129     * Initializes a button.<p>
130     *
131     * @param button the button to initialize
132     */
133    private static void setButtonStyle(CmsPushButton button) {
134
135        button.setSize(Size.small);
136        button.setHeight("20px");
137
138        button.setText(Messages.get().key(Messages.GUI_PREVIEW_BUTTON_REMOVE_0));
139    }
140
141    /**
142     * Fills the contend data.<p>
143     *
144     * @param infoBean the image info bean
145     * @param crop the cropping text
146     * @param point the focal point text
147     */
148    public void fillContent(CmsResourceInfoBean infoBean, String crop, String point) {
149
150        m_displayPath.setText(infoBean.getResourcePath());
151        if (infoBean instanceof CmsImageInfoBean) {
152            CmsImageInfoBean imageInfo = (CmsImageInfoBean)infoBean;
153            m_displayFormat.setText(imageInfo.getWidth() + " x " + imageInfo.getHeight());
154        }
155        String path = infoBean.getResourcePath();
156        String typePrefix = "???";
157        int slashPos = path.lastIndexOf("/");
158        String name;
159        if (slashPos >= 0) {
160            name = path.substring(slashPos + 1);
161        } else {
162            name = path;
163        }
164        int dotPos = name.lastIndexOf(".");
165        if (dotPos >= 0) {
166            String extension = name.substring(dotPos + 1).toLowerCase();
167            if ("jpg".equals(extension) || "jpeg".equals(extension)) {
168                typePrefix = "JPEG";
169            } else {
170                typePrefix = extension.toUpperCase();
171            }
172        }
173        m_removePoint.setEnabled(CmsStringUtil.isEmptyOrWhitespaceOnly(infoBean.getNoEditReason()));
174        m_displayType.setText(Messages.get().key(Messages.GUI_PREVIEW_VALUE_TYPE_1, typePrefix));
175        setCropFormat(crop);
176        setFocalPoint(point);
177    }
178
179    /**
180     * Sets the crop format.<p>
181     *
182     * @param cropFormat the crop format
183     */
184    public void setCropFormat(String cropFormat) {
185
186        boolean visible = (cropFormat != null);
187        if (cropFormat == null) {
188            cropFormat = "";
189        }
190        m_labelCropFormat.setVisible(visible);
191        m_removeCrop.setVisible(visible);
192        m_displayCropFormat.setText(cropFormat);
193    }
194
195    /**
196     * Sets the focal point.<p>
197     *
198     * @param focalPoint the focal point
199     */
200    public void setFocalPoint(String focalPoint) {
201
202        boolean visible = (focalPoint != null);
203        if (focalPoint == null) {
204            focalPoint = "";
205        }
206        m_labelPoint.setVisible(visible);
207        m_removePoint.setVisible(visible);
208        m_displayPoint.setText(focalPoint);
209    }
210
211}