//
// Visuzalization module for TETDM 
// FocusDisplay.java Version 0.36
// Copyright(C) 2011 2012 WATARU SUNAYAMA ALL RIGHTS RESERVED.
//
// This program is provided under the licenses.
// You Should read the README file.
//


package module.VisualizationModules.FocusDisplay;

import source.*;
import source.TextData.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;


public class FocusDisplay extends VisualizationModule implements AdjustmentListener, MouseWheelListener
{
	Sizepanel sizepanel;	
	
	//Text pane
	JTextPane textPanel;
	JScrollPane scrollPanel;

	//for DISPLAY
	String displayText;	//display text
	
	int type;	// keywords, segments, sentences
	int kind;	// main, sub, set

    String key,seg,sen,main,sub;
    String kindWords[][];


	public FocusDisplay()
	{
		setModuleID(1016);
		setCategoryID(8);		
		setToolType(1);		
	}

	public void initializePanel()
	{
		textPanel = new JTextPane();
		textPanel.setContentType("text/plain");		
		scrollPanel = new JScrollPane(textPanel);
		add(scrollPanel);
		
		scrollPanel.addMouseWheelListener(this);
		scrollPanel.getVerticalScrollBar().addAdjustmentListener(this);		
		
		sizepanel = new Sizepanel();
		sizepanel.setPreferredSize(new Dimension(0,60));		
		add("South",sizepanel);
		
		type = 1;
		kind = 0;
		kindWords = new String[3][3];

	}
	
	public void initializeData()
	{
		if(isMenuInJapanese())
		{
			key =  inJapanese[0];
			seg =  inJapanese[1];
			sen =  inJapanese[2];
			main = inJapanese[3];
			sub = inJapanese[4];
			String list[] =  {main,sub,key,main,sub,seg,main,sub,sen};
			for(int i=0;i<3;i++)
			    for(int j=0;j<3;j++)
				kindWords[i][j] = list[i*3+j];
			displayText = inJapanese[5];
		}
		else
		{
			key = "KEY";
			seg = "SEG";
			sen = "SEN";
			String list[] = {"MAIN","SUB","KEYWORDS","MAIN","SUB","SEGMENTS","MAIN","SUB","SENTENCES"};
			for(int i=0;i<3;i++)
			    for(int j=0;j<3;j++)
				kindWords[i][j] = list[i*3+j];
			displayText = "Nothing";
		}
	}
	
	public void displayOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 4500:
			case 0:
				initialDisplay();
				break;
				
			case 11:
				type = 0;
				kind = 0;
				break;

			case 12:
				type = 0;
				kind = 1;
				break;
				
			case 13:
				type = 0;
				kind = 2;
				break;
				
			case 21:
				type = 1;
				kind = 0;
				break;
				
			case 22:
				type = 1;
				kind = 1;
				break;
				
			case 23:
				type = 1;
				kind = 2;
				break;
				
			case 31:
				type = 2;
				kind = 0;
				break;
				
			case 32:
				type = 2;
				kind = 1;
				break;
				
			case 33:
				type = 2;
				kind = 2;
				break;
		}		
	}	
	
	public void initialDisplay()
	{
	    if(isMenuInJapanese())
		{
		    key = inJapanese[0];
		    seg = inJapanese[1];
		    sen = inJapanese[2];
		}

		textPanel.setFont(new Font(text.getFontName(), Font.BOLD, getFontSize()));
		
		displayText = "";
		switch(type)
		{
			case 0: switch(kind)
			{
				case 0:	if(getMainFocusKeyword() >= 0)
					displayText = "<"+key+(getMainFocusKeyword()+1)+">\n"+text.keyword[getMainFocusKeyword()].word;
					break;
				case 1: if(getSubFocusKeyword() >= 0)
					displayText = "<"+key+(getSubFocusKeyword()+1)+">\n"+text.keyword[getSubFocusKeyword()].word;
					break;
				case 2:
                    boolean focusKeywords[] = getFocusKeywords();
                    for(int i=0;i<text.keywordNumber;i++)
                        if(focusKeywords[i])
                            displayText += "<"+key+(i+1)+">\n"+text.keyword[i].word+"\n";
					break;
			}
				break;				
				
			case 1: switch(kind)
			{
				case 0:	if(getMainFocusSegment() >= 0)
					displayText = "<"+seg+(text.segment[getMainFocusSegment()].segmentID+1)+">\n"+text.segment[getMainFocusSegment()].segmentText;
					break;
				case 1: if(getSubFocusSegment() >= 0)
					displayText = "<"+seg+(text.segment[getSubFocusSegment()].segmentID+1)+">\n"+text.segment[getSubFocusSegment()].segmentText;
					break;
				case 2:
                    boolean focusSegments[] = getFocusSegments();
                    for(int i=0;i<text.segmentNumber;i++)
                        if(focusSegments[i])
                            displayText += "<"+seg+(text.segment[i].segmentID+1)+">\n"+text.segment[i].segmentText+"\n";
					break;
			}
				break;
				
			case 2:	switch(kind)
			{
				case 0:	if(getMainFocusSentence() >= 0)
					displayText = "<"+sen+(text.sentence[getMainFocusSentence()].sentenceID+1)+">\n"+text.sentence[getMainFocusSentence()].sentenceText;
					break;
				case 1: if(getSubFocusSentence() >= 0)
					displayText = "<"+sen+(text.sentence[getSubFocusSentence()].sentenceID+1)+">\n"+text.sentence[getSubFocusSentence()].sentenceText;
					break;
				case 2:
                    boolean focusSentences[] = getFocusSentences();
                    for(int i=0;i<text.sentenceNumber;i++)
                        if(focusSentences[i])
                            displayText += "<"+sen+(text.sentence[i].sentenceID+1)+">\n"+text.sentence[i].sentenceText+"\n";
					break;
			}
				break;
		}
		
		textPanel.setText(displayText);
		textPanel.setCaretPosition(0);
		
		sizepanel.repaint();
	}
	
	//AdjustmentListener
	public void adjustmentValueChanged(AdjustmentEvent e)
	{
		writeBarDragLog();
	}
	
	//MouseWheelListener
	public void mouseWheelMoved(MouseWheelEvent mwe)
	{
		double rotation = mwe.getWheelRotation();
		writeWheelLog(rotation);
	}				
	
	class Sizepanel extends JPanel implements MouseListener
	{
		int sizex,sizey;
		int mousex,mousey;
		
	    //		String kindWords[][] = {{"MAIN","SUB","KEYWORDS"},{"MAIN","SUB","SEGMENTS"},{"MAIN","SUB","SENTENCES"}};
		
		int x[] = new int[3];
		int y[] = new int[3];
		
		//constructer
		Sizepanel()
		{
			addMouseListener(this);
		}
		
		//background
		public void draw_background(Graphics2D g2)
		{
			sizex = getWidth();
			sizey = getHeight();
			
			g2.setColor(new Color(0xfa, 0x80, 0x72));
			g2.fillRect(0,0,sizex,sizey/2);
			g2.setColor(new Color(0xff, 0xd7, 0x72));			
			g2.fillRect(sizex/6,0,sizex/6,sizey/2);
			g2.fillRect(3*sizex/6,0,sizex/6,sizey/2);
			g2.fillRect(5*sizex/6,0,sizex/6,sizey/2);

			g2.setColor(new Color(0xff, 0xff, 0x00));
			g2.fillRect(0,sizey/2,sizex/3,sizey);
			g2.setColor(new Color(0x00, 0xbf, 0xff));
			g2.fillRect(sizex/3,sizey/2,sizex/3,sizey);
			g2.setColor(new Color(0x00, 0xff, 0x00));
			g2.fillRect(2*sizex/3,sizey/2,sizex/3,sizey);
			
		
			
			g2.setFont(new Font(text.getFontName(), Font.BOLD, 20));		
			g2.setColor(Color.black);
			
			y[0] = sizey/4+10;
			y[1] = sizey/4+10;
			y[2] = 3*sizey/4+10;			
			
			for(int i=0;i<3;i++)
			{
				x[0] = (1+4*i)*sizex/12 - kindWords[i][0].length()*5-5;
				x[1] = (3+4*i)*sizex/12 - kindWords[i][1].length()*5-5;
				x[2] = (1+2*i)*sizex/6 - kindWords[i][2].length()*5-15;
				
				for(int j=0;j<3;j++)
				{
					if(i == type && j == kind)
						g2.setColor(Color.red);	
					else
						g2.setColor(Color.black);
					
					g2.drawString(kindWords[i][j],x[j],y[j]);
				}
			}
		}
		
		//////////paint
		public void paintComponent(Graphics g)
		{
			Graphics2D g2 = (Graphics2D)g;
			
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// Anti aliasing
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);			
			
			draw_background(g2);		//background
		}
		
		public void update(Graphics g)		//avoid from blinking
		{
			paintComponent(g);
		}
		

		
		//MouseListener
		public void mousePressed(MouseEvent me){}	//Press
		public void mouseReleased(MouseEvent me){}	//Release
		public void mouseEntered(MouseEvent me){}	//Enter an area
		public void mouseExited(MouseEvent me){}	//Exit an area
		
		public void mouseClicked(MouseEvent me)	//Click
		{
			writeClickLog();
			
			mousex = me.getX();
			mousey = me.getY();
			
			type = 3 * mousex / sizex;

			if(mousey > sizey/2)
				kind = 2;
			else 
				kind = (int)(6 * mousex / sizex) % 2;

			displayOperations(0);
		}
	}	
}
