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.acacia.client;
029
030import org.opencms.acacia.client.widgets.CmsFormWidgetWrapper;
031import org.opencms.acacia.client.widgets.CmsStringWidget;
032import org.opencms.acacia.client.widgets.I_CmsEditWidget;
033import org.opencms.acacia.client.widgets.I_CmsFormEditWidget;
034import org.opencms.acacia.shared.CmsAttributeConfiguration;
035import org.opencms.acacia.shared.CmsContentDefinition;
036import org.opencms.acacia.shared.CmsType;
037
038import java.util.HashMap;
039import java.util.Map;
040
041import com.google.gwt.dom.client.Element;
042
043/**
044 * Service providing form widget renderer for entity attributes.<p>
045 */
046public class CmsWidgetService implements I_CmsWidgetService {
047
048    /** The attribute configurations. */
049    private Map<String, CmsAttributeConfiguration> m_attributeConfigurations;
050
051    /** The in-line renderer. */
052    private I_CmsEntityRenderer m_defaultRenderer;
053
054    /** Renderers by attribute. */
055    private Map<String, I_CmsEntityRenderer> m_rendererByAttribute = new HashMap<String, I_CmsEntityRenderer>();
056
057    /** Map of renderer by type name. */
058    private Map<String, I_CmsEntityRenderer> m_rendererByType;
059
060    /** Map of renderers by name. */
061    private Map<String, I_CmsEntityRenderer> m_renderers = new HashMap<String, I_CmsEntityRenderer>();
062
063    /** The registered widget factories. */
064    private Map<String, I_CmsWidgetFactory> m_widgetFactories;
065
066    /**
067     * Constructor.<p>
068     */
069    public CmsWidgetService() {
070
071        m_rendererByType = new HashMap<String, I_CmsEntityRenderer>();
072        m_widgetFactories = new HashMap<String, I_CmsWidgetFactory>();
073        m_attributeConfigurations = new HashMap<String, CmsAttributeConfiguration>();
074    }
075
076    /**
077     * @see org.opencms.acacia.client.I_CmsWidgetService#addChangedOrderPath(java.lang.String)
078     */
079    public void addChangedOrderPath(String valuePath) {
080
081        // not implemented
082    }
083
084    /**
085     * @see org.opencms.acacia.client.I_CmsWidgetService#addConfigurations(java.util.Map)
086     */
087    public void addConfigurations(Map<String, CmsAttributeConfiguration> configurations) {
088
089        m_attributeConfigurations.putAll(configurations);
090    }
091
092    /**
093     * @see org.opencms.acacia.client.I_CmsWidgetService#addRenderer(org.opencms.acacia.client.I_CmsEntityRenderer)
094     */
095    public void addRenderer(I_CmsEntityRenderer renderer) {
096
097        m_renderers.put(renderer.getName(), renderer);
098    }
099
100    /**
101     * Adds a renderer for the given type.<p>
102     *
103     * @param typeName the type name
104     * @param renderer the renderer
105     */
106    public void addRenderer(String typeName, I_CmsEntityRenderer renderer) {
107
108        m_rendererByType.put(typeName, renderer);
109    }
110
111    /**
112     * @see org.opencms.acacia.client.I_CmsWidgetService#addWidgetFactory(java.lang.String, org.opencms.acacia.client.I_CmsWidgetFactory)
113     */
114    public void addWidgetFactory(String widgetName, I_CmsWidgetFactory widgetFactory) {
115
116        m_widgetFactories.put(widgetName, widgetFactory);
117    }
118
119    /**
120     * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeFormWidget(java.lang.String)
121     */
122    public I_CmsFormEditWidget getAttributeFormWidget(String attributeName) {
123
124        if (m_attributeConfigurations != null) {
125            CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
126            if (config != null) {
127                I_CmsWidgetFactory factory = m_widgetFactories.get(config.getWidgetName());
128                if (factory != null) {
129                    return factory.createFormWidget(config.getWidgetConfig());
130                }
131            }
132        }
133        // no configuration or widget factory found, return default string widget
134        return new CmsFormWidgetWrapper(new CmsStringWidget());
135    }
136
137    /**
138     * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeHelp(java.lang.String)
139     */
140    public String getAttributeHelp(String attributeName) {
141
142        if (m_attributeConfigurations != null) {
143            CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
144            if (config != null) {
145                return config.getHelp();
146            }
147        }
148        return null;
149    }
150
151    /**
152     * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeInlineWidget(java.lang.String, com.google.gwt.dom.client.Element)
153     */
154    public I_CmsEditWidget getAttributeInlineWidget(String attributeName, Element element) {
155
156        if (m_attributeConfigurations != null) {
157            CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
158            if (config != null) {
159                I_CmsWidgetFactory factory = m_widgetFactories.get(config.getWidgetName());
160                if (factory != null) {
161                    return factory.createInlineWidget(config.getWidgetConfig(), element);
162                }
163            }
164        }
165        // no widget configured
166        return null;
167    }
168
169    /**
170     * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeLabel(java.lang.String)
171     */
172    public String getAttributeLabel(String attributeName) {
173
174        if (m_attributeConfigurations != null) {
175            CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
176            if (config != null) {
177                return config.getLabel();
178            }
179        }
180        return attributeName;
181    }
182
183    /**
184     * @see org.opencms.acacia.client.I_CmsWidgetService#getDefaultAttributeValue(java.lang.String, java.lang.String)
185     */
186    public String getDefaultAttributeValue(String attributeName, String simpleValuePath) {
187
188        CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
189        return (config != null) && (config.getDefaultValue() != null) ? config.getDefaultValue() : "";
190    }
191
192    /**
193     * Gets the renderer instance for a specific attribute.<p>
194     *
195     * @param attributeName the attribute for which we want the renderer
196     *
197     * @return the renderer instance
198     */
199    public I_CmsEntityRenderer getRendererForAttribute(String attributeName) {
200
201        int openCmsIndex = attributeName.indexOf("opencms://");
202        if (openCmsIndex != -1) {
203            attributeName = attributeName.substring(openCmsIndex);
204        }
205        I_CmsEntityRenderer result = m_rendererByAttribute.get(attributeName);
206        if (result == null) {
207            return m_defaultRenderer;
208        } else {
209            return result;
210        }
211    }
212
213    /**
214     * @see org.opencms.acacia.client.I_CmsWidgetService#getRendererForAttribute(java.lang.String, org.opencms.acacia.shared.CmsType)
215     */
216    public I_CmsEntityRenderer getRendererForAttribute(String attributeName, CmsType attributeType) {
217
218        return getRendererForAttribute(attributeName);
219    }
220
221    /**
222     * @see org.opencms.acacia.client.I_CmsWidgetService#getRendererForType(org.opencms.acacia.shared.CmsType)
223     */
224    public I_CmsEntityRenderer getRendererForType(CmsType entityType) {
225
226        if (m_rendererByType.containsKey(entityType.getId())) {
227            return m_rendererByType.get(entityType.getId());
228        }
229        return m_defaultRenderer;
230    }
231
232    /**
233     * Initializes the widget service with the given content definition.<p>
234     *
235     * @param definition the content definition
236     */
237    public void init(CmsContentDefinition definition) {
238
239        m_attributeConfigurations = definition.getConfigurations();
240    }
241
242    /**
243     * @see org.opencms.acacia.client.I_CmsWidgetService#isDisplayCompact(java.lang.String)
244     */
245    public boolean isDisplayCompact(String attributeName) {
246
247        if (m_attributeConfigurations != null) {
248            CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
249            if (config != null) {
250                return config.isDisplayColumn();
251            }
252        }
253        return false;
254    }
255
256    /**
257     * @see org.opencms.acacia.client.I_CmsWidgetService#isDisplaySingleLine(java.lang.String)
258     */
259    public boolean isDisplaySingleLine(String attributeName) {
260
261        if (m_attributeConfigurations != null) {
262            CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName);
263            if (config != null) {
264                return config.isDisplaySingleLine();
265            }
266        }
267        return false;
268    }
269
270    /**
271     * @see org.opencms.acacia.client.I_CmsWidgetService#isVisible(java.lang.String)
272     */
273    public boolean isVisible(String attributeName) {
274
275        boolean result = true;
276        if (m_attributeConfigurations.containsKey(attributeName)) {
277            result = m_attributeConfigurations.get(attributeName).isVisible();
278        }
279        return result;
280    }
281
282    /**
283     * @see org.opencms.acacia.client.I_CmsWidgetService#registerComplexWidgetAttribute(java.lang.String, java.lang.String, java.lang.String)
284     */
285    public void registerComplexWidgetAttribute(String attrName, String rendererName, String configuration) {
286
287        I_CmsEntityRenderer renderer = m_renderers.get(rendererName);
288        if (renderer != null) {
289            renderer = renderer.configure(configuration);
290            m_rendererByAttribute.put(attrName, renderer);
291        } else {
292            log("Invalid entity renderer: " + rendererName);
293        }
294    }
295
296    /**
297     * Adds the default complex type renderer.<p>
298     *
299     * @param renderer the renderer
300     */
301    public void setDefaultRenderer(I_CmsEntityRenderer renderer) {
302
303        m_defaultRenderer = renderer;
304    }
305
306    /**
307     * @see org.opencms.acacia.client.I_CmsWidgetService#setWidgetFactories(java.util.Map)
308     */
309    public void setWidgetFactories(Map<String, I_CmsWidgetFactory> widgetFactories) {
310
311        m_widgetFactories = widgetFactories;
312    }
313
314    /**
315     * @see org.opencms.acacia.client.I_CmsWidgetService#shouldRemoveLastValueAfterUnfocus(org.opencms.acacia.client.widgets.I_CmsEditWidget)
316     */
317    public boolean shouldRemoveLastValueAfterUnfocus(I_CmsEditWidget widget) {
318
319        return false;
320    }
321
322    /**
323     * Log method for debugging.<p>
324     *
325     * @param message the message to log
326     */
327    private native void log(String message) /*-{
328        if ($wnd.console) {
329            $wnd.console.log(message);
330        }
331    }-*/;
332
333}