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


package module.MiningModules.EditModule;

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 EditModule extends MiningModule implements ActionListener
{
    JButton button[];
	
	String numberedText;

    String segment;
    String buttonNamesInJapanese[];
	
	public EditModule()
	{
		setModuleID(1);
		pairingVisualizationID = new int[1];
		pairingVisualizationID[0] = 1;
	}

	public void initializePanel()
	{
		buttonNamesInJapanese = fileReadArray();
		
		operationPanel.setBackground(Color.RED);

	    button = new JButton[5];

	    for(int i=0;i<5;i++)
		{
		    button[i] = new JButton();
		    button[i].setBackground(Color.RED);
		    button[i].addActionListener(this);
		    operationPanel.add(button[i]);
		}
	}	
	
	public void initializeData()
    {
		String buttonNamesInEnglish[] = {"original text","save text","size --","size ++","numbered"};

		if(isMenuInJapanese())
		{
			for(int i=0;i<5;i++)
				button[i].setText(buttonNamesInJapanese[i]);
			segment = buttonNamesInJapanese[5];
		}
		else
		{
			for(int i=0;i<5;i++)
				button[i].setText(buttonNamesInEnglish[i]);
			segment = "SEGMENT";
		}
    }
	
	void makeNumberedText()
	{
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);	
		
		try{
			for(int i=0;i<text.segmentNumber;i++)
			{
				bw.write("<"+segment+" : "+(i+1)+">\n");
				for(int j=0;j<text.segment[i].sentenceNumber;j++)
				{
					bw.write((text.segment[i].sentence[j].positionOfSentence+1)+": ");
					for(int k=0;k<text.segment[i].sentence[j].wordNumber;k++)
						if(!text.segment[i].sentence[j].word[k].equals("\n"))
							bw.write(text.segment[i].sentence[j].word[k]);
					bw.write("\n");
				}
			}
			bw.flush();
		}
		catch(Exception e){
			System.out.println("writing ERROR in EditModule");
		}		

		numberedText = sw.toString();		
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:	
				setData(0,text.originalText);
				break;
				
			case 1:
				makeNumberedText();
				setData(1,numberedText);			
				displayOperations(0);
				break;
		}
	}
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == button[0])
			displayOperations(100);

		if(e.getSource() == button[1])
			displayOperations(1);

		if(e.getSource() == button[2])
			displayOperations(9);

		if(e.getSource() == button[3])
			displayOperations(10);

		if(e.getSource() == button[4])
			miningOperations(1);
	}
}
