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

package module.VisualizationModules.TextDisplayHtml;

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

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

import java.io.*;

public class TextDisplayHtml extends TextDisplay
{
    StringWriter sw;
    BufferedWriter bw;
    
	public TextDisplayHtml()
	{
		setModuleID(3);
		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
			setToolType(2);		
	}
	
	public void initializePanel()
	{
		text1 = new JTextPane();
		text1.setContentType("text/html");
		scroll1 = new JScrollPane(text1);
		add(scroll1);
		
		scroll1.addMouseWheelListener(this);
		scroll1.getVerticalScrollBar().addAdjustmentListener(this);
        
        text1.addMouseListener(this);
	}
	
	public void displayOperations(int optionNumber)
	{
	
		
		switch(optionNumber)
		{
			case 0:
                if(checkDataNumbers() == false)
                    return;
                
				text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
				
				if(displayText == null)
					return;
                
                sw = new StringWriter();            //For words
				bw = new BufferedWriter(sw);
                
				try{
					bw.write("<style type=text/css> <!-- BODY, TH, TD { font: bold; font-size: ");
					bw.write(Integer.toString(fontsize));
//					bw.write("pt; font-family: sans-serif;} -->");
					bw.write("pt; font-family: "+text.getFontName()+";} -->");					
					bw.write(displayText);
					bw.write("</style>");
					bw.flush();
				}
				catch(Exception e){
					System.out.println("writing ERROR in TextDisplayHtml-A");
                    e.printStackTrace();                    
				}
				
				try{
					text1.setText(sw.toString());
					text1.setCaretPosition(0);
				}
				catch(Exception e){
					System.out.println("DISPLAY FAILURE in TextDisplayHtml");
                    e.printStackTrace();                    
				}				
				break;
				
			case 1:
				System.out.println("Option was changed from 1 to 7!!");
				
			case 7:
                if(checkDataNumbers() == false)
                    return;
                
                sw = new StringWriter();            //For words
                bw = new BufferedWriter(sw);
                
				text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
				
				try{
					bw.write("<style type=text/css> <!-- BODY, TH, TD { font: bold; font-size: ");
					bw.write(Integer.toString(fontsize));
//					bw.write("pt; font-family: sans-serif;} -->");
					bw.write("pt; font-family: "+text.getFontName()+";} -->");					
					bw.write(displayText);
					bw.write("</style>");
					bw.flush();
				}
				catch(Exception e){
					System.out.println("writing ERROR in TextDisplayHtml-B");
                    e.printStackTrace();                    
				}				
				
				text1.setText(sw.toString());
				break;
				
			case 8:
				text1.setText(displayText);
				break;				
		}		
	}	
}
