001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.component.renderer;
020
021import icy.gui.lut.ColormapIcon;
022import icy.image.colormap.IcyColorMap;
023import icy.util.ReflectionUtil;
024
025import java.awt.Dimension;
026import java.awt.Insets;
027
028import javax.swing.JButton;
029import javax.swing.JComboBox;
030import javax.swing.JList;
031
032/**
033 * @author Stephane
034 */
035public class ColormapComboBoxRenderer extends CustomComboBoxRenderer
036{
037    /**
038     * 
039     */
040    private static final long serialVersionUID = 8439070623266035911L;
041
042    /**
043     * @deprecated Use {@link #ColormapComboBoxRenderer(JComboBox)} instead
044     */
045    @Deprecated
046    public ColormapComboBoxRenderer(JComboBox combo, int w, int h)
047    {
048        this(combo);
049    }
050
051    public ColormapComboBoxRenderer(JComboBox combo)
052    {
053        super(combo);
054    }
055
056    @Override
057    protected void updateItem(JList list, Object value)
058    {
059        if (value instanceof IcyColorMap)
060        {
061            final IcyColorMap colormap = (IcyColorMap) value;
062            final JComboBox comboBox = getComboBox();
063            final Dimension dim = comboBox.getSize();
064            int btnWidth;
065
066            try
067            {
068                // a bit ugly but we really want to access it
069                final JButton popBtn = (JButton) ReflectionUtil.getFieldObject(comboBox.getUI(), "arrowButton", true);
070                
071                btnWidth = popBtn.getWidth();
072                if (btnWidth <= 0)
073                    btnWidth = popBtn.getPreferredSize().width;
074                if (btnWidth <= 0)
075                    btnWidth = 20;
076            }
077            catch (Exception e)
078            {
079                btnWidth = 20;
080            }
081
082            final Insets insets = getInsets();
083
084            dim.width -= btnWidth + insets.left + insets.right;
085            dim.height -= insets.top + insets.bottom + 2;
086
087            setIcon(new ColormapIcon(colormap, dim.width, dim.height));
088            setText("");
089            setToolTipText("Set " + colormap.getName() + " colormap");
090            setEnabled(list.isEnabled());
091            setFont(list.getFont());
092        }
093        else
094            super.updateItem(list, value);
095    }
096}