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

import java.util.Iterator;

import javax.swing.JComponent;

import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.Instance;
import edu.stanford.smi.protege.resource.LocalizedText;
import edu.stanford.smi.protege.resource.ResourceKey;
import edu.stanford.smi.protege.util.DeleteAction;
import edu.stanford.smi.protege.util.ModalDialog;
import edu.stanford.smi.protege.util.Selectable;

/**
 * Action to attempt to delete an instance.  If the instance being deleted is a class then that class cannot have
 * any instance.  The reason for this restriction is that people have been known to delete a metaclass without realizing
 * that all of its instances will disappear.  It might be better to just do the check on metaclasses...
 *
 * @author Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class DeleteInstancesAction extends DeleteAction {

    public DeleteInstancesAction(Selectable selectable) {
        this(ResourceKey.INSTANCE_DELETE, selectable);
    }

    public DeleteInstancesAction(ResourceKey key, Selectable selectable) {
        super(key, selectable);
    }

    /**
     * @deprecated Use ResourceKey version
     */
    @Deprecated
	public DeleteInstancesAction(String text, Selectable selectable) {
        super(text, selectable);
    }

    private boolean canDelete(Instance instance) {
        boolean result = true;
        if (instance instanceof Cls) {
            Cls cls = (Cls) instance;
            int instanceCount = cls.getInstanceCount();
            result = instanceCount == 0;
            if (!result) {
                String text = LocalizedText.getText(ResourceKey.DELETE_CLASS_FAILED_DIALOG_TEXT);
                ModalDialog.showMessageDialog((JComponent) getSelectable(), text);
            }
        }
        return result;
    }

    protected void onAboutToDelete(Object o) {
        // do nothing by default
    }

    protected void onAfterDelete(Object o) {
        // do nothing
    }

    @Override
	public void onDelete(Object o) {
        Instance instance = (Instance) o;
        if (canDelete(instance)) {
            onAboutToDelete(instance);
            deleteInstance(instance);
            onAfterDelete(o);
        }
    }

    //ESCA-JAVA0130
    public void deleteInstance(Instance instance) {
        instance.getKnowledgeBase().deleteFrame(instance);
    }

    @Override
	public void onSelectionChange() {
        boolean isEditable = true;
        Iterator i = getSelection().iterator();
        while (i.hasNext()) {
            Instance instance = (Instance) i.next();
            if (!instance.isEditable()) {
                isEditable = false;
                break;
            }
        }
        setAllowed(isEditable);
    }
}
