/*
 * 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 utils;

/* Example from http://www.java2s.com/ExampleCode/Swing-Components/TabbableCurrencyTable.htm */

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.EventObject;
import java.util.Vector;

import javax.swing.CellEditor;
import javax.swing.FocusManager;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;


public class ButtonEditor extends BasicCellEditor implements ActionListener,
							     TableCellEditor {
    JButton button;
    public ButtonEditor(JButton button) {
	super(button);
	this.button = button;
	button.addActionListener(this);
    }

    public JButton getButton() { return button; }

    public void setForeground(Color foreground) {
	this.foreground = foreground;
	editor.setForeground(foreground);
    }

    public void setBackground(Color background) {
	this.background = background;
	editor.setBackground(background);
    }

    public void setFont(Font font) {
	this.font = font;
	editor.setFont(font);
    }

    public Object getCellEditorValue() {
	return null;
    }

    public void editingStarted(EventObject event) {
	// Edit starting - click the button if necessary
	if (!(event instanceof MouseEvent)) {
	    // Keyboard event - click the button
	    SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
			((JButton) editor).doClick();
		    }
		});
	}
    }

    public Component getTableCellEditorComponent(JTable tbl, Object value,
						 boolean isSelected, int row, int column) {
	editor.setForeground(foreground != null ? foreground : tbl
			     .getForeground());
	editor.setBackground(background != null ? background : tbl
			     .getBackground());
	editor.setFont(font != null ? font : tbl.getFont());

	return editor;
    }

    protected void setValue(Object value) {
    }

    public void actionPerformed(ActionEvent evt) {
	// Button pressed - stop the edit
	stopCellEditing();
    }

    protected Color foreground;

    protected Color background;

    protected Font font;
}

class BasicCellEditor implements CellEditor, PropertyChangeListener {
    public BasicCellEditor() {
	this.editor = null;
    }

    public BasicCellEditor(JComponent editor) {
	this.editor = editor;
	editor.addPropertyChangeListener(this);
    }

    public Object getCellEditorValue() {
	return null;
    }

    public boolean isCellEditable(EventObject evt) {
	editingEvent = evt;
	return true;
    }

    public boolean shouldSelectCell(EventObject evt) {
	return true;
    }

    public boolean stopCellEditing() {
	fireEditingStopped();
	return true;
    }

    public void cancelCellEditing() {
	fireEditingCanceled();
    }

    public void addCellEditorListener(CellEditorListener l) {
	listeners.add(CellEditorListener.class, l);
    }

    public void removeCellEditorListener(CellEditorListener l) {
	listeners.remove(CellEditorListener.class, l);
    }

    // Returns the editing component
    public JComponent getComponent() {
	return editor;
    }

    // Sets the editing component
    public void setComponent(JComponent comp) {
	editor = comp;
    }

    // Returns the event that triggered the edit
    public EventObject getEditingEvent() {
	return editingEvent;
    }

    // Method invoked when the editor is installed in the table.
    // Overridden in derived classes to take any convenient
    // action.
    public void editingStarted(EventObject event) {
    }

    protected void fireEditingStopped() {
	Object[] l = listeners.getListenerList();
	for (int i = l.length - 2; i >= 0; i -= 2) {
	    if (l[i] == CellEditorListener.class) {
		if (changeEvent == null) {
		    changeEvent = new ChangeEvent(this);
		}
		((CellEditorListener) l[i + 1]).editingStopped(changeEvent);
	    }
	}
    }

    protected void fireEditingCanceled() {
	Object[] l = listeners.getListenerList();
	for (int i = l.length - 2; i >= 0; i -= 2) {
	    if (l[i] == CellEditorListener.class) {
		if (changeEvent == null) {
		    changeEvent = new ChangeEvent(this);
		}
		((CellEditorListener) l[i + 1]).editingCanceled(changeEvent);
	    }
	}
    }

    // Implementation of the PropertyChangeListener interface
    public void propertyChange(PropertyChangeEvent evt) {
	if (evt.getPropertyName().equals("ancestor")
	    && evt.getNewValue() != null) {
	    // Added to table - notify the editor
	    editingStarted(editingEvent);
	}
    }

    protected static JCheckBox checkBox = new JCheckBox();

    protected static ChangeEvent changeEvent;

    protected JComponent editor;

    protected EventListenerList listeners = new EventListenerList();

    protected EventObject editingEvent;
}

