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 GmbH & Co. KG, 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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.gwt.shared.CmsGwtConstants;
033import org.opencms.i18n.CmsEncoder;
034import org.opencms.i18n.CmsMessages;
035import org.opencms.json.JSONObject;
036import org.opencms.main.CmsLog;
037import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
038import org.opencms.xml.types.A_CmsXmlContentValue;
039
040import java.util.List;
041import java.util.Locale;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Provides a standard HTML form textarea widget, for use on a widget dialog.<p>
047 *
048 * Displays a textarea with 4 rows to enter String values conveniently.<p>
049 *
050 * @since 6.0.0
051 */
052public class CmsTextareaWidget extends A_CmsWidget implements I_CmsADEWidget {
053
054    /** Logger instance for this class. */
055    private static final Log LOG = CmsLog.getLog(CmsTextareaWidget.class);
056
057    /** Default number of rows to display. */
058    private static final int DEFAULT_ROWS_NUMBER = 4;
059
060    /**
061     * Creates a new textarea widget.<p>
062     */
063    public CmsTextareaWidget() {
064
065        // default configuration is to display 4 rows
066        this(DEFAULT_ROWS_NUMBER);
067    }
068
069    /**
070     * Creates a new textarea widget with the given number of rows.<p>
071     *
072     * @param rows the number of rows to display
073     */
074    public CmsTextareaWidget(int rows) {
075
076        super("" + rows);
077    }
078
079    /**
080     * Creates a new textarea widget with the given configuration.<p>
081     *
082     * @param configuration the configuration to use
083     */
084    public CmsTextareaWidget(String configuration) {
085
086        super(configuration);
087    }
088
089    /**
090     * Converts locale to the necessary format for the Typograf library.
091     * @param contentLocale the locale
092     * @return the locale for Typograf
093     */
094    public static  String getTypografLocale(Locale contentLocale) {
095
096        String localeStr = contentLocale.toString();
097        if (contentLocale.getLanguage().equals("en")) {
098            localeStr = "en-US";
099        } else {
100            localeStr = contentLocale.getLanguage();
101        }
102        return localeStr;
103    }
104
105    /**
106     * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale)
107     */
108    public String getConfiguration(
109        CmsObject cms,
110        A_CmsXmlContentValue schemaType,
111        CmsMessages messages,
112        CmsResource resource,
113        Locale contentLocale) {
114
115        JSONObject json = new JSONObject();
116        try {
117            json.put(CmsGwtConstants.JSON_TEXTAREA_CONFIG, getConfiguration());
118            String localeStr = getTypografLocale(contentLocale);
119            json.put(CmsGwtConstants.JSON_TEXTAREA_LOCALE, localeStr);
120        } catch (Exception e) {
121            LOG.error(e.getLocalizedMessage(), e);
122        }
123        String result = json.toString();
124        return result;
125    }
126
127    /**
128     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
129     */
130    public List<String> getCssResourceLinks(CmsObject cms) {
131
132        return null;
133    }
134
135    /**
136     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
137     */
138    public DisplayType getDefaultDisplayType() {
139
140        return DisplayType.wide;
141    }
142
143    /**
144     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
145     */
146    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
147
148        String id = param.getId();
149        StringBuffer result = new StringBuffer(16);
150        int rows = DEFAULT_ROWS_NUMBER;
151        try {
152            rows = Integer.valueOf(getConfiguration()).intValue();
153        } catch (Exception e) {
154            // ignore
155        }
156
157        result.append("<td class=\"xmlTd\">");
158        result.append("<textarea class=\"xmlInput maxwidth");
159        if (param.hasError()) {
160            result.append(" xmlInputError");
161        }
162        result.append("\" name=\"");
163        result.append(id);
164        result.append("\" rows=\"");
165        result.append(rows);
166        result.append("\" cols=\"60\" style=\"overflow:auto;\">");
167        result.append(CmsEncoder.escapeXml(param.getStringValue(cms)));
168        result.append("</textarea>");
169        result.append("</td>");
170
171        return result.toString();
172    }
173
174    /**
175     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
176     */
177    public String getInitCall() {
178
179        return null;
180    }
181
182    /**
183     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
184     */
185    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
186
187        return null;
188    }
189
190    /**
191     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
192     */
193    public String getWidgetName() {
194
195        return CmsTextareaWidget.class.getName();
196    }
197
198    /**
199     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
200     */
201    public boolean isInternal() {
202
203        return true;
204    }
205
206    /**
207     * @see org.opencms.widgets.I_CmsWidget#newInstance()
208     */
209    public I_CmsWidget newInstance() {
210
211        return new CmsTextareaWidget(getConfiguration());
212    }
213
214}