
//
// Core Program for TETDM 
// PopUpWindow.java Version 0.44
// Copyright(C) 2012-2014 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//

package source;

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

public class PopUpForSetModule extends JFrame
{
	Container pane = getContentPane();
	Control control;
		
	SetModulePanel setModulePanel;
	
	int miningIDs[];
	int visualizationIDs[];

    int lastMiningIDs[];
    int lastVisualizationIDs[];
	int lastPanelNumber;
	
	PopUpForSetModule(SettingData sp1, int X, int Y, int width, int height, Control control, int option)
	{		
		super("Set Panels");
		pane.setLayout(new BorderLayout());
		
		setBounds(X, Y, width, height);
//		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		setAlwaysOnTop(true);
		pane.setBackground(Color.white);
//		setUndecorated(true);//TEST

//        pane.add(new SetToolPanel(control));/*FOR TEST*/
		setModulePanel = new SetModulePanel(control, option);
        pane.add(setModulePanel);/*FOR TEST*/
		
		setVisible(true);
        this.control = control;
		this.addWindowListener(new MyWindowListener());
		
		createBackupIDs();
	}
	
	public void setMenu()
	{
		setModulePanel.setMenu();
	}
	
	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e)// x button
		{
			control.writeActionLog("CLOSE-MODULE-SELECT-WINDOW");
			control.btx.setSelected(false);			
		}		
		
		public void windowClosed(WindowEvent e)
		{
			if(checkDifference() == true)
				return;
			
			control.savePanelInformation();
		}
	}
	
	
	boolean checkDifference()
	{
		getPanelInformation();
		
		if(miningIDs.length != lastPanelNumber)
			return false;
		
		for(int i=0;i<lastPanelNumber;i++)
		{
			if(lastMiningIDs[i] != miningIDs[i])
				return false;
			if(lastVisualizationIDs[i] != visualizationIDs[i])
				return false;
		}
		return true;
	}
	
	
	void getPanelInformation()
	{
		miningIDs = new int[control.panelNumber];
		visualizationIDs = new int[control.panelNumber];	
		
		for(int i=0;i<control.panelNumber;i++)
		{
			miningIDs[i] = control.panel[i].miningIDs[control.panel[i].selected_module];
			visualizationIDs[i] = control.panel[i].visualizationIDs[control.panel[i].pair_visu[control.panel[i].selected_module]];			
		}
	}
    
    public void createBackupIDs()
    {
        getPanelInformation();
        
		lastPanelNumber = miningIDs.length;
		
        lastMiningIDs = new int[miningIDs.length];
        lastVisualizationIDs = new int[visualizationIDs.length];		
        
        for(int i=0;i<miningIDs.length;i++)
            lastMiningIDs[i] = miningIDs[i];
        
        for(int i=0;i<visualizationIDs.length;i++)
            lastVisualizationIDs[i] = visualizationIDs[i];
    }	
}
