/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * 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 org.jivesoftware.sparkimpl.plugin.viewer;

import org.jivesoftware.resource.SparkRes;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

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

/**
 * A swing renderer used to display labels within a table.
 */
public class PluginRenderer extends JLabel implements TableCellRenderer {
    private Border unselectedBorder;
    private Border selectedBorder;
    private final boolean isBordered = true;

    public PluginRenderer() {
        setOpaque(true);
    }

    @Override
	public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {

        final Icon icon = SparkRes.getImageIcon(SparkRes.Icon.PLUGIN_IMAGE);
        setIcon(icon);
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        }
        else {
            setForeground(Color.black);
            setBackground(Color.white);
        }

        if (isBordered) {
            if (isSelected) {
                if (selectedBorder == null) {
                    selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5,
                            table.getSelectionBackground());
                }
                setBorder(selectedBorder);
            }
            else {
                if (unselectedBorder == null) {
                    unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5,
                            table.getBackground());
                }
                setBorder(unselectedBorder);
            }
        }
        return this;
    }
}

