/*
 * 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.criteriaSelection;

import ecatalog.ECatalog;
import ecatalog.gui.MainGui;
import utils.ImmutableComboBoxModel;
import utils.HeaderedComboBox;
import dpc.utils.DebugUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import java.util.LinkedList;
import java.util.EventObject;


class ItemComboBox {
    ItemComboBoxModel itemComboBoxModel;
    ComboBoxFilterProperties filterProperties;

    MyComboBox comboBox;
    Dimension popUpPreferredSize;
    boolean setMinimumPreferredWidth;

    public JComponent getComponent() { return comboBox; }

    void requestFocus() {
	comboBox.requestFocus();
    }

    public void init(ItemComboBoxModel itemComboBoxModel, ComboBoxFilterProperties filterProperties, boolean setMinimumPreferredWidth) {
	this.itemComboBoxModel = itemComboBoxModel;
	this.filterProperties = filterProperties;
	this.setMinimumPreferredWidth = setMinimumPreferredWidth;

	comboBox = new MyComboBox();
	popUpPreferredSize = comboBox.getPopUpPreferredSize();
	comboBox.setPopUpPreferredSize(popUpPreferredSize);
    }

    public void update() {
	ComboBoxItem comboBoxItem = itemComboBoxModel.getComboBoxItem(-1, false, true, false);
	comboBox.systemSetSelectedItem(comboBoxItem.getValue());
	comboBox.setForeground(comboBoxItem.getColor());
	if (filterProperties != null)
	    comboBox.header.update(filterProperties);  //not really necessary
    }

    public void setEnabled(boolean enabled) {
	comboBox.setEnabled(enabled);
    }

    class MyComboBox extends HeaderedComboBox {
	MyComboBoxModel model;
	MyComboBoxCellRenderer renderer;

	ComboBoxHeader header;
	boolean settingSelected = false;
	Object settingSelectedItem = null;

	MyComboBox() {
	    setFont(MainGui.font);

	    model = new MyComboBoxModel();
	    setModel(model);
	    
	    renderer = new MyComboBoxCellRenderer();
	    setRenderer(renderer);

	    setToolTipText("..."); //otherwise it does not call getToolTipText();

	    if (filterProperties != null) {
		header = new ComboBoxHeader(filterProperties);
		header.addComboBoxHeaderListener(new MyComboBoxHeaderListener());
		setHeaderComponent(header.getPanel());
	    }
	    setStepped(true);

	    addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!settingSelected) valueSelected(); }});
	    addPopupMenuListener(new PopupMenuListener() {
		public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 
		    itemComboBoxModel.popupMenuWillBecomeVisible();
		    if (filterProperties != null)
			header.update(filterProperties);
		}
		public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
		public void popupMenuCanceled(PopupMenuEvent e) {}
		});
	}

	public void valueSelected() {
	    Object aValue = getSelectedItem();

	    if (aValue == null) {
		ECatalog.error(new Error("A null value was selected?!"));
		throw new Error(); //it is already done, but to avoid the java compiling error
	    }
	    itemComboBoxModel.selectedItemChanged(aValue);
	}

	public JToolTip createToolTip() { return MainGui.createToolTip(); }

	public String getToolTipText(MouseEvent event) { 
	    ComboBoxItem comboBoxItem = itemComboBoxModel.getComboBoxItem(-1, false, false, true);
	    return comboBoxItem.getToolTipText();
	}

	class MyComboBoxHeaderListener implements ComboBoxHeaderListener {
	    public void textFilterChanged(String text) {
		itemComboBoxModel.textFilterChanged(text); 
		updatePopUp();
	    }
	    public void filterIncompatibleValuesChanged(boolean filter) { 
		itemComboBoxModel.filterIncompatibleValuesChanged(filter); 
		updatePopUp();
	    }
	    public void sortDirectionChanged(int direction) { 
		itemComboBoxModel.sortDirectionChanged(direction); 
		updatePopUp();
	    }

	    void updatePopUp() {
		model.fireContentChanged();
		scrollPopUpToBeginning();
	    }
	}

	/* Not a user change, but a system change */
        public void systemSetSelectedItem(Object item) {
            settingSelected = true;
            settingSelectedItem = item;

            setSelectedItem(item);

            settingSelected = false;
        }

	public Dimension getMinimumSize() {
	    if (!setMinimumPreferredWidth)
		return super.getMinimumSize();
	    return new Dimension(10, super.getMinimumSize().height);
	}

	public Dimension getPreferredSize() {
	    if (!setMinimumPreferredWidth)
		return super.getPreferredSize();
	    return new Dimension(10, super.getPreferredSize().height);
	}

	class MyComboBoxModel extends ImmutableComboBoxModel {
	    public int getSize() {
                if (settingSelected)
                    return 1;
		return itemComboBoxModel.getNbrComboBoxItems();
	    }
	    public Object getElementAt(int popupItemIndex) {
                if (settingSelected)
                    return settingSelectedItem;
		//System.out.println("comboboxmodel.getElementAt(comboBoxIndex = " + comboBoxIndex + ", popupItemIndex = " + popupItemIndex);

		ComboBoxItem comboBoxItem = itemComboBoxModel.getComboBoxItem(popupItemIndex, false, false, false);
		return comboBoxItem.getValue();
	    }
	}

	class MyComboBoxCellRenderer extends BasicComboBoxRenderer {
	    int currentPopupItemIndex;

	    public MyComboBoxCellRenderer() {
		setOpaque(true);
	    }

	    public Component getListCellRendererComponent(JList list, Object value, int popupItemIndex, boolean isSelected, boolean cellHasFocus) {
		ComboBoxItem comboBoxItem = itemComboBoxModel.getComboBoxItem(popupItemIndex, true, true, false);

		super.getListCellRendererComponent(list, comboBoxItem.getText(), popupItemIndex, isSelected, cellHasFocus); 
		setForeground(comboBoxItem.getColor());

		currentPopupItemIndex = popupItemIndex;
		return this;
	    }

	    public String getToolTipText(MouseEvent event) { 
		ComboBoxItem comboBoxItem = itemComboBoxModel.getComboBoxItem(currentPopupItemIndex, false, false, true);
		return comboBoxItem.getToolTipText();
	    }

	    public JToolTip createToolTip() { return MainGui.createToolTip(); }  //TODO major: it does not work! Swing ignores it!
	}
    }
}


