/*
 *  Copyright 2013, Arondor
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.arondor.common.reflection.gwt.client.view;

import java.util.Collection;
import java.util.logging.Logger;

import com.arondor.common.reflection.gwt.client.presenter.ImplementingClassPresenter.Display;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
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.ListBox;

public class ImplementingClassView extends Composite implements Display
{
    private static final Logger LOG = Logger.getLogger(ImplementingClassView.class.getName());

    private ListBox implementingListInput = new ListBox();

    public ImplementingClassView()
    {
        initWidget(implementingListInput);
    }

    public void setImplementingClasses(Collection<String> implementingClasses)
    {
        LOG.finest("Selected classes : " + implementingClasses);
        implementingListInput.clear();
        for (String implementingClass : implementingClasses)
        {
            implementingListInput.addItem(implementingClass);
        }
    }

    private void doSelect(String className)
    {
        LOG.finest("Selecting class : " + className + " from a choice of " + implementingListInput.getItemCount()
                + " items");
        for (int idx = 0; idx < implementingListInput.getItemCount(); idx++)
        {
            if (implementingListInput.getItemText(idx).equals(className))
            {
                implementingListInput.setSelectedIndex(idx);
                return;
            }
        }
        LOG.warning("Could not select class : " + className);
    }

    public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<String> valueChangeHandler)
    {
        return implementingListInput.addChangeHandler(new ChangeHandler()
        {
            public void onChange(ChangeEvent event)
            {
                if (implementingListInput.getSelectedIndex() != -1)
                {
                    valueChangeHandler.onValueChange(new MyValueChangeEvent<String>(implementingListInput
                            .getValue(implementingListInput.getSelectedIndex())));
                }
            }
        });
    }

    public void setBaseClassName(String baseClassName)
    {
        doSelect(baseClassName);
    }

    public void selectImplementingClass(String implementingClassName)
    {
        doSelect(implementingClassName);
    }
}

@@ -1352,24 +1352,66 @@
 ListBox();%0A%0A
+    private String selectedClass = null;%0A%0A
     public I
@@ -1433,16 +1433,16 @@
 sView()%0A
-
     %7B%0A  
@@ -1807,24 +1807,232 @@
 tingClass);%0A
+            if (selectedClass != null && selectedClass.equals(implementingClass))%0A            %7B%0A                implementingListInput.setSelectedIndex(implementingListInput.getItemCount() - 1);%0A            %7D%0A
         %7D%0A  
@@ -2078,32 +2078,67 @@
 lassName)%0A    %7B%0A
+        selectedClass = className;%0A
         LOG.fine
@@ -2989,173 +2989,229 @@
     
-valueChangeHandler.onValueChange(new MyValueChangeEvent%3CString%3E(implementingListInput%0A                            .getValue(implementingListInput.getSelectedIndex())
+String value = implementingListInput.getValue(implementingListInput.getSelectedIndex());%0A                    selectedClass = value;%0A                    valueChangeHandler.onValueChange(new MyValueChangeEvent%3CString%3E(value
 ));%0A

Support late-defined class view selection