// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   UrlEditor.java

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
import lt.ktu.gmj.propertySheet.*;

public class UrlEditor extends StringablePropertyEditor
	implements PropertyStringer
{

	private static boolean bTryOpenFile = true;
	private PropertyProvider provider;

	public UrlEditor(PropertyProvider propertyprovider)
	{
		provider = propertyprovider;
		provider.addObserver(this);
		setStringer(this);
	}

	boolean CanOpenFile(String s)
	{
		if(!bTryOpenFile)
			return false;
		if(s == null)
			return false;
		try
		{
			FileInputStream fileinputstream = null;
			fileinputstream = new FileInputStream(s);
			if(fileinputstream != null)
			{
				fileinputstream.close();
				return true;
			}
		}
		catch(SecurityException _ex) { }
		catch(IOException _ex) { }
		return false;
	}

	boolean CanOpenUrl(String s)
	{
		try
		{
			URL url = new URL(s);
			System.out.println("Will read data from: " + url.getHost() + url.getFile());
			URLConnection urlconnection = url.openConnection();
			System.out.println("Data type will be: " + urlconnection.getContentType() + " and size: " + urlconnection.getContentLength());
			if(urlconnection.getContentLength() > 0)
				return true;
		}
		catch(MalformedURLException _ex) { }
		catch(IOException _ex) { }
		return false;
	}

	synchronized String OpenNewFile(String s)
	{
		if(!bTryOpenFile)
			return null;
		String s1 = null;
		Frame frame = new Frame();
		FileDialog filedialog = null;
		try
		{
			filedialog = new FileDialog(frame, "Load shedule from file", 0);
		}
		catch(SecurityException _ex) { }
		catch(Exception _ex) { }
		if(filedialog == null)
		{
			bTryOpenFile = false;
			return null;
		}
		filedialog.show();
		s1 = filedialog.getFile();
		if(s1 == null)
		{
			return null;
		} else
		{
			s1 = filedialog.getDirectory() + s1;
			return s1;
		}
	}

	public String getAsString()
	{
		return provider.get().toString();
	}

	public void setAsString(String s)
		throws InvalidPropertyException
	{
		if(CanOpenUrl(s))
		{
			provider.set(s);
			return;
		}
		if(CanOpenFile(s))
		{
			provider.set(s);
			return;
		}
		String s1 = OpenNewFile(s);
		if(s1 != null)
		{
			if(CanOpenUrl(s1))
			{
				if(component() instanceof JTextField)
					((JTextField)component()).setText(s1);
				provider.set(s1);
				return;
			}
			if(CanOpenFile(s1))
			{
				if(component() instanceof JTextField)
					((JTextField)component()).setText(s1);
				provider.set(s1);
				return;
			}
		}
		throw new InvalidPropertyException("Cann't open input file: " + s);
	}

}
