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

package source;

//import source.*;
import source.TextData.*;
import source.Utility.*;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.regex.*;

//Reconstruct panel from outside of select panel
//add new visualization pair for a mining

public class SetToolPanel2 extends JPanel implements ActionListener
{
	Control control;
	Select select;
	PopUpForModuleSelect parentFrame;
	
	JPanel labelPanel;
	JLabel label1, label2;
	JLabel label3, label4;
	JLabel label5, label6;	
	TopPanel topPanel;
	JPanel buttonPanel;	
	JButton bt0, bt10, bt40;
	
	MiningModule mining[];
	VisualizationModule visual[];
	String miningNames[];
	String visualizationNames[];
	ModuleData moduleData;
	SettingData settingData;
	
	int option;
	
	String inJapanese[];
	
	boolean cancel = false;

	SetToolPanel2(Control control, PopUpForModuleSelect parentFrame, Select select, String inJapanese[], int option)
	{
		this.control = control;
		this.select = select;
		this.parentFrame = parentFrame;
		this.option = option;
		
		mining = control.moduleData.mining;
		visual = control.moduleData.visual;
		miningNames = control.moduleData.module_names;
		visualizationNames = control.moduleData.visu_module_names;
		moduleData = control.moduleData;
		settingData = control.sp1;
	

		
		setLayout(new BorderLayout());		
		
		labelPanel = new JPanel(new GridLayout(3,2));
		labelPanel.setBackground(new Color(0x8b,0x47,0x26));
		add(labelPanel, BorderLayout.NORTH);
		
		this.inJapanese = inJapanese;		
		if(settingData.inJapanese)
		{
			bt0 = new JButton(inJapanese[5]);
			bt10 = new JButton(inJapanese[6]);
			bt40 = new JButton(inJapanese[7]);		
			label1 = new JLabel(inJapanese[8]);
			label2 = new JLabel(inJapanese[9]);
		}
		else
		{
			bt0 = new JButton("Reload Settings");
			bt10 = new JButton("Save Settings");
			bt40 = new JButton("Cancel");		
			label1 = new JLabel("Mining Tools");
			label2 = new JLabel("Visualization Tools");
		}

		
		label1.setFont(new Font(settingData.fontName, Font.BOLD, 18));
		label1.setForeground(Color.yellow);
		labelPanel.add(label1);
		

		label2.setFont(new Font(settingData.fontName, Font.BOLD, 18));
		label2.setForeground(Color.yellow);
		labelPanel.add(label2);
		
		label3 = new JLabel("  -> ");
		label3.setFont(new Font(settingData.fontName, Font.BOLD, 18));		
		label3.setForeground(new Color(0x7f,0xff,0xf0));
		labelPanel.add(label3);
		
		label4 = new JLabel("  -> ");
		label4.setFont(new Font(settingData.fontName, Font.BOLD, 18));		
		label4.setForeground(new Color(0x7f,0xff,0xf0));
		labelPanel.add(label4);
		
		label5 = new JLabel("");
		label5.setFont(new Font(settingData.fontName, Font.BOLD, 14));		
		label5.setForeground(new Color(0x7f,0xff,0xf0));
		labelPanel.add(label5);
		
		label6 = new JLabel("");
		label6.setFont(new Font(settingData.fontName, Font.BOLD, 14));		
		label6.setForeground(new Color(0x7f,0xff,0xf0));
		labelPanel.add(label6);		
		
		
		topPanel = new TopPanel(control, parentFrame, select, this);
		topPanel.setBackground(new Color(0x8b,0x47,0x26));
		add(topPanel);
		
		buttonPanel = new JPanel(new GridLayout());
		buttonPanel.setBackground(Color.cyan);
		add(buttonPanel, BorderLayout.SOUTH);		
		
		
		
		bt0.addActionListener(this);
//		bt0.setBackground(Color.cyan);
		buttonPanel.add(bt0);
		
		bt10.addActionListener(this);
//		bt10.setBackground(Color.cyan);
		buttonPanel.add(bt10);
        
		bt40.addActionListener(this);
//		bt40.setBackground(Color.cyan);
		buttonPanel.add(bt40);		
	}
	
	void setMenu()
	{
		if(settingData.inJapanese)
		{
			bt0.setText(inJapanese[5]);
			bt10.setText(inJapanese[6]);
			bt40.setText(inJapanese[7]);		
			label1.setText(inJapanese[8]);
			label2.setText(inJapanese[9]);
		}
		else
		{
			bt0.setText("Reload Settings");
			bt10.setText("Save Settings");
			bt40.setText("Cancel");		
			label1.setText("Mining Tools");
			label2.setText("Visualization Tools");
		}
		repaint();
	}
	

	
	void resetAllPairingVisualization()
	{
		for(int k=0;k<miningNames.length;k++)
		{
			mining[k].pairingVisualizationID = new int[mining[k].pairingVisuID.length];
			for(int i=0;i<mining[k].pairingVisuID.length;i++)
				mining[k].pairingVisualizationID[i] = mining[k].pairingVisuID[i];
			
			moduleData.setModuleIndex(k, mining[k].pairingVisuID);
			
			for(int i=0;i<visualizationNames.length;i++)		
				moduleData.createPairingMiningID(i);			
		}
	}
	/*
	void setAllPairingVisualization()
	{
		for(int k=0;k<miningNames.length;k++)
		{
			mining[k].pairingVisualizationID = new int[mining[k].pairingVisuID.length];
			for(int i=0;i<mining[k].pairingVisuID.length;i++)
				mining[k].pairingVisualizationID[i] = mining[k].pairingVisuID[i];
			
			moduleData.setModuleIndex(k, mining[k].pairingVisuID);
			
			for(int i=0;i<visualizationNames.length;i++)		
				moduleData.createPairingMiningID(i);			
		}
	}	
*/
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == bt0)//Load Original Settings
		{
			resetAllPairingVisualization();            
			settingData.readOriginalSettingFile();
			moduleData.initializeVisualiationModuleSelection();
			repaint();
		}
		
		if(e.getSource() == bt10)//Save Settings
		{
			settingData.saveSettingFile();
		}

		// Quit Settings
		if(e.getSource() == bt40)//Cancel
		{
			cancel = true;
			
			moduleData.initializeVisualiationModuleSelection();
			if(topPanel.informationWindow != null)
				topPanel.informationWindow.dispose();
			if(topPanel.information != null)
				topPanel.information.dispose();	

			control.writeActionLog("CLOSE-MODULE-SELECT-WINDOW-SINGLE panelID= "+select.getPanelID());
			parentFrame.dispose();			
		}
	}
	
	public void closeWindows()
	{
		if(topPanel.informationWindow != null)
			topPanel.informationWindow.dispose();
		if(topPanel.information != null)
			topPanel.information.dispose();		
	}
}	



class TopPanel extends JPanel implements MouseMotionListener, MouseListener
{
	Control control;
	PopUpForModuleSelect parentFrame;
	Select select;
	
	SetToolPanel2 parentPanel;
	
	MiningModule mining[];
	VisualizationModule visual[];
	String miningNames[];
	String visualizationNames[];
	int miningToolType[];
	int visualizationToolType[];	
	ModuleData moduleData;
	SettingData settingData;
	
	Image miningModuleImage[];
	Image visualizationModuleImage[];	
	
	String typeWord[] = new String[]{"","(Si)","(Pr)","(sP)",""};
	

	PopUpForModuleInformation informationWindow;
	PopInformation information;    
    boolean displayInformation = false;
	
	int lastTouchMining = -1, lastTouchVisualization = -1;
	int setMiningIndex = -1, setVisualizationIndex = -1;
	
	int sortedVIndex[];
	int sortedMIndex[];	
	int sortedMiningReverseIndex[];
	int sortedVisualizationReverseIndex[];
	
	int fontSize = 20;
	
	int sizeX, sizeY;	
	int mouseX, mouseY;	
	
	int miningItemNumber, visualizationItemNumber;
	int miningLengthMax, visualizationLengthMax;
	
	int miningX, miningY;
	int miningHeight, miningWidth;
	int xNumberMining, yNumberMining;
	int miningWidthLimit, visualizationWidthLimit;
	
	int visualizationX, visualizationY;	
	int visualizationWidth, visualizationHeight;	
	int xNumberVisualization, yNumberVisualization;

	int touchNumberMining = -1, touchNumberVisualization = -1;
	
	boolean panelsReConstruction = false;
	
	String panelInformation;
	
	int option;
	
    TopPanel(Control control, PopUpForModuleSelect parentFrame, Select select, SetToolPanel2 parentPanel)
    {
		this.control = control;
		this.parentFrame = parentFrame;
		this.select = select;
		this.parentPanel = parentPanel;
		option = parentPanel.option;
		
		mining = control.moduleData.mining;
		visual = control.moduleData.visual;
		miningNames = control.moduleData.module_names;
		visualizationNames = control.moduleData.visu_module_names;
		moduleData = control.moduleData;
		settingData = control.sp1;		
		
		miningToolType = new int[miningNames.length];
		for(int i=0;i<miningNames.length;i++)
			miningToolType[i] = control.moduleData.mining[i].getToolType();
		
		visualizationToolType = new int[visualizationNames.length];
		for(int i=0;i<visualizationNames.length;i++)
			visualizationToolType[i] = control.moduleData.visual[i].getToolType();
			
		miningModuleImage = control.moduleData.miningModuleImage;
		visualizationModuleImage = control.moduleData.visualizationModuleImage;
		
		sortedMIndex = control.moduleData.miningDisplayOrderIndex;
		sortedVIndex = control.moduleData.visualizationDisplayOrderIndex;
		sortedMiningReverseIndex = control.moduleData.miningDisplayOrderReverseIndex;
		sortedVisualizationReverseIndex = control.moduleData.visualizationDisplayOrderReverseIndex;		
		
		
		
		addMouseMotionListener(this);
		addMouseListener(this);
		
		miningItemNumber = miningNames.length;
		visualizationItemNumber = visualizationNames.length;
		
		if(option == 0) //character base
		{
			 miningLengthMax = miningNames[0].length();		//These lengths are based on ENGLISH Name
			 for(int i=1;i<miningItemNumber;i++)
			 if(miningNames[i].length() > miningLengthMax)
			 miningLengthMax = miningNames[i].length();
			 miningLengthMax += 4;


			 visualizationLengthMax = visualizationNames[0].length();
			 for(int i=1;i<visualizationItemNumber;i++)
			 if(visualizationNames[i].length() > visualizationLengthMax)
			 visualizationLengthMax = visualizationNames[i].length();
			 visualizationLengthMax += 4;
		}
		else //image base
		{
			miningLengthMax = 10;
			visualizationLengthMax = 10;
		}
		
		panelInformation = FILEIO.TextFileAllReadCode(new File(control.absolutePath + "source" + File.separator + "Information.txt"));
        
        /*
		if(select.dummyShow == false)
		{
			setMiningIndex = moduleData.findModule(select.miningIDs[select.selected_module]);
//			this.parentPanel.label3.setText(miningNames[setMiningIndex]);moduleData.miningModuleNamesInJapanese
			
			if(settingData.inJapanese)
				this.parentPanel.label3.setText("  -> "+moduleData.miningModuleNamesInJapanese[setMiningIndex]);
			else
				this.parentPanel.label3.setText("  -> "+moduleData.module_names[setMiningIndex]);	
		}
         */
	}
	



	void draw_background(Graphics2D g2)
	{
		sizeX = getWidth();
		sizeY = getHeight();
		miningX = 10;
		miningY = 10;
		miningWidth = (sizeX-20)/2-5;
		miningHeight = sizeY-20;
		visualizationX = sizeX/2;
		visualizationY = 10;
		visualizationWidth = (sizeX-20)/2;
		visualizationHeight = sizeY-20;
		
		
		if(option == 0)// character base
		{			
			 miningWidthLimit = miningLengthMax * fontSize;
			 visualizationWidthLimit = visualizationLengthMax * fontSize;
			 
			 miningWidthLimit *= 0.6;
			 visualizationWidthLimit *= 0.6;
			 			 
			 xNumberMining = (int)(miningWidth/miningWidthLimit);
			 if(xNumberMining == 0)
			 xNumberMining = 1;
			 yNumberMining = (int)(miningItemNumber/xNumberMining);
			 if(miningItemNumber > xNumberMining * yNumberMining)
			 yNumberMining++;
			 			
			//		System.out.println("miningItemNumber= "+miningItemNumber+"xNumberMining= "+xNumberMining+" yNumberMining= "+yNumberMining);
						
			 xNumberVisualization = (int)(visualizationWidth/visualizationWidthLimit);
			 if(xNumberVisualization == 0)
			 xNumberVisualization = 1;
			 yNumberVisualization = (int)(visualizationItemNumber/xNumberVisualization);
			 if(visualizationItemNumber > xNumberVisualization * yNumberVisualization)
			 yNumberVisualization++;
		}
		else	// image base
		{		
			int miningArea = miningWidth * miningHeight;
			int visualizationArea = visualizationWidth * visualizationHeight;
		
			int miningOneArea = (int)(miningArea/miningItemNumber);
			int visualizationOneArea = (int)(visualizationArea/visualizationItemNumber);		
		
			int miningSquare = (int)Math.sqrt(miningOneArea);
			int visualizationSquare = (int)Math.sqrt(visualizationOneArea);
		
			xNumberMining = (int)(miningWidth/miningSquare) + 1;
			yNumberMining = (int)(miningHeight/miningSquare) + 1;		
		
			xNumberVisualization = (int)(visualizationWidth/visualizationSquare) + 1;
			yNumberVisualization = (int)(visualizationHeight/visualizationSquare) + 1;
		}
		

		if(yNumberMining * (xNumberMining-1) >= miningItemNumber)
			xNumberMining--;
		
		if(yNumberVisualization * (xNumberVisualization-1) >= visualizationItemNumber)
			xNumberVisualization--;		
				
		g2.setColor(new Color(0,128,255));
		g2.fillRect(0,0, sizeX, sizeY);
	}
		
	public void drawBoxes(Graphics2D g2, int sortedIndex[], int x, int y, int width, int height, int xNumber, int yNumber, String names[], int type[], Image images[])
	{
		int boxSizeX = width/xNumber;
		int boxSizeY = height/yNumber;
		
		int imageSize = boxSizeX;
		if(imageSize > boxSizeY)
			imageSize = boxSizeY;
		
		int setFontSize = fontSize;
		
        BasicStroke wideStroke = new BasicStroke(4.0f);
        g2.setStroke(wideStroke);		
		g2.setColor(Color.white);
		
		for(int i=0;i<xNumber+1;i++)
			g2.drawLine(x+boxSizeX*i,y,x+boxSizeX*i,y+boxSizeY*yNumber);			
		
		for(int i=0;i<yNumber+1;i++)
			g2.drawLine(x,y+boxSizeY*i,x+boxSizeX*xNumber,y+boxSizeY*i);			
		
		if(setFontSize >= boxSizeY)
			setFontSize = boxSizeY - 2;
		
		g2.setFont(new Font(settingData.fontName, Font.BOLD, setFontSize));
		for(int i=0;i<xNumber;i++)
			for(int j=0;j<yNumber;j++)
				if(i*yNumber+j < names.length)
				{
					g2.drawImage(images[sortedIndex[i*yNumber+j]],x + boxSizeX*i+2, y + boxSizeY*j+2, imageSize-4, imageSize-4, this);
					if(option == 0)
					{
						String displayName = names[sortedIndex[i*yNumber+j]];
						if(displayName.length() + typeWord[type[sortedIndex[i*yNumber+j]]].length() > (boxSizeX - imageSize)/setFontSize)
						{
							int strLength = (boxSizeX - imageSize)/setFontSize - (int)(typeWord[type[sortedIndex[i*yNumber+j]]].length()*0.5) - 1;
							if(strLength < 0)
								strLength = 0;
							displayName = displayName.substring(0,strLength);
						}
						g2.drawString(displayName+typeWord[type[sortedIndex[i*yNumber+j]]], 
									  x + boxSizeX*i + (int)(setFontSize*0.5) + imageSize, y + (int)(boxSizeY*(j+0.5)) + (int)(setFontSize*0.5) - 2);
					}
				}
	}
	
	public void drawOneBox(Graphics2D g2, int sortedReverseIndex[], int x, int y, int width, int height, int xNumber, int yNumber, int n, int limit, Color color)
	{				
		int boxSizeX = width/xNumber;
		int boxSizeY = height/yNumber;
		
		if(n == -1 || n >= limit)
			return;
		
		int i = sortedReverseIndex[n]/yNumber;
		int j = sortedReverseIndex[n]%yNumber;

        BasicStroke wideStroke = new BasicStroke(4.0f);
        g2.setStroke(wideStroke);		
		g2.setColor(color);
		
		g2.drawRect(x + boxSizeX*i, y + boxSizeY*j ,boxSizeX,boxSizeY);
	}
	
	public void drawOneBoxBackground(Graphics2D g2, int sortedReverseIndex[], int x, int y, int width, int height, int xNumber, int yNumber, int n, int limit, Color color)
	{
		int boxSizeX = width/xNumber;
		int boxSizeY = height/yNumber;
		
		if(n == -1 || n >= limit)
			return;		
		
		int i = sortedReverseIndex[n]/yNumber;
		int j = sortedReverseIndex[n]%yNumber;
				
		g2.setColor(color);
		g2.fillRect(x + boxSizeX*i, y + boxSizeY*j ,boxSizeX, boxSizeY);		
	}
	
	public void drawPairBoxes(Graphics2D g2, int sortedReverseIndex[], int x, int y, int width, int height, int xNumber, int yNumber, int n[], int limit)
	{
		for(int i=0;i<n.length;i++)
			drawOneBox(g2, sortedReverseIndex, x, y, width, height, xNumber, yNumber, n[i], limit, Color.RED);
	}
	

	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// Anti aliasing
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);		
		
		draw_background(g2);	//Set background
		
//		if(english)
//		{
//			drawBoxes(g2, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, miningNames);
//			drawBoxes(g2, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization,
//					  visualizationNames);
//		}
		
		if(setMiningIndex != -1)
		{
			drawOneBoxBackground(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, setMiningIndex, miningItemNumber, new Color(0xff,0x7f,0x00));

			for(int i=0;i<mining[setMiningIndex].pairingVisualizationIndex.length;i++)
				drawOneBoxBackground(g2, sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, 
									 mining[setMiningIndex].pairingVisualizationIndex[i], visualizationItemNumber, new Color(0xff,0xa5,0x00));				
		}
		if(setVisualizationIndex != -1)
		{
			drawOneBoxBackground(g2, sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, 
				   setVisualizationIndex, visualizationItemNumber, new Color(0xff,0x7f,0x00));
		
			for(int i=0;i<visual[setVisualizationIndex].pairingMiningIndex.length;i++)
				drawOneBoxBackground(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, 
									 visual[setVisualizationIndex].pairingMiningIndex[i], miningItemNumber, new Color(0xff,0xa5,0x00));				
		}
		
		for(int i=0;i<miningNames.length;i++)
			if(moduleData.miningModuleSelection[i] == false)
				drawOneBoxBackground(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, i, miningItemNumber, Color.gray);
		for(int i=0;i<visualizationNames.length;i++)
			if(moduleData.visualizationModuleSelection[i] == false)
				drawOneBoxBackground(g2, sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, i, visualizationItemNumber, Color.gray);		
		
		if(settingData.inJapanese)
		{
			drawBoxes(g2, sortedMIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, moduleData.miningModuleNamesInJapanese, miningToolType, miningModuleImage);
			drawBoxes(g2, sortedVIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization,
				  moduleData.visualizationModuleNamesInJapanese, visualizationToolType, visualizationModuleImage);		
		}
		else
		{
			drawBoxes(g2, sortedMIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, moduleData.module_names, miningToolType, miningModuleImage);
			drawBoxes(g2, sortedVIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization,
					  moduleData.visu_module_names, visualizationToolType, visualizationModuleImage);			
		}

		
		if(setVisualizationIndex == -1)
		{
			if(setMiningIndex != -1)
				drawPairBoxes(g2, sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, 
							  mining[setMiningIndex].pairingVisualizationIndex, visualizationItemNumber);	
			else if(touchNumberMining >= 0 && touchNumberMining < miningItemNumber)
				drawPairBoxes(g2, sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, 
							  mining[touchNumberMining].pairingVisualizationIndex, visualizationItemNumber);			
		}

		if(setMiningIndex == -1)
		{
			if(setVisualizationIndex != -1)
				drawPairBoxes(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, 
							  visual[setVisualizationIndex].pairingMiningIndex, miningItemNumber);
			else if(touchNumberVisualization >= 0 && touchNumberVisualization < visualizationItemNumber)
				drawPairBoxes(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, 
							  visual[touchNumberVisualization].pairingMiningIndex, miningItemNumber);
		}
		
		drawOneBox(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, setMiningIndex, miningItemNumber, Color.red);		
		drawOneBox(g2, sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, 
				   setVisualizationIndex, visualizationItemNumber, Color.red);		
		
		drawOneBox(g2, sortedMiningReverseIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, touchNumberMining, miningItemNumber, Color.green);
		drawOneBox(g2,  sortedVisualizationReverseIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, 
				   touchNumberVisualization, visualizationItemNumber, Color.green);
		
			
		
//		createModuleInformationWindow();
	}
		
	public void update(Graphics g)		//Avoid from blinking
	{
		paintComponent(g);
	}		

	public int findNumber(int sortedIndex[], int x, int y, int width, int height, int xNumber, int yNumber, int limit)
	{
		int boxSizeX = width/xNumber;
		int boxSizeY = height/yNumber;
		
		if(mouseX <= x || mouseY <= y || mouseX >= x+boxSizeX*xNumber || mouseY >= y+boxSizeY*yNumber)
			return -1;
		
		int i = (int)((mouseX-x)/boxSizeX);
		int j = (int)((mouseY-y)/boxSizeY);
		
		if(i*yNumber+j >= limit)
			return -1;
		
		return sortedIndex[i*yNumber+j];
	}
	
	public boolean createModuleInformation(int sortedReverseIndex[], int x, int y, int width, int height, int xNumber, int yNumber, int number, int lastNumber, int limit, String information[], boolean right)
	{
		int boxSizeX,boxSizeY,i,j;
		int informationSizeX,informationSizeY;
		int informationFontSize;
		Point position = getLocationOnScreen();
		
		int rightMargin, leftMargin;
		
		if(number >=0 && number < limit)
		{
			boxSizeX = width/xNumber;
			boxSizeY = height/yNumber;
			i = sortedReverseIndex[number]/yNumber;
			j = sortedReverseIndex[number]%yNumber;			
			
			if(right)
			{
				rightMargin = sizeX - (x + boxSizeX*(i+1)) + sizeX * (control.panelNumber-select.getPanelID() - 1);
				leftMargin = sizeX * select.getPanelID();
			}
			else
			{
				leftMargin = (int)(sizeX * (select.getPanelID() + 0.5) + boxSizeX*i);
				rightMargin = sizeX - (x + boxSizeX*(i+1)) + sizeX * (control.panelNumber-select.getPanelID() - 1);					
			}
		
			//System.out.println("Left = "+leftMargin+" , Right = "+rightMargin);
			
			if(rightMargin > leftMargin)
			{
				informationSizeX = rightMargin;
				right = true;
			}
			else
			{
				informationSizeX = leftMargin;
				right = false;
			}
			
			int maxLength = information[number].indexOf(System.getProperty("line.separator")); //line 1
			if(information[number].length() - maxLength > maxLength)	// if line 2 > line 1
				maxLength = information[number].length() - maxLength;
			
//			informationFontSize = informationSizeX / (int)((information[number].length()+1));
			informationFontSize = informationSizeX / (int)((maxLength+1));			
			if(informationFontSize > fontSize)
			{
				informationFontSize = fontSize;
//				informationSizeX = informationFontSize *  (int)((information[number].length()+1));
				informationSizeX = informationFontSize *  (int)((maxLength+1));			
			}
//			informationSizeY = (int)(informationFontSize+8);
			informationSizeY = (int)(informationFontSize+8) * 2;
			
			if(number == lastNumber)
			{
				//System.out.println("number="+number);
				return false;
			}
			
			int posX, posY;
			
			if(right)
			{
				posX = (int)position.getX() + x + boxSizeX*(i+1) + boxSizeY;
				posY = (int)position.getY() + y + boxSizeY*(j-1);
				if(posY < 10)
					posY = 10;
			}
			else
			{
				posX = (int)position.getX() + x + boxSizeX*i - informationSizeX - boxSizeY;
				posY = (int)position.getY() + y + boxSizeY*(j-1);
				if(posX < 10)
					posX = 10;				
				
				if(posY < 10)
					posY = 10;				
			}
			
			informationWindow = new PopUpForModuleInformation(posX, posY, informationSizeX, informationSizeY, information[number], informationFontSize, settingData.fontName);

			//System.out.println("Information Created");
			
			return true;
		}
		return false;
	}
    
	public void createModuleInformationWindow()
	{
        if(touchNumberMining >= 0 && touchNumberMining < miningItemNumber && touchNumberMining != lastTouchMining)
        {
            if(informationWindow != null)
                informationWindow.dispose();
            createModuleInformation(sortedMiningReverseIndex, miningX, miningY, miningWidth, miningHeight, xNumberMining, yNumberMining,
                                          touchNumberMining, lastTouchMining, miningItemNumber, moduleData.miningInformationText, true);
			lastTouchVisualization = -1;
         	lastTouchMining = touchNumberMining;
            return;
		}

        if(touchNumberVisualization >= 0 && touchNumberVisualization < visualizationItemNumber && touchNumberVisualization != lastTouchVisualization)
        {
            if(informationWindow != null)
                informationWindow.dispose();
            createModuleInformation(sortedVisualizationReverseIndex, visualizationX, visualizationY, visualizationWidth, visualizationHeight, xNumberVisualization, yNumberVisualization,
                                    touchNumberVisualization, lastTouchVisualization, visualizationItemNumber, moduleData.visualizationInformationText, false);
            lastTouchMining = -1;
            lastTouchVisualization = touchNumberVisualization;
        }
        
		if(touchNumberMining == -1 && touchNumberVisualization == -1)
			if(informationWindow != null)
				informationWindow.dispose();
	}
    
	/*
	public void createModuleInformationWindow()
	{
		boolean created;
		PopUpForModuleInformation backup = informationWindow;
		
	
	
		created = createModuleInformation(sortedMiningReverseIndex, miningX, miningY, miningWidth, miningHeight, xNumberMining, yNumberMining, 
									touchNumberMining, lastTouchMining, miningItemNumber, moduleData.miningInformationText, true);
		if(created)
		{
			lastTouchVisualization = -1;
			if(backup != null)
				backup.dispose();			
		}
		lastTouchMining = touchNumberMining;
	
			
		created = createModuleInformation(sortedVisualizationReverseIndex, visualizationX, visualizationY, visualizationWidth, visualizationHeight, xNumberVisualization, yNumberVisualization, 
									touchNumberVisualization, lastTouchVisualization, visualizationItemNumber, moduleData.visualizationInformationText, false);
		if(created)
		{
			lastTouchMining = -1;
			if(backup != null)
				backup.dispose();			
		}
		lastTouchVisualization = touchNumberVisualization;		
		
		if(touchNumberMining == -1 && touchNumberVisualization == -1)
			if(backup != null)
				backup.dispose();		
	}
     */

	public void createInformation()
	{
		Point position = getLocationOnScreen();
		int setFontSize;
        
        int width = getWidth()-20;
        int height = getHeight();
        int fontA = width/38;
        int fontB = height/29;
        
        if(fontA < fontB)
        {
            setFontSize = fontA;
            height = setFontSize * 29;
        }
        else
        {
            setFontSize = fontB;
            width = setFontSize * 38;
        }
		if(setFontSize > fontSize)
			setFontSize = fontSize;

		information = new PopInformation((int)position.getX()+10, (int)position.getY()+10, width, height, panelInformation, setFontSize, settingData.fontName);
	}
    
	//MouseMotionListener
	public void mouseDragged(MouseEvent me){}	
	public void mouseMoved(MouseEvent me)
	{
		mouseX = me.getX();
		mouseY = me.getY();
		
//        if(mouseY >= 0 && mouseY < miningY)
        if(mouseY > 10 && (mouseX < miningX || mouseX > visualizationX + visualizationWidth))
        {
            if(displayInformation == false)
            {
                displayInformation = true;
                createInformation();
            }
        }
        else
        {
            if(displayInformation == true)
                information.dispose();
            
            displayInformation = false;                
        }
        
		touchNumberMining = findNumber(sortedMIndex, miningX,miningY,miningWidth,miningHeight,xNumberMining,yNumberMining, miningItemNumber);
		touchNumberVisualization = findNumber(sortedVIndex, visualizationX,visualizationY,visualizationWidth,visualizationHeight,xNumberVisualization,yNumberVisualization, visualizationItemNumber);
		
		
//		System.out.println("MINING="+touchNumberMining+" LAST MINING="+lastTouchMining);
		
        if(touchNumberMining >= 0 && touchNumberMining < miningItemNumber && touchNumberMining != lastTouchMining)
        {
			if(setMiningIndex == -1)
			{
				if(settingData.inJapanese)
				{
					this.parentPanel.label3.setText("  -> "+moduleData.miningModuleNamesInJapanese[touchNumberMining]);
					this.parentPanel.label5.setText(moduleData.miningInformationText[touchNumberMining]);
				}
				else
				{
					this.parentPanel.label3.setText("  -> "+moduleData.module_names[touchNumberMining]);
					this.parentPanel.label5.setText(moduleData.miningInformationText[touchNumberMining]);
				}				
			}
			
			lastTouchVisualization = -1;			
         	lastTouchMining = touchNumberMining;            
		}
		else
			lastTouchMining = -1;
		
        if(touchNumberVisualization >= 0 && touchNumberVisualization < visualizationItemNumber && touchNumberVisualization != lastTouchVisualization)
        {
			if(setVisualizationIndex == -1)
			{
				if(settingData.inJapanese)
				{
					this.parentPanel.label4.setText("  -> "+moduleData.visualizationModuleNamesInJapanese[touchNumberVisualization]);
					this.parentPanel.label6.setText(moduleData.visualizationInformationText[touchNumberVisualization]);
				}
				else
				{
					this.parentPanel.label4.setText("  -> "+moduleData.visu_module_names[touchNumberVisualization]);
					this.parentPanel.label6.setText(moduleData.visualizationInformationText[touchNumberVisualization]);
				}				
			}			

            lastTouchMining = -1;
            lastTouchVisualization = touchNumberVisualization;
        }
		else
			lastTouchVisualization = -1;		
		
		if(touchNumberMining == -1 && setMiningIndex == -1)
		{
			this.parentPanel.label3.setText("  -> ");
			this.parentPanel.label5.setText("");			
		}
		
		if(touchNumberVisualization == -1 && setVisualizationIndex == -1)
		{
			this.parentPanel.label4.setText("  -> ");
			this.parentPanel.label6.setText("");			
		}		
		

		
//		System.out.println(touchNumberMining+":"+touchNumberVisualization);

		repaint();
	}
	
	//MouseListener
	public void mousePressed(MouseEvent me){}	//Press
	public void mouseReleased(MouseEvent me){}	//Release
	public void mouseEntered(MouseEvent me){}	//Enter an area
	public void mouseExited(MouseEvent me)
    {
        if(informationWindow != null)
            informationWindow.dispose();
        
        if(information != null)
            information.dispose();
        
        lastTouchMining = -1;
        lastTouchVisualization = -1;
        touchNumberMining = -1;
        touchNumberVisualization = -1;
        repaint();
        
    }	//Exit an area
	public void mouseClicked(MouseEvent me)	//Click
	{
//		System.out.println("CLICKED:TOUCH IN ="+touchNumberMining+":"+touchNumberVisualization+" SET="+setMiningIndex+":"+setVisualizationIndex);		
		
		//Right Click
		if((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0)
		{
//			System.out.println("RightClick:(touch)"+touchNumberMining+","+touchNumberVisualization+" (set) "+setMiningIndex+","+setVisualizationIndex);
			if(touchNumberMining >= 0 && touchNumberMining < miningItemNumber)
            {
				if(moduleData.miningModuleSelection[touchNumberMining] == true)
				{
					moduleData.miningModuleSelection[touchNumberMining] = false;
					setMiningIndex = -1;
					this.parentPanel.label3.setText("  -> ");
				}
                else
					moduleData.miningModuleSelection[touchNumberMining] = true;					

                moduleData.initializeVisualiationModuleSelection();				
				panelsReConstruction = true;				
                repaint();
                return;
            }
			
			if(touchNumberVisualization >= 0 && touchNumberVisualization < visualizationItemNumber)
            {
                if(setMiningIndex != -1)
				{
					if(pairingCheck(setMiningIndex, touchNumberVisualization))
					{
						deletePairingVisualization(setMiningIndex, touchNumberVisualization);
						moduleData.initializeVisualiationModuleSelection();
						setVisualizationIndex = -1;
						
						if(settingData.inJapanese)
							this.parentPanel.label4.setText("  -> "+moduleData.visualizationModuleNamesInJapanese[setVisualizationIndex]);
						else
							this.parentPanel.label4.setText("  -> "+moduleData.visu_module_names[setVisualizationIndex]);	
					}
					else
					{
						moduleData.visualizationModuleSelection[touchNumberVisualization] = true;
						addPairingVisualization(setMiningIndex, touchNumberVisualization);
					}
					panelsReConstruction = true;					   
				}
                repaint();
                return;
            }
		}
		
		// Double Clicked
		if (me.getClickCount() >= 2)
		{
			if(touchNumberMining >= 0 && touchNumberMining < miningItemNumber)
			{
				setMiningIndex = touchNumberMining;
				if(settingData.inJapanese)
					this.parentPanel.label3.setText("  -> "+moduleData.miningModuleNamesInJapanese[setMiningIndex]);
				else
					this.parentPanel.label3.setText("  -> "+moduleData.module_names[setMiningIndex]);			
				

				if(mining[setMiningIndex].pairingVisualizationIndex.length == 0)
					return;				
				setVisualizationIndex = mining[setMiningIndex].pairingVisualizationIndex[0];
				
				if(settingData.inJapanese)				
					this.parentPanel.label4.setText("  -> "+moduleData.visualizationModuleNamesInJapanese[setVisualizationIndex]);
				else
					this.parentPanel.label4.setText("  -> "+moduleData.visu_module_names[setVisualizationIndex]);					
			}
			
			if(touchNumberVisualization >= 0 && touchNumberVisualization < visualizationItemNumber)
			{
				setVisualizationIndex = touchNumberVisualization;
				
				if(settingData.inJapanese)
					this.parentPanel.label4.setText("  -> "+moduleData.visualizationModuleNamesInJapanese[setVisualizationIndex]);
				else
					this.parentPanel.label4.setText("  -> "+moduleData.visu_module_names[setVisualizationIndex]);					
				
				if(visual[setVisualizationIndex].pairingMiningIndex.length == 0)
					return;
				setMiningIndex = visual[setVisualizationIndex].pairingMiningIndex[0];
				
				if(settingData.inJapanese)
					this.parentPanel.label3.setText("  -> "+moduleData.miningModuleNamesInJapanese[setMiningIndex]);
				else
					this.parentPanel.label3.setText("  -> "+moduleData.module_names[setMiningIndex]);					
			}
			
			setMining(setMiningIndex);
			
			
			
//			System.out.println("DOUBLE CLICKED:TOUCH="+touchNumberMining+":"+touchNumberVisualization+" SET="+setMiningIndex+":"+setVisualizationIndex);
			if(setMiningIndex != -1 && setVisualizationIndex != -1)				
				setPanel();
		}
		
		if(touchNumberMining >= 0 && touchNumberMining < miningItemNumber)
		{
			setMining(touchNumberMining);
			
			if(touchNumberMining == setMiningIndex)
			{
				setMiningIndex = -1;
				this.parentPanel.label3.setText("  -> ");				
			}
			else
			{
				setMiningIndex = touchNumberMining;
				if(settingData.inJapanese)				
					this.parentPanel.label3.setText("  -> "+moduleData.miningModuleNamesInJapanese[setMiningIndex]);
				else
					this.parentPanel.label3.setText("  -> "+moduleData.module_names[setMiningIndex]);					
			}
		}
		
		if(touchNumberVisualization >= 0 && touchNumberVisualization < visualizationItemNumber)
		{
			setVisualization(touchNumberVisualization);

			if(touchNumberVisualization == setVisualizationIndex)
			{
				setVisualizationIndex = -1;
				this.parentPanel.label4.setText("  -> ");				
			}
			else
			{
				setVisualizationIndex = touchNumberVisualization;
				if(settingData.inJapanese)				
					this.parentPanel.label4.setText("  -> "+moduleData.visualizationModuleNamesInJapanese[setVisualizationIndex]);
				else
					this.parentPanel.label4.setText("  -> "+moduleData.visu_module_names[setVisualizationIndex]);					
			}
		}
		
		repaint();

//		System.out.println("CLICKED:TOUCH OUT="+touchNumberMining+":"+touchNumberVisualization+" SET="+setMiningIndex+":"+setVisualizationIndex);		
		
		if(setMiningIndex != -1 && setVisualizationIndex != -1)			
			setPanel();
	}
	
	void setMining(int miningIndex)
	{
		if(moduleData.miningModuleSelection[miningIndex] == false)
		{
			moduleData.miningModuleSelection[miningIndex] = true;
			moduleData.initializeVisualiationModuleSelection();
			panelsReConstruction = true;
		}	
	}
	
	void setVisualization(int visualizationIndex)
	{
		if(moduleData.visualizationModuleSelection[visualizationIndex] == false)
		{
			moduleData.visualizationModuleSelection[visualizationIndex] = true;
			panelsReConstruction = true;			
		}
		else
			moduleData.initializeVisualiationModuleSelection();		
	}
	
	void setPanel()
	{
		if(moduleData.visualizationModuleSelection[setVisualizationIndex] == false)
		{
			moduleData.visualizationModuleSelection[setVisualizationIndex] = true;
			panelsReConstruction = true;			
		}
        
        //create backup
        control.createBackupIDs();		

		if(panelsReConstruction == true)
		{
//          System.out.println("RECONSTRUCTION");
			
            addPairingVisualization(setMiningIndex, setVisualizationIndex);
			
            moduleData.initializeModuleSubset();
			
            settingData.miningID[select.getPanelID()] = mining[setMiningIndex].getModuleID();
            settingData.visualizationID[select.getPanelID()] = visual[setVisualizationIndex].getModuleID();
			
            try{
                control.panelsReConstruction();
            }
            catch (Exception e) {
                System.out.println("COMBINATION ERROR at setPanel in SetToolPanel2.java A - printing ERRORS as follows");
                e.printStackTrace();

                control.traceBackupIDs();
                control.panelSet();
            }
		}
        
        try{
			select.changePanel(mining[setMiningIndex].getModuleID(), visual[setVisualizationIndex].getModuleID());
        }
        catch (Exception e) {
            System.out.println("COMBINATION ERROR at setPanel in SetToolPanel2.java B - printing ERRORS as follows");
            e.printStackTrace();

            control.traceBackupIDs();
            control.panelSet();
        }
        
		settingData.miningID[select.getPanelID()] = mining[setMiningIndex].getModuleID();
		settingData.visualizationID[select.getPanelID()] = visual[setVisualizationIndex].getModuleID();			

		if(informationWindow != null)
			informationWindow.dispose();
		parentFrame.dispose();
		
		
	}
	
	public int[] addOneData(int array[], int data)
	{
		int length = array.length;
		int newArray[] = new int[length+1];
		
		for(int i=0;i<length;i++)
			newArray[i] = array[i];
		newArray[length] = data;
		
		return newArray;
	}
	 
	public boolean pairingCheck(int mindex, int vindex)
	{
		int length = mining[mindex].pairingVisualizationID.length;
		int vid = visual[vindex].getModuleID();
		
		for(int i=0;i<length;i++)
			if(mining[mindex].pairingVisualizationID[i] == vid)
				return true;
		return false;
	}
	
	public void addPairingVisualization(int mindex, int vindex)
	{
		//PairingVisualization
		int length = mining[mindex].pairingVisualizationID.length;
		int vid = visual[vindex].getModuleID();
		
		for(int i=0;i<length;i++)
			if(mining[mindex].pairingVisualizationID[i] == vid)
				return;
		
		mining[mindex].pairingVisualizationID = addOneData(mining[mindex].pairingVisualizationID, vid);
		mining[mindex].pairingVisualizationIndex = addOneData(mining[mindex].pairingVisualizationIndex, vindex);			

		//PairingMining
		moduleData.createPairingMiningID(vindex);
		
//		System.out.println("Added:"+mindex+","+vindex);
	}

	public int[] deleteOneData(int array[], int deleteIndex)
	{
		int length = array.length;
		int newArray[] = new int[length-1];
        int count = 0;
		
		for(int i=0;i<length;i++)
            if(i != deleteIndex)
                newArray[count++] = array[i];
		
		return newArray;
	}
    
    public void deletePairingVisualization(int mindex, int vindex)
    {
		//PairingVisualization
		int length = mining[mindex].pairingVisualizationID.length;
		int vid = visual[vindex].getModuleID();
        int check = -1;
        
        if(length == 1) //NOT DELETE
            return;
        
		for(int i=0;i<length;i++)
			if(mining[mindex].pairingVisualizationID[i] == vid)
            {
                check = i;
                break;
            }
        if(check == -1) //NOT FOUND
            return;
        
        mining[mindex].pairingVisualizationID = deleteOneData(mining[mindex].pairingVisualizationID, check);
		mining[mindex].pairingVisualizationIndex = deleteOneData(mining[mindex].pairingVisualizationIndex, check);
        
		//PairingMining
		moduleData.createPairingMiningID(vindex);
		
//		System.out.println("Deleted:"+mindex+","+vindex);		
    }
}


class InformationPanel extends JPanel
{
	JTextPane text1;	
	
	InformationPanel(String message, int fontSize, String fontName)		
	{
		setBackground(new Color(0xff, 0xf8, 0xdc));        
		text1 = new JTextPane();
		text1.setContentType("text/plain");        
		text1.setFont(new Font(fontName, Font.BOLD, fontSize));
		text1.setForeground(Color.black);
		text1.setBackground(new Color(0xff, 0xf8, 0xdc));
        text1.setText(message);
        text1.setEditable(false);
		text1.setCaretPosition(0);
        add(text1);
		
		/*
		setBackground(new Color(0xff, 0xf8, 0xdc));
		JLabel label = new JLabel(message);
		label.setForeground(Color.black);
		label.setFont(new Font(settingData.fontName, Font.BOLD, fontSize));
		label.setBackground(new Color(0xff, 0xf8, 0xdc));		
		add(label);
		 */
	}
}

class PopUpForModuleInformation extends JFrame
{
	InformationPanel informationPanel;
	Container pane = getContentPane();	
	
	PopUpForModuleInformation(int X, int Y, int width, int height, String message, int fontSize, String fontName)
	{
		super("Information");
		pane.setLayout(new BorderLayout());
		
		setBounds(X, Y, width, height);
		setBackground(new Color(0xff, 0xf8, 0xdc));		
		setAlwaysOnTop(true);
		setUndecorated(true);
		
		informationPanel = new InformationPanel(message, fontSize, fontName);
		pane.add(informationPanel);
		setVisible(true);
	}
}

class InformationTextPanel extends JPanel
{
	JTextPane text1;

	InformationTextPanel(String displayText, int fontSize, String fontName)
	{
		setBackground(new Color(0xff, 0xf8, 0xdc));        
		text1 = new JTextPane();
		text1.setContentType("text/plain");        
		text1.setFont(new Font(fontName, Font.BOLD, fontSize));
		text1.setForeground(Color.black);
		text1.setBackground(new Color(0xff, 0xf8, 0xdc));
        text1.setText(displayText);
        text1.setEditable(false);
		text1.setCaretPosition(0);
        add(text1);
	}
}

class PopInformation extends JFrame
{
	InformationTextPanel informationTextPanel;
	Container pane = getContentPane();
	
	PopInformation(int X, int Y, int width, int height, String message, int fontSize, String fontName)
	{
		super("Information");
		pane.setLayout(new BorderLayout());
		
		setBounds(X, Y, width, height);
		setBackground(new Color(0xff, 0xf8, 0xdc));		
		setAlwaysOnTop(true);
		setUndecorated(true);
        
		informationTextPanel = new InformationTextPanel(message, fontSize, fontName);
		pane.add(informationTextPanel);
		setVisible(true);
	}
}
