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

import javax.swing.JComponent;
import javax.swing.JSplitPane;

import edu.stanford.smi.protege.model.Slot;
import edu.stanford.smi.protege.resource.Icons;
import edu.stanford.smi.protege.resource.LocalizedText;
import edu.stanford.smi.protege.resource.ResourceKey;
import edu.stanford.smi.protege.ui.InstanceDisplay;
import edu.stanford.smi.protege.ui.SubslotPane;
import edu.stanford.smi.protege.util.CollectionUtilities;
import edu.stanford.smi.protege.util.LabeledComponent;
import edu.stanford.smi.protege.util.SelectionEvent;
import edu.stanford.smi.protege.util.SelectionListener;

/**
 * The standard slot tab.  This tab displays slots and subslots in a tree and allows
 * the user to edit top-level slots.
 *
 * @author    Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class SlotsTab extends AbstractTabWidget {
    private SubslotPane _slotsPanel;
    private SuperslotsPanel _superslotsPanel;
    private InstanceDisplay _slotDisplay;
    private JSplitPane _mainSplitter;
    private JSplitPane _clsesSplitter;

    private JComponent createMainSplitter() {
        _mainSplitter = createLeftRightSplitPane("SlotsTab.left_right", 250);
        _mainSplitter.setLeftComponent(createSlotsSplitter());
        _slotDisplay = (InstanceDisplay) createSlotDisplay();
        _mainSplitter.setRightComponent(_slotDisplay);
        return _mainSplitter;
    }

    private JComponent createSuperslotsPanel() {
        _superslotsPanel = new SuperslotsPanel(getProject());
        _superslotsPanel.addSelectionListener(new SelectionListener() {
            public void selectionChanged(SelectionEvent event) {
                Collection selection = _superslotsPanel.getSelection();
                if (selection.size() == 1) {
                    Slot slot = (Slot) CollectionUtilities.getFirstItem(selection);
                    _slotsPanel.setDisplayParent(slot);
                }
            }
        });
        return _superslotsPanel;
    }

    protected JComponent createSlotDisplay() {
        return new InstanceDisplay(getProject());
    }

    private JComponent createSlotsPanel() {
        _slotsPanel = new SubslotPane(getProject());
        _slotsPanel.addSelectionListener(new SelectionListener() {
            public void selectionChanged(SelectionEvent event) {
                transmitSelection();
            }
        });
        return _slotsPanel;
    }

    private JComponent createSlotsSplitter() {
        _clsesSplitter = createTopBottomSplitPane("SlotTab.left.top_bottom", 400);
        _clsesSplitter.setTopComponent(createSlotsPanel());
        _clsesSplitter.setBottomComponent(createSuperslotsPanel());
        return _clsesSplitter;
    }
    
    public LabeledComponent getLabeledComponent() {
        return _slotsPanel.getLabeledComponent();
    }

    public void initialize() {
        setIcon(Icons.getSlotIcon());
        setLabel(LocalizedText.getText(ResourceKey.SLOTS_VIEW_TITLE));
        add(createMainSplitter());
        transmitSelection();
        setClsTree(_slotsPanel.getDropComponent());
    }

    private void transmitSelection() {
        Slot selection = (Slot) CollectionUtilities.getFirstItem(_slotsPanel.getSelection());
        _superslotsPanel.setSlot(selection, _slotsPanel.getDisplayParent());
        _slotDisplay.setInstance(selection);
    }

}
