/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protegex.widget.scatterbox;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import edu.stanford.smi.protege.model.*;
import edu.stanford.smi.protegex.widget.abstracttable.*;

/**
 *  Description of the class
 *
 * @author    William Grosso <grosso@smi.stanford.edu>
 */
public class ScatterboxTableCellRenderer extends GenericTableCellRenderer implements Constants {
    private AbstractTableWidgetValue _abstractTableWidgetValue;
    private ScatterboxWidget _widget;
    private ScatterboxWidgetState _widgetState;
    private JPanel _nullPanel;
    private JPanel _selectedNullPanel;
    private Border _draggingBorder;
    private KBQueryUtils _queryUtilsObject;

    public ScatterboxTableCellRenderer(ScatterboxWidget widget, Color draggingBoxColor, KBQueryUtils queryUtilsObject) {
        super(false);
        _queryUtilsObject = queryUtilsObject;
        _widget = widget;
        _widgetState = _widget.getState();
        _abstractTableWidgetValue = new AbstractTableWidgetValue();
        _abstractTableWidgetValue.color = Color.black;
        _nullPanel = new JPanel();
        _nullPanel.setOpaque(true);
        _selectedNullPanel = new JPanel();
        _selectedNullPanel.setOpaque(true);
        _selectedNullPanel.setBackground(NULL_SELECTED_COLOR);
        _draggingBorder = BorderFactory.createLineBorder(draggingBoxColor);
    }

    private void customizeComponentForDagging(JComponent component, int row, int column) {
        if ((_widget.isCurrentlyDragging())
            && (row == _widget.getCurrentlyDraggingOverRow())
            && (column == _widget.getCurrentlyDraggingOverColumn())) {
            component.setBorder(_draggingBorder);
        } else {
            component.setBorder(null);
        }
        return;
    }

    public Component getTableCellRendererComponent(
        JTable table,
        Object value,
        boolean isSelected,
        boolean hasFocus,
        int row,
        int column) {
        JComponent returnValue;
        if (null == value) {
            if ((hasFocus) && (table.isEditing())) {
                table.editingCanceled(new ChangeEvent(this));
            }
            if (isSelected) {
                returnValue = _selectedNullPanel;
            } else {
                returnValue = _nullPanel;
            }
            customizeComponentForDagging(returnValue, row, column);
        } else {
            Instance entryInstance = (Instance) value;
            Instance rangeInstance = _queryUtilsObject.getRangeObject(entryInstance);
            _abstractTableWidgetValue.instance = rangeInstance;
            _abstractTableWidgetValue.slot = _queryUtilsObject.getRangeValueSlot(entryInstance);
            _abstractTableWidgetValue.values = rangeInstance.getOwnSlotValues(_abstractTableWidgetValue.slot);
            returnValue =
                (JComponent) super.getTableCellRendererComponent(table, _abstractTableWidgetValue, isSelected, hasFocus, row, column);
        }
        return returnValue;
    }
}
