
//
// Core Program for TETDM 
// PopUpForSetModule.java Version 1.00
// Copyright(C) 2012-2018 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;
	

	String windowName = "ALLSET";	
		
	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, String windowTitle)
	{
		super(windowTitle);		
		

		if(option == 0)//LETTER
			windowName += "NAME";
		else//IMAGE
			windowName += "IMAGE";
		

		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;
        
        WINDOWACTIONLOG windowActionLog = new WINDOWACTIONLOG(this, windowName);
        this.addComponentListener(windowActionLog.componentListener);                    //FOR LOG
        this.addWindowListener(new MyWindowListener());
		
		createBackupIDs();
	}
	
	public void setMenu()
	{
		setModulePanel.setMenu();
	}
    
    public void closeInformationWindow()
    {
        if(setModulePanel != null)
            setModulePanel.closeInformationWindow();
    }
	
	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e)// x button
		{
			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];
    }	
}
