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

import com.google.gwt.editor.client.adapters.TakesValueEditor;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ValueListBox;

/**
 * List box widget that adapts to UiCommon list model items.
 *
 * @param <T>
 *            List box item type.
 */
public class ListModelListBox<T> extends ValueListBox<T> implements EditorWidget<T, TakesValueEditor<T>> {

    private TakesConstrainedValueEditor<T> editor;

    /**
     * Creates a list box that renders its items using the specified {@link Renderer}.
     *
     * @param renderer
     *            Renderer for list box items.
     */
    public ListModelListBox(Renderer<T> renderer) {
        super(renderer);
    }

    @Override
    public TakesConstrainedValueEditor<T> asEditor() {
        if (editor == null) {
            editor = TakesConstrainedValueEditor.of(this, this, this);
        }
        return editor;
    }

    public ListBox asListBox() {
        return (ListBox) getWidget();
    }

    @Override
    public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) {
        return asListBox().addKeyUpHandler(handler);
    }

    @Override
    public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
        return asListBox().addKeyDownHandler(handler);
    }

    @Override
    public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
        return asListBox().addKeyPressHandler(handler);
    }

    @Override
    public int getTabIndex() {
        return asListBox().getTabIndex();
    }

    @Override
    public void setAccessKey(char key) {
        asListBox().setAccessKey(key);
    }

    @Override
    public void setFocus(boolean focused) {
        asListBox().setFocus(focused);
    }

    @Override
    public void setTabIndex(int index) {
        asListBox().setTabIndex(index);
    }

    @Override
    public boolean isEnabled() {
        return asListBox().isEnabled();
    }

    @Override
    public void setEnabled(boolean enabled) {
        asListBox().setEnabled(enabled);
    }

}

@@ -70,47 +70,295 @@
 gwt.
-editor.client.adapters.TakesValueEditor
+core.client.Scheduler;%0Aimport com.google.gwt.core.client.Scheduler.ScheduledCommand;%0Aimport com.google.gwt.dom.client.Document;%0Aimport com.google.gwt.editor.client.adapters.TakesValueEditor;%0Aimport com.google.gwt.event.dom.client.DomEvent;%0Aimport com.google.gwt.event.dom.client.KeyCodes
 ;%0Aim
@@ -402,32 +402,86 @@
 KeyDownHandler;%0A
+import com.google.gwt.event.dom.client.KeyPressEvent;%0A
 import com.googl
@@ -1279,24 +1279,24 @@
 renderer) %7B%0A
-
         supe
@@ -1304,24 +1304,852 @@
 (renderer);%0A
+        addKeyPressHandler(new KeyPressHandler() %7B%0A%0A            @Override%0A            public void onKeyPress(KeyPressEvent event) %7B%0A                int keyCode = event.getNativeEvent().getKeyCode();%0A                if (keyCode == KeyCodes.KEY_DOWN %7C%7C keyCode == KeyCodes.KEY_UP%0A                        %7C%7C keyCode == KeyCodes.KEY_RIGHT %7C%7C keyCode == KeyCodes.KEY_LEFT) %7B%0A%0A                    // deferring is required to allow the ListBox's selected index to update first%0A                    Scheduler.get().scheduleDeferred(new ScheduledCommand() %7B%0A%0A                        @Override%0A                        public void execute() %7B%0A                            DomEvent.fireNativeEvent(Document.get().createChangeEvent(), asListBox());%0A                        %7D%0A                    %7D);%0A                %7D%0A            %7D%0A        %7D);%0A
     %7D%0A%0A    @

Update list box upon arrow key press