/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on Nov 25, 2004
 */
package uk.ac.ic.doc.kevlar.gui.showers;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import uk.ac.ic.doc.kevlar.gui.internal.caches.ColorCache;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.textbox.MultilineTextBox;
import uk.ac.ic.doc.kevlar.publicInterfaces.IType;
import uk.ac.ic.doc.kevlar.publicUtil.strings.StringUtil;

/**
 * This is the most primitive of Showers, it takes an IType, toStrings() it and dumps it.
 * This is all u can safely do in the framework at the moment until more advanced classloaders and
 * plugins are sorted out.
 * @author tora
 */
public class AnyShower extends AbstractShower {

    private static final int MAX_CHAR_COUNT = 32768;
    
	//private SWTTextBox textBox;
	private MultilineTextBox textBox2;
	//private PrettyBox prettyBox;
	//VerticalScrollBar vbar;
	public AnyShower(IShowerProvider provider)
	{		
		super(provider);
		//prettyBox = new PrettyBox();
		//prettyBox.setSizeFillBoth(new Point(0, 0));
		//addChild(prettyBox);
		
		textBox2 = new MultilineTextBox();
		textBox2.getTextArea().setEditable(false);
		
		
		//textBox = new SWTTextBox(SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL);
		textBox2.setPosition(new Point(0, 0));
		textBox2.setSizeFillBoth(new Point(0, 0));
		
		//Text text = textBox.getTextBox();
		//text.setBackground(ColorCache.get(255, 255, 255));
		
		//FontData[] fontData = text.getFont().getFontData();
		//if(fontData.length > 0)
		//{
		    //text.setFont(FontCache.get(fontData[0].getName(), 12, 0));
		//}
		
		addChild(textBox2);
	}

	/*
	public synchronized void hide() {
		textBox.getTextBox().setVisible(false);
		super.hide();
	}
	
	public synchronized void show()
	{
		textBox.getTextBox().setVisible(true);
		super.show();
	}*/

	
	protected synchronized void addObjectFromGUIThread(IType o)
	{
	    if(textBox2.getText().length() > MAX_CHAR_COUNT)
	    {
	        String buff = textBox2.getText();
	        int end = buff.length() - MAX_CHAR_COUNT;
	        int count = 0;
	        for(int carrot = 0; carrot < end ; )
	        {
	            carrot = buff.indexOf(StringUtil.NEWLINE, carrot);
	            count++;
	            carrot++;
	        }
	        
	        textBox2.getTextArea().deleteLines(count);
	    }
	    
	    textBox2.getTextArea().append(o.toString());
	    
	    if(isVisible())
	    {    
	        textBox2.getTextArea().ctrlEnd();
	    }
	}
	

    
    public 	synchronized void drawBefore(GC gc, Rectangle redrawArea) {
       //gc.setBackground(ColorCache.get(255,255,255));
       //c.fillRectangle(4 - redrawArea.x, 4 - redrawArea.y, getSize().x - 8, getSize().y - 8);
    }

    public void fromGUIThreadExecutionStarted(String programName, Calendar startedTime)
    {
        textBox2.getTextArea().append(format(startedTime) + ": " + programName) ;
        textBox2.getTextArea().ctrlEnd();
    }
    
    public String format(Calendar time)
    {
        return new SimpleDateFormat("h:mm a").format(time.getTime());
    }
    
    public void show()
    {
        super.show();
        textBox2.getTextArea().ctrlEnd();
    }

}
