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.editor;
020
021import java.awt.Color;
022import java.awt.Component;
023import java.awt.event.MouseAdapter;
024import java.awt.event.MouseEvent;
025
026import javax.swing.AbstractCellEditor;
027import javax.swing.JLabel;
028import javax.swing.JTable;
029import javax.swing.JTree;
030import javax.swing.UIManager;
031import javax.swing.table.TableCellEditor;
032import javax.swing.tree.TreeCellEditor;
033
034import icy.resource.ResourceUtil;
035import icy.resource.icon.IcyIcon;
036
037/**
038 * @author Stephane
039 */
040public class VisibleCellEditor extends AbstractCellEditor implements TableCellEditor, TreeCellEditor
041{
042    /**
043     * 
044     */
045    private static final long serialVersionUID = -3974658249790735980L;
046
047    protected JLabel label;
048    int iconSize;
049    boolean visible;
050
051    public VisibleCellEditor(int iconSize)
052    {
053        label = new JLabel();
054
055        label.addMouseListener(new MouseAdapter()
056        {
057            @Override
058            public void mouseReleased(MouseEvent e)
059            {
060                visible = !visible;
061                stopCellEditing();
062            }
063        });
064
065        this.iconSize = iconSize;
066        visible = true;
067    }
068
069    @Override
070    public Object getCellEditorValue()
071    {
072        return Boolean.valueOf(visible);
073    }
074
075    @Override
076    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
077    {
078        visible = ((Boolean) value).booleanValue();
079
080        if (visible)
081            label.setIcon(new IcyIcon(ResourceUtil.ICON_VISIBLE, iconSize));
082        else
083            label.setIcon(new IcyIcon(ResourceUtil.ICON_NOT_VISIBLE, iconSize));
084
085        if (isSelected)
086        {
087            label.setForeground(table.getSelectionForeground());
088            label.setBackground(table.getSelectionBackground());
089        }
090        else
091        {
092            Color background = table.getBackground();
093            if (background == null || background instanceof javax.swing.plaf.UIResource)
094            {
095                final Color alternateColor = UIManager.getColor("Table.alternateRowColor");
096                if (alternateColor != null && ((row & 1) == 0))
097                    background = alternateColor;
098            }
099
100            label.setForeground(table.getForeground());
101            label.setBackground(background);
102        }
103
104        return label;
105    }
106
107    @Override
108    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
109            boolean leaf, int row)
110    {
111        visible = ((Boolean) value).booleanValue();
112
113        if (visible)
114            label.setIcon(new IcyIcon(ResourceUtil.ICON_VISIBLE, iconSize));
115        else
116            label.setIcon(new IcyIcon(ResourceUtil.ICON_NOT_VISIBLE, iconSize));
117
118        label.setForeground(tree.getForeground());
119        label.setBackground(tree.getBackground());
120
121        return label;
122    }
123}