/*
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 is free software; 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.
 * 
 * MZmine 2 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
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */

package net.sf.mzmine.modules.visualization.tic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import net.sf.mzmine.datamodel.RawDataFile;

/**
 * 
 */
class RemoveFilePopupMenu extends JMenu implements MenuListener, ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Hashtable<JMenuItem, RawDataFile> menuItemFiles;
    private TICVisualizerWindow visualizer;

    RemoveFilePopupMenu(TICVisualizerWindow visualizer) {
	super("Remove plot of file...");
	addMenuListener(this);
	this.visualizer = visualizer;
    }

    /**
     * @see javax.swing.event.MenuListener#menuSelected(javax.swing.event.MenuEvent)
     */
    public void menuSelected(MenuEvent event) {
	removeAll();
	RawDataFile[] files = visualizer.getRawDataFiles();

	// if we have only one file, we cannot remove it
	if (files.length == 1)
	    return;

	menuItemFiles = new Hashtable<JMenuItem, RawDataFile>();
	for (RawDataFile file : files) {
	    JMenuItem newItem = new JMenuItem(file.getName());
	    newItem.addActionListener(this);
	    menuItemFiles.put(newItem, file);
	    add(newItem);
	}

    }

    /**
     * @see javax.swing.event.MenuListener#menuDeselected(javax.swing.event.MenuEvent)
     */
    public void menuDeselected(MenuEvent arg0) {
	// do nothing
    }

    /**
     * @see javax.swing.event.MenuListener#menuCanceled(javax.swing.event.MenuEvent)
     */
    public void menuCanceled(MenuEvent arg0) {
	// do nothing
    }

    /**
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent event) {
	Object src = event.getSource();
	RawDataFile file = menuItemFiles.get(src);
	if (file != null)
	    visualizer.removeRawDataFile(file);

	// Disable menu?
	RawDataFile[] files = visualizer.getRawDataFiles();
	if (files.length == 1) {
	    this.setEnabled(false);
	}
    }
}

@@ -917,16 +917,42 @@
 stener;%0A
+import java.util.HashSet;%0A
 import j
@@ -1147,16 +1147,54 @@
 ataFile;
+%0Aimport net.sf.mzmine.main.MZmineCore;
 %0A%0A/**%0A *
@@ -1781,16 +1781,43 @@
 veAll();
+%0A%0A%09// get all project files
 %0A%09RawDat
@@ -1828,33 +1828,77 @@
 e%5B%5D 
-f
+openF
 iles = 
-visualizer
+MZmineCore.getProjectManager()%0A%09%09.getCurrentProject()
 .get
-Raw
 Data
@@ -1910,188 +1910,393 @@
 ();%0A
-%0A%09// if we have only one file, we cannot remove it%0A%09if (files.length == 1)%0A%09    return;%0A%0A%09menuItemFiles = new Hashtable%3CJMenuItem, RawDataFile%3E();%0A%09for (RawDataFile file : files) %7B
+%09HashSet%3CRawDataFile%3E visualizedFiles = new HashSet%3CRawDataFile%3E();%0A%09for (RawDataFile file : visualizer.getRawDataFiles())%0A%09    visualizedFiles.add(file);%0A%0A%09menuItemFiles = new Hashtable%3CJMenuItem, RawDataFile%3E();%0A%09for (RawDataFile file : openFiles) %7B%0A%0A%09    // if file is not part of plot, skip it%0A%09    if (!visualizedFiles.contains(file))%0A%09%09continue;%0A%0A%09    // add a menu item for file
 %0A%09  

Change to file order in RemoveFilePopupMenu to reflect order in legend