All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdesktop.swingx.renderer.DefaultComboBoxRenderer Maven / Gradle / Ivy

The newest version!
package org.jdesktop.swingx.renderer;

import java.awt.Component;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import org.jdesktop.swingx.JXComboBox;

/*
 
 ersetzt javax.swing.plaf.basic.BasicComboBoxRenderer

public class BasicComboBoxRenderer extends JLabel
implements ListCellRenderer, Serializable {

wie DefaultListRenderer

E == Object
 */
public class DefaultComboBoxRenderer extends AbstractRenderer implements ListCellRenderer {

	private static final long serialVersionUID = -441370707689161796L;
	private static final Logger LOG = Logger.getLogger(DefaultComboBoxRenderer.class.getName());

    public DefaultComboBoxRenderer() {
    	// null ==> createDefaultComponentProvider()
        this((ComponentProvider) null);
    }
    /*
public abstract class ComponentProvider implements Serializable, UIDependent {
public class LabelProvider extends ComponentProvider {
     */
    public DefaultComboBoxRenderer(ComponentProvider componentProvider) {
        super(componentProvider);
		LOG.config("ctor ComponentProvider componentProvider componentController="+componentController);
//        this.cellContext = new ListCellContext();
		this.cc = new ComboBoxContext();
    }

    static Border etchedNoFocusBorder = BorderFactory.createEtchedBorder();
    Border getEmptyNoFocusBorder() {
    	return new EmptyBorder(1, 1, 1, 1);
    }
    JXComboBox cb;
    ComboBoxContext cc;
	@Override
	public Component getListCellRendererComponent(JList list, E value, 
			int index, boolean isSelected, boolean cellHasFocus) {

//      public void installContext(JComboBox component, Object value, int row, int column,
//              boolean selected, boolean focused, boolean expanded, boolean leaf) {
		cc.installContext(cb, value, index, -1, isSelected, cellHasFocus, false, false);
		
		System.out.println("ComboBoxDemo getListCellRendererComponent: list:"+list
    			+"\n value:"+value+"/"+(value==null?"null":value.getClass())
    			+"\n index="+index+" , isSelected="+isSelected+" , cellHasFocus="+cellHasFocus
    			+"\n ListCellRendererComponent hashCode=@"+Integer.toHexString(this.hashCode())
    				);
    		
        JComponent comp = componentController.getRendererComponent(cc);
        /* in componentController.getRendererComponent(context)
        if (context != null) {
            configureVisuals(context);
            configureContent(context);
        }

         */
        comp.setBorder(index == -1 ? getEmptyNoFocusBorder() : etchedNoFocusBorder);
//			if (isSelected) {
//				setBackground(list.getSelectionBackground());
//				setForeground(list.getSelectionForeground());
//			} else {
//				setBackground(list.getBackground());
//				setForeground(list.getForeground());
//			}
//
//			setFont(list.getFont());
//
//			if (value instanceof Icon) {
//				setIcon((Icon) value);
//			} else {
//				setText((value == null) ? "" : value.toString());
//			}
			
			return comp;
	}

	@Override
	protected ComponentProvider createDefaultComponentProvider() {
		ComponentProvider labelProvider = new LabelProvider(createDefaultStringValue());
		LOG.config("ComponentProvider labelProvider:"+labelProvider);
		return labelProvider;
	}

    private StringValue createDefaultStringValue() {
        return MappedValues.STRING_OR_ICON_ONLY;
    }

}