//
// Mining module for TETDM 
// TopicDistribution.java Version 0.1 2012.12.25
// Copyright(C) 2012 Tatsuya Ishii, Tomoki Kajinami All RIGHTS RESERVED.
// This program is provided under the license.
// You should read the README file.
//

package module.MiningModules.TopicDistribution;

import source.*;
import source.TextData.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TopicDistribution extends MiningModule implements ActionListener, FocusListener{
	
	private JComboBox modeSelector;

	private int topicList[];
	
	public TopicDistribution(){
		setModuleID(23);						// Set your module ID after you have got it
		setCategoryID(5);		
		pairingVisualizationID=new int[1];
		pairingVisualizationID[0]=16;
        focusExecute=true;
	}

	public void initializePanel(){
		if(isMenuInJapanese()){
			String[] modeName={inJapanese[0],inJapanese[1],inJapanese[2],inJapanese[3],inJapanese[4]};
			modeSelector=new JComboBox(modeName);
		}
		else{
			String[] modeName={"Topic over segment number","Topic as subject over segment number","Topic over sentence number","Topic as subject over sentence number","Topic over keyword number"};
			modeSelector=new JComboBox(modeName);
		}
		
		modeSelector.addActionListener(this);	
		modeSelector.addFocusListener(this);	
//		modeSelector.getEditor().getEditorComponent().addFocusListener(this);
		operationPanel.add(modeSelector);
	}
	
	public void initializeData(){}
	
	public void miningOperations(int optionNumber){
		switch(optionNumber){
			case 4500:
                boolean focusKeywords[] = getFocusKeywords();
				int count=0;
				for(int i=0;i<text.keywordNumber;i++){
					if(focusKeywords[i]){
						count++;
					}
				}
				if(count == 0){
					topicList=getNewDataIntegerArray(11,0);
				}
				else{
					topicList = new int[count];
					count=0;
					for(int i=0;i<text.keywordNumber;i++){
						if(focusKeywords[i]){
							topicList[count]=i;
							count++;
						}
					}
				}
				setDataString(98,makeData());
				displayOperations(0);
				break;
			
			case 0:
                topicList=getNewDataIntegerArray(11,0);
				setDataString(98,makeData());
				displayOperations(0);
				break;
		}
	}
	
	private String makeData(){
		StringWriter sw=new StringWriter();
		BufferedWriter bw=new BufferedWriter(sw);
		
		try{
			if(topicList.length==0) bw.write("Dummy,1"+System.getProperty("line.separator"));
			for(int i=0;i<topicList.length;i++){
				switch(modeSelector.getSelectedIndex()){
					case 0:
						bw.write(text.keyword[topicList[i]].word+","+(double)text.keyword[topicList[i]].segmentFrequency/text.segmentNumber+System.getProperty("line.separator"));
						break;
					case 1:
						bw.write(text.keyword[topicList[i]].word+","+(double)text.keyword[topicList[i]].segmentFrequencyAsSubject/text.segmentNumber+System.getProperty("line.separator"));
						break;
					case 2:
						bw.write(text.keyword[topicList[i]].word+","+(double)text.keyword[topicList[i]].sentenceFrequency/text.sentenceNumber+System.getProperty("line.separator"));
						break;
					case 3:
						bw.write(text.keyword[topicList[i]].word+","+(double)text.keyword[topicList[i]].frequencyAsSubject/text.sentenceNumber+System.getProperty("line.separator"));
						break;
					case 4:
						bw.write(text.keyword[topicList[i]].word+","+(double)text.keyword[topicList[i]].frequency/text.keywordNumber+System.getProperty("line.separator"));
						break;
				}
			}
			
			bw.flush();
		}
		catch(Exception e){
			System.out.println("writing ERROR in RadarChartTest");
		}
		return sw.toString();
	}
	
	//FocusListener
	public void focusGained(FocusEvent e)
	{
		writeClickLog();
	}
	
	public void focusLost(FocusEvent e)	{}	
	
	public void actionPerformed(ActionEvent e){
		writeButtonLog(e);
		miningOperations(0);
	}
}
