package usda.weru.util;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import com.klg.jclass.table.*;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;


public class ToolTipsTable extends JCTable {

	public boolean debugFlg = false;
	
	java.util.Hashtable ht = new java.util.Hashtable();

	public ToolTipsTable()
	{
		super();
	}
	
	/**
	 * 
	 * @param tableID 
	 * @param inputFile 
	 */
	public ToolTipsTable(String tableID, File inputFile)
	{
		super();

		try {
			BufferedReader in = new BufferedReader(new FileReader(inputFile));
			String temp;
			while((temp = in.readLine())!=null) {
				StringTokenizer st = new StringTokenizer(temp,"=");
				if (st.countTokens() != 2) continue;
				String id = st.nextToken().trim();
				if (!id.startsWith(tableID)) continue;
				try {
					id = id.substring(tableID.length() + 1);
					String value = st.nextToken().trim();
					if (debugFlg) //System.out.println("U_lTT: " + id + " " + value);					
					ht.put(id, value);
				} catch (java.util.NoSuchElementException e) {
					// skip invalid lines - comments or nothing after =
				}	
			}
			in.close();
		} catch (IOException e) {
			//System.err.println("Unable to load tooltips file");
		}
	}

	private String[] labels;
	
	/**
	 * 
	 * @param arr 
	 */
	public void setLabels(String[] arr)
	{
		labels = arr.clone();
//		ToolTipManager.sharedInstance().registerComponent(this);
	}


	/**
	 * 
	 * @param event 
	 * @return 
	 */
    @Override
	public String getToolTipText(MouseEvent event) {
		JCCellPosition cp = XYToCell(event.getX(), event.getY());
		int column = cp.column;
////System.out.println("CS_gTTT: " + column);
		if (column < 0) return null;
		try {
			String rtn = (String) ht.get(labels[column]);
			return "<html>" + ((rtn == null) ? "no help" : rtn) + "</html>";
		} catch (java.lang.ArrayIndexOutOfBoundsException e) {
			return "<html>" + "out of bounds " + column + "</html>";
		}
/*		Object data = getDataSource().getTableColumnLabel(column);
		if (data == null) return null;
//			return tips[column];
//System.out.println("CS_gTTT: " + column + " " + data);
		return "test";*/
	}

/**
 * Override Swing's poor label position choice.  The
 * new behaviour is very simple: show the label at the
 * current location of the mouse.
 */
	public Point getToolTipLocation(MouseEvent event) {
		return new Point(event.getX()+10, event.getY());
	}
}

