/*******************************************************************************
* 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 24-Nov-2004
 *
 */

package uk.ac.ic.doc.kevlar.gui.internal.taskpane;

import java.io.FileNotFoundException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ColorCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.FontCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ImageCache;

/**
 * @author Kate
 *
 */


public class Heading extends ADrawableObject{

	private String message;
	private Point size;
	private Point position;
	private Image divider;
	private final int VISIBLE_CHARS = 15; /* this is used for displaying pipeline names
										   * on the thumbnail and depends on the width of the
										   * thumbnail and the size and boldness of the font */
	private Color color;
	
	public Heading(String message, Point size, Point position)
	{
		super();
		this.message = message;
		this.size = size;
		this.position = position;
		divider = null;
		color = color = new Color(Display.getDefault(), 0, 0, 0);
		
		setPosition(position);
		setSize(size);
		
		customizeString();
	}
	
	public String getMessage()
	{
		return message;
	}
	
	public void setMessage(String newMessage)
	{
		message = newMessage;
		customizeString();
	}
	
	private void customizeString()
	{
		if (message.length()>=16)
		{
			/* insert a 'newline' into a message */
			
			if (message.indexOf(" ")!=-1)
			{
				int splitAt = 24;
				
				while (message.charAt(splitAt)!=' ')
				{
					splitAt--;
				}
			
				message = message.substring(0,splitAt)+"\n"+ message.substring(splitAt, message.length());
			}
			else
			{
				message = message.substring(0, VISIBLE_CHARS)+"...";
			}
		}
	}
		
	public void drawBefore(GC gc, Rectangle area)
	{
		
		gc.setFont(FontCache.get("Arial", 8, SWT.BOLD));
		gc.setForeground(color);
		
		if (divider!=null)
		{
			gc.drawText(message, 8-area.x, 15-area.y, true);
			gc.drawImage(divider, 8-area.x, 5-area.y);
		}
		
		else
		{
			gc.drawText(message, 8-area.x, 5-area.y, true);
		}
	}
	
	public void drawAfter(GC gc, Rectangle area)
	{
		gc.setFont(FontCache.get("Arial", 8, SWT.BOLD));
		gc.setForeground(color);
		gc.drawText(message, -area.x, 50-area.y, true);
	}
	
	public void setDivider(String imageName)
	{
		try{
			divider = ImageCache.get(imageName);
		}
		catch(FileNotFoundException e)
		{
			divider = null;
		}
	}
	
	public void setColour(Color color)
	{
		this.color = color;
	}
	
	
}
