001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.form.I_CmsHasPlaceholder;
032import org.opencms.gwt.client.util.CmsDomUtil;
033import org.opencms.gwt.shared.CmsGwtLog;
034
035import java.text.ParseException;
036
037import com.google.gwt.dom.client.Element;
038import com.google.gwt.event.dom.client.FocusEvent;
039import com.google.gwt.event.dom.client.FocusHandler;
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.PasswordTextBox;
045import com.google.gwt.user.client.ui.SimplePanel;
046
047import elemental2.dom.HTMLInputElement;
048import jsinterop.base.Js;
049
050/**
051 * Provides a standard HTML form password widget, for use on a widget dialog.<p>
052 *
053 */
054public class CmsPasswordWidget extends Composite implements I_CmsEditWidget, I_CmsHasPlaceholder {
055
056    /**The main panel of this widget. */
057    SimplePanel m_mainPanel = new SimplePanel();
058
059    /** Value of the activation. */
060    private boolean m_active = true;
061
062    /** The Password input field. */
063    private PasswordTextBox m_passwordTextBox = new PasswordTextBox();
064
065    /**
066     * Constructs an CmsComboWidget.<p>
067     */
068    public CmsPasswordWidget() {
069
070        // All composites must call initWidget() in their constructors.
071        m_mainPanel.add(m_passwordTextBox);
072        initWidget(m_mainPanel);
073        m_mainPanel.setStyleName(I_CmsWidgetsLayoutBundle.INSTANCE.widgetCss().passwordTextBox());
074        m_passwordTextBox.addValueChangeHandler(new ValueChangeHandler<String>() {
075
076            public void onValueChange(ValueChangeEvent<String> event) {
077
078                fireChangeEvent();
079
080            }
081
082        });
083        m_passwordTextBox.addFocusHandler(new FocusHandler() {
084
085            public void onFocus(FocusEvent event) {
086
087                CmsDomUtil.fireFocusEvent(CmsPasswordWidget.this);
088            }
089        });
090    }
091
092    /**
093     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
094     */
095    public HandlerRegistration addFocusHandler(FocusHandler handler) {
096
097        return addDomHandler(handler, FocusEvent.getType());
098    }
099
100    /**
101     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
102     */
103    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
104
105        return addHandler(handler, ValueChangeEvent.getType());
106    }
107
108    /**
109     * Represents a value change event.<p>
110     *
111     */
112    public void fireChangeEvent() {
113
114        ValueChangeEvent.fire(this, m_passwordTextBox.getValue());
115    }
116
117    /**
118     * @see com.google.gwt.user.client.ui.HasValue#getValue()
119     */
120    public String getValue() {
121
122        try {
123            return m_passwordTextBox.getValueOrThrow();
124        } catch (ParseException e) {
125            e.printStackTrace();
126        }
127        return null;
128    }
129
130    /**
131     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
132     */
133    public boolean isActive() {
134
135        return m_active;
136    }
137
138    /**
139     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
140     */
141    public void onAttachWidget() {
142
143        super.onAttach();
144    }
145
146    /**
147     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
148     */
149    public boolean owns(Element element) {
150
151        // TODO implement this in case we want the delete behavior for optional fields
152        return false;
153
154    }
155
156    /**
157     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
158     */
159    public void setActive(boolean active) {
160
161        if (m_active == active) {
162            return;
163        }
164
165        m_active = active;
166        if (m_active) {
167            getElement().setAttribute("contentEditable", "true");
168            getElement().removeClassName(org.opencms.acacia.client.css.I_CmsLayoutBundle.INSTANCE.form().inActive());
169            getElement().focus();
170        } else {
171            getElement().setAttribute("contentEditable", "false");
172            getElement().addClassName(org.opencms.acacia.client.css.I_CmsLayoutBundle.INSTANCE.form().inActive());
173        }
174        if (active) {
175            fireChangeEvent();
176        }
177
178    }
179
180    /**
181     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
182     */
183    public void setName(String name) {
184
185        // no input field so nothing to do
186
187    }
188
189    /**
190     * @see org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder#setPlaceholder(java.lang.String)
191     */
192    @Override
193    public void setPlaceholder(String placeholder) {
194
195        try {
196            HTMLInputElement input = Js.cast(m_passwordTextBox);
197            input.placeholder = placeholder == null ? "" : placeholder;
198        } catch (Exception e) {
199            CmsGwtLog.log(e.getLocalizedMessage());
200        }
201
202    }
203
204    /**
205     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
206     */
207    public void setValue(String value) {
208
209        setValue(value, false);
210
211    }
212
213    /**
214     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
215     */
216    public void setValue(String value, boolean fireEvents) {
217
218        m_passwordTextBox.setValue(value, fireEvents);
219    }
220
221}