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 noEditReason the reason why the properties are not editable
080     */
081    public CmsPropertyForm(String id, String value, String noEditReason) {
082
083        m_id = id;
084        m_originalValue = value;
085        m_isChanged = false;
086        //m_parentWidth = width;
087        m_parent = new FlowPanel();
088        m_parent.addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().propertyForm());
089        // set form label
090        m_label = new CmsLabel(m_id);
091        m_label.addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().labelField());
092        m_parent.add(m_label);
093
094        // set form text box
095        m_inputPanel = new FlowPanel();
096        m_inputPanel.addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().inputField());
097        m_textBox = new CmsTextBox();
098        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(noEditReason)) {
099            m_textBox.setTitle(noEditReason);
100            m_textBox.setReadOnly(true);
101        }
102        m_textBox.setFormValueAsString(m_originalValue);
103        m_textBox.addValueChangeHandler(new ValueChangeHandler<String>() {
104
105            /**
106             * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(ValueChangeEvent event)
107             */
108            public void onValueChange(ValueChangeEvent<String> event) {
109
110                m_isChanged = true;
111                m_textBox.setChangedStyle();
112            }
113        });
114        m_textBox.addKeyPressHandler(new KeyPressHandler() {
115
116            public void onKeyPress(KeyPressEvent event) {
117
118                // make sure the value change event is fired on the first change inside the text box
119                if (!isChanged()) {
120                    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
121
122                        public void execute() {
123
124                            if (!isChanged()) {
125
126                                if (((getValue() == null) && (m_originalValue != null))
127                                    || (!getValue().equals(m_originalValue))) {
128                                    ValueChangeEvent.fire(m_textBox, getValue());
129                                }
130                            }
131                        }
132                    });
133                }
134            }
135        });
136        m_inputPanel.add(m_textBox);
137        m_parent.add(m_inputPanel);
138        initWidget(m_parent);
139    }
140
141    /**
142     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
143     */
144    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
145
146        return m_textBox.addValueChangeHandler(handler);
147    }
148
149    /**
150     * Returns the id of the property.<p>
151     *
152     * @return the id
153     */
154    public String getId() {
155
156        return m_id;
157    }
158
159    /**
160     * Returns the field value.<p>
161     *
162     * @return the field value
163     */
164    public String getValue() {
165
166        return m_textBox.getFormValueAsString();
167    }
168
169    /**
170     * Returns the isChanged.<p>
171     *
172     * @return the isChanged
173     */
174    public boolean isChanged() {
175
176        return m_isChanged;
177    }
178
179    /**
180     * Sets the style of the parent panel.<p>
181     *
182     * @param style the css class
183     */
184    public void setFormStyle(String style) {
185
186        m_parent.addStyleName(style);
187    }
188
189}