package gui;

import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.net.URL;
import java.text.NumberFormat;

import evolution.World;
import evolution.InfoList;
import evolution.SystemMessage;
import evolution.events.*;

public class InfoTable extends JTable {

    public InfoTable(InfoList infoList, String iconPath) {
        this.iconPath = iconPath;
        if(nf == null) initNumberFormat();
        createComponents(infoList);
        init();
    }

    protected void initNumberFormat() {
        nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(2);
    }

    protected void createComponents(InfoList infoList) {
        components = new ArrayList();
        while (infoList.next()) {
	    /* doesn't seem to work... 
            URL url = ClassLoader.getSystemResource(iconPath + infoList.getName() + ".gif");
            if (url == null)
                components.add("");
            else components.add(new ImageIcon(url));
	    */
        components.add(infoList.getName());
        switch (infoList.getType()) {
                case InfoList.STRING:
                    components.add(infoList.getString());
                    break;
                case InfoList.PERCENTAGE:
                    components.add(nf.format(infoList.getPercentage()) + "%");
                    //components.add(new InfoProgressBar((int)infoList.getPercentage()));
                    break;
                case InfoList.VALUE:
                    components.add(nf.format(infoList.getValue()));
                    //components.add(new InfoProgressBar((int)infoList.getPercentage()));
                    break;
                case InfoList.BOOLEAN:
                    components.add(new Boolean(infoList.getBoolean()));
                    //components.add(new InfoCheckBox(infoList.getBoolean()));
                    break;
            }
        }
    }

    protected void init() {
        int numRows = components.size() / NUMCOLS;
        setModel(new InfoTableModel(components, numRows, NUMCOLS));
        TableColumnModel tcm = getColumnModel();
        tcm.getColumn(0).setPreferredWidth(115);
        tcm.getColumn(0).setMaxWidth(115);
        tcm.getColumn(0).setMinWidth(115);
        setTableHeader(null);
        setRowSelectionAllowed(false);
        setColumnSelectionAllowed(false);
    }

    private static final int NUMCOLS = 2; // 3
    private static String iconPath;
    private List components;
    private static NumberFormat nf;
}
