/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.ade.contenteditor.client.widgets;

import com.alkacon.acacia.client.css.I_LayoutBundle;
import com.alkacon.acacia.client.widgets.I_EditWidget;

import org.opencms.ade.contenteditor.client.css.I_CmsLayoutBundle;
import org.opencms.gwt.client.ui.css.I_CmsInputCss;
import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Provides a display only widget, for use on a widget dialog.<p>
 *  
 * */
public class CmsTextboxWidget extends Composite implements I_EditWidget {

    /** The layout bundle. */
    protected static final I_CmsInputCss CSS = I_CmsInputLayoutBundle.INSTANCE.inputCss();

    /** Default value. */
    private String m_defaultValue = "";

    /** The input test area.*/
    TextBox m_textbox = new TextBox();

    /** The token to control activation. */
    private boolean m_active = true;

    /** The fader of this widget. */
    Panel m_fadePanel = new SimplePanel();

    /**The main panel of this widget. */
    Panel m_mainPanel = new FlowPanel();

    /**
     * Creates a new display widget.<p>
     * @param config 
     */
    public CmsTextboxWidget(String config) {

        // All composites must call initWidget() in their constructors.
        if ((config != "") || (config != null)) {
            parseConfig(config);
        }
        m_fadePanel.addDomHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {

                m_textbox.setFocus(true);
                m_textbox.setCursorPos(m_textbox.getText().length());
            }
        }, ClickEvent.getType());
        m_fadePanel.setStyleName(CSS.inputTextBoxFaider());
        m_textbox.addFocusHandler(new FocusHandler() {

            public void onFocus(FocusEvent event) {

                m_mainPanel.remove(m_fadePanel);

            }
        });
        m_mainPanel.getElement().getStyle().setMarginRight(12, Unit.PX);
        m_mainPanel.add(m_textbox);
        m_mainPanel.add(m_fadePanel);

        m_textbox.setStyleName(I_CmsLayoutBundle.INSTANCE.widgetCss().textBox());
        /* m_textarea.getTextAreaContainer().setStyleName(I_CmsLayoutBundle.INSTANCE.widgetCss().textAreaBoxPanel()); */
        m_textbox.addValueChangeHandler(new ValueChangeHandler<String>() {

            public void onValueChange(ValueChangeEvent<String> event) {

                fireChangeEvent();

            }
        });
        m_textbox.addBlurHandler(new BlurHandler() {

            public void onBlur(BlurEvent event) {

                m_mainPanel.add(m_fadePanel);
                m_textbox.setCursorPos(0);

            }
        });
        initWidget(m_mainPanel);
    }

    /**
     * Parse the configuration String.<p>
     * @param config the configuration String
     */
    private void parseConfig(String config) {

        setValue(config);
    }

    /**
     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
     */
    public HandlerRegistration addFocusHandler(FocusHandler handler) {

        return null;
    }

    /**
     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
     */
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {

        return addHandler(handler, ValueChangeEvent.getType());
    }

    /**
     * Represents a value change event.<p>
     * 
     */
    public void fireChangeEvent() {

        String result = "";
        if (m_textbox.getText() != null) {
            result = m_textbox.getText();
        }

        ValueChangeEvent.fire(this, result);
    }

    /**
     * @see com.google.gwt.user.client.ui.HasValue#getValue()
     */
    public String getValue() {

        return m_textbox.getText();
    }

    /**
     * @see com.alkacon.acacia.client.widgets.I_EditWidget#isActive()
     */
    public boolean isActive() {

        return m_active;
    }

    /**
     * @see com.alkacon.acacia.client.widgets.I_EditWidget#onAttachWidget()
     */
    public void onAttachWidget() {

        super.onAttach();
    }

    /**
     * @see com.alkacon.acacia.client.widgets.I_EditWidget#setActive(boolean)
     */
    public void setActive(boolean active) {

        if (m_active == active) {
            return;
        }

        m_active = active;
        if (m_active) {
            getElement().setAttribute("contentEditable", "true");
            getElement().removeClassName(I_LayoutBundle.INSTANCE.form().inActive());
            getElement().focus();
            m_textbox.setText(m_defaultValue);
        } else {
            getElement().setAttribute("contentEditable", "false");
            getElement().addClassName(I_LayoutBundle.INSTANCE.form().inActive());
        }
        if (!active) {
            m_textbox.setText("");
        }
        if (active) {
            fireChangeEvent();
        }

    }

    /**
     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
     */
    public void setValue(String value) {

        // set the value and start changeEvent
        m_textbox.setText(value);
        m_defaultValue = value;
    }

    /**
     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
     */
    public void setValue(String value, boolean fireEvents) {

        // set the saved value to the textArea
        m_textbox.setText(value);
        if (fireEvents) {
            fireChangeEvent();
        }
    }

}

@@ -4807,23 +4807,65 @@
     
-se
+m_defaul
 tValue
-(
+ = 
 config
+;%0D%0A        setValue(m_defaultValue
 );%0D%0A
@@ -7297,39 +7297,8 @@
 );%0D%0A
-        m_defaultValue = value;
 %0D%0A  

Correct an issue.