/*
 * Created on 24.02.2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package de.farafin.snEADy.control.GaphixStuff;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.VolatileImage;


/**
 * @author larsonmars
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GrTextArea extends Canvas implements I_GraphixConstants{
	private VolatileImage[] border=new VolatileImage[4];

	protected int w,h;
	protected int borderW;
	
	protected int showW,showH;
	
	protected int lines=0;
	protected int showedLinesStart=0;
	
	protected StringBuffer text;
	protected StringBuffer drawText;
	protected boolean lim;
	
	protected VolatileImage img_text;
	protected int drawMode;
	protected int alligment;
	
	
	public GrTextArea(int x, int y, int width,int height,char allig){
		this(x,y,width,height,null,allig,false);
	}	
	public GrTextArea(int x, int y, int width,int height,String initText,int allig,boolean limited){
		super();
		lim=limited;
		if (initText!=null){
			text=new StringBuffer(width/8*height/14);
			text.append(initText);
		}
		drawText=new StringBuffer(width/8*height/14);
		

		lines=height/14;
		alligment=allig;
		
		borderW=C_GraphixData.menuImagesO[M_TEXTAREA].getHeight();
		w=width+2*borderW;
		h=height+2*borderW;
		showW=w-2*borderW;
		showH=h-2*borderW;

		setBounds(x-borderW,y-borderW,w,h);

		
		border[0]=C_GraphixTools.createBackBufferVI(w,borderW);
		border[1]=C_GraphixTools.createBackBufferVI(borderW,showH);
		border[2]=C_GraphixTools.createBackBufferVI(w,borderW);
		border[3]=C_GraphixTools.createBackBufferVI(borderW,showH);
		
		renderBorders(new boolean[]{true,true,true,true});
		img_text=C_GraphixTools.createBackBufferVI(showW,showH);
		renderText();

	}

	public void action(){}
	
	private void renderBorders(boolean force[]){
		Graphics g;
		
		int i;
		int anzX=w/borderW-2;
		int anzY=h/borderW-2;

		
		do{
			if (force[0])
	    		do{
		    		if (border[0].validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE)
		    			border[0]=C_GraphixTools.createBackBufferVI(w,borderW);
		    		g=border[0].getGraphics();
		    		g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],0,0,borderW,borderW,0,0,borderW,borderW,null);
		    		g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],w-borderW,0,w,borderW,2*borderW,0,3*borderW,borderW,null);
		    		for (i=1;i<=anzX;i++)
		    			g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],i*borderW,0,borderW+(i+1)*borderW,borderW,borderW,0,2*borderW,borderW,null);
		    		g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],w-borderW,0,w,borderW,2*borderW,0,3*borderW,borderW,null);
	    		}while (border[0].contentsLost());
			if (force[2])
	    		do{
		    		if (border[2].validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE)
		        		border[2]=C_GraphixTools.createBackBufferVI(w,borderW);
	    			g=border[2].getGraphics();
	        		g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],0,0,borderW,borderW,6*borderW,0,7*borderW,borderW,null);
	        		
	        		for (i=1;i<=anzX;i++)
	        			g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],i*borderW,0,borderW+(i+1)*borderW,borderW,5*borderW,0,6*borderW,borderW,null);
	        		
	        		g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],w-borderW,0,w,borderW,4*borderW,0,5*borderW,borderW,null);
	    		}while (border[2].contentsLost());
			if (force[1])
	    		do{
		    		if (border[1].validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE)
		    			border[1]=C_GraphixTools.createBackBufferVI(borderW,showH);
	    			g=border[1].getGraphics();
	        		for (i=0;i<=anzY;i++)
	        			g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],0,i*borderW,borderW,(i+1)*borderW,3*borderW,0,4*borderW,borderW,null);
	    		}while (border[1].contentsLost());
			if (force[3])
	    		do{
		    		if (border[3].validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE)
		    			border[3]=C_GraphixTools.createBackBufferVI(borderW,showH);
	    			g=border[3].getGraphics();
	        		for (i=0;i<=anzY;i++)
	        			g.drawImage(C_GraphixData.menuImagesO[M_TEXTAREA],0,i*borderW,borderW,(i+1)*borderW,7*borderW,0,8*borderW,borderW,null);
	    		}while (border[3].contentsLost());
    		
    		force[0]=border[0].contentsLost();
    		force[1]=border[1].contentsLost();
    		force[2]=border[2].contentsLost();
    		force[3]=border[3].contentsLost();
		}while(force[0] || force[1] || force[2] || force[3]); 
		
	}
	
	public void setText(String newText,int mode,boolean grUpd){
		drawMode=mode;
		text.delete(0,text.length());
		text.append(newText);
		renderText();
		if (grUpd)repaint();
	}
	private void genDrawText(){
		int l=text.length();
		int i,x=-1,y=0;
		int maxC=showW/8;
		int lastSpace=0;
		char actC;
		drawText.delete(0,drawText.length());
		for (i=0;i<l;i++){
			if (y<lines){
				actC=text.charAt(i);
				x++;
				if (actC=='\n'){
					y++;
					x=0;
					drawText.append(actC);
				}
				else if (x>maxC){
					if (i-lastSpace<=maxC){
						drawText.setCharAt(lastSpace,'\n');
						x=i-lastSpace;
						drawText.append(actC);
					}
					else{
						drawText.append('\n');
						if (actC!=' ')drawText.append(actC);
						x=0;
					}
					y++;
					if (y>=lines && i<l){
						drawText.setCharAt(lastSpace,' ');
						drawText.replace(i-3,i,"...");
					}
					
				}
				else{
					if (actC==' ')lastSpace=i;
					drawText.append(actC);
				}
			}			
		}
	}
	private void renderText(){
		Graphics g;
		if (text==null)return;
		
		genDrawText();
		do{
    		if (img_text.validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE){
    			img_text=C_GraphixTools.createBackBufferVI(showW,showH);
    		}
    		g=img_text.getGraphics();
    		if (drawMode==F_MARKED)
    			g.setColor(COL_BLUE2);
    		else
    			g.setColor(COL_BLUE1);
    		g.fillRect(0,0,showW,showH);
    		if (alligment==ALLIG_LEFT)
    			C_GraphixTools.drawBitmapString(drawText.toString(),0,0,M_FONTSERP8X14,g,'l',drawMode);
    		else
    			C_GraphixTools.drawBitmapString(drawText.toString(),showW,0,M_FONTSERP8X14,g,'r',drawMode);
		}while(img_text.contentsLost());
	
	}
	
	public void paint(Graphics g){
		boolean force[]=new boolean[4];
		g.setColor(COL_BLUE1);
		force[0]=(border[0].validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK);
		force[1]=(border[1].validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK);
		force[2]=(border[2].validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK);
		force[3]=(border[3].validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK);
		renderBorders(force);
		g.drawImage(border[0],0,0,null);
		g.drawImage(border[1],w-borderW,borderW,null);
		g.drawImage(border[2],0,h-borderW,null);
		g.drawImage(border[3],0,borderW,null);
		
		if (text!=null){
			if (img_text.validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)
				renderText();
			g.drawImage(img_text,borderW,borderW,null);
		}
		
	}
	public void repaint(){
		paint(getGraphics());
	}
	public void updateDSelect(int newDSel){
		showedLinesStart=newDSel;
		repaint();
	}
	
}

