/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: Nikolay Gorodnov
 * Created: 10.08.2010 15:49:29
 *
 * $Id$
 */
package com.haulmont.cuba.web.toolkit.ui;

import com.haulmont.cuba.toolkit.gwt.client.ui.VTwinColumnSelect;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.ClientWidget;
import com.vaadin.ui.TwinColSelect;

@SuppressWarnings("serial")
@ClientWidget(VTwinColumnSelect.class)
public class TwinColumnSelect extends TwinColSelect {

    private OptionStyleGenerator styleGenerator;

    private boolean nativeSelect;

    @Override
    protected int paintOptions(PaintTarget target, String[] selectedKeys, int keyIndex, Object id, String key, String caption, Resource icon) throws PaintException {
        target.startTag("so");
        if (icon != null) {
            target.addAttribute("icon", icon);
        }
        target.addAttribute("caption", caption);
        if (id != null && id.equals(getNullSelectionItemId())) {
            target.addAttribute("nullselection", true);
        }
        target.addAttribute("key", key);
        final boolean selected = isSelected(id) && keyIndex < selectedKeys.length;
        if (selected) {
            target.addAttribute("selected", true);
            selectedKeys[keyIndex++] = key;
        }
        if (styleGenerator != null) {
            target.addAttribute("style", styleGenerator.generateStyle(this, id, selected));
        }
        target.endTag("so");
        return keyIndex;
    }

    @Override
    protected String getComponentType() {
        return nativeSelect ? "nativetwincolumn" : "twincoluumn";
    }

    public OptionStyleGenerator getStyleGenerator() {
        return styleGenerator;
    }

    public void setStyleGenerator(OptionStyleGenerator styleGenerator) {
        this.styleGenerator = styleGenerator;
        requestRepaint();
    }

    public boolean isNativeSelect() {
        return nativeSelect;
    }

    public void setNativeSelect(boolean nativeSelect) {
        this.nativeSelect = nativeSelect;
        requestRepaint();
    }

    public interface OptionStyleGenerator {
        String generateStyle(AbstractSelect source, Object itemId, boolean selected);
    }

}

@@ -584,16 +584,61 @@
 Select;%0D
+%0Aimport org.apache.commons.lang.StringUtils;%0D
 %0A%0D%0A@Supp
@@ -1647,36 +1647,22 @@
     
-target.addAttribute(%22
+String 
 style
-%22,
+ =
  sty
@@ -1706,18 +1706,134 @@
 elected)
-)
 ;
+%0D%0A            if (!StringUtils.isEmpty(style)) %7B%0D%0A                target.addAttribute(%22style%22, style);%0D%0A            %7D
 %0D%0A      

fix style handling in TwinColumn