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.ui.css.I_CmsLayoutBundle;
031import org.opencms.gwt.client.ui.input.CmsLabel;
032import org.opencms.gwt.client.ui.input.CmsTextBox;
033import org.opencms.util.CmsStringUtil;
034
035import com.google.gwt.core.client.Scheduler;
036import com.google.gwt.core.client.Scheduler.ScheduledCommand;
037import com.google.gwt.event.dom.client.KeyPressEvent;
038import com.google.gwt.event.dom.client.KeyPressHandler;
039import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
040import com.google.gwt.event.logical.shared.ValueChangeEvent;
041import com.google.gwt.event.logical.shared.ValueChangeHandler;
042import com.google.gwt.event.shared.HandlerRegistration;
043import com.google.gwt.user.client.ui.Composite;
044import com.google.gwt.user.client.ui.FlowPanel;
045
046/**
047 * The widget to display a simple form with a label and an text box.<p>
048 *
049 * @since 8.0.
050 */
051public class CmsPropertyForm extends Composite implements HasValueChangeHandlers<String> {
052
053    /** The flag to indicate if the text box value is changed. */
054    protected boolean m_isChanged;
055
056    /** The original value. */
057    protected String m_originalValue;
058
059    /** The text box. */
060    protected CmsTextBox m_textBox;
061
062    /** The id of the property. */
063    private String m_id;
064
065    /** The text box panel. */
066    private FlowPanel m_inputPanel;
067
068    /** The label. */
069    private CmsLabel m_label;
070
071    /** The parent panel. */
072    private FlowPanel m_parent;
073
074    /**
075     * The constructor.<p>
076     *
077     * @param id the id of the property from
078     * @param value the property value
079     * @param label the label text to display
080     * @param noEditReason the reason why the properties are not editable
081     */
082    public CmsPropertyForm(String id, String value, String label, String noEditReason) {
083
084        m_id = id;
085        m_originalValue = value;
086        m_isChanged = false;
087        //m_parentWidth = width;
088        m_parent = new FlowPanel();
089        m_parent.addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().propertyForm());
090        // set form label
091        m_label = new CmsLabel(label);
092        m_label.addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().labelField());
093        m_parent.add(m_label);
094
095        // set form text box
096        m_inputPanel = new FlowPanel();
097        m_inputPanel.addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().inputField());
098        m_textBox = new CmsTextBox();
099        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(noEditReason)) {
100            m_textBox.setTitle(noEditReason);
101            m_textBox.setReadOnly(true);
102        }
103        m_textBox.setFormValueAsString(m_originalValue);
104        m_textBox.addValueChangeHandler(new ValueChangeHandler<String>() {
105
106            /**
107             * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(ValueChangeEvent event)
108             */
109            public void onValueChange(ValueChangeEvent<String> event) {
110
111                m_isChanged = true;
112                m_textBox.setChangedStyle();
113            }
114        });
115        m_textBox.addKeyPressHandler(new KeyPressHandler() {
116
117            public void onKeyPress(KeyPressEvent event) {
118
119                // make sure the value change event is fired on the first change inside the text box
120                if (!isChanged()) {
121                    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
122
123                        public void execute() {
124
125                            if (!isChanged()) {
126
127                                if (((getValue() == null) && (m_originalValue != null))
128                                    || (!getValue().equals(m_originalValue))) {
129                                    ValueChangeEvent.fire(m_textBox, getValue());
130                                }
131                            }
132                        }
133                    });
134                }
135            }
136        });
137        m_inputPanel.add(m_textBox);
138        m_parent.add(m_inputPanel);
139        initWidget(m_parent);
140    }
141
142    /**
143     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
144     */
145    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
146
147        return m_textBox.addValueChangeHandler(handler);
148    }
149
150    /**
151     * Returns the id of the property.<p>
152     *
153     * @return the id
154     */
155    public String getId() {
156
157        return m_id;
158    }
159
160    /**
161     * Returns the field value.<p>
162     *
163     * @return the field value
164     */
165    public String getValue() {
166
167        return m_textBox.getFormValueAsString();
168    }
169
170    /**
171     * Returns the isChanged.<p>
172     *
173     * @return the isChanged
174     */
175    public boolean isChanged() {
176
177        return m_isChanged;
178    }
179
180    /**
181     * Sets the style of the parent panel.<p>
182     *
183     * @param style the css class
184     */
185    public void setFormStyle(String style) {
186
187        m_parent.addStyleName(style);
188    }
189
190}