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.ui.input.CmsPrincipalSelection;
032import org.opencms.gwt.client.util.CmsDomUtil;
033
034import com.google.gwt.dom.client.Element;
035import com.google.gwt.event.dom.client.FocusEvent;
036import com.google.gwt.event.dom.client.FocusHandler;
037import com.google.gwt.event.logical.shared.ValueChangeEvent;
038import com.google.gwt.event.logical.shared.ValueChangeHandler;
039import com.google.gwt.event.shared.HandlerRegistration;
040import com.google.gwt.user.client.ui.Composite;
041
042/**
043 * The principal select widget.<p>
044 **/
045public class CmsPrincipalWidget extends Composite implements I_CmsEditWidget {
046
047    /** Value of the activation. */
048    private boolean m_active = true;
049
050    /** The principal selector. */
051    private CmsPrincipalSelection m_principalSelect;
052
053    /**
054     * Constructs an CmsComboWidget with the in XSD schema declared configuration.<p>
055     * @param config the configuration string given from OpenCms XSD
056     * @param icon the icon image CSS class
057     */
058    public CmsPrincipalWidget(String config, String icon) {
059
060        m_principalSelect = new CmsPrincipalSelection(config);
061        m_principalSelect.getTextAreaContainer().addStyleName(
062            I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().vfsInputBox());
063        m_principalSelect.addValueChangeHandler(new ValueChangeHandler<String>() {
064
065            public void onValueChange(ValueChangeEvent<String> event) {
066
067                fireChangeEvent();
068
069            }
070        });
071        m_principalSelect.getTextAreaContainer().getTextBox().addFocusHandler(new FocusHandler() {
072
073            public void onFocus(FocusEvent event) {
074
075                CmsDomUtil.fireFocusEvent(CmsPrincipalWidget.this);
076            }
077        });
078        initWidget(m_principalSelect);
079
080    }
081
082    /**
083     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
084     */
085    public HandlerRegistration addFocusHandler(FocusHandler handler) {
086
087        return addDomHandler(handler, FocusEvent.getType());
088    }
089
090    /**
091     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
092     */
093    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
094
095        return addHandler(handler, ValueChangeEvent.getType());
096    }
097
098    /**
099     * Represents a value change event.<p>
100     *
101     */
102    public void fireChangeEvent() {
103
104        ValueChangeEvent.fire(this, m_principalSelect.getFormValueAsString());
105    }
106
107    /**
108     * @see com.google.gwt.user.client.ui.HasValue#getValue()
109     */
110    public String getValue() {
111
112        return m_principalSelect.getFormValueAsString();
113    }
114
115    /**
116     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
117     */
118    public boolean isActive() {
119
120        return m_active;
121    }
122
123    /**
124     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
125     */
126    public void onAttachWidget() {
127
128        super.onAttach();
129    }
130
131    /**
132     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
133     */
134    public boolean owns(Element element) {
135
136        return getElement().isOrHasChild(element);
137    }
138
139    /**
140     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
141     */
142    public void setActive(boolean active) {
143
144        if (m_active == active) {
145            return;
146        }
147        m_active = active;
148        if (active) {
149            fireChangeEvent();
150        }
151
152    }
153
154    /**
155     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
156     */
157    public void setName(String name) {
158
159        m_principalSelect.setName(name);
160
161    }
162
163    /**
164     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
165     */
166    public void setValue(String value) {
167
168        setValue(value, false);
169    }
170
171    /**
172     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
173     */
174    public void setValue(String value, boolean fireEvents) {
175
176        m_principalSelect.setFormValueAsString(value);
177        if (fireEvents) {
178            fireChangeEvent();
179        }
180    }
181}