/*
 Eteria IRC Client, an RFC 1459 compliant client program written in Java.
 Copyright (C) 2000-2001  Javier Kohen <jkohen at tough.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 import java.awt.*;
 import java.net.URL;
 import java.net.MalformedURLException;
 import java.text.*;
 import java.util.*;
 
 import ar.com.jkohen.awt.ChatPanel;
 import ar.com.jkohen.irc.Channel;
 import ar.com.jkohen.irc.MircMessage;
 import ar.com.jkohen.util.*;
 import com.splendid.awtchat.*;
 
 public abstract class OutputWindow extends ChatPanel implements HyperlinkReceiver, Observer, CopyText
 {
 	protected SmileyTextArea text_canvas;
 
 	protected boolean hyperlinks = true;
 	private Resources res;
 	private boolean hasfocus;
 	private DateFormat dateformat = null;
 
 	protected static final MessageFormat DATE = new MessageFormat(Resources.getString("msg.date"));
 	
 	protected static final MessageFormat INFO = new MessageFormat(Resources.getString("msg.info"));
 	protected static final MessageFormat ERROR = new MessageFormat(Resources.getString("msg.error"));
 	protected static final MessageFormat NOTICE	= new MessageFormat(Resources.getString("msg.notice"));
 	protected static final MessageFormat WARNING = new MessageFormat(Resources.getString("msg.warning"));
 	
 	protected static final MessageFormat SERV_NOTICE = new MessageFormat(Resources.getString("msg.serv_notice"));
 	protected static final MessageFormat STRIP_NOTICE = new MessageFormat(Resources.getString("msg.strip_notice"));
 
 	public OutputWindow(String title)
 	{
 		super(title);
 		text_canvas = new SmileyTextArea(this, this);
 	}
 
 	public void update(Observable o, Object arg)
 	{
 		ConfigurationProperties props = (ConfigurationProperties) o;
 
 		if (arg == null || arg.equals("scroll_speed"))
 			text_canvas.setMode(props.getInt("scroll_speed"));
 			
 		if (arg == null || arg.equals("date_format"))
 			setDateFormat(props.getInt("date_format"));
 	}
 	
 	public void clear()
 	{
 		text_canvas.clear();
 	}
 		
 	/* High level methods.
 	 */
 	 
 	public void printError(String s)
 	{
 		Object [] o = { getDate(), s };
 		text_canvas.append(ERROR.format(o));
 
 		postTextEvent();
 	}
 
 	public void printInfo(String s)
 	{
 		Object [] o = { getDate(), s };
 		text_canvas.append(INFO.format(o));
 		
 		postTextEvent();
 	}
 
 	public void printWarning(String s)
 	{
 		Object [] o = { getDate(), s };
 		text_canvas.append(WARNING.format(o));
 
 		postTextEvent();
 	}
 
 	public void printNotice(String s, String whom)
 	{
 		Object [] o = { getDate(), whom, s };
 		if (hyperlinks)
 			text_canvas.append(NOTICE.format(o));
 		else
 			text_canvas.appendNoUrls(NOTICE.format(o));
 
 		postTextEvent();
 	}
 	
 	public void printServerNotice(String s, String whom)
 	{
 		try
 		{
 			Object o[] = STRIP_NOTICE.parse(s);
 			if (o[0] != null || !o[0].equals(""))
 				s = o[0].toString();
 		}
 		catch (ParseException ex) {}
 					
 		Object [] obj = { getDate(), whom, s };
 		text_canvas.append(SERV_NOTICE.format(obj));
 
 		postTextEvent();
 	}
 
 	public void setDateFormat(int i)
 	{
 		if (i < 1)
 		{
 			dateformat = null;
 			return;
 		}
 		
 		switch (i)
 		{
 		default:
 		case 1:
 			dateformat = DateFormat.getTimeInstance(DateFormat.SHORT);
 			break;
 		case 2:
 			dateformat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
 			break;
 		case 3:
 			dateformat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
 		}
 	}
 		
 	public String getDate()
 	{
 		String date = "";
 		if (dateformat != null)
 		{
 			Object o[] = { dateformat.format(new Date()) };
 			date = DATE.format(o);
 		}
 		
 		return(date);
 	}
 	
  
 	/* Low level methods.
 	 */
 	 
 	public void output(String s)
 	{
 		text_canvas.append(s);
 
 		postTextEvent();
 	}
 
 	public void setFont(Font f)
 	{
 		super.setFont(f);
 		text_canvas.setFont(f);
 	}
 
 	public void handleHyperlink(String link)
 	{
 		if (Channel.isChannel(link))
 		{
 			joinChannel(link);
  		}
  		else
  		{
 			try
 			{
 				visitURL(new URL(link));
 			}
 			catch (MalformedURLException e)
 			{
 				printError(res.getString("eirc.not_an_url"));
 			}
 		}
 	}
 	
 	public void handleNick(String nick)
 	{
 		talkTo(nick);
 	}
 	
 	public void addText(String s)
 	{
 		copyText(s);
 	}
 	
 	/* Send nickname string to channel messages textfield. */
 	protected abstract void talkTo(String nick);
 	
 	/* Makes the client join the specified Channel. */
 	protected abstract void joinChannel(String name);
 
 	/* Makes the browser visit the specified URL. */
 	protected abstract void visitURL(URL url);
 	
 	/* Open Cute-paste window */
 	protected abstract void copyText(String s);
 }