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

import java.awt.event.*;
import java.util.*;

import javax.swing.*;

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

/**
 * An action to remove an item.  Note that this is different from deleting the item.  Removing it causes the reference
 * to the item to be removed from this particular place but the same item may still be refered to elsewhere.
 *
 * @author    Ray Fergerson <fergerson@smi.stanford.edu>
 */
public abstract class RemoveAction extends AllowableAction {

    protected RemoveAction(ResourceKey key, Selectable selectable) {
        super(key, selectable);
    }

    protected RemoveAction(String text, Selectable selectable) {
        this(text, selectable, Icons.getRemoveIcon());
    }

    protected RemoveAction(String text, Selectable selectable, Icon icon) {
        this("Remove", text, selectable, icon);
    }

    protected RemoveAction(String name, String text, Selectable selectable, Icon icon) {
        super(name, text, icon, selectable);
    }

    public void actionPerformed(ActionEvent event) {
        if (isAllowed() && confirmRemove()) {
            onRemove();
        }
    }

    private boolean confirmRemove() {
        boolean result;
        Project project = ProjectManager.getProjectManager().getCurrentProject();
        if (project != null && project.getDisplayConfirmationOnRemove()) {
            String text = LocalizedText.getText(ResourceKey.DIALOG_CONFIRM_REMOVE_TEXT);
            int choice = ModalDialog.showMessageDialog((JComponent) getSelectable(), text, ModalDialog.MODE_YES_NO);
            result = choice == ModalDialog.OPTION_YES;
        } else {
            result = true;
        }
        return result;
    }

    public void onRemove(Collection objects) {
        Iterator i = objects.iterator();
        while (i.hasNext()) {
            Object o = i.next();
            onRemove(o);
        }
    }

    //ESCA-JAVA0130 
    public void onRemove(Object o) {
        Log.getLogger().warning("onRemove called: should have been overridden");
    }

    public final void onRemove() {
        Collection objects = new ArrayList(getSelection());
        onRemove(objects);
    }
}
