/*
 * ECatalog is a database front-end, with two main features:
 * 1. Use of preferences
 *  A preference-based approach, where the user is allowed to define the importance of each criterion.
 *  Then the items are ranked accordingly to his criteria.
 * 2. Trade-off analysis
 *  A cooperative database approach, where the system "argues" with the user about his criteria.
 *  When there are no matching items, the system explains the minimal conflicting set and
 *  give some possible strong and weak relaxations about his criteria.
 * This package also containts the software and the set-up details used for our User Study,
 * comparing the use or not of the two previous features mentioned above.
 *
 * Copyright (C) 2006 David Portabella Clotet, Artificial Intelligence Laboratory, EPFL
 * 
 * This file is part of ecatalog-1.0.zip
 * 
 * ECatalog is free software and a free user study set-up;
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ECatalog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECatalog; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * @version 1.0
 * @author David Portabella
 * To contact the author:
 * email: david@portabella.name and david.portabella@epfl.ch
 * 
 * More information about ECatalog:
 *  http://sourceforge.net/projects/ecatalog/
 *  http://icwww.epfl.ch/~portabel/ecatalogs/
 */

package ecatalog.gui.visualization;

import ecatalog.ECatalog;
import ecatalog.db.Database;
import ecatalog.db.Attribute;
import ecatalog.db.Item;

import utils.ButtonEditor;
import utils.IconButton;
import utils.itemTable.*;
import javax.swing.table.TableCellRenderer;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

import java.util.LinkedList;


/* This class add the "ItemAction Button" to an existing ItemTableRenderer class */
abstract public class ItemActionProxyItemTableRenderer extends ECatalogItemTableRenderer {
    ECatalogItemTableRenderer proxy;
    ItemActionButtonItemTableCellRenderer itemActionButtonItemTableCellRenderer;
    ItemActionButtonItemTableCellEditor itemActionButtonItemTableCellEditor;
    int itemActionButtonWidth;
    VisualizationGui.Window window;
    ItemTableModel model;

    LinkedList<ItemActionProxyItemTableRendererListener>dl = new LinkedList<ItemActionProxyItemTableRendererListener>();


    abstract public JButton createNewItemActionButton();

    /* This method must be called before calling to init() */
    /*
    public void setProxy() {
	this.proxy = proxy;
    }
    */

    public ItemActionProxyItemTableRenderer(ECatalogItemTableRenderer proxy, Database db, ItemTableModel model, VisualizationGui.Window window) { 
	itemActionButtonItemTableCellRenderer = new ItemActionButtonItemTableCellRenderer();
	itemActionButtonItemTableCellEditor = new ItemActionButtonItemTableCellEditor();
	itemActionButtonWidth = computeItemActionButtonWidth();

	this.proxy = proxy;
	this.model = model;
	this.window = window;
	proxy.init(db);
    }

	
    public void addItemActionProxyItemTableRendererListener(ItemActionProxyItemTableRendererListener l) { dl.add(l); }
    public void removeItemActionProxyItemTableRendererListener(ItemActionProxyItemTableRendererListener l) { dl.remove(l); }

    /* setProxy must be called before calling this method!! */
    /*
    public void init(Database db, ItemTableModel model) {
	this.model = (VisualizationGui.MyItemTableModel) model;
	proxy.init(db);
    }
    */

    public void init(Database db) {
	throw new Error("This function should be called. Use the constructor or init(Database db, ItemTableModel model) instead.");
    }

    public boolean isCellEditable(int itemIndex, int rowItem, int tableCol) {
	if (tableCol == 0)
	    return true;
	return proxy.isCellEditable(itemIndex, rowItem, tableCol - 1);
    }

    /* Get the number of columns */
    public int getNbrColumns() {
	return 1 + proxy.getNbrColumns();
    }

    /* Get the number of rows for each item */
    public int getNbrItemRows() {
	return proxy.getNbrItemRows();
    }

    /* Get the column name
     * @param itemRow  the row index (not the table row, but the item row)
     * @param tableCol  the column index
     */
    public String getColumnName(int itemRow, int tableCol) {
	if (tableCol == 0)
	    return "";
	return proxy.getColumnName(itemRow, tableCol - 1);
    }


    /* Get the horizontal and vertical span
     * @param itemRow  the row index (not the table row, but the item row)
     * @param tableCol  the column index
     */
    public CellSpan getCellSpan(int itemRow, int tableCol) {
	if (tableCol == 0)
	    return new CellSpan(1, proxy.getNbrItemRows());
	return proxy.getCellSpan(itemRow, tableCol - 1);
    }


    /* Get the cell size
     * @param itemRow   the row index (not the table row, but the item row)
     * @param tableCol  the column index
     * @result          the size for the specified cell.
     cellSize.width == -1 means no width preference (use default)
     cellSize.height == -1 means no height preference (use default)
     cellSize == null means no width nor height preference (use default)
    */
    public CellSize getCellSize(int itemRow, int tableCol) {
	if (tableCol == 0)
	    return new CellSize(itemActionButtonWidth, -1);
	return proxy.getCellSize(itemRow, tableCol-1);
    }


    /* Get the cell which will be rendered, given itemRow and tableCol
     * @param itemRow  the row index (not the table row, but the item row)
     * @param tableCol  the column index
     * @retrun the cell to be used. (change [int,int] by a class width itemRow and tableCol attributes)
     */
    public CellIndex getVisibleCell(int itemRow, int tableCol) {
	if (tableCol == 0)
	    return new CellIndex(0,0);
	CellIndex cellIndex = proxy.getVisibleCell(itemRow, tableCol-1);
	cellIndex.tableColumn++;
	return cellIndex;
    }

    /* the class implementing this interface should know to extract the information from the Item 
     * @param item      the item from where to extract the requested information
     * @param itemRow   the row index (not the table row, but the item row)
     * @param tableCol  the column index
     */
    public Object getValueAt(Object item, int itemRow, int tableCol) {
	if (tableCol == 0)
	    return "itemAction";
	return proxy.getValueAt(item, itemRow, tableCol-1);
    }

    /* gets the renderer for the specified cell 
     * @param itemRow  the row index (not the table row, but the item row)
     * @param tableCol  the column index
     * @result a renderer for this cell, or null if non specified (in this case, the default one should be used)
     */
    public ItemTableCellRenderer getItemTableCellRenderer(int itemIndex, int itemRow, int tableCol) {
	if (tableCol == 0)
	    return itemActionButtonItemTableCellRenderer;
	return proxy.getItemTableCellRenderer(itemIndex, itemRow, tableCol-1);
    }

    /* gets the renderer for the specified cell 
     * @param itemRow  the row index (not the table row, but the item row)
     * @param tableCol  the column index
     * @result a renderer for this cell, or null if non specified (in this case, the default one should be used)
     */
    public ItemTableCellEditor getItemTableCellEditor(int itemIndex, int itemRow, int tableCol) {
	if (tableCol == 0)
	    return itemActionButtonItemTableCellEditor;
	return proxy.getItemTableCellEditor(itemIndex, itemRow, tableCol-1);
    }

    /* Return the attribute index associated to the cell specified by itemRow,tableCol.
     * @param itemRow  the row index (not the table row, but the item row)
     * @param tableCol  the column index

     * @ result         the attribute index associated to the cell specified by itemRow,tableCol.
     *                  If that cell is not associated to any attribute, return -1
     */
    public int getItemAttributeIndex(int itemRow, int tableCol) {
	if (tableCol == 0)
	    return -1;
	return proxy.getItemAttributeIndex(itemRow, tableCol-1);
    }

    /* returns whether the attribute is satisfied by the constraints given by the user 
     * ignoring the database
     */
    public boolean isCellSatisfied(Item item, int itemRow, int tableCol) {
	if (tableCol == 0)
	    return true;
	return proxy.isCellSatisfied(item, itemRow, tableCol-1);
    }

    public int computeItemActionButtonWidth() {
	return createNewItemActionButton().getPreferredSize().width + 3;
    }

    class ItemActionButtonItemTableCellRenderer implements ItemTableCellRenderer {
	JButton button = createNewItemActionButton();
	public Component getItemTableCellRendererComponent(ItemTable table, Object value, boolean isSelected, boolean hasFocus, 
							   int itemIndex, int itemRow, int tableCol) {
	    return button;
	}
    }

    class ItemActionButtonItemTableCellEditor extends ButtonEditor implements ItemTableCellEditor {
	int lastItemIndex = -1;

	ItemActionButtonItemTableCellEditor() {
	    super(createNewItemActionButton());
	    getButton().addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { itemActionSelected(); } });
	}

	public Component getItemTableCellEditorComponent(ItemTable table, Object value, boolean isSelected, int itemIndex, int itemRow, int tableCol) {
	    lastItemIndex = itemIndex;
	    return super.getTableCellEditorComponent(table, value, isSelected, 0, 0);
	}

	public void itemActionSelected() {
	    Item item = (Item) model.getItem(lastItemIndex);
	    SelectedItem selectedItem = new SelectedItem(item, window, lastItemIndex);

	    for (ItemActionProxyItemTableRendererListener l : dl)
		l.itemActionSelected(selectedItem);
	}
    }
    /*
    class ItemActionButtonItemTableCellEditor extends AbstractCellEditor implements ItemTableCellEditor {
	int lastItemIndex = -1;
	JButton button;

	ItemActionButtonItemTableCellEditor() {
	    button = new ItemActionButton();
	    button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { fireEditingStopped(); itemAction(); } });
	}

	public Object getCellEditorValue() {
	    return null;
	}

	public Component getItemTableCellEditorComponent(ItemTable table, Object value, boolean isSelected, int itemIndex, int itemRow, int tableCol) {
	    lastItemIndex = itemIndex;
	    return button;
	}

	public void itemAction() {
	    for (ItemActionProxyItemTableRendererListener l : dl)
		l.itemAction(lastItemIndex);
	}
    }
    */
}


