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

public class ExtendedPanel extends RequestPanel {

    JTextField _provider;
    JComboBox _operations;
    IdentifierPanel _identifier;
    AttributesPanel _attributes;
    JButton _newAttribute;
    JButton _clear;
    JButton _loadSchema;
    Installer _installer;

    public ExtendedPanel(State s) {
        super(s);
        _installer = new ExtendedInstaller();

        addRequestIdPanel();

        _operations = new JComboBox();
        _operations.addActionListener(this);
        _loadSchema = new JButton("Load Schema");
        _loadSchema.addActionListener(this);
        JPanel p = new JPanel();
        p.setLayout(new LinearLayout());
        p.add(_operations);
        p.add(_loadSchema);
        _form.add("Operation", p);

        refreshOperations();

        _provider = new JTextField(25);
        _provider.addActionListener(this);
	// need this only if the schema is not accessible
        // _form.add("Provider Id", _provider);

	_identifier = new IdentifierPanel();
	_identifier.addActionListener(this);
	_form.add("Identifier", _identifier);

        _attributes = new AttributesPanel();
        _attributes.addActionListener(this);
        _form.add("Arguments", _attributes);

        refreshAttributes();

        p = new JPanel();
        p.setLayout(new LinearLayout());
        _form.add("", p);

        _newAttribute = new JButton("New Argument");
        _newAttribute.addActionListener(this);
        p.add(_newAttribute);
        _clear = new JButton("Clear Form");
        _clear.addActionListener(this);
        p.add(_clear);
        _submit = new JButton("Submit");
        _submit.addActionListener(this);
        p.add(_submit);
    }

    public Installer getInstaller() {
        return _installer;
    }

    public SpmlRequest getRequest() {

        ExtendedRequest req = new ExtendedRequest();
        
        req.setRequestId(_reqid.getRequestId());
        req.setAsynchronous(_reqid.isAsync());
        req.setIdentifier(_identifier.getIdentifier());

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

        SchemaItem op = (SchemaItem)_operations.getSelectedItem();
        if (op != null) {
            req.setOperationIdentifier(op.getName());
            req.setProviderIdentifier(op.getProviderIdentifier());
        }

        req.setAttributes(_attributes.getAttributes(true));

        return req;
    }

    public void actionPerformed(ActionEvent e) {
        
        Object src = e.getSource();

        if (src == _operations) {
            refreshAttributes();
            refreshRequest();
        }
        else if (src == _newAttribute) {
            _attributes.addRow();
            resizeSplit();
        }
        else if (src == _clear) {
            _identifier.setText(null);
            refreshAttributes();
            refreshRequest();
        }
        else if (src == _loadSchema) {
            // submit a request, when Installer gets the response
            // we'll refresh the object class combo
            SchemaRequest r = new SchemaRequest();
            _state.doRequest(r, new ExtendedInstaller());
        }
        else {
            // doesn't really matter what it is, rebuild the
            // request XML
            SpmlRequest req = refreshRequest();
            if (req != null && src == _submit)
                _state.doRequest(req, new ExtendedInstaller());
        }
    }

    private void refreshOperations() {

        Vector items = null;
        List schemas = _state.getSchemas();
        if (schemas != null)
            items = SchemaItem.getExtendedRequests(schemas);
        else
            items = new Vector();

        _operations.setModel(new DefaultComboBoxModel(items));
    }

    private void refreshAttributes() {

        List attdefs = null;
        SchemaItem item = (SchemaItem)_operations.getSelectedItem();
        if (item != null) {
            ExtendedRequestDefinition def = item.getRequestDefinition();
            attdefs = def.getParameterDefinitions();
        }
        _attributes.reset(attdefs);
        resizeSplit();
    }

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

        public void install(SpmlResponse response) {

            if (response instanceof SchemaResponse) {
                // State should have captured the schema as a side effect,
                // can rebuild our combo box
                refreshOperations();
                refreshAttributes();
                refreshRequest();
            }
            else {
                refreshResponse(response);
            }
        }
    }


}
