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