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

import javax.swing.*;

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

/**
 * Panel to display included projects and allow the user to change them.
 */

class ChangeIncludedProjectsPanel extends JComponent {
    private SelectableList _list;

    ChangeIncludedProjectsPanel(Project project) {
        setLayout(new BorderLayout());
        _list = ComponentFactory.createSelectableList(null);
        ComponentUtilities.setListValues(_list, project.getDirectIncludedProjectURIs());
        LabeledComponent c = new LabeledComponent("Directly Included Projects", new JScrollPane(_list));
        c.addHeaderButton(createSelectProjectAction());
        c.addHeaderButton(createRemoveProjectAction(_list));
        add(c);
        setPreferredSize(new Dimension(300, 300));
    }

    public Collection getIncludedProjectURIs() {
        return ComponentUtilities.getListValues(_list);
    }

    private Action createSelectProjectAction() {
        return new AddAction(ResourceKey.PROJECT_ADD) {
            public void onAdd() {
                JFileChooser chooser = ComponentFactory.createFileChooser("Select Project", "Project Files", "pprj");
                int openDialogResult = chooser.showOpenDialog(ChangeIncludedProjectsPanel.this);
                switch (openDialogResult) {
                    case JFileChooser.ERROR_OPTION:
                        // Get this on 'close"
                        break;
                    case JFileChooser.CANCEL_OPTION:
                        break;
                    case JFileChooser.APPROVE_OPTION:
                        ComponentUtilities.addListValue(_list, chooser.getSelectedFile().toURI());
                        break;
                    default:
                        Assert.fail("bad result: " + openDialogResult);
                        break;
                }
            }
        };
    }

    private Action createRemoveProjectAction(SelectableList list) {
        return new RemoveAction(ResourceKey.PROJECT_REMOVE, list) {
            public void onRemove(Collection values) {
                ComponentUtilities.removeListValues(_list, values);
            }
        };
    }
}
