package org.ovirt.engine.ui.common.widget.label;

import org.ovirt.engine.ui.common.widget.renderer.EmptyValueRenderer;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

public class TextBoxLabel extends TextBoxLabelBase<String> {

    private boolean hasFocus = false;

    public TextBoxLabel() {
        super(new EmptyValueRenderer<String>() {
            @Override
            public String render(String value) {
                String renderedText = super.render(value);
                renderedText = SafeHtmlUtils.htmlEscape(renderedText);
                return renderedText;
            }
        });
    }

    public TextBoxLabel(String text) {
        this();
        setText(text);
    }

    @Override
    protected void addHandlers() {
        super.addHandlers();

        addFocusHandler(new FocusHandler() {
            @Override
            public void onFocus(FocusEvent event) {
                hasFocus = true;
            }
        });

        addBlurHandler(new BlurHandler() {
            @Override
            public void onBlur(BlurEvent event) {
                hasFocus = false;
            }
        });
    }

    /**
     * Overridden to return "" from an empty text box.
     *
     * @see com.google.gwt.user.client.ui.TextBoxBase#getValue()
     */
    @Override
    public String getValue() {
        String raw = super.getValue();
        return raw == null ? "" : raw; //$NON-NLS-1$
    }

    @Override
    public void setText(String text) {
        super.setText(text);

        final int cursorPosition = getCursorPos();
        final int selectionLength = getSelectionLength();

        if (hasFocus) {
            // Needs to be deferred, because the setSelection works
            // only after the element has been attached to the document
            // which is not yet true
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                @Override
                public void execute() {
                    setFocus(true);
                    setSelectionRange(cursorPosition, selectionLength);
                }
            });
        }
    }

}

@@ -47,79 +47,8 @@
 l;%0A%0A
-import org.ovirt.engine.ui.common.widget.renderer.EmptyValueRenderer;%0A%0A
 impo
@@ -84,24 +84,24 @@
 .Scheduler;%0A
+
 import com.g
@@ -367,52 +367,69 @@
 ort 
-com.google.gwt.safehtml.shared.SafeHtmlUtils
+org.ovirt.engine.ui.common.widget.renderer.EmptyValueRenderer
 ;%0A%0Ap
@@ -552,24 +552,24 @@
 oxLabel() %7B%0A
+
         supe
@@ -606,272 +606,8 @@
 g%3E()
- %7B%0A            @Override%0A            public String render(String value) %7B%0A                String renderedText = super.render(value);%0A                renderedText = SafeHtmlUtils.htmlEscape(renderedText);%0A                return renderedText;%0A            %7D%0A        %7D
 );%0A 

remove Escape characters for TextBoxLabel