// Copyright 2005, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/

package com.danger.text;

import danger.app.Application;
import danger.app.Registrar;
import danger.app.Event;
import danger.net.HTTPConnection;
import danger.net.HTTPTransaction;
import java.util.Vector;

public class
Text
extends Application
{
	public
	Text()
	{
		(new DemoWindow(true)).show();
	}

	public boolean
	receiveEvent(Event e)
	{
		switch (e.type)
		{
			case Events.EVENT_GET_DISPLAY_TEXT:
				new GetDisplayText().show();
				return true;

			case Events.EVENT_GREENBAR:
				new Greenbar().show();
				return true;

			case Events.EVENT_CONVERSATION:
				new Conversation().show();
				return true;

			case Events.EVENT_NESTED_MARGINS:
				new NestedMargins().show();
				return true;

			case Events.EVENT_IMAGES:
				new Images().show();
				return true;

			case Events.EVENT_HTML:
				new HTMLWindow().show();
				return true;

			case Events.EVENT_EMBEDDED:
				new Embedded().show();
				return true;

			case Events.EVENT_FORMATTING:
				new Formatting().show();
				return true;
		}

		return super.receiveEvent(e);
	}

	public void
	networkEvent(Object o)
	{
		if (o instanceof HTTPTransaction)
		{
			HTTPTransaction ht = (HTTPTransaction) o;
			int seq = ht.getSequenceID();

			synchronized (sRequests)
			{
				for (int i = sRequests.size() - 1; i >= 0; i--)
				{
					HTTPRequest r = (HTTPRequest) sRequests.elementAt(i);

					if (r.mSeq == seq)
					{
						sRequests.removeElement(r);
						r.received(ht);
						return;
					}
				}
			}
		}

		super.networkEvent(o);
	}

	public static void
	request(String url, HTTPRequest callback)
	{
		callback.mSeq = sSequence++;
		sRequests.addElement(callback);
		HTTPConnection.get(url, "", (short) 0, callback.mSeq);
	}

	public abstract static class
	HTTPRequest
	{
		private int mSeq;
		public abstract void received(HTTPTransaction ht);
	}

	private static int sSequence = 0;
	private static Vector sRequests = new Vector();
}
