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.menu.search;
020
021import icy.image.ImageUtil;
022import icy.search.SearchResult;
023import icy.util.GraphicsUtil;
024import icy.util.StringUtil;
025
026import java.awt.Component;
027import java.awt.Image;
028
029import javax.swing.ImageIcon;
030import javax.swing.JTable;
031import javax.swing.SwingConstants;
032import javax.swing.plaf.ColorUIResource;
033
034import org.pushingpixels.substance.api.ComponentState;
035import org.pushingpixels.substance.api.SubstanceColorScheme;
036import org.pushingpixels.substance.api.renderers.SubstanceDefaultTableCellRenderer;
037import org.pushingpixels.substance.internal.utils.SubstanceColorSchemeUtilities;
038
039/**
040 * This class is a renderer to display the filtered data.
041 * 
042 * @author Thomas Provoost & Stephane
043 */
044public class SearchResultTableCellRenderer extends SubstanceDefaultTableCellRenderer
045{
046    /**
047     * 
048     */
049    private static final long serialVersionUID = -6758382699884570205L;
050
051    @Override
052    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
053            int row, int column)
054    {
055        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
056
057        if (value instanceof SearchResult)
058        {
059            final SearchResult result = (SearchResult) value;
060
061            final String title = result.getTitle();
062            final String description = result.getDescription();
063            final Image img = result.getImage();
064            final int cellWidth = (int) (table.getCellRect(row, column, false).width * 0.90);
065            String text;
066
067            if (img != null)
068                setIcon(new ImageIcon(ImageUtil.scale(img, 32, 32)));
069            else
070                setIcon(null);
071
072            if (StringUtil.isEmpty(title))
073                text = "<b>Unknow</b>";
074            else
075                text = "<b>" + title + "</b>";
076            if (!StringUtil.isEmpty(description))
077                text += "<br>" + GraphicsUtil.limitStringFor(table, description, cellWidth);
078            setText("<html>" + text);
079
080            setToolTipText(result.getTooltip());
081            setVerticalAlignment(SwingConstants.CENTER);
082            setVerticalTextPosition(SwingConstants.CENTER);
083
084            // override enabled state
085            if (!result.isEnabled())
086            {
087                final ComponentState state;
088
089                if (isSelected)
090                    state = ComponentState.DISABLED_SELECTED;
091                else
092                    state = ComponentState.DISABLED_UNSELECTED;
093
094                final SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities.getColorScheme(table, state);
095
096                // modify foreground
097                setForeground(new ColorUIResource(colorScheme.getForegroundColor()));
098                // disable result
099                setEnabled(false);
100            }
101            else
102                setEnabled(table.isEnabled());
103        }
104
105        return this;
106    }
107}