
package org.openspml.browser;

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

// avoid awt conflict
import java.util.List;


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

public abstract class RequestPanel extends JPanel 
  implements ActionListener {

    State _state;
    JSplitPane _split;
    SimpleForm _form;
    RequestIdPanel _reqid;
    JButton _submit;
    JTabbedPane _tabs;
    JTextArea _requestXml;
    JTextArea _responseXml;
    Installer _defaultInstaller;

    public static void println(String msg) {
        System.out.println(msg);
    }

    public abstract SpmlRequest getRequest();

    public Installer getInstaller() {
        return _defaultInstaller;
    }

    public RequestPanel(State s) {

	setLayout(new BorderLayout());
	_state = s;
        _defaultInstaller = new DefaultInstaller();

        _split = new JSplitPane();
        _split.setOrientation(JSplitPane.VERTICAL_SPLIT);
        add(_split, BorderLayout.CENTER);

        JPanel top = new JPanel();
        _split.add(top, JSplitPane.TOP);
        _tabs = new JTabbedPane();
        _split.add(_tabs, JSplitPane.BOTTOM);

        top.setLayout(new BorderLayout());
        _form = new SimpleForm();
        top.add(_form, BorderLayout.WEST);



        _requestXml = new JTextArea();
        JScrollPane sp = new JScrollPane();
        SwingUtil.add(sp, _requestXml);
        _tabs.add(sp, "Request XML");

        _responseXml = new JTextArea();
        sp = new JScrollPane();
        SwingUtil.add(sp, _responseXml);
        _tabs.add(sp, "Response XML");
    }

    public void addRequestIdPanel() {
        _reqid = new RequestIdPanel();
        _reqid.addActionListener(this);
        _form.add("Request Id", _reqid);
    }

    public void addSubmitButton() {

        _submit = new JButton("Submit");
        _submit.addActionListener(this);
        _form.add("", _submit);
    }

    public void resizeSplit() {

        // will revalidate and move the split bar
        // I would imagine this works as long as the panel
        // isn't in a JScrollPane
        _split.resetToPreferredSizes();
    }

    public void actionPerformed(ActionEvent e) {
        
        // System.out.println(e.toString());

        SpmlRequest req = refreshRequest();
        if (req != null && e.getSource() == _submit) {
            Installer inst = getInstaller();
            if (inst != null)
                _state.doRequest(req, inst);
        }
    }

    public SpmlRequest refreshRequest() {

        // always regenerate this
        _tabs.setSelectedIndex(0);
        _requestXml.setText(null);

        // null this now or wait until submit?
        _responseXml.setText(null);

        SpmlRequest req = getRequest();
        if (req != null)
            _requestXml.setText(req.toXml());

        return req;
    }

    public void refreshResponse(SpmlResponse res) {

        _responseXml.setText(res.toXml());
        _tabs.setSelectedIndex(1);
    }

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

        public void install(SpmlResponse response) {
            refreshResponse(response);
        }
    }

}
