/**
*
* Copyright © 2011-2012, Share It Smart, Inc. All Rights Reserved.
*
* @ author Michael L. Baker
* @ author Chandramouli Panigrahi   
* @ author Appala Naidu Pilla
* @ author Swathi Gangula
*
*
*/
package com.shareitsmart.template;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

/**
 * 
 * This class is used to draw a multilineLabel in TemplateCreation.
 *
 */
public class MultiLineLabel extends JLabel
{
    /**
	 * Global variable declaration in MultiLineLabel
	 */
	private static final long serialVersionUID = 1L;
	protected int fontAttributes = Font.PLAIN;
    protected float alignment;
    protected Color col = Color.BLACK;
    protected String style = "ARIAL";
    protected int size = 18;
    protected int spacing = 0;   
	public Font resultfont=new Font("ARIAL",Font.PLAIN,18);	
	
    public String getStrline() {
		return strline;
	}

	public void setStrline(String strline) {
		this.strline = strline;
	}
	public String strline;
   

	/**
     ** Constructor - make a multiline label
     **/
    public MultiLineLabel(String text, float alignment)
    {
        this.alignment = alignment;
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setAlignmentX(CENTER_ALIGNMENT);
        if(text != null)
            setText(text);
    }

    /**
     ** Constructor, defaults to centered text
     **/
    public MultiLineLabel(String text)
    {
        this(text, CENTER_ALIGNMENT);
    }

    /**
     * Constructor, empty with the given alignment
     */
    public MultiLineLabel(float alignment)
    {
        this(null, alignment);
    }

    /**
     * Constructor, empty with the given alignment and line spacing
     */
    public MultiLineLabel(float alignment, int spacing)
    {
        this(null, alignment);
        this.spacing = spacing;
    }

    /**
     ** Constructor - make an empty multiline label
     **/
    public MultiLineLabel()
    {
        this(null, CENTER_ALIGNMENT);
    }
  
    public Font getResultfont() {
		return resultfont;
	}

	public void setResultfont(Font resultfont) {
		this.resultfont = resultfont;
	}
	public Color getCol() {
			return col;
		}

	public void setCol(Color col) {
			this.col = col;
		}
    
    
    public void setText(String text)
    {
        // clear the existing lines from the panel
        removeAll();
        addText(text);
        setStrline(text);
    }
  
    public void addText(String text)
    {
        addText(text,size);
    }
    
    public void addText(String text, int size)
    {
        if(spacing > 0)
            add(Box.createVerticalStrut(spacing));

        String strs[] = splitLines(text);        
        JLabel l;
        for (int i = 0; strs != null && i < strs.length; i++) {
            l = new JLabel(strs[i]);
            setStrline(strs[i]);
            l.setAlignmentX(alignment);

            if (col != null){
                l.setForeground(col);             
            }
             if(style!=null)
            	 l.setFont(new Font( style, Font.PLAIN,size));
            add(l);
        }   
    }
  
    //  Splits "string" by "Delimiter"
        
    public static String[] split(String str, String delimiter)
    {
        List<String> strings = new ArrayList<String>();
        int start = 0;
        int len = str.length();
        int dlen = delimiter.length();
        int offset = str.lastIndexOf(delimiter); // First of all, find the
        // Last occurance of the Delimiter
        // Stop empty delimiters
        if (dlen < 1)
            return null;
        else if (offset < 0) // one element
        {
            String[] result = {str};
            return result;
        }

        //
        // Append the delimiter onto the end if it doesn't already exit
        //
        if (len > offset + dlen) {
            str += delimiter;
            len += dlen;
        }

        do {
            // Get the new Offset
            offset = str.indexOf(delimiter, start);
            strings.add(str.substring(start, offset));

            // Get the new Start position
            start = offset + dlen;
        } while ((start < len) && (offset != -1));

        // Convert the list into an Array of Strings       
        String result[] = new String[strings.size()];
        strings.toArray(result);      
        return result;
    }

    /**
     * Splits "string" into lines (stripping end-of-line characters)
     * 
     * @param str - the string to be split
     * @returns an array of Strings
     */
    public static String[] splitLines(String str)
    {
        return (str == null ? null : split(str, "\n"));
    }

  
    public void setForeground(Color col)
    {
        this.col = col;    
        Component[] components = this.getComponents();
        for (int i = 0; i < components.length; i++) {
       Component component = components[i];
       component.setForeground(col);      
       setCol(component.getForeground());
    }        
        
    }
   public Color getForeground(){	  
	   return getCol();
   }
    
    public void setFont(String style,int font,int fontSize)
    {
    	this.style = style;  
    	this.fontAttributes=font;
    	this.size=fontSize;
        Component[] components = this.getComponents();
        for (int i = 0; i < components.length; i++) {
        	Component component = components[i];
        	component.setFont(new Font(style,fontAttributes,size));      
        	setResultfont( component.getFont());
        }        
          
    }
    
    public Font getFont(){
    	return  getResultfont(); 	
    }
    
	}