

//
// Core Program for TETDM 
// PopUpForSelectionAssist.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.*;
import javax.swing.event.*;
import javax.swing.JComponent.*;

public class PopUpForSelectionAssist extends JFrame implements AdjustmentListener, MouseWheelListener
{
	JScrollPane scroll;
	SelectionAssistPanel selectionAssistPanel;
	Container pane = getContentPane();
	
	Control control;
	
	public int X, Y=65;
	public int windowSizeX, windowSizeY;
	
	boolean logStart = false;
	boolean autoSet = false;
	
	boolean panelCreated = false;
	
	PopUpForSelectionAssist myFrame;
	String windowName = "COMBINATION";

	PopUpForSelectionAssist(int width, int height, Control control, String windowTitle)
	{
		super(windowTitle);
		pane.setLayout(new BorderLayout());
		
		this.control = control;
		
		
		//Window cannot be closed in super light mode
		if(control.sp1.mode == 0)
		{
			setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//			setAlwaysOnTop(true);
		}
		pane.setBackground(Color.blue);
//		setUndecorated(true);
		
		selectionAssistPanel = new SelectionAssistPanel(this, control);
		selectionAssistPanel.setBackground(Color.blue);
		scroll = new JScrollPane(selectionAssistPanel);
		scroll.setBackground(Color.blue);
		pane.add(scroll);		
				
		scroll.addMouseWheelListener(this);		
		scroll.getHorizontalScrollBar().addAdjustmentListener(this);

        
        WINDOWACTIONLOG windowActionLog = new WINDOWACTIONLOG(this, windowName);
        this.addComponentListener(windowActionLog.componentListener);                    //FOR LOG
        this.addWindowListener(new MyWindowListener());
        
		



		
		windowSizeX= 220*control.selectionAssistData.dataNumber;
		windowSizeY = 170;		
				
		if(windowSizeX > width)
			windowSizeX = width;
		
		X = (width-windowSizeX)/2;
		
		if(control.sp1.mode > 0 && control.selectionAssistData.manSelectionNumber > 0)
			windowSizeY += 15;
		if(windowSizeX / control.selectionAssistData.dataNumber < 220)
			windowSizeY += 15;			
		
		if(control.sp1.gameMode == true)
			Y += 65;
			
		
		setBounds(X, Y, windowSizeX, windowSizeY);

		autoSet=true;
		setVisible(true);		
		autoSet=false;
		
		myFrame = this;
	}
	
    void reConstruction()
    {
		autoSet = true;
        pane.remove(scroll);
        
		selectionAssistPanel = new SelectionAssistPanel(this, control);
		selectionAssistPanel.setBackground(Color.blue);
		scroll = new JScrollPane(selectionAssistPanel);
		scroll.setBackground(Color.blue);
		
		scroll.addMouseWheelListener(this);
		scroll.getHorizontalScrollBar().addAdjustmentListener(this);		
		
		pane.add(scroll);
        pane.validate();  //java7 revalidate()
		
		autoSet = false;
    }
    

	//AdjustmentListener
	public void adjustmentValueChanged(AdjustmentEvent e)
    {
		if(autoSet == true)
			return;
		
		if(panelCreated == true)
			ACTIONLOG.writeMouseLog("COMBINATION", "DRAG(BAR-ADJUSTED)");
	}		

	
	//MouseWheelListener
	public void mouseWheelMoved(MouseWheelEvent mwe)
	{
		double rotation = mwe.getWheelRotation();
		ACTIONLOG.writeMouseWheelLog("COMBINATION", rotation);
	}	
	
	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e)// x button
		{
			control.autoSet=true;
			control.bt72.setSelected(false);
			control.autoSet=false;
		}
		
		public void windowClosed(WindowEvent e)
		{
			control.autoSet=true;
			control.bt72.setSelected(false);
			control.autoSet=false;
		}
		

	}
	
	
}

class SelectionAssistPanel extends JPanel implements ActionListener
{
	PopUpForSelectionAssist popFrame;
	Control control;
	
	JPanel topLabelPanel,bottomButtonPanel;
	JButton okButton;
	
	//ORIGINAL
	
	JButton originalSelectionButton[];	
	JButton originalDeleteButton[];
	JLabel originalLabel[];
	
	//manually saved
	
	JButton manSelectionButton[];	
	JButton manDeleteButton[];	
	
	//automatically saved
	
	JButton selectionButton[];	
	JLabel autoLabel[];
	
	
	SelectionAssistData ad;
	
	
	SelectionAssistPanel(PopUpForSelectionAssist popFrame, Control control)
	{
		int throughCount = 0;
		
		this.popFrame = popFrame;
		this.control = control;
		ad = control.selectionAssistData;
		
		setLayout(new BorderLayout());
		topLabelPanel = new JPanel(new GridLayout(1, ad.dataNumber));
		bottomButtonPanel = new JPanel(new GridLayout(1, ad.dataNumber));
		topLabelPanel.setBackground(Color.blue);
		bottomButtonPanel.setBackground(Color.blue);
		add(bottomButtonPanel);
		add(topLabelPanel,BorderLayout.NORTH);

		
		selectionButton = new JButton[ad.selectionNumber];
		autoLabel = new JLabel[ad.selectionNumber];
		
		manSelectionButton = new JButton[ad.manSelectionNumber];
		manDeleteButton = new JButton[ad.manSelectionNumber];
		
		originalSelectionButton = new JButton[ad.originalSelectionNumber];
		originalDeleteButton = new JButton[ad.originalSelectionNumber];
		originalLabel = new JLabel[ad.originalSelectionNumber];

		
		//originally prepared panel combination		

		for(int i=0;i<ad.originalSelectionNumber;i++)
		{
			if(ad.originalMode[i] > control.sp1.mode)
				continue;				
				
			if(control.sp1.inJapanese)				
				originalLabel[i] = new JLabel(ad.inJapanese[3+i]);
			else
				originalLabel[i] = new JLabel("-----");

			originalLabel[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));				
			originalLabel[i].setForeground(Color.white);
			topLabelPanel.add(originalLabel[i]);
		}			
			
		for(int i=0;i<ad.originalSelectionNumber;i++)
		{
			if(ad.originalMode[i] > control.sp1.mode)
				continue;
				
			originalSelectionButton[i] = new JButton(ad.originalSmallIcon[i]);
			originalSelectionButton[i].addActionListener(this);
			bottomButtonPanel.add(originalSelectionButton[i]);
		}
			
		if(control.sp1.mode == 0)
				return;
		
		//manually saved panel combination
		for(int i=0;i<ad.manSelectionNumber;i++)
		{			
			manSelectionButton[i] = new JButton(ad.manSmallIcon[i]);
			manSelectionButton[i].addActionListener(this);
			bottomButtonPanel.add(manSelectionButton[i]);//////
			
			if(control.sp1.inJapanese)			
				manDeleteButton[i] = new JButton(ad.inJapanese[1]);
			else
				manDeleteButton[i] = new JButton("DELETE the left one");
			
			manDeleteButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));			
			manDeleteButton[i].addActionListener(this);
			topLabelPanel.add(manDeleteButton[i]);			
		}		
		
		if(control.sp1.mode == 1)
			return;		
		
		//automatically saved panel combination		
		for(int i=0;i<ad.selectionNumber;i++)
		{
			selectionButton[i] = new JButton(ad.smallIcon[i]);
			selectionButton[i].addActionListener(this);
			bottomButtonPanel.add(selectionButton[i]);
			
			if(control.sp1.inJapanese)
				autoLabel[i] = new JLabel(ad.inJapanese[2]);
			else			
				autoLabel[i] = new JLabel("AUTO SAVED");
			
			autoLabel[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));			
			autoLabel[i].setForeground(Color.white);
			topLabelPanel.add(autoLabel[i]);			
		}

	
	}



	public void closeWindow()
	{	control.autoSet = true;
		control.bt72.setSelected(false);
		control.autoSet = false;
        control.closeCombinationWindow();
	}


	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{

		
		for(int i=0;i<ad.selectionNumber;i++)
			if(e.getSource() == selectionButton[i])
			{
				ACTIONLOG.writeButtonLog("COMBINATION", "AUTO:"+i, (Component)e.getSource());
				control.focus.clearAllFocus();
				control.text.focus.clearAllFocus();
				control.panelSet(ad.miningIDs[i], ad.visualizationIDs[i], ad.textIDs[i], ad.sizeX[i]);
			}
		
		for(int i=0;i<ad.manSelectionNumber;i++)
			if(e.getSource() == manSelectionButton[i])
			{
                ACTIONLOG.writeButtonLog("COMBINATION", "MAN:"+i, (Component)e.getSource());
				control.focus.clearAllFocus();
				control.text.focus.clearAllFocus();
				control.panelSet(ad.manMiningIDs[i], ad.manVisualizationIDs[i], ad.manTextIDs[i], ad.manSizeX[i]);
			}
		
		for(int i=0;i<ad.originalSelectionNumber;i++)
			if(e.getSource() == originalSelectionButton[i])
			{
                ACTIONLOG.writeButtonLog("COMBINATION", "ORIGINAL:"+i, (Component)e.getSource());
				control.focus.clearAllFocus();
				control.text.focus.clearAllFocus();
				///////////////// EXP ///////////////
				control.ex1.addExpBySelectionAssistButton(i);
				if(control.expPanel != null)
					control.expPanel.repaint();
                
                //////////////////USE SHRINK DATA //////
                if(control.focus.shrinkCount > 0)
                    for(int j=0;j<ad.originalTextIDs[i].length;j++)
                        ad.originalTextIDs[i][j] = 1;   //segment partial
                
				control.panelSet(ad.originalMiningIDs[i], ad.originalVisualizationIDs[i], ad.originalTextIDs[i], ad.originalSizeX[i]);
			}
		
		for(int i=0;i<ad.manSelectionNumber;i++)
			if(e.getSource() == manDeleteButton[i])
			{
                ACTIONLOG.writeButtonLog("COMBINATION", "MAN-DELETE:"+i, (Component)e.getSource());
				//delete //reconstruction
                ad.manDataDelete(i);
                popFrame.reConstruction();                
			}			
							
		if(e.getSource() == okButton)
		{
            ACTIONLOG.writeButtonLog("COMBINATION", "OK", (Component)e.getSource());
			closeWindow();
		}		
	}
}
