
package org.openspml.browser;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
// avoid awt conflict
import java.util.List;

import org.openspml.util.*;
import org.openspml.message.*;

public class SchemaPanel extends RequestPanel {

    JTextField _provider;
    JTextField _schema;
    SchemaTree _schemaTree;
    SchemaInstaller _installer;

    public SchemaPanel(State s) {
        super(s);
        _installer = new SchemaInstaller();

        addRequestIdPanel();

        _provider = new JTextField(25);
        _provider.addActionListener(this);
        _form.add("Provider Identifier", _provider);

        _schema = new JTextField(25);
        _schema.addActionListener(this);
        _form.add("Schema Identifier", _schema);

        addSubmitButton();

        _schemaTree = new SchemaTree();
        JScrollPane sp = new JScrollPane();
        SwingUtil.add(sp, _schemaTree);
        _tabs.add(sp, "Schema Tree");
    }

    public Installer getInstaller() {
        return _installer;
    }

    public SpmlRequest getRequest() {

        SchemaRequest req = new SchemaRequest();

        String pid = SwingUtil.getText(_provider);
        if (pid != null)
            req.setProviderIdentifier(pid);

	String sid = SwingUtil.getText(_schema);
	if (sid != null)
	    req.setSchemaIdentifier(sid);

        return req;
    }

    /**
     * Handler eventually called via State.doRequest in the Swing
     * event thread to install a response into the GUI.
     */
    private class SchemaInstaller extends Installer {

        public void install(SpmlResponse response) {

            refreshResponse(response);
            
            SchemaResponse sr = (SchemaResponse)response;
            _schemaTree.setSchemas(sr.getSchemas());
            _tabs.setSelectedIndex(2);
        }
    }

}
