/*
 * Copyright 2008 Sander Berents
 *
 * 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 org.gwtlib.client.list.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * A Color ListBox.
 *
 * @author Sander Berents
 */
public class ColorListBox extends StyleListBox {
  ChangeListener _listener = new ChangeListener() {
    public void onChange(Widget sender) {
      int sel = getSelectedIndex();
      if(sel == -1) {
        DOM.removeElementAttribute(getElement(), "style");
      } else {
        Element child = DOM.getChild(getElement(), sel);
        String style = DOM.getElementAttribute(child, "style");
        if(style != null) DOM.setElementAttribute(getElement(), "style", style);
      }
    }
  };

  public ColorListBox() {
    this(false);
  }

  public ColorListBox(boolean isMultipleSelect) {
    super(isMultipleSelect);
    addChangeListener(_listener);
  }
  
  public void setColors(String[] colors) {
    clear();
    for(int i = 0; i < colors.length; ++i) {
      addItem("", String.valueOf(i));
      Element child = DOM.getChild(getElement(), i);
//      DOM.setStyleAttribute(child, "background-color", colors[i]);
      DOM.setElementAttribute(child, "style", "background-color:" + colors[i]);
    }
    int sel = getSelectedIndex();
    if(sel != -1) DOM.setElementAttribute(getElement(), "style", "background-color:" + colors[sel]);
  }
  
  public void setItemSelected(int index, boolean selected) {
    super.setItemSelected(index, selected);
    _listener.onChange(this);
  }

  public void setSelectedIndex(int index) {
    super.setSelectedIndex(index);
    _listener.onChange(this);
  }
}

@@ -649,66 +649,90 @@
 gwt.
-user.client.DOM;%0Aimport com.google.gwt.user.client.Element
+event.dom.client.ChangeEvent;%0Aimport com.google.gwt.event.dom.client.ChangeHandler
 ;%0Aim
@@ -767,25 +767,11 @@
 ent.
-ui.ChangeListener
+DOM
 ;%0Aim
@@ -806,16 +806,14 @@
 ent.
-ui.Widge
+Elemen
 t;%0A%0A
@@ -930,24 +930,22 @@
 ange
-Listener _listen
+Handler _handl
 er =
@@ -955,22 +955,21 @@
 w Change
-Listen
+Handl
 er() %7B%0A 
@@ -996,21 +996,25 @@
 nge(
-Widget sender
+ChangeEvent event
 ) %7B%0A
@@ -1511,24 +1511,22 @@
 ange
-Listener(_listen
+Handler(_handl
 er);
@@ -2124,38 +2124,37 @@
 selected);%0A    _
-listen
+handl
 er.onChange(this
@@ -2145,28 +2145,28 @@
 er.onChange(
-this
+null
 );%0A  %7D%0A%0A  pu
@@ -2249,14 +2249,13 @@
    _
-listen
+handl
 er.o
@@ -2262,20 +2262,20 @@
 nChange(
-this
+null
 );%0A  %7D%0A%7D

Apply GWT 2.x upgrade gwtlib-list.patch [Alan Hadsell].