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

net.sf.sfac.gui.utils.IconArrayCellRenderer Maven / Gradle / Ivy

Go to download

This project is the core part of the Swing Framework and Components (SFaC). It contains the Swing framework classes and the graphical component classes.

The newest version!
/*-------------------------------------------------------------------------
 Copyright 2009 Olivier Berlanger

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -------------------------------------------------------------------------*/
package net.sf.sfac.gui.utils;


import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;


/**
 * Renderer displaying an array of icon.
 */
@SuppressWarnings("serial")
public class IconArrayCellRenderer extends JComponent implements TableCellRenderer {


    private static final Border NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);

    private Icon[] icons;


    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        if (isSelected) {
            setBackground(table.getSelectionBackground());
            setForeground(table.getSelectionForeground());
        } else {
            setBackground(table.getBackground());
            setForeground(table.getForeground());
        }

        setEnabled(table.isEnabled());
        icons = (Icon[]) value;

        Border border = null;
        if (hasFocus) {
            if (isSelected) {
                border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = UIManager.getBorder("List.focusCellHighlightBorder");
            }
        } else {
            border = NO_FOCUS_BORDER;
        }
        setBorder(border);

        return this;
    }


    protected void paintComponent(Graphics g) {
        // fill background
        Color oldCol = g.getColor();
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(oldCol);
        // paint icons
        if (icons != null) {
            int posX = 2;
            int posY = 2;
            for (Icon ico : icons) {
                ico.paintIcon(this, g, posX, posY);
                posX += ico.getIconWidth() + 2;
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy