
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 AddPanel extends RequestPanel {

    JComboBox _classes;
    IdentifierPanel _identifier;
    JButton _loadSchema;
    JButton _newAttribute;
    JButton _clear;
    AttributesPanel _attributes;
    AddInstaller _installer;

    public AddPanel(State s) {
        super(s);
        _installer = new AddInstaller();

        addRequestIdPanel();

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

        refreshObjectClasses();

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

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

        refreshAttributes();

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

        _newAttribute = new JButton("New Attribute");
        _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 void actionPerformed(ActionEvent e) {
        
        Object src = e.getSource();

        if (src == _classes) {
            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, _installer);
        }
        else {
            SpmlRequest req = refreshRequest();
            if (req != null && src == _submit)
                _state.doRequest(req, _installer);
        }
    }

    public Installer getInstaller() {
        return _installer;
    }

    public SpmlRequest getRequest() {

        AddRequest req = new AddRequest();

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

        // spec issue, have to assume objectclass is unique or else
        // pass a schemaIdentifier with the request
        SchemaItem item = (SchemaItem)_classes.getSelectedItem();
        if (item != null)
            req.setObjectClass(item.getName());
        
        // pass true to filter null values
        req.setAttributes(_attributes.getAttributes(true));

        return req;
    }

    private void refreshObjectClasses() {

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

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

    private void refreshAttributes() {

        List attdefs = null;
        SchemaItem item = (SchemaItem)_classes.getSelectedItem();
        if (item != null) {
            ObjectClassDefinition def = item.getClassDefinition();
            attdefs = def.getAttributeDefinitions();
        }
        _attributes.reset(attdefs);
        resizeSplit();
    }

    /**
     * Handler eventually called via State.doRequest in the Swing
     * event thread to install a response into the GUI.
     */
    private class AddInstaller 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
                refreshObjectClasses();
                refreshAttributes();
                refreshRequest();
            }
            else {
                refreshResponse(response);
            }
        }
    }

}
