//
// Visuzalization module for TETDM 
// TextDisplay.java Version 0.31
// Copyright(C) 2011 WATARU SUNAYAMA ALL RIGHTS RESERVED.
//
// This program is provided under the license.
// You should read the README file.
//

package module.VisualizationModules.FileDisplay;

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

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

import java.io.File;


public class FileDisplay extends VisualizationModule implements MouseListener, AdjustmentListener, MouseWheelListener
{
	//Text pane
	JTextPane text1;
	JScrollPane scroll1;
	
	//filename panel
	JPanel panel;
	JLabel label;
	String fileString;
	boolean fileExist;

	//for DISPLAY
	String filename;	//FILENAME
	String displayText;	//display text

    String nothing;

	int fontsize;

	public FileDisplay()
	{
		setModuleID(5);
		setCategoryID(1);
		setUseMode(0);
        dataNumbers = new int[]{0,0,0,1,    // b,i,d,S
            0,0,0,0,    // bA,iA,dA,SA
            0,0,0};     // bA2,iA2,dA2
	}

	public void initializePanel()
	{
		text1 = new JTextPane();
		text1.setContentType("text/plain");		
		scroll1 = new JScrollPane(text1);
		add(scroll1);
		
		scroll1.addMouseWheelListener(this);
		scroll1.getVerticalScrollBar().addAdjustmentListener(this);		

		panel = new JPanel();
		label = new JLabel();
		
		panel.add(label);
		add("South",panel);
		
		filename = "";
        
		text1.addMouseListener(this);        
	}
	
	public void initializeData()
	{
		fontsize = 14;
		fileExist = false;

		if(isMenuInJapanese())
		{
		    displayText = inJapanese[0];
			fileString = inJapanese[1];
		}
		else
		{
		    displayText = "Nothing";
			fileString = "FILE NAME :";
		}
	}
	
	public void displayOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
				text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
				text1.setText(displayText);
				text1.setCaretPosition(0);				
				break;
				
			case 991:
				if(fileExist)
//					FILEIO.TextFileWrite2(new File(filename), text1.getText());
                    FILEIO.TextFileWriteCode(new File(filename), text1.getText(), "SHIFT-JIS");
				break;
				
			case 992:
				if(!filename.equals(""))
//					FILEIO.TextFileWrite2(new File(filename), text1.getText());
                    FILEIO.TextFileWriteCode(new File(filename), text1.getText(), "SHIFT-JIS");
				break;				
				
			case 9:
				fontsize--;
				displayOperations(0);
				break;
				
			case 10:
				fontsize++;
				displayOperations(0);
				break;
		}		
	}	
	
	public boolean setData(int dataID, String filename)
	{
        switch(dataID)
        {
            case 10000:
				this.filename = filename;
				
				displayText = fileRead(filename);
				if(displayText.equals(""))
				{
					fileExist = false;
					if(isMenuInJapanese())
						displayText = inJapanese[2];
					else
						displayText = "File NOT found";
				}
				else
					fileExist = true;
				
				displayOperations(0);
				panel.remove(label);
				label = new JLabel(fileString+filename);
				panel.add(label);
				panel.revalidate();
                return true;
        }
        
		return false;
	}
    /*
	public void setData(int dataID, String filename)
	{
		switch(dataID)
		{
			case 47:
				this.filename = filename;
				
				displayText = fileRead(filename);
				if(displayText.equals(""))
				{
					fileExist = false;
					if(isMenuInJapanese())
						displayText = inJapanese[2];
					else
						displayText = "File NOT found";
				}
				else
					fileExist = true;
				
				displayOperations(0);
				panel.remove(label);
				label = new JLabel(fileString+filename);
				panel.add(label);
				panel.revalidate();
				break;
		}
	}
	*/
	public void setFont()
	{
		fontsize = getFontSize();
	}
	
	//AdjustmentListener
	public void adjustmentValueChanged(AdjustmentEvent e)
    {
		writeBarDragLog();
	}
	
	//MouseWheelListener
	public void mouseWheelMoved(MouseWheelEvent mwe)
	{
		double rotation = mwe.getWheelRotation();
		writeWheelLog(rotation);
	}
    
    //Mouse Event
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e)
    {
        writePressLog();
    }
    
    public void mouseReleased(MouseEvent e)			//get selectedText
    {
        writeReleaseLog();
        
        if(text1.getSelectedText() != null)
        {
            setMainFocusString(text1.getSelectedText());
            executeAllByFocus();
        }
    }
}
