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.acacia.client.widgets;
029
030import org.opencms.acacia.client.css.I_CmsWidgetsLayoutBundle;
031import org.opencms.gwt.client.I_CmsHasResizeOnShow;
032import org.opencms.gwt.client.ui.input.CmsTextArea;
033import org.opencms.util.CmsStringUtil;
034
035import com.google.gwt.dom.client.Element;
036import com.google.gwt.event.dom.client.FocusEvent;
037import com.google.gwt.event.dom.client.FocusHandler;
038import com.google.gwt.event.logical.shared.HasResizeHandlers;
039import com.google.gwt.event.logical.shared.ResizeEvent;
040import com.google.gwt.event.logical.shared.ResizeHandler;
041import com.google.gwt.event.logical.shared.ValueChangeEvent;
042import com.google.gwt.event.logical.shared.ValueChangeHandler;
043import com.google.gwt.event.shared.HandlerRegistration;
044import com.google.gwt.user.client.ui.Composite;
045
046/**
047 * Provides a display only widget, for use on a widget dialog.<p>
048 *
049 * */
050public class CmsTextareaWidget extends Composite implements I_CmsEditWidget, HasResizeHandlers, I_CmsHasResizeOnShow {
051
052    /** The monospace style key. */
053    public static final String STYLE_MONSPACE = "monospace";
054
055    /** The proportional style key. */
056    public static final String STYLE_PROPORTIONAL = "proportional";
057
058    /** Default number of rows to display. */
059    private static final int DEFAULT_ROWS_NUMBER = 5;
060
061    /** The token to control activation. */
062    private boolean m_active = true;
063
064    /** The input test area.*/
065    private CmsTextArea m_textarea = new CmsTextArea();
066
067    /**
068     * Creates a new display widget.<p>
069     *
070     * @param config the widget configuration string
071     */
072    public CmsTextareaWidget(String config) {
073
074        // All composites must call initWidget() in their constructors.
075        initWidget(m_textarea);
076        int configheight = DEFAULT_ROWS_NUMBER;
077        boolean useProportional = false;
078        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(config)) {
079            for (String conf : config.split("\\|")) {
080                if (STYLE_PROPORTIONAL.equals(conf)) {
081                    useProportional = true;
082                } else if (STYLE_MONSPACE.equals(conf)) {
083                    useProportional = false;
084                } else {
085                    try {
086                        int rows = Integer.parseInt(conf);
087                        if (rows > 0) {
088                            configheight = rows;
089                        }
090                    } catch (Exception e) {
091                        // nothing to do
092                    }
093                }
094            }
095        }
096        m_textarea.setRows(configheight);
097        m_textarea.setProportionalStyle(useProportional);
098        m_textarea.getTextArea().addStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().textAreaBox());
099        m_textarea.getTextAreaContainer().addStyleName(
100            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().textAreaBoxPanel());
101        m_textarea.addValueChangeHandler(new ValueChangeHandler<String>() {
102
103            public void onValueChange(ValueChangeEvent<String> event) {
104
105                fireChangeEvent();
106
107            }
108        });
109        m_textarea.addResizeHandler(new ResizeHandler() {
110
111            public void onResize(ResizeEvent event) {
112
113                fireResizeEvent(event);
114
115            }
116        });
117
118    }
119
120    /**
121     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
122     */
123    public HandlerRegistration addFocusHandler(FocusHandler handler) {
124
125        return addDomHandler(handler, FocusEvent.getType());
126    }
127
128    /**
129     * @see com.google.gwt.event.logical.shared.HasResizeHandlers#addResizeHandler(com.google.gwt.event.logical.shared.ResizeHandler)
130     */
131    public HandlerRegistration addResizeHandler(ResizeHandler handler) {
132
133        return addHandler(handler, ResizeEvent.getType());
134    }
135
136    /**
137     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
138     */
139    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
140
141        return addHandler(handler, ValueChangeEvent.getType());
142    }
143
144    /**
145     * Represents a value change event.<p>
146     *
147     */
148    public void fireChangeEvent() {
149
150        String result = "";
151        if (m_textarea.getFormValueAsString() != null) {
152            result = m_textarea.getFormValueAsString();
153        }
154
155        ValueChangeEvent.fire(this, result);
156    }
157
158    /**
159     * Represents a resize event.<p>
160     * @param event from text area panel
161     */
162    public void fireResizeEvent(ResizeEvent event) {
163
164        ResizeEvent.fire(this, event.getWidth(), event.getHeight());
165    }
166
167    /**
168     * @see com.google.gwt.user.client.ui.HasValue#getValue()
169     */
170    public String getValue() {
171
172        return m_textarea.getFormValueAsString();
173    }
174
175    /**
176     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
177     */
178    public boolean isActive() {
179
180        return m_active;
181    }
182
183    /**
184     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
185     */
186    public void onAttachWidget() {
187
188        super.onAttach();
189    }
190
191    /**
192     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
193     */
194    public boolean owns(Element element) {
195
196        return getElement().isOrHasChild(element);
197
198    }
199
200    /**
201     * @see org.opencms.gwt.client.I_CmsHasResizeOnShow#resizeOnShow()
202     */
203    public void resizeOnShow() {
204
205        m_textarea.resizeOnShow();
206    }
207
208    /**
209     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
210     */
211    public void setActive(boolean active) {
212
213        if (m_active == active) {
214            return;
215        }
216
217        m_active = active;
218        m_textarea.setEnabled(m_active);
219        if (m_active) {
220            getElement().removeClassName(org.opencms.acacia.client.css.I_CmsLayoutBundle.INSTANCE.form().inActive());
221            getElement().focus();
222        } else {
223            getElement().addClassName(org.opencms.acacia.client.css.I_CmsLayoutBundle.INSTANCE.form().inActive());
224        }
225        if (active) {
226            fireChangeEvent();
227        }
228
229    }
230
231    /**
232     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
233     */
234    public void setName(String name) {
235
236        m_textarea.setName(name);
237
238    }
239
240    /**
241     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
242     */
243    public void setValue(String value) {
244
245        setValue(value, false);
246    }
247
248    /**
249     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
250     */
251    public void setValue(String value, boolean fireEvents) {
252
253        // set the saved value to the textArea
254        m_textarea.setFormValueAsString(value);
255        if (fireEvents) {
256            fireChangeEvent();
257        }
258    }
259
260}