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

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.ui.*;
import edu.stanford.smi.protege.util.*;

/**
 * The cls tree display on the instances panel.  This is different from the one on the classes panel in that it is less
 * functional.  The user can not create or delete classes from this panel.  The user also cannot change the "browse slot"
 * from this panel.
 * 
 * @author Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class InstanceClsesPanel extends SelectableContainer {
    private Project _project;
    private SelectableTree _clsTree;
    private LabeledComponent _labeledComponent;

    public InstanceClsesPanel(Project project) {
        _project = project;
        add(createHeaderPanel(), BorderLayout.NORTH);
        _labeledComponent = createClsesPanel();
        add(_labeledComponent, BorderLayout.CENTER);
        setSelectable(_clsTree);
    }
    
    private JComponent createHeaderPanel() {
        JLabel label = ComponentFactory.createLabel(_project.getName());
        label.setIcon(Icons.getProjectIcon());
        String classBrowserLabel = LocalizedText.getText(ResourceKey.CLASS_BROWSER_TITLE);
        String forProjectLabel = LocalizedText.getText(ResourceKey.CLASS_BROWSER_FOR_PROJECT_LABEL);
        HeaderComponent header = new HeaderComponent(classBrowserLabel, forProjectLabel, label);
        header.setColor(Colors.getClsColor());
        return header;
    }

    private LabeledComponent createClsesPanel() {
        Cls root = _project.getKnowledgeBase().getRootCls();
        _clsTree = ComponentFactory.createSelectableTree(null, new ParentChildRoot(root));
        _clsTree.setLargeModel(true);

        FrameRenderer renderer = FrameRenderer.createInstance();
        renderer.setDisplayDirectInstanceCount(!_project.getSuppressInstanceCounting());
        _clsTree.setCellRenderer(renderer);
        _clsTree.setSelectionRow(0);
        String classHiearchyLabel = LocalizedText.getText(ResourceKey.CLASS_BROWSER_HIERARCHY_LABEL);
        LabeledComponent c = new LabeledComponent(classHiearchyLabel, ComponentFactory.createScrollPane(_clsTree));
        c.setBorder(ComponentUtilities.getAlignBorder());
        c.addHeaderButton(getViewClsAction());
        c.setFooterComponent(new ClsTreeFinder(_project.getKnowledgeBase(), _clsTree));
        return c;
    }

    public JTree getDropComponent() {
        return _clsTree;
    }
    
    public LabeledComponent getLabeledComponent() {
        return _labeledComponent;
    }

    private Action getViewClsAction() {
        return new ViewAction(ResourceKey.CLASS_VIEW, this) {
            public void onView(Object o) {
                Cls cls = (Cls) o;
                _project.show(cls);
            }
        };
    }

    public void setSelectedCls(Cls cls) {
        Collection path = ModelUtilities.getPathToRoot(cls);
        ComponentUtilities.setSelectedObjectPath(_clsTree, path);
    }
}
