/*
 * 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.gui.MainGui;
import ecatalog.ECatalog;
import ecatalog.db.*;
import org.w3c.dom.Element;

import info.clearthought.layout.TableLayout;

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


class ConstraintGuiSelector extends ConstraintGui {
    final int selectorWidth = 70;
    ConstraintGui[] constraintGuis;
    int defaultConstraintGuiIndex;
    String label = "selector";

    int currentConstraintGuiIndex = -1;
    ConstraintGui currentConstraintGui = null;
    TableLayout layout;
    JComboBox constraintTypeComboBox;
    boolean systemChangingFields = false;

    /* configure the constraint gui */
    public void configure(Element config) {
	ECatalog.error(new Error("This constraint should not be used in the config-file"));
	throw new Error(); //it is already done, but to avoid the java compiling error
    }

    public void configure(ConstraintGui[] constraintGuis, int defaultConstraintGuiIndex) {
	this.constraintGuis = constraintGuis;
	this.defaultConstraintGuiIndex = defaultConstraintGuiIndex;
    }

    public void attach(MainGui mainGui, Database db, ECatalog ecatalog, WeightSelector weightSelector) {
	super.attach(mainGui, db, ecatalog, weightSelector);

	initGui();
	setConstraintGui(defaultConstraintGuiIndex, false, true);
    }

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

    public void reset() {
	setConstraintGui(defaultConstraintGuiIndex, false, true);
	currentConstraintGui.reset();
    }

    public String getConstraintTypeLabel() {
	return label;
    }

    public Constraint getConstraint() {
	return currentConstraintGui.getConstraint();
    }
    
    public void detach() {
	currentConstraintGui.detach();
    }

    public void requestFocus() {
	ECatalog.error(new Error("TODO"));
	throw new Error(); //it is already done, but to avoid the java compiling error
    }

    public void update() throws SQLException {
	if (currentConstraintGui != null)
	    currentConstraintGui.update();
    }

    /*
     * @param systemChange  wether the change comes from the system or from the user side. 
     */
    public void setConstraintGui(int constraintGuiIndex, boolean ecatalogUpdate, boolean systemChange) {
	if (currentConstraintGuiIndex == constraintGuiIndex)
	    return;

	systemChangingFields = true;
	constraintTypeComboBox.setSelectedIndex(constraintGuiIndex);
	systemChangingFields = false;

	ConstraintGui constraintGui = constraintGuis[constraintGuiIndex];
	constraintGui.attach(mainGui, db, ecatalog, weightSelector);
	if (currentConstraintGui != null) {
	    constraintGui.getConstraint().importDetails(currentConstraintGui.getConstraint());
	    constraintGui.getConstraint().setWeight(currentConstraintGui.getConstraint().getWeight());
	    currentConstraintGui.detach();
	    mainPanel.remove(currentConstraintGui.mainPanel);
	}

	constraintGui.mainPanel.setOpaque(false);
	mainPanel.add(constraintGui.mainPanel, "2, 0");

	currentConstraintGui = constraintGui;
	currentConstraintGuiIndex = constraintGuiIndex;
	
	if (!systemChange && ecatalog.getLog() != null)
	    ecatalog.getLog().changeConstraintType(currentConstraintGui);

	if (ecatalogUpdate)
	    ecatalog.update();
    }

    void initGui() {
	mainPanel = new JPanel();
	//layout = new TableLayout(new double[][] {{TableLayout.PREFERRED, 3, TableLayout.FILL},{TableLayout.PREFERRED}});
	layout = new TableLayout(new double[][] {{selectorWidth, 3, TableLayout.FILL},{TableLayout.PREFERRED}});
	mainPanel.setLayout(layout);


	// Create the constraint type combobox
	String[] types = new String[constraintGuis.length];
	for (int i = 0; i < types.length; i++)
	    types[i] = constraintGuis[i].getConstraintTypeLabel();
	constraintTypeComboBox = new JComboBox(types) {
		public JToolTip createToolTip() { return MainGui.createToolTip(); }
		public Dimension getPreferredSize() { return new Dimension(selectorWidth, super.getPreferredSize().height); }
	    };
	constraintTypeComboBox.setToolTipText("How do you want to filter?");

	constraintTypeComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
	    if (!systemChangingFields) setConstraintGui(constraintTypeComboBox.getSelectedIndex(), true, false); 
	}});
	constraintTypeComboBox.setFont(mainGui.font);
	mainPanel.add(constraintTypeComboBox, "0, 0, l, t");
    }
}


