//
// Mining module for TETDM 
// FocusInformation.java Version 0.30
// Copyright(C) 2011-2018 WATARU SUNAYAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//

package module.MiningModules.FocusInformation;

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 FocusInformation extends MiningModule implements ActionListener
{
    String displayTextList[];
    int wordAttribute[];
    int wordNumber;
    int wordCount;
    
    boolean multiLineMode;

	public FocusInformation()
    {
		setModuleID(54);
		setCategoryID(8);
        setUseMode(0);
		pairingVisualizationID = new int[]{2};
		setToolType(2);
        
		focusExecute = true;
	}

	public void initializePanel()
    {
        toggleButton = new JToggleButton[1];
        toggleButton[0] = new JToggleButton();
        toggleButton[0].setBackground(Color.red);
        toggleButton[0].addActionListener(this);
        operationPanel.add(toggleButton[0]);
        

    }
    
    public void initializeData()
    {
        multiLineMode = false;
        
        if(isMenuInJapanese())
            toggleButton[0].setText(inJapanese[0]);
        else
            toggleButton[0].setText("Multi Lines Display");
    }

	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case -1:
                if(toggleButton[0].isSelected())
                {
                    displayOperations(199);
                    displayOperations(300);
                }
                else
                {
                    displayOperations(199);
                    displayOperations(200);
                }
                break;
                
            case 4500:
			case 0:
				check_text();
				break;
		}
	}

    public void checkSentence(int i, int j)
    {
        for(int k=0;k<text.segment[i].sentence[j].wordNumber;k++)
        {
            displayTextList[wordCount] = text.segment[i].sentence[j].word[k].replaceAll(System.getProperty("line.separator"),"");
            if(text.segment[i].sentence[j].wordIDList[k] >= 0)
            {
                if(focusKeywords[text.segment[i].sentence[j].wordIDList[k]])
                    wordAttribute[wordCount++] = 22;
                else
                    wordAttribute[wordCount++] = 1;
            }
            else
                wordAttribute[wordCount++] = 1;
        }
    }
    
    public void checkSegment(int i)
    {
		try
		{
            for(int j=0;j<text.segment[i].sentenceNumber;j++)
                if(focusSentences[ text.segment[i].positionOfFirstSentence + j ])
                    checkSentence(i,j);
                else
                {
                    for(int k=0;k<text.segment[i].sentence[j].wordNumber;k++)
                    {
                        displayTextList[wordCount] = text.segment[i].sentence[j].word[k].replaceAll(System.getProperty("line.separator"),"");
                        wordAttribute[wordCount++] = 21;
                    }
                }
		}
		catch(Exception e){
			System.out.println("Error in FocusWord3");
		}
    }

    public void check_text()
	{
        multiLineMode = toggleButton[0].isSelected();
        
        focusKeywords = getFocusKeywords();
        focusSentences = getFocusSentences();
        focusSegments = getFocusSegments();

        ////////////
        int count=0;
        wordNumber = 0;
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
            {
                wordNumber += text.segment[i].wordNumber;
                count++;
            }
        
        displayTextList = new String[wordNumber+count];
        wordAttribute = new int[wordNumber+count];
        wordCount = 0;
        ////////////
        
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
            {
                displayTextList[wordCount] = "["+(text.segment[i].segmentID+1)+"]";
                wordAttribute[wordCount++] = 21;
                
                checkSegment(i);
                
                if(multiLineMode)
                    displayTextList[wordCount-1] += System.getProperty("line.separator")+System.getProperty("line.separator");
                else
                    displayTextList[wordCount-1] += System.getProperty("line.separator");
            }

        if(count == 0)
        {
            displayTextList = new String[1];
            wordAttribute = new int[1];
            
            if(isMenuInJapanese())
                displayTextList[0] = inJapanese[1];
            else
                displayTextList[0] = "No Focus Selected";
            wordAttribute[0] = 10;
        }
        
        setDataStringArray(1,displayTextList);
        setDataIntegerArray(2,wordAttribute);
	}
    
    public void text_change()
    {
        multiLineMode = toggleButton[0].isSelected();
        
        wordCount = 0;
        ////////////
        
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
            {
                wordCount++;
                for(int j=0;j<text.segment[i].sentenceNumber;j++)
                    wordCount += text.segment[i].sentence[j].wordNumber;
                
                displayTextList[wordCount-1] = displayTextList[wordCount-1].replaceAll(System.getProperty("line.separator"),"");
                if(multiLineMode)
                    displayTextList[wordCount-1] += System.getProperty("line.separator")+System.getProperty("line.separator");
                else
                    displayTextList[wordCount-1] += System.getProperty("line.separator");
            }
        
        if(wordCount == 0)
        {
            displayTextList = new String[1];
            wordAttribute = new int[1];
            
            if(isMenuInJapanese())
                displayTextList[0] = inJapanese[1];
            else
                displayTextList[0] = "No Focus Selected";
            wordAttribute[0] = 10;
        }
        
        setDataStringArray(1,displayTextList);
        setDataIntegerArray(2,wordAttribute);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        writeButtonLog(e);
        
        if(e.getSource() == toggleButton[0])
        {
            text_change();
            miningOperations(-1);
        }
    }

}


