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.preview.CmsCroppingParamBean;
031import org.opencms.ade.galleries.client.preview.CmsFocalPointController;
032import org.opencms.ade.galleries.client.preview.CmsImageFormatHandler;
033import org.opencms.ade.galleries.client.preview.CmsImagePreviewHandler;
034import org.opencms.ade.galleries.client.preview.I_CmsPreviewHandler;
035import org.opencms.ade.galleries.shared.CmsPoint;
036import org.opencms.ade.galleries.shared.CmsResourceInfoBean;
037import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode;
038
039import java.util.HashMap;
040import java.util.Iterator;
041import java.util.Map;
042import java.util.Map.Entry;
043
044import com.google.gwt.event.logical.shared.ValueChangeEvent;
045import com.google.gwt.event.logical.shared.ValueChangeHandler;
046import com.google.gwt.user.client.Command;
047import com.google.gwt.user.client.ui.FlowPanel;
048import com.google.gwt.user.client.ui.Widget;
049
050/**
051 * The widget to display the properties of the selected resource.<p>
052 *
053 * @since 8.0.
054 */
055public class CmsPropertiesTab extends A_CmsPreviewDetailTab implements ValueChangeHandler<String> {
056
057    /** The tab handler. */
058    private I_CmsPreviewHandler<?> m_handler;
059
060    /** The image info widget. */
061    private CmsImageInfoDisplay m_imageInfoDisplay = new CmsImageInfoDisplay(this::removeCrop, this::removePoint);
062
063    /** The resource info. */
064    private CmsResourceInfoBean m_info;
065
066    /** The panel for the properties. */
067    private FlowPanel m_propertiesPanel;
068
069    /**
070     * The constructor.<p>
071     *
072     * @param dialogMode the dialog mode
073     * @param height the properties tab height
074     * @param width the properties tab width
075     * @param handler the tab handler to set
076     */
077    public CmsPropertiesTab(GalleryMode dialogMode, int height, int width, I_CmsPreviewHandler<?> handler) {
078
079        super(dialogMode, height, width);
080        m_handler = handler;
081        m_propertiesPanel = new FlowPanel();
082        m_propertiesPanel.addStyleName(
083            org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle.INSTANCE.previewDialogCss().propertiesList());
084        m_propertiesPanel.addStyleName(
085            org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle.INSTANCE.previewDialogCss().clearFix());
086        Widget additional = handler.getAdditionalWidgetForPropertyTab();
087        if (handler instanceof CmsImagePreviewHandler) {
088            m_main.add(m_imageInfoDisplay);
089            ((CmsImagePreviewHandler)handler).addImagePointChangeHandler(this::updateImageInfo);
090            ((CmsImagePreviewHandler)handler).addCroppingChangeHandler(this::updateImageInfo);
091        }
092        m_main.add(m_propertiesPanel);
093        if (additional != null) {
094            m_main.add(additional);
095        }
096
097    }
098
099    /**
100     * The generic function to display the resource properties.<p>
101     *
102     * @param info the information
103     */
104    public void fillContent(CmsResourceInfoBean info) {
105
106        m_info = info;
107        // width of a property form
108        m_propertiesPanel.clear();
109        Map<String, String> properties = info.getProperties();
110        String noEditReason = info.getNoEditReason();
111        if (properties != null) {
112            Iterator<Entry<String, String>> it = properties.entrySet().iterator();
113            while (it.hasNext()) {
114
115                Entry<String, String> entry = it.next();
116                CmsPropertyForm property = new CmsPropertyForm(
117                    entry.getKey(),
118                    entry.getValue(),
119                    info.getPropertyLabel(entry.getKey()),
120                    noEditReason);
121                property.addValueChangeHandler(this);
122                m_propertiesPanel.add(property);
123            }
124        }
125        updateImageInfo();
126        setChanged(false);
127    }
128
129    /**
130     * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)
131     */
132    public void onValueChange(ValueChangeEvent<String> event) {
133
134        setChanged(true);
135    }
136
137    /**
138     * Will be triggered, when the save button is clicked.<p>
139     *
140     * @param afterSaveCommand the command to execute after the properties have been saved
141     */
142    public void saveProperties(Command afterSaveCommand) {
143
144        Map<String, String> properties = new HashMap<String, String>();
145        for (Widget property : m_propertiesPanel) {
146            CmsPropertyForm form = ((CmsPropertyForm)property);
147            if (form.isChanged()) {
148                properties.put(form.getId(), form.getValue());
149            }
150        }
151        m_handler.saveProperties(properties, afterSaveCommand);
152    }
153
154    /**
155     * @see org.opencms.ade.galleries.client.preview.ui.A_CmsPreviewDetailTab#getHandler()
156     */
157    @Override
158    protected I_CmsPreviewHandler<?> getHandler() {
159
160        return m_handler;
161    }
162
163    /**
164     * Gets the current cropping as a string.<p>
165     *
166     * @return the string representing the currently selected cropping
167     */
168    private String getCrop() {
169
170        CmsImagePreviewHandler handler = getImagePreviewHandler();
171        if (handler != null) {
172            CmsCroppingParamBean crop = handler.getCroppingParam();
173            if ((crop != null) && crop.isCropped()) {
174                return "" + crop.getCropWidth() + " x " + crop.getCropHeight();
175            } else {
176                return null;
177            }
178        } else {
179            return null;
180        }
181    }
182
183    /**
184     * Gets the image preview handler, or null if there is no image preview handler.<p>
185     *
186     * @return the image preview handler or null
187     */
188    private CmsImagePreviewHandler getImagePreviewHandler() {
189
190        if (m_handler == null) {
191            return null;
192        }
193        if (m_handler instanceof CmsImagePreviewHandler) {
194            return (CmsImagePreviewHandler)m_handler;
195        }
196        return null;
197    }
198
199    /**
200     * Gets the currently selected focal point as a string.<p>
201     *
202     * @return the focal point string
203     */
204    private String getPoint() {
205
206        CmsImagePreviewHandler handler = getImagePreviewHandler();
207        if (handler == null) {
208            return null;
209        }
210
211        CmsPoint point = handler.getImageInfo().getFocalPoint();
212        if (point == null) {
213            return null;
214        }
215        int x = (int)point.getX();
216        int y = (int)point.getY();
217        return "" + x + " , " + y;
218    }
219
220    /**
221     * Removes the cropping.
222     */
223    private void removeCrop() {
224
225        CmsImageFormatHandler formatHandler = getImagePreviewHandler().getFormatHandler();
226        formatHandler.onRemoveCropping();
227        updateImageInfo();
228    }
229
230    /**
231     * Removes the focal point.
232     */
233    private void removePoint() {
234
235        CmsFocalPointController controller = getImagePreviewHandler().getFocalPointController();
236        controller.reset();
237    }
238
239    /**
240     * Updates the image information.
241     */
242    private void updateImageInfo() {
243
244        String crop = getCrop();
245        String point = getPoint();
246        m_imageInfoDisplay.fillContent(m_info, crop, point);
247    }
248}