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.gwt.client.ui.input;
029
030import com.google.common.base.Objects;
031import com.google.gwt.event.logical.shared.ValueChangeEvent;
032import com.google.gwt.event.logical.shared.ValueChangeHandler;
033import com.google.gwt.event.shared.EventBus;
034import com.google.gwt.event.shared.GwtEvent;
035import com.google.gwt.event.shared.HandlerRegistration;
036import com.google.gwt.event.shared.SimpleEventBus;
037
038/**
039 * The default string model implementation.<p>
040 *
041 * @since 8.0.0
042 */
043public class CmsDefaultStringModel implements I_CmsStringModel {
044
045    /** The event bus for this class. */
046    EventBus m_eventBus = new SimpleEventBus();
047
048    /**
049     * A flag which indicates that the model is currently being updated, which is used to prevent infinite recursion.<p>
050     */
051    private boolean m_active;
052
053    /** The id. */
054    private String m_id;
055
056    /** The model value. */
057    private String m_value;
058
059    /**
060     * Creates a new string model.<p>
061     *
062     * @param id the model id
063     */
064    public CmsDefaultStringModel(String id) {
065
066        m_id = id;
067        m_value = "";
068    }
069
070    /**
071     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
072     */
073    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
074
075        return m_eventBus.addHandler(ValueChangeEvent.getType(), handler);
076    }
077
078    /**
079     * @see com.google.gwt.event.shared.HasHandlers#fireEvent(com.google.gwt.event.shared.GwtEvent)
080     */
081    public void fireEvent(GwtEvent<?> event) {
082
083        m_eventBus.fireEvent(event);
084    }
085
086    /**
087     * @see org.opencms.gwt.client.ui.input.I_CmsStringModel#getId()
088     */
089    public String getId() {
090
091        return m_id;
092    }
093
094    /**
095     * @see org.opencms.gwt.client.ui.input.I_CmsStringModel#getValue()
096     */
097    public String getValue() {
098
099        return m_value;
100    }
101
102    /**
103     * @see org.opencms.gwt.client.ui.input.I_CmsStringModel#setValue(java.lang.String, boolean)
104     */
105    public void setValue(String value, boolean notify) {
106
107        if (!m_active) {
108            m_active = true;
109            try {
110
111                boolean changed = !Objects.equal(value, m_value);
112                m_value = value;
113                if (notify && changed) {
114                    ValueChangeEvent.fire(this, value);
115                }
116            } finally {
117                m_active = false;
118            }
119        }
120
121    }
122
123}