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

import utils.*;

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

import java.util.LinkedList;



class ComboBoxHeader {
    LinkedList<ComboBoxHeaderListener> dl = new LinkedList<ComboBoxHeaderListener>();

    JTextField filterTextField;
    JToggleButton filterIncompatibleValuesButton, desSortButton, ascSortButton;
    JPanel panel;
    boolean systemChaningGui = false;

    JPanel getPanel() { return panel; }

    ComboBoxHeader(ComboBoxFilterProperties filterProperties) {
	panel = new JPanel();
	GridBagLayout gbl = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints();
	panel.setLayout(gbl);

	if (filterProperties.isTextFilter) {
	    filterTextField = new JTextField();
	    filterTextField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!systemChaningGui) textFilterChanged(); }});
	    filterTextField.getDocument().addDocumentListener(new DocumentListener() {
		    public void changedUpdate(DocumentEvent e) { if (!systemChaningGui) textFilterChanged(); }
		    public void removeUpdate(DocumentEvent e) { if (!systemChaningGui) textFilterChanged(); }
		    public void insertUpdate(DocumentEvent e) { if (!systemChaningGui) textFilterChanged(); }
		});

	    filterTextField.setToolTipText("filter by this text");
	    c.weightx = 1.0D; c.fill = GridBagConstraints.HORIZONTAL;
	    gbl.setConstraints(filterTextField, c);
	    panel.add(filterTextField);
	}

	if (filterProperties.isIncompatibleFilter) {
	    filterIncompatibleValuesButton = new JToggleButton("C");
	    filterIncompatibleValuesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!systemChaningGui) filterIncompatibleValuesChanged(ComboBoxHeader.this.filterIncompatibleValuesButton.isSelected()); }});
	    filterIncompatibleValuesButton.setToolTipText("show only Compatible values?");
	    filterIncompatibleValuesButton.setPreferredSize(new Dimension(20,15));
	    filterIncompatibleValuesButton.setMargin(new Insets(1, 1, 1, 1));

	    c.weightx = 0.0D; c.fill = GridBagConstraints.NONE;
	    gbl.setConstraints(filterIncompatibleValuesButton, c);
	    panel.add(filterIncompatibleValuesButton);
	}

	if (filterProperties.isAscSortButton) {
	    ascSortButton = new JToggleButton("<");
	    ascSortButton.setToolTipText("sort, first show the values with less compatible items");
	    ascSortButton.setPreferredSize(new Dimension(20,15));
	    ascSortButton.setMargin(new Insets(1, 1, 1, 1));
	    ascSortButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!systemChaningGui) sortButton(1, ComboBoxHeader.this.ascSortButton.isSelected()); }});
	    c.weightx = 0.0D; c.fill = GridBagConstraints.NONE;
	    gbl.setConstraints(ascSortButton, c);
	    panel.add(ascSortButton);
	}

	if (filterProperties.isDesSortButton) {
	    desSortButton = new JToggleButton(">");
	    desSortButton.setToolTipText("sort, first show the values with more compatible items");
	    desSortButton.setPreferredSize(new Dimension(20, 15));
	    desSortButton.setMargin(new Insets(1, 1, 1, 1));
	    desSortButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!systemChaningGui) sortButton(-1, ComboBoxHeader.this.desSortButton.isSelected()); }});
	    c.weightx = 0.0D; c.fill = GridBagConstraints.NONE;
	    gbl.setConstraints(desSortButton, c);
	    panel.add(desSortButton);
	}
    }

    /* TODO: also change the panel accordinglt to filterProperties */
    void update(ComboBoxFilterProperties filterProperties) {
	systemChaningGui = true;
	if (filterTextField != null)
	    filterTextField.setText(filterProperties.textFilter);

	if (filterIncompatibleValuesButton != null)
	    filterIncompatibleValuesButton.setSelected(filterProperties.filterIncompatibleValues);

	if (ascSortButton != null)
	    ascSortButton.setSelected((filterProperties.sortDirection == 1));

	if (desSortButton != null)
	    desSortButton.setSelected((filterProperties.sortDirection == -1));
	systemChaningGui = false;
    }

    void textFilterChanged() {
	String text = filterTextField.getText();
	for (ComboBoxHeaderListener l : dl)
	    l.textFilterChanged(text);
    }

    void filterIncompatibleValuesChanged(boolean filter) {
	for (ComboBoxHeaderListener l : dl)
	    l.filterIncompatibleValuesChanged(filter);
    }

    void sortButton(int direction, boolean selected) {
	if (selected == false)
	    direction = 0;

	systemChaningGui = true;
	if (direction == -1)
	    ascSortButton.setSelected(false);
	else
	    desSortButton.setSelected(false);
	systemChaningGui = false;

	for (ComboBoxHeaderListener l : dl)
	    l.sortDirectionChanged(direction);
    }

    /* Add a listener */
    public void addComboBoxHeaderListener(ComboBoxHeaderListener l) { dl.add(l); }

    /* Remove a listener */
    public void removeComboBoxHeaderListener(ComboBoxHeaderListener l) {dl.remove(l); }
}

