/*
 * Created on 25.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.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
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 GrSelCanvas extends GrTextArea{
	protected String[] list;
	private VolatileImage img_list,img_listSel;
	
	protected int selected=-1;
	
	
	
	public GrSelCanvas(int x, int y, int width,int height,String[] itemList){
		super(x,y,width,height,'l');
		list=itemList;
		//setBackground(new Color(50,60,120));
		
		img_list=C_GraphixTools.createBackBufferVI(showW,showH);
		img_listSel=C_GraphixTools.createBackBufferVI(showW,showH);
		
		lines=showH/14;
		
		drawFileList(img_list,COL_BLUE1,F_NORMAL);
		drawFileList(img_listSel,COL_BLUE2,F_MARKED);
		
		addMouseListener(new MouseListener(){
			public void mouseEntered(MouseEvent e){}
   			public void mouseClicked(MouseEvent e){}
   			public void mousePressed(MouseEvent e){
   				int sel;
   				if (list==null)return;
   				sel=(e.getY()-borderW)/14;
   				if (sel!=selected && sel<list.length){
   					selected=sel;
   					action();
   					repaint();
   				}
			}
   			public void mouseReleased(MouseEvent e){}
   			public void mouseExited(MouseEvent e){}
		});
		addMouseMotionListener(new MouseMotionListener(){
			public void mouseDragged(MouseEvent e){
   				int sel;
   				if (list==null)return;
   				sel=(e.getY()-borderW)/14;
   				if (sel!=selected && sel<list.length){
   					selected=sel;
   					repaint();
   				}				}
   			public void mouseMoved(MouseEvent e){}
		});

	}
	public void updateList(String[] newL){
		list=null;
		list=newL;
		repaint();
	}
	
	private String[] genListStrings(String[] list){
		int i,sl,l=list.length,maxL=w/8;
		String[] ret=new String[l];
		for (i=0;i<l;i++){
			sl=list[i].lastIndexOf('.');
			if (sl>maxL)
				ret[i]=list[i].substring(0,maxL-3) + "...";
			else
				ret[i]=list[i].substring(0,sl);
			
		}
		
		return ret;
	}
	private void drawFileList(VolatileImage img,Color col,int fontID){
		int i;
		Graphics g=img.getGraphics();
		String[] listStr=null;
		if (list!=null)	listStr=genListStrings(list);
		do{
			i=0;
    		if (img.validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE){
    			img=C_GraphixTools.createBackBufferVI(showW,showH);
    			g.dispose();
    			g=img.getGraphics();
    		}
    		g.setColor(col);
    		g.fillRect(0,0,showW,showH);
    		if (list!=null)
	    		while(!img.contentsLost() && i<lines && i<list.length){
	    			C_GraphixTools.drawBitmapString(listStr[showedLinesStart+i],0,i*14,M_FONTSERP8X14,g,'l',fontID);
	    			//img_sel.getGraphics().drawImage(menuImages[M_BG],0,i*14,null);
	    			i++;
	    		}
		}while(img.contentsLost());
			
	}

	private void drawList(Graphics g){
		if (img_list.validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)
			drawFileList(img_list,COL_BLUE1,F_NORMAL);
		g.drawImage(img_list,borderW,borderW,w-borderW,borderW+selected*14,0,0,showW,selected*14,null);
		if (img_listSel.validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)
			drawFileList(img_list,COL_BLUE1,F_NORMAL);
		g.drawImage(img_listSel,borderW,borderW+selected*14,w-borderW,borderW+(selected+1)*14,0,selected*14,showW,(selected+1)*14,null);
		if (img_list.validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)
			drawFileList(img_list,COL_BLUE1,F_NORMAL);
		g.drawImage(img_list,borderW,borderW+(selected+1)*14,w-borderW,h-borderW,0,(selected+1)*14,showW,showH,null);
	}
	public void updateDSelect(int newDSel){
		selected=selected+(showedLinesStart-newDSel);
		showedLinesStart=newDSel;
		drawFileList(img_list,COL_BLUE1,F_NORMAL);
		drawFileList(img_listSel,COL_BLUE2,F_MARKED);
		drawList(getGraphics());
	}
	public void paint(Graphics g){
		
		super.paint(g);
		drawList(g);
		
	}
	public void repaint(){
		paint(getGraphics());
	}

}
