//
// Mining module for TETDM 
// WordInformation.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.WordInformation;

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 WordInformation extends MiningModule implements ActionListener
{
    double data[][];
	String header[];
    int dataNumber;

    String dataNames[];
    String speechNames[];

	JToggleButton tButton[];
    int buttonNumber;
	ButtonGroup buttonGroup;	
    
    ShrinkPanel shrinkPanel;
    OsusumePanel osusumePanel;
    JPanel lowerPanel;

    
	public WordInformation()
    {
		setModuleID(35);
		setCategoryID(2);
		setUseMode(0);
		pairingVisualizationID = new int[]{24,4};
		setToolType(3);		
	}

	public void initializePanel()
	{
		String names[] = {"Word","Speech","Freq","PosiNega","SegFreq","SubFreq"};
		
        header = new String[6];
		if(isMenuInJapanese())
            for(int i=0;i<6;i++)
                header[i] = inJapanese[i];
		else
            for(int i=0;i<6;i++)
                header[i]= names[i];
		
		operationPanel.setBackground(Color.RED);		
        buttonNumber = header.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(header[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(header,1,buttonNumber+1, this, isMenuInJapanese(), text.control.absolutePath);
        lowerPanel.add(shrinkPanel);
        osusumePanel = new OsusumePanel(header,1,buttonNumber+1, this, isMenuInJapanese(), text.control.absolutePath);
        lowerPanel.add(osusumePanel);

        add(lowerPanel, BorderLayout.SOUTH);
	}	

	public void initializeData()
    {
        dataNumber = text.keywordNumber;

    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case -1:
                displayOperations(33);  //Frequency Sort for TableDisplay
                break;
                
			case 0:
				createWordData();
                shrinkPanel.initializeData(data,0);
                osusumePanel.initializeData(data,0);
				break;
		}
	}
	
	public void createWordData()
	{
		dataNames = new String[text.keywordNumber];
		for(int i=0;i<text.keywordNumber;i++)
			dataNames[i] = text.keyword[i].word;
		
        /*
        speechNames = new String[8];
        for(int i=0;i<8;i++)
            speechNames[i] = inJapanese[6+i];
         */
        
		data = new double[header.length][dataNames.length];
		
		for(int i=0;i<dataNames.length;i++)
		{
			data[0][i] = text.keyword[i].partOfSpeech;
			data[1][i] = text.keyword[i].frequency;
//            data[2][i] = text.keyword[i].sentenceFrequency;
            data[2][i] = text.keyword[i].posiNega+1;
			data[3][i] = text.keyword[i].segmentFrequency;
			data[4][i] = text.keyword[i].frequencyAsSubject;
		}

		setDataStringArray(0, header);
		setDataStringArray(1, dataNames);
		setDataDoubleArray2(0, data);
//		setDataInteger(0,3);// Keyword 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
			}

	}
  
}


