/*
 * 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.instancetable;

import java.awt.*;

import javax.swing.*;

import edu.stanford.smi.protege.model.*;

/**
 *  Static methods to make sure that InstanceTable is valid. Returns a string. A
 *  null return value means "okay"
 *
 * @author    William Grosso <grosso@smi.stanford.edu>
 */
public class InstanceRowConfigurationChecks {

    public static boolean checkValidity(Cls cls, Slot slot) {
        if ((null == cls) || (null == slot)) {
            return false;
        }
        boolean returnValue = true;
        returnValue = returnValue && slotTargetOfTypeInstance(cls, slot);
        returnValue = returnValue && slotHasRightCardinality(cls, slot);
        // returnValue = returnValue && singleAllowedClass(cls, slot);
        return returnValue;
    }

    public static JComponent getDetailedWarnings(Cls cls, Slot slot) {
        JTextArea returnValue = new JTextArea();
        boolean noErrorsFound = singleAllowedClass(cls, slot, returnValue);
        if (noErrorsFound) {
            returnValue.setForeground(Color.black);
            returnValue.setText("The widget seems to be correctly configured.");
        }
        return returnValue;
    }

    public static JComponent getShortWarning(Cls cls, Slot slot) {
        if (checkValidity(cls, slot)) {
            return null;
        }
        JTextField returnValue = new JTextField();
        returnValue.setForeground(Color.red);
        returnValue.setText("Widget incorrectly configured. See Forms Layout Panel for details.");
        return returnValue;
    }

    protected static boolean singleAllowedClass(Cls cls, Slot slot) {
        return cls.getTemplateSlotAllowedClses(slot).size() == 1;
    }

    protected static boolean singleAllowedClass(Cls cls, Slot slot, JTextArea warningDisplayArea) {
        if (!singleAllowedClass(cls, slot)) {
            warningDisplayArea.setForeground(Color.red);
            warningDisplayArea.append("The slot must have a single allowed class. \n");
            return false;
        }
        return true;
    }

    protected static boolean slotHasRightCardinality(Cls cls, Slot slot) {
        return !cls.getTemplateSlotAllowsMultipleValues(slot);
    }

    protected static boolean slotHasRightCardinality(Cls cls, Slot slot, JTextArea warningDisplayArea) {
        if (!slotHasRightCardinality(cls, slot)) {
            warningDisplayArea.setForeground(Color.red);
            warningDisplayArea.append("The slot must be a single-valued slot. \n");
            return false;
        }
        return true;
    }

    // validity checks
    protected static boolean slotTargetOfTypeInstance(Cls cls, Slot slot) {
        return cls.getTemplateSlotValueType(slot) == ValueType.INSTANCE;
    }

    // configuration checks
    protected static boolean slotTargetOfTypeInstance(Cls cls, Slot slot, JTextArea warningDisplayArea) {
        if (!slotTargetOfTypeInstance(cls, slot)) {
            warningDisplayArea.setForeground(Color.red);
            warningDisplayArea.append("The Target of the slot must be of type instance. \n");
            return false;
        }
        return true;
    }
}
