// 
// The Waveset SPML General License 
// 
// Version 0.1, April 2003
// Copyright (C) 2003 Waveset Technologies, Inc..
// 6034 West Courtyard Drive, Suite 210, Austin, Texas 78730
// All rights reserved.
// 
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions, and the disclaimers in Sections 6
//    and 7 below.
// 
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the disclaimers in Sections 6
//    and 7 below in the documentation and/or other materials provided
//    with the distribution.
// 
// 3. The end-user documentation included with the redistribution, if
//    any, must include the following acknowledgment:
// 
//    "This product includes software developed by 
//     Waveset Technologies, Inc. (www.waveset.com)."
// 
//    Alternately, this acknowledgment may appear in the software itself, if
//    and wherever such third-party acknowledgments normally appear.
//  
// 4. The names "Waveset" and "Waveset Technologies, Inc." must not be
//    used to endorse or promote products derived from this software
//    without the prior written permission of Waveset. For written
//    permission, please contact www.waveset.com.
//  
// 5. Products derived from this software may not be called "Waveset",
//    nor may "Waveset" appear in their name, without the prior written
//    permission of Waveset.
//  
// 6. NO WARRANTY 
// 
//    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED
//    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//    DISCLAIMED.
// 
// 7.  LIMITATION OF LIABILITY
// 
//    IN NO EVENT SHALL THE WAVESET OR ITS LICENSORS BE LIABLE FOR ANY
//    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
//    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
//    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
//    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
//    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// End of Terms and Conditions
// ====================================================================
//
// Component Name: org.openspml.browser.SOAPTracer
//
// Author(s): Jeff Larson
//
// Description:
//
// A popup window that displays a trace of SOAP message activity.
// Implemented as a non-modal Frame so it can be used without
// impacting the application.
// 
//
//

package org.openspml.browser;

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

import java.util.List;
import java.util.ArrayList;


import org.openspml.client.SOAPMonitor;
import org.openspml.client.SOAPMonitorListener;
import org.openspml.client.SpmlClient;

public class SOAPTracer extends JFrame implements SOAPMonitor {

    List _listeners;
    SpmlClient _client;

    public JButton _clear;
    public JTextArea _trace;

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

    public SOAPTracer() {
	this(null);
    }

    public SOAPTracer(SpmlClient client) {

	enableEvents(AWTEvent.WINDOW_EVENT_MASK);
	try {
            setClient(client);
	    initComponents();
	    this.show();
	}
	catch(Exception e) {
	    e.printStackTrace();
	}
    }

    public void addListener(SOAPMonitorListener l) {
	if (_listeners == null)
	    _listeners = new ArrayList();
	_listeners.add(l);
    }

    public void removeListener(SOAPMonitorListener l) {
	if (_listeners != null) {
	    _listeners.remove(l);
	    // call this?
	    l.closed(this);
	}
    }

    public void setClient(SpmlClient client) {

        if (_client != null)
            _client.setMonitor(null);

        _client = client;

        if (_client != null)
            _client.setMonitor(this);
    }

    private void initComponents() throws Exception  {

	JPanel contentPane = (JPanel)getContentPane();
	contentPane.setLayout(new BorderLayout());
	contentPane.setPreferredSize(new Dimension(400, 600));

	ImageIcon spmlIcon = new ImageIcon(SOAPTracer.class.getResource("openSpml.gif"));
	setIconImage(spmlIcon.getImage());

	setSize(new Dimension(1000, 700));
	setTitle("OpenSPML SOAP Tracer");

        JMenuBar menuBar = new JMenuBar();
	this.setJMenuBar(menuBar);

        // File menu

        JMenu menu = new JMenu();
        menuBar.add(menu);
        menu.setText("File");

        // File->Exit
        JMenuItem item = new JMenuItem();
        menu.add(item);
        item.setText("Exit");
	item.addActionListener(new ActionListener()  {
		public void actionPerformed(ActionEvent e) {
                    dispose();
		}
	    });


        JPanel buttons = new JPanel();
        buttons.setLayout(new FlowLayout());
        _clear = new JButton("Clear");
        buttons.add(_clear);

	_clear.addActionListener(new ActionListener()  {
		public void actionPerformed(ActionEvent e) {
                    _trace.setText(null);
		}
	    });

	contentPane.add(buttons, BorderLayout.SOUTH);

        _trace = new JTextArea();
        JScrollPane sp = new JScrollPane();
        SwingUtil.add(sp, _trace);
	contentPane.add(sp, BorderLayout.CENTER);
    }

    /**
     * Overload this to be sure we unregister ourselves from the client.
     */
    public void dispose() {

	informListeners();
        super.dispose();
    }

    private void informListeners() {

	if (_listeners != null) {
	    for (int i = 0 ; i < _listeners.size() ; i++) {
		SOAPMonitorListener l = (SOAPMonitorListener)_listeners.get(i);
		if (l != null)
		    l.closed(this);
	    }
	    // can we remove these?
	    _listeners = null;
	}

        if (_client != null) {
            _client.setMonitor(null);
            _client = null;
        }
    }

    protected void processWindowEvent(WindowEvent e) {
	super.processWindowEvent(e);
	if (e.getID() == WindowEvent.WINDOW_CLOSING) {
	    // for some reason don't get into dispose when X'ing?
	    informListeners();
	}
    }

    //////////////////////////////////////////////////////////////////////
    //
    // SOAPMonitor
    //
    //////////////////////////////////////////////////////////////////////

    public void send(String payload) {

        _trace.append("\n------------------------------SEND------------------------------\n");
        _trace.append(payload);
    }

    public void receive(String payload) {

        _trace.append("\n-----------------------------RECIEVE----------------------------\n");
        _trace.append(payload);
    }

    public void trace(String msg) {
	_trace.append("TRACE: " );
        _trace.append(msg);
	_trace.append("\n");
    }

}
