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

import java.util.*;

import javax.swing.*;

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

/**
 *  Description of the Class
 *
 * @author    William Grosso <grosso@smi.stanford.edu>
 */
public class SymbolEditor extends ComboBoxEditor {
    protected AlphabeticalComboBoxModel _model;

    public SymbolEditor() {
        this(null, null);
    }

    public SymbolEditor(Instance instance, Slot slot) {
        super(instance, slot);
    }

    public void configureUIFromKB() {
        if (_readingFromModel) {
            return;
        }
        _readingFromModel = true;
        if ((null != _instance) && (null != _slot)) {
            Cls instanceCls = _instance.getDirectType();
            Collection possibleValues = instanceCls.getTemplateSlotAllowedValues(_slot);
            ArrayList modifiedValues = new ArrayList(possibleValues);
            modifiedValues.add(NULL_STRING);
            ((AlphabeticalComboBoxModel) _model).setContents(modifiedValues);
            setValue();
        }
        _readingFromModel = false;
    }

    protected ComboBoxModel getComboBoxModel() {
        _model = new AlphabeticalComboBoxModel();
        return _model;
    }

    public void setValue() {
        Object value = getValue();
        if (null == value) {
            _model.setSelectedItem(NULL_STRING);
        } else {
            _model.setSelectedItem(value);
        }
    }

    public void storeValueInKB() {
        Object value = _model.getSelectedItem();
        if (value == NULL_STRING) {
            _instance.setOwnSlotValue(_slot, null);
        } else {
            _instance.setOwnSlotValue(_slot, value);
        }
    }
}
