package fortiss.results.listeners.label;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import fortiss.results.Menu;
import fortiss.results.Reporter;

public class MenuListener implements TreeSelectionListener {

	private DefaultMutableTreeNode sel;
	private DefaultMutableTreeNode par;
	// private ArrayList<ArrayList<Double>> data;
	private int seriesIndex;
	private int index;

	/**
	 * Identifies the field selected by the user in the results tree and calls
	 * plotResults. Only leafs can be selected. @see MenuSelectionModel
	 */
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		Menu h = (Menu) e.getSource();
		TreePath selPath = e.getPath();
		sel = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
		try {
			par = (DefaultMutableTreeNode) e.getPath().getParentPath().getLastPathComponent();
		} catch (Exception e1) {
			System.out.println(">> There are no results to show.");
		}
		index = getBuildingIndex();

		if (h.isPathSelected(selPath)) {
			if (h.getSelectionCount() == 1) {
				Reporter.plotPanel.clearSeries();
			}
			Reporter.plotPanel.addSeries(getSeriesName(), Reporter.output.getDataSeries(index, sel.toString()));
			Reporter.plotPanel.paintSeries();
		} else {
			Reporter.plotPanel.removeSeries(getSeriesName(), Reporter.output.getDataSeries(index, sel.toString()));
			Reporter.plotPanel.paintSeries();
		}
	}

	/**
	 * @return the index of the building to which the selected results correspond.
	 */
	private int getBuildingIndex() {
		for (int i = 0; i < Reporter.output.size(); i++) {
			if (Reporter.output.buildingNames.get(i).equals(par.toString())) {
				return i;
			}
		}
		return 0;
	}

	/**
	 * @return a name to be assigned to a data series in the results interface.
	 */
	public String getSeriesName() {
		String name = par.toString() + " - " + Reporter.output.getDataLabel(index, seriesIndex);
		return name;
	}

}

@@ -414,81 +414,8 @@
 ar;%0A
-%09// private ArrayList%3CArrayList%3CDouble%3E%3E data;%0A%09private int seriesIndex;%0A
 %09pri
@@ -1868,55 +1868,21 @@
 %22 + 
-Reporter.output.getDataLabel(index, seriesIndex
+sel.toString(
 );%0A%09

Fix incorrect label for plotted series