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

import ecatalog.ECatalog;
import ecatalog.db.*;
import ecatalog.jaxb.ecatalogConfig.ECatalogConfigType;

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


public class AnalysisGui {
    ECatalog ecatalog;
    MainGui mainGui;
    ECatalogConfigType config;
    Database db;

    JEditorPane analysisText;
    JScrollPane analysisTextScrollPane;


    public void showAnalysis(String possibleRelaxationsText, String minimalUnsolvableSubproblemText) {
	String text = possibleRelaxationsText + "<br><br><hr>\n" + minimalUnsolvableSubproblemText;
	setText(text);
    }

    public void setText(String text) {
	ECatalog.lowlevelMsg("<Analysis>" + text + "</Analysis>");
	analysisText.setText((text != null) ? text : "");

	//move to the beginning. TODO: it does not work
	analysisTextScrollPane.getViewport().setViewPosition(new Point(0, 0));
    }

    public void requestFocus() {
	analysisText.requestFocus();
    }

    public void executeRelaxation(String relaxationCode) {
	ecatalog.executeRelaxation(relaxationCode);
    }


    JPanel init(MainGui mainGui, ECatalog ecatalog) {
	this.ecatalog = ecatalog;
	this.mainGui = mainGui;
	this.config = ecatalog.getConfig();
	this.db = ecatalog.getDatabase();


	JPanel panel = new JPanel();
	GridBagLayout gbl = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints();
	panel.setLayout(gbl);

	String title = (config.isConflictAnalysisDisplayed()) ? "Trade-off analysis" : "Info";
	JLabel aLabel = new JLabel(title);
	aLabel.setFont(new Font(mainGui.fontName, Font.BOLD, 18));
	aLabel.setBackground(Color.BLUE);
	aLabel.setOpaque(true);
	aLabel.setForeground(Color.WHITE);
	c.anchor = GridBagConstraints.NORTHWEST; c.gridx=0; c.gridy=0; c.gridwidth = 1; c.weightx = 1.0D; c.weighty = 0.0D; c.fill = GridBagConstraints.HORIZONTAL;
	gbl.setConstraints(aLabel, c);
	panel.add(aLabel);

	Component box = Box.createVerticalStrut(10);
	c.anchor = GridBagConstraints.NORTHWEST; c.gridx=0; c.gridy=1; c.gridwidth = 1; c.weightx = 1.0D; c.weighty = 0.0D; c.fill = GridBagConstraints.HORIZONTAL;
	gbl.setConstraints(box, c);
	panel.add(box);


	//mainPanel.add(Box.createVerticalStrut(10));
	analysisText = new JEditorPane("text/html", "");
	analysisText.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { 
	    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
		try { executeRelaxation(e.getDescription()); }
		catch (NumberFormatException ee) { 
		    ECatalog.error(ee); 
		    throw new Error(); //it is already done, but to avoid the java compiling error
		}
	}});
	analysisText.setEditable(false);

	analysisTextScrollPane = new JScrollPane(analysisText);
	//sp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Analysis", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font(fontName, Font.BOLD, 14)));
	analysisTextScrollPane.setPreferredSize(new Dimension(800, 400));

	c.anchor = GridBagConstraints.CENTER; c.gridx=0; c.gridy=2; c.gridwidth = 1; c.weightx = 1.0D; c.weighty = 1.0D; c.fill = GridBagConstraints.BOTH;
	gbl.setConstraints(analysisTextScrollPane, c);
	panel.add(analysisTextScrollPane);
	return panel;
    }

}

