001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.contenteditor.client;
029
030import org.opencms.acacia.client.CmsWidgetService;
031import org.opencms.acacia.client.widgets.CmsFileWidget;
032import org.opencms.acacia.client.widgets.CmsFormWidgetWrapper;
033import org.opencms.acacia.client.widgets.CmsGalleryWidget;
034import org.opencms.acacia.client.widgets.CmsImageGalleryWidget;
035import org.opencms.acacia.client.widgets.CmsTextareaWidget;
036import org.opencms.acacia.client.widgets.CmsTextboxWidget;
037import org.opencms.acacia.client.widgets.CmsTinyMCEWidget;
038import org.opencms.acacia.client.widgets.I_CmsEditWidget;
039import org.opencms.util.CmsStringUtil;
040
041import java.util.Collection;
042import java.util.Map;
043
044/**
045 * Default OpenCms widget service implementation.<p>
046 */
047public class CmsDefaultWidgetService extends CmsWidgetService {
048
049    /** The paths to be skipped during locale synchronization. */
050    private Collection<String> m_skipPaths;
051
052    /** The locale synchronization values. */
053    private Map<String, String> m_syncValues;
054
055    /**
056     * @see org.opencms.acacia.client.CmsWidgetService#addChangedOrderPath(java.lang.String)
057     */
058    @Override
059    public void addChangedOrderPath(String attributePath) {
060
061        m_skipPaths.add(attributePath);
062    }
063
064    /**
065     * @see org.opencms.acacia.client.CmsWidgetService#getDefaultAttributeValue(java.lang.String, java.lang.String)
066     */
067    @Override
068    public String getDefaultAttributeValue(String attributeName, String simpleValuePath) {
069
070        if (!isSkipValue(simpleValuePath) && m_syncValues.containsKey(simpleValuePath)) {
071            return m_syncValues.get(simpleValuePath);
072        }
073        return super.getDefaultAttributeValue(attributeName, simpleValuePath);
074    }
075
076    /**
077     * Returns the paths to be skipped during locale synchronization.<p>
078     *
079     * @return the paths to be skipped during locale synchronization
080     */
081    public Collection<String> getSkipPaths() {
082
083        return m_skipPaths;
084    }
085
086    /**
087     * Returns the locale synchronization values.<p>
088     *
089     * @return the locale synchronization values
090     */
091    public Map<String, String> getSyncValues() {
092
093        return m_syncValues;
094    }
095
096    /**
097     * Sets the paths to be skipped during locale synchronization.<p>
098     *
099     * @param skipPaths the paths to be skipped during locale synchronization to set
100     */
101    public void setSkipPaths(Collection<String> skipPaths) {
102
103        m_skipPaths = skipPaths;
104    }
105
106    /**
107     * Sets the locale synchronization values.<p>
108     *
109     * @param syncValues the locale synchronization values to set
110     */
111    public void setSyncValues(Map<String, String> syncValues) {
112
113        m_syncValues = syncValues;
114    }
115
116    /**
117     * @see org.opencms.acacia.client.CmsWidgetService#shouldRemoveLastValueAfterUnfocus(org.opencms.acacia.client.widgets.I_CmsEditWidget)
118     */
119    @Override
120    public boolean shouldRemoveLastValueAfterUnfocus(I_CmsEditWidget widget) {
121
122        if (widget instanceof CmsFormWidgetWrapper) {
123            widget = ((CmsFormWidgetWrapper)widget).getEditWidget();
124        }
125        if ((widget instanceof CmsTextareaWidget)
126            || (widget instanceof CmsTextboxWidget)
127            || (widget instanceof CmsTinyMCEWidget)) {
128            String value = widget.getValue();
129            if (" ".equals(value)) {
130                return false;
131            }
132            return CmsStringUtil.isEmptyOrWhitespaceOnly(value);
133        } else if ((widget instanceof CmsFileWidget)
134            || (widget instanceof CmsImageGalleryWidget)
135            || (widget instanceof CmsGalleryWidget)) {
136            return CmsStringUtil.isEmptyOrWhitespaceOnly(widget.getValue());
137        }
138        return false;
139    }
140
141    /**
142     * Returns if the given path should be skipped for locale synchronization.<p>
143     *
144     * @param valuePath the value path
145     *
146     * @return <code>true</code> if the given path should be skipped for locale synchronization
147     */
148    private boolean isSkipValue(String valuePath) {
149
150        if (m_skipPaths != null) {
151            for (String skipPath : m_skipPaths) {
152                if (valuePath.startsWith(skipPath)) {
153                    return true;
154                }
155            }
156        }
157        return false;
158    }
159
160}