
//
// Mining module for TETDM 
// HalfText.java Version 0.30
// This is one of the sample modules.
// You should read the README file.
//

package module.MiningModules.HalfText;

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

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

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

public class HalfText extends MiningModule implements ActionListener
{
    int toggleButtonNumber;
    int buttonNumber;
    ButtonGroup bg;
    
    TextData originalTextData;
    TextData tempTextData;
    int frequency[];
    
	public HalfText()
	{
		setModuleID(25);						// Set your module ID after you have got it
		setCategoryID(1);
        setUseMode(0);
		pairingVisualizationID = new int[]{21};
		setToolType(2);
        
        focusExecute = true;
	}
	
	public void initializePanel()
    {
        toggleButtonNumber = 3;
        buttonNumber = 1;
        
		operationPanel.setBackground(Color.RED);
        operationPanel.setLayout(new GridLayout(1,4));
        
	    toggleButton = new JToggleButton[toggleButtonNumber];
        button = new JButton[1];
        
        bg = new ButtonGroup();
        
        String toggleButtonNamesInEnglish[] = {"Focus-NotFocus","First-Second Half","Odd-Even Half","Create ATTENTION"};
        
	    for(int i=0;i<toggleButtonNumber;i++)
		{
		    toggleButton[i] = new JToggleButton();
		    toggleButton[i].setBackground(Color.RED);
		    toggleButton[i].addActionListener(this);
		    operationPanel.add(toggleButton[i]);
            bg.add(toggleButton[i]);
		}
        
        button[0] = new JButton();
        button[0].setBackground(Color.RED);
        button[0].addActionListener(this);
        operationPanel.add(button[0]);

        
        if(isMenuInJapanese())
        {
            for(int i=0;i<toggleButtonNumber;i++)
                toggleButton[i].setText(inJapanese[i]);
            button[0].setText(inJapanese[toggleButtonNumber]);
        }
        else
        {
            for(int i=0;i<toggleButtonNumber;i++)
                toggleButton[i].setText(toggleButtonNamesInEnglish[i]);
            button[0].setText(toggleButtonNamesInEnglish[toggleButtonNumber]);
        }
        
        toggleButton[0].setSelected(true);
    }

	public void initializeData()
    {
        originalTextData = text.control.text;
        if(originalTextData.segmentPartialTextData[1] == null)
            createSegmentPartialData(1);
        tempTextData = originalTextData.segmentPartialTextData[1];
        
        frequency = new int[text.keywordNumber];
        focusSegments = new boolean[originalTextData.segmentNumber];
    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case 4500:
			case 0:
                if(toggleButton[0].isSelected())
                    createFocusNotFocus();
                else if(toggleButton[1].isSelected())
                    createFirstSecondHalf();
                else if(toggleButton[2].isSelected())
                    createOddEvenHalf();
				break;
		}
	}
    
    
    
    
    public void setTempFocus(int n)
    {
        setFocusSegmentsLocal(tempTextData, focusSegments);
        tempTextData.createSegmentPartialData(originalTextData, focusSegments, 1);
        
        for(int i=0;i<text.keywordNumber;i++)
            frequency[i] = tempTextData.keyword[i].segmentFrequency;
        
        int count=0;
        for(int i=0;i<originalTextData.segmentNumber;i++)
            if(focusSegments[i])
                count++;
        setDataInteger(n, count);
        
        setDataIntegerArray(n, frequency);
    }

    public void createFocusNotFocus()
    {
        //Focus
        focusSegments = originalTextData.focus.getFocusSegments();
        setTempFocus(0);
        
        //Not Focus
        /*
        for(int i=0;i<text.segmentNumber;i++)
            focusSegments[text.segment[i].segmentID] = !focusSegments[text.segment[i].segmentID];
        */
        for(int i=0;i<originalTextData.segmentNumber;i++)
            focusSegments[i] = !focusSegments[i];
        setTempFocus(1);
    }
    
    public void createFocusNotFocusLoop()
    {
        //Focus
        focusSegments = originalTextData.focus.getFocusSegments();
        setTempFocus(10);

        for(int i=0;i<originalTextData.segmentNumber;i++)
            focusSegments[i] = !focusSegments[i];
        setTempFocus(11);

    }
    
    public void createFocusKeywordsLoop()
    {
        resetAllFocus();
        
        displayOperations(9);   //initializeAttention
        
        int lowestFrequency = originalTextData.segmentNumber/10;
        
        createProcessingPop();
        
        focusKeywords = new boolean[text.keywordNumber];
        for(int i=0;i<text.keywordNumber;i++)
        {
            if(i>0)
                focusKeywords[i-1] = false;
            
            focusKeywords[i] = true;
            if(text.keyword[i].frequency > lowestFrequency)
            {
//           System.out.println("keyword = "+text.keyword[i].word);
                setFocusKeywords(focusKeywords,"","");
                setDataInteger(12,i);
                createFocusNotFocusLoop();
            }
            
            setProcessingPercent((int)(i*100/text.keywordNumber));
        }
        
        disposeProcessingPop();
        
        displayOperations(12);  //set Attention
        repaintOthersByFocus();
    }
    
    
    public void createFirstSecondHalf()
    {
        //First Half
        for(int i=0;i<originalTextData.segmentNumber;i++)
            focusSegments[i] = false;
        
        for(int i=0;i<text.segmentNumber;i++)
            if(text.segment[i].segmentID < originalTextData.segmentNumber/2)
                focusSegments[text.segment[i].segmentID] = true;
        
        setTempFocus(0);
        
        //Second Half
        for(int i=0;i<originalTextData.segmentNumber;i++)
            focusSegments[i] = false;
        
        for(int i=0;i<text.segmentNumber;i++)
            if(text.segment[i].segmentID >= originalTextData.segmentNumber/2)
                focusSegments[text.segment[i].segmentID] = true;
        
        setTempFocus(1);
    }
    
    public void createOddEvenHalf()
    {
        //Odd Half
        for(int i=0;i<originalTextData.segmentNumber;i++)
            focusSegments[i] = false;
        
        for(int i=0;i<text.segmentNumber;i++)
            if(text.segment[i].segmentID %2 == 0)
                focusSegments[text.segment[i].segmentID] = true;
        
        setTempFocus(0);
        
        //Even Half
        for(int i=0;i<originalTextData.segmentNumber;i++)
            focusSegments[i] = false;
        
        for(int i=0;i<text.segmentNumber;i++)
            if(text.segment[i].segmentID %2 != 0)
                focusSegments[text.segment[i].segmentID] = true;
        
        setTempFocus(1);
    }
    
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);
        
        if(e.getSource() == toggleButton[0])
        {
            createFocusNotFocus();
        }
        
        if(e.getSource() == toggleButton[1])
        {
            createFirstSecondHalf();
        }
        
        if(e.getSource() == toggleButton[2])
        {
            createOddEvenHalf();
        }
        
        if(e.getSource() == button[0])
        {
            createFocusKeywordsLoop();
        }
	}
}
