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

/**
 * Abstract class that implements many of the common "widget" methods.
 *
 * @author    Ray Fergerson <fergerson@smi.stanford.edu>
 */
public abstract class AbstractWidget extends JPanel {
    private WidgetDescriptor _descriptor;
    private boolean _isDesignTime;
    private Project _project;
    private ListenerCollection _selectionListeners = new ListenerList(new SelectionEventDispatcher());

    public AbstractWidget() {
        setLayout(new BorderLayout());
        setName(StringUtilities.getClassName(this));
    }

    public void dispose() {
        // do nothing
    }

    public void setup(WidgetDescriptor descriptor, boolean isDesignTime, Project project) {
        _descriptor = descriptor;
        _isDesignTime = isDesignTime;
        _project = project;
    }

    public WidgetDescriptor getDescriptor() {
        return _descriptor;
    }

    public KnowledgeBase getKnowledgeBase() {
        return _project.getKnowledgeBase();
    }

    public Project getProject() {
        return _project;
    }

    public PropertyList getPropertyList() {
        return _descriptor.getPropertyList();
    }

    public void setPropertyList(PropertyList list) {
        Assert.assertTrue("design time", isDesignTime());
        _descriptor.setPropertyList(list);
    }

    public String getStringProperty(String name, String defaultString) {
        String property = getPropertyList().getString(name);
        if (property == null) {
            property = defaultString;
        }
        return property;
    }

    public boolean isDesignTime() {
        return _isDesignTime;
    }

    public boolean isRuntime() {
        return !_isDesignTime;
    }

    public void show(Cls cls, Slot slot) {
        getProject().show(cls, slot);
    }

    public void showInstance(Instance instance) {
        getProject().show(instance);
    }

    public String getLabel() {
        return _descriptor.getLabel();
    }

    public void setLabel(String label) {
        _descriptor.setLabel(label);
    }

    public void addSelectionListener(SelectionListener listener) {
        _selectionListeners.add(this, listener);
    }

    public void removeSelectionListener(SelectionListener listener) {
        _selectionListeners.remove(this, listener);
    }

    public void notifySelectionListeners() {
        _selectionListeners.postEvent(this, SelectionEvent.SELECTION_CHANGED);
    }

    public void clearSelection() {
        // do nothing
    }

    public Collection getSelection() {
        // do nothing
        return Collections.EMPTY_LIST;
    }

    public static boolean equals(Object o1, Object o2) {
        return SystemUtilities.equals(o1, o2);
    }

    protected void beginTransaction(String name) {
        getKnowledgeBase().beginTransaction(name);
    }

    protected void beginTransaction(String name, String appliedToFrameName) {
        getKnowledgeBase().beginTransaction(name, appliedToFrameName);
    }

    /**
     * @deprecated Use commitTranscation() or rollbackTransaction()
     */
    protected void endTransaction() {
        getKnowledgeBase().commitTransaction();
    }

    protected void commitTransaction() {
        getKnowledgeBase().commitTransaction();
    }

    protected void rollbackTransaction() {
        getKnowledgeBase().rollbackTransaction();
    }

    public void paint(Graphics g) {
        ComponentUtilities.enableTextAntialiasing(g);
        super.paint(g);
    }
}
