
package org.openspml.browser;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;

import org.openspml.message.*;

/**
 * Helper class to maintan a list of selectable items
 * from multiple schemas.
 */
public class SchemaItem {

    Schema _schema;
    ObjectClassDefinition _class;
    ExtendedRequestDefinition _request;

    public SchemaItem(Schema s, ObjectClassDefinition ocd) {
        _schema = s;
        _class = ocd;
    }

    public SchemaItem(Schema s, ExtendedRequestDefinition erd) {
        _schema = s;
        _request = erd;
    }

    public Schema getSchema() {
        return _schema;
    }

    public ObjectClassDefinition getClassDefinition() {
        return _class;
    }

    public ExtendedRequestDefinition getRequestDefinition() {
        return _request;
    }

    public ProviderIdentifier getProviderIdentifier() {
        return (_schema != null) ? 
            _schema.getProviderIdentifier() : null;
    }

    public String getName() {
        String name = null;

        if (_class != null)
            name = _class.getName();
        else if (_request != null)
            name = _request.getOperationIdentifierString();

        return name;
    }

    public String getDisplayName() {
        String name = null;

        if (_class != null) {
            name = (String)_class.getProperty("displayName");
            if (name == null)
                name = _class.getName();
        }
        else if (_request != null) {
            name = (String)_request.getProperty("displayName");
            if (name == null)
                name = _request.getOperationIdentifierString();
        }

        return name;
    }

    public String toString() {
        String s = getDisplayName();
        if (s == null)
            s = "???";
        return s;
    }

    //////////////////////////////////////////////////////////////////////
    //
    // Static factory methods
    //
    //////////////////////////////////////////////////////////////////////

    /**
     * Build a Vector of SchemaItems for object classes in a list
     * of schemas.  Inteded for use with a JComboBox.
     */
    public static Vector getExtendedRequests(List schemas) {

        Vector items = new Vector();

        if (schemas != null) {
            for (int i = 0 ; i < schemas.size() ; i++) {
                Schema s = (Schema)schemas.get(i);
                List ops = s.getExtendedRequestDefinitions();
                if (ops != null) {
                    for (int j = 0 ; j < ops.size() ; j++) {
                        ExtendedRequestDefinition op =
                            (ExtendedRequestDefinition)ops.get(j);

                        items.add(new SchemaItem(s, op));
                    }
                }
            }
        }
        return items;
    }

    /**
     * Build a Vector of SchemaItems for object classes in a list
     * of schemas.  Inteded for use with a JComboBox.
     */
    public static Vector getObjectClasses(List schemas) {

        Vector items = new Vector();

        if (schemas != null) {
            for (int i = 0 ; i < schemas.size() ; i++) {
                Schema s = (Schema)schemas.get(i);
                List classes = s.getObjectClassDefinitions();
                if (classes != null) {
                    for (int j = 0 ; j < classes.size() ; j++) {
                        ObjectClassDefinition def =
                            (ObjectClassDefinition)classes.get(j);

                        items.add(new SchemaItem(s, def));
                    }
                }
            }
        }
        return items;
    }

}
