//
// Mining module for TETDM 
// SegmentInformation.java Version 0.39
// Copyright(C) 2015 WATARU SUNAYAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//

package module.MiningModules.SegmentInformation;

import module.MiningModules.WordInformation.ShrinkPanel;
import module.MiningModules.WordInformation.OsusumePanel;

import source.*;
import source.TextData.*;

import java.io.*;
import java.util.*;

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


public class SegmentInformation extends MiningModule implements ActionListener
{
	String columnNames[];
	String dataNames[];
	
	double data[][];
	
	JToggleButton tButton[];
    int buttonNumber;   //column number - 1
	ButtonGroup buttonGroup;
    
    
    ShrinkPanel shrinkPanel;
    OsusumePanel osusumePanel;
    JPanel lowerPanel;
    
	public SegmentInformation()
    {
		setModuleID(36);
		setCategoryID(4);
		setUseMode(2);
		pairingVisualizationID = new int[]{24,4};
		setToolType(3);	
	}

	public void initializePanel()
	{
		String names[] = {"Seg.No","Sentence","Word","Kind of Keyword","Originality"};		
		
		if(isMenuInJapanese())
			columnNames = inJapanese;
		else
			columnNames = names;
		
		operationPanel.setBackground(Color.RED);		
        buttonNumber = columnNames.length-1;     
	    tButton = new JToggleButton[buttonNumber];		
        operationPanel.setLayout(new GridLayout(1,buttonNumber));
        
		buttonGroup = new ButtonGroup();
		
	    for(int i=0;i<buttonNumber;i++)
		{
		    tButton[i] = new JToggleButton(columnNames[i+1]);
			tButton[i].setBackground(Color.red);
		    tButton[i].addActionListener(this);			
		    operationPanel.add(tButton[i]);
			buttonGroup.add(tButton[i]);
		}
        ///////DATA SHRINK///////
        
        lowerPanel = new JPanel();
        lowerPanel.setLayout(new GridLayout(2,1));
        
        shrinkPanel = new ShrinkPanel(columnNames,1,buttonNumber+1, this, isMenuInJapanese(), text.control.absolutePath);
        lowerPanel.add(shrinkPanel);
        osusumePanel = new OsusumePanel(columnNames,1,buttonNumber+1, this, isMenuInJapanese(), text.control.absolutePath);
        lowerPanel.add(osusumePanel);
        
        add(lowerPanel, BorderLayout.SOUTH);
	}
	
	public void initializeData(){}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{               
			case 0:
                createSegmentData();
                shrinkPanel.initializeData(data,0);
                osusumePanel.initializeData(data,0);
				break;
		}
	}
	
	public void createSegmentData()
	{
        if(text.segmentNumber == 0)
            return;
        
		dataNames = new String[text.segmentNumber];
		for(int i=0;i<text.segmentNumber;i++)
			dataNames[i] = "["+(text.segment[i].segmentID+1+"]");
		
		text.segmentRelation.createStrongestLink(text.segmentRelation.cos);
		
		data = new double[columnNames.length][dataNames.length];
		
		for(int i=0;i<dataNames.length;i++)
		{
			data[0][i] = text.segment[i].sentenceNumber;
			data[1][i] = text.segment[i].wordNumber;
			data[2][i] = text.segment[i].kindKeywordNumber;
			data[3][i] = (100-text.segmentRelation.value[i]*100);
		}		
		
		setDataStringArray(0, columnNames);
		setDataStringArray(1, dataNames);
		setDataDoubleArray2(0, data);
		setDataInteger(0,1);// Segment focus
	}
    
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);/////NEW LOG
        
		for(int i=0;i<buttonNumber;i++)
			if(e.getSource() == tButton[i])
			{
				setDataDoubleArray(0, data[i]);
				displayOperations(0);	//save text and re-calculation in all modules
			}
	}
}


