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



package module.VisualizationModules.TextDisplayColor;

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.*;
import javax.swing.text.*;

import java.util.regex.*;

import javax.swing.plaf.ComponentUI;

public class TextDisplayColor extends VisualizationModule implements  MouseListener, AdjustmentListener, MouseWheelListener
{
	//Text pane
    NonWrappingTextPane text1;
	JScrollPane scroll1;
    
    JTextPane text2;
    JScrollPane scroll2;

	//for DISPLAY
	String displayText;	//display text
    String displayTextList[];
	
	
	
	//for text DISPLAY
	StyleContext sc;
  	DefaultStyledDocument doc;
	MutableAttributeSet attr[];

	int sentenceAttribute[];
    int wordAttribute[];
    int textAttribute[];
    int textLength[];
	
    //COLOR
    int colorVariation = 23;
    
	//WORD
	String colorWord;
    
    String dictionary[];
    int wordsColor;
    
    boolean selectedData[];
    int sentLength[];
    int startFocusLine, endFocusLine;
    int startPosition, endPosition;
    
    //
    int yellowSentenceNumber;
    int yellowWordNumber;
	
	int fontsize;
    
    boolean focusCancel = false;    /////////////////////
    
    boolean clicked = false;

	public TextDisplayColor()
	{
		setModuleID(2);
		setCategoryID(1);
		setUseMode(0);
        dataNumbers = new int[]{1,3,0,2,    // b,i,d,S
            1,2,0,1,    // bA,iA,dA,SA
            0,0,0};     // bA2,iA2,dA2
		setToolType(3);
	}
    
    
    public class NonWrappingTextPane extends JTextPane
    {
        public NonWrappingTextPane() {
            super();
        }
        
        public NonWrappingTextPane(StyledDocument doc) {
            super(doc);
        }
        
        // Override getScrollableTracksViewportWidth
        // to preserve the full width of the text
        /*
        public boolean getScrollableTracksViewportWidth()
        {
            Component parent = getParent();
            ComponentUI ui = getUI();
            
            return parent != null ? (ui.getPreferredSize(this).width <= parent.getSize().width) : true;
        }
         */
        public boolean getScrollableTracksViewportWidth()
        {
            return false;
        }
    }
    

	public void initializePanel()
	{
//        text1 = new JTextPane();
        text1 = new NonWrappingTextPane();
        text1.addMouseListener(this);
        
        text2 = new JTextPane();
        text2.addMouseListener(this);
        
        scroll1 = new JScrollPane(text1);
        scroll1.addMouseWheelListener(this);
        scroll1.getVerticalScrollBar().addAdjustmentListener(this);
        add(scroll1);
        
        scroll2 = new JScrollPane(text2);
        scroll2.addMouseWheelListener(this);
        scroll2.getVerticalScrollBar().addAdjustmentListener(this);
		
		colorSettings();
	}
	
	public void initializeData()
	{
		fontsize = 14;
		
		if(isMenuInJapanese())
		    displayText = inJapanese[0];
		else
		    displayText = "Nothing";
		
		colorWord = "K";
		sentenceAttribute = new int[text.sentenceNumber];
		wordAttribute = new int[text.wordNumber];
        for(int i=0;i<text.sentenceNumber;i++)
            sentenceAttribute[i] = 0;
        for(int i=0;i<text.wordNumber;i++)
            wordAttribute[i] = 0;
        
        yellowSentenceNumber = 0;
        yellowWordNumber = 0;
	}
	
	public void setFont()
	{
		fontsize = getFontSize();
	}	

	public void displayOperations(int optionNumber)
	{
		switch(optionNumber)
		{
            case 4500:
                if(focusCancel == true)
                    return;
                
                if(selectedData == null)
                {
                    selectedData = getFocusSegments();
                    sentLength = new int[text.segmentNumber];
                    makeDocumentBySelectedSegments();
                }
                else if(selectedData.length == text.segmentNumber)
                {
                    selectedData = getFocusSegments();
                    sentLength = new int[text.segmentNumber];
                    makeDocumentBySelectedSegments();
                }
                else if(selectedData.length == text.sentenceNumber)
                {
                    selectedData = getFocusSentences();
                    sentLength = new int[text.sentenceNumber];
                    makeDocumentBySelectedSentences();
                }
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                
                System.out.println("FOCUS SEGMENTS");
                break;
                
                
            case 0:
                if(focusCancel == true)
                {
                    sentLength = new int[text.sentenceNumber];
                    
                    makeDocumentBySentence();
                    
                    text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                    text1.setDocument(doc);
                    text1.setCaretPosition(0);
                    break;
                }
                
                if(selectedData == null)
                {
                    if(displayTextList != null && sentenceAttribute != null)
                        if(displayTextList.length == sentenceAttribute.length)
                        {
                            makeDocumentByTextList();
                            
                            text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                            text1.setDocument(doc);
                            text1.setCaretPosition(0);
                            break;
                        }

                    
                    sentLength = new int[text.sentenceNumber];
                    
                    selectedData = new boolean[text.sentenceNumber];
                    for(int i=0;i<text.sentenceNumber;i++)
                        selectedData[i] = false;
                    
                    makeDocumentBySentence();
                    
                    //                if(!getMainFocusString().equals(""))
                    //                {
                    //                    colorWord = getMainFocusString();
                    //                    setWordColor();
                    //                }
                    
                    text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                    text1.setDocument(doc);
                    text1.setCaretPosition(0);
                    break;
                }


                if(selectedData.length == text.segmentNumber)
                    makeDocumentBySelectedSegments();
                else if(selectedData.length == text.sentenceNumber)
                    makeDocumentBySelectedSentences();

                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;
                
				
			case 11:
				makeDocumentByWords();
                
				text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));	
				text1.setDocument(doc);
				text1.setCaretPosition(0);
				break;
                
            case 12:
                makeDocumentByWords();
                
                text2.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text2.setDocument(doc);
                text2.setCaretPosition(0);
                break;

			case 1:
				text.fileSave(text1.getText(),2);
				break;
                
            case 2:
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;
                
            case 3:
                text2.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text2.setDocument(doc);
                text2.setCaretPosition(0);
                break;
                
				
			case 9:
				fontsize--;
				displayOperations(0);
				break;
				
			case 10:
				fontsize++;
				displayOperations(0);
				break;
				
			case 25:
				displayText = text1.getText();				
			case 24:
				setWordColor();
                
				text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));				
				text1.setDocument(doc);
				text1.setCaretPosition(0);
				break;
                
            case 30:makeDocumentBySentence(yellowSentenceNumber);
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;
                
            case 31:makeDocumentByWords(yellowSentenceNumber, yellowWordNumber);
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;

            case 35:
                displayText = text1.getText();
                //                setWordsColor();
                if(selectedData.length == text.segmentNumber)
                {
                    makeDocumentByDictionaryToSegments();
                    copyToFocusSegments();
                }
                else
                {
                    makeDocumentByDictionaryToSentences();
                    copyToFocusSentences();
                }
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;
                
            case 36:
                if(selectedData.length == text.segmentNumber)
                {
                    System.out.println("display by segment in TextDisplayColor "+text.segmentNumber);
                    makeDocumentBySelectedSegments();
                    copyToFocusSegments();
                }
                else if(selectedData.length == text.sentenceNumber)
                {
                    System.out.println("display by sentence in TextDisplayColor "+text.sentenceNumber);
                    makeDocumentBySelectedSentences();
                    copyToFocusSentences();
                }
                else
                    return;
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;
                
            case 37:    //KEEP CARET POSITION
                if(selectedData.length == text.segmentNumber)
                {
                    System.out.println("display by segment in TextDisplayColor "+text.segmentNumber);
                    makeDocumentBySelectedSegments();
                    copyToFocusSegments();
                }
                else if(selectedData.length == text.sentenceNumber)
                {
                    System.out.println("display by sentence in TextDisplayColor "+text.sentenceNumber);
                    makeDocumentBySelectedSentences();
                    copyToFocusSentences();
                }
                else
                    return;
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(endPosition);
                break;
                
            case 199:
                createDocumentByTextList();
                break;
                
            case 201:
                createDocumentByTextList();
                
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;                
                
                
            case 300:   //JTextPane
                remove(scroll1);
                add(scroll2);
                revalidate();
                text2.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text2.setDocument(doc);
                text2.setCaretPosition(0);
                break;
                
            case 200:   //NonWrappingTextPane
                remove(scroll2);
                add(scroll1);
                revalidate();
                text1.setFont(new Font(text.getFontName(), Font.BOLD, fontsize));
                text1.setDocument(doc);
                text1.setCaretPosition(0);
                break;
		}		
	}
    
    public void createDocumentByTextList()
    {
        if(displayTextList != null && textAttribute != null)
            if(displayTextList.length == textAttribute.length)
                makeDocumentByTextList();
    }
    
    public boolean setData(int dataID, int n)
    {
        System.out.println("data="+n);
        switch(dataID)
        {
            case 0:yellowSentenceNumber = n;
                return true;
                
            case 1:yellowWordNumber = n;
                return true;
                
            case 2:wordsColor = n;
                return true;
        }
        return false;
    }
    
	public boolean setData(int dataID, int data[])
	{
        switch(dataID)
        {
            case 0:
			    for(int i=0;i<data.length;i++)
                    if(data[i]>=0 && data[i]<colorVariation)
                        sentenceAttribute[i] = data[i];
                    else
                        sentenceAttribute[i] = 0;
                return true;
                
            case 1:
			    for(int i=0;i<data.length;i++)
                    if(data[i]>=0 && data[i]<colorVariation)
                        wordAttribute[i] = data[i];
                    else
                        wordAttribute[i] = 0;
                return true;
                
            case 2:
                textLength = new int[data.length];
                textAttribute = new int[data.length];
                for(int i=0;i<data.length;i++)
                    if(data[i]>=0 && data[i]<colorVariation)
                        textAttribute[i] = data[i];
                    else
                        textAttribute[i] = 0;
                return true;
        }
        
		return false;
	}
    
	public boolean setData(int dataID, String t)
	{
        switch(dataID)
        {
            case 0:		displayText = t;
                return true;
                
            case 1:     colorWord = t;
                return true;
        }
        
		return false;
	}
    
    public boolean setData(int dataID, String data[])
    {
        switch(dataID)
        {
            case 0:
                dictionary = new String[data.length];
                for(int i=0;i<data.length;i++)
                    dictionary[i] = data[i];
                return true;
                
            case 1:
                displayTextList = data;
                return true;
        }
        
        return false;
    }
    
    public boolean setData(int dataID, boolean data[])
    {
        switch(dataID)
        {
            case 0:
                selectedData = new boolean[data.length];
                sentLength = new int[data.length];
                for(int i=0;i<data.length;i++)
                    selectedData[i] = data[i];
                return true;
        }
        
        return false;
    }
    
    public boolean setData(int dataID, boolean data)
    {
        switch(dataID)
        {
            case 0:
                focusCancel = data;
                return true;
        }
        
        return false;
    }
	
	void colorSettings()
	{
		attr = new SimpleAttributeSet[colorVariation];
		for(int i=0;i<attr.length;i++)
			attr[i] = new SimpleAttributeSet();

		Color cl[] = new Color[attr.length];
		cl[0] = new Color(255, 255, 255);
		cl[1] = new Color(255, 255, 55);//4.0
		cl[2] = new Color(220, 220, 0);//3.5
		cl[3] = new Color(205, 205, 0);//3.0
		cl[4] = new Color(190, 190, 0);//2.5
		cl[5] = new Color(155, 155, 0);//2.0
		cl[6] = new Color(120, 120, 0);//1.5
		cl[7] = new Color(110, 110, 0);//1.0
		cl[8] = new Color(100, 100, 0);//0.5
		cl[9] = new Color(50, 50, 0);	//0
		cl[10] = new Color(255, 255, 255);//white
		cl[11] = new Color(0,0,0);		//black
		cl[12] = new Color(255, 255, 0);//yeloow
		cl[13] = new Color(255, 164, 0);//orange
		cl[14] = new Color(160, 82, 45);//brown
		cl[15] = new Color(128, 0, 0);//red-brown
		cl[16] = new Color(190, 190, 0);//2.5
		cl[17] = new Color(176,196,222);//lightsteelblue
		cl[18] = new Color(255, 192, 203);//pink
		cl[19] = new Color(102, 205, 170);//aquamarine
		cl[20] = new Color(50, 205, 50);//limegreen
        cl[21] = new Color(0xFF, 0xDE, 0xAD);
        cl[22] = new Color(0xFF, 0x7F, 0x8E);//red


		for(int i=0;i<attr.length;i++)
			StyleConstants.setBackground(attr[i], cl[i]);
		
		sc = new StyleContext();
	}
    
    ///////////FOR DATAMINING USE
    void copyToFocusSegments()
    {
        setFocusSegments(selectedData);
        
        repaintOthersByFocus();
    }
    
    void copyToFocusSentences()
    {
        setFocusSentences(selectedData);
        
        repaintOthersByFocus();
    }


    public void makeDocumentByDictionaryToSegments()
    {
        selectedData = new boolean[text.segmentNumber];
        sentLength = new int[text.segmentNumber];

        MutableAttributeSet attribute = attr[1];
        if(wordsColor >= 0 && wordsColor <= 20)
            attribute = attr[wordsColor];
        
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        
        String sent;
        for(int i=0;i<text.segmentNumber;i++)
        {
            sent = "["+(text.segment[i].segmentID+1)+"]"+text.segment[i].segmentText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
            sentLength[i] = sent.length();
//            String sent = text.segment[i].segmentText;
 
            selectedData[i] = false;
            int count=0;
            for(int k=0; k<dictionary.length; k++)
            {
                Pattern pattern = Pattern.compile(dictionary[k]);
                Matcher matcher = pattern.matcher(sent);
                
                while(matcher.find())
                {
                    if(count == 0)
                    {
                        try{
                            doc.insertString(offset, sent, attr[1]);
                        }
                        catch (BadLocationException ble){
                            System.err.println("Accessed at illegal position out of the document.");
                        }
                    }
                    String matchstr = matcher.group();
                    doc.setCharacterAttributes(offset+matcher.start(), matchstr.length(), attribute, true);
                    selectedData[i] = true;
                    count++;
                }
            }
            
            if(selectedData[i] == false)
            {
                try{
                    doc.insertString(offset, sent, attr[10]);
                }
                catch (BadLocationException ble){
                    System.err.println("Accessed at illegal position out of the document.");
                }
            }
            offset += sent.length();
        }
    }
    
    public void makeDocumentByDictionaryToSentences()
    {
        selectedData = new boolean[text.sentenceNumber];
        sentLength = new int[text.sentenceNumber];
        
        MutableAttributeSet attribute = attr[1];
        if(wordsColor >= 0 && wordsColor <= 20)
            attribute = attr[wordsColor];
        
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        
        String sent;
        for(int i=0;i<text.sentenceNumber;i++)
        {
            sent = "("+(text.sentence[i].sentenceID+1)+")"+text.sentence[i].sentenceText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
            
            sentLength[i] = sent.length();
            //            String sent = text.segment[i].segmentText;
            
            selectedData[i] = false;
            int count=0;
            for(int k=0; k<dictionary.length; k++)
            {
                Pattern pattern = Pattern.compile(dictionary[k]);
                Matcher matcher = pattern.matcher(sent);
                
                while(matcher.find())
                {
                    if(count == 0)
                    {
                        try{
                            doc.insertString(offset, sent, attr[1]);
                        }
                        catch (BadLocationException ble){
                            System.err.println("Accessed at illegal position out of the document.");
                        }
                    }
                    String matchstr = matcher.group();
                    doc.setCharacterAttributes(offset+matcher.start(), matchstr.length(), attribute, true);
                    selectedData[i] = true;
                    count++;
                }
            }
            
            if(selectedData[i] == false)
            {
                try{
                    doc.insertString(offset, sent, attr[10]);
                }
                catch (BadLocationException ble){
                    System.err.println("Accessed at illegal position out of the document.");
                }
            }
            offset += sent.length();
        }
    }
    
    void makeDocumentBySelectedSegments()
    {
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        try{
            String sent;
            for(int i=0;i<text.segmentNumber;i++)
            {
                sent = "["+(text.segment[i].segmentID+1)+"]"+text.segment[i].segmentText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
                
                sentLength[i] = sent.length();
                if(selectedData[i])
                    doc.insertString(offset, sent, attr[1]);
                else
                    doc.insertString(offset, sent, attr[10]);
                offset += sent.length();
            }
        }
        catch (BadLocationException ble){
            System.err.println("Accessed at illegal position out of the document.");
        }
    }
    
    void makeDocumentByTextList()
    {
        System.out.println("makeDocumentByTextList "+displayTextList.length);
        
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        try{
            for(int i=0;i<displayTextList.length;i++)
            {
                textLength[i] = displayTextList[i].length();
                doc.insertString(offset, displayTextList[i], attr[textAttribute[i]]);
                offset += textLength[i];
            }
        }
        catch (BadLocationException ble){
            System.err.println("Accessed at illegal position out of the document.");
        }
    }
    
    void makeDocumentBySelectedSentences()
    {
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        try{
            String sent;
            for(int i=0;i<text.sentenceNumber;i++)
            {
                sent = "("+(text.sentence[i].sentenceID+1)+")"+text.sentence[i].sentenceText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
                sentLength[i] = sent.length();
                
                if(selectedData[i])
                    doc.insertString(offset, sent, attr[1]);
                else
                    doc.insertString(offset, sent, attr[10]);
                offset += sent.length();
            }
        }
        catch (BadLocationException ble){
            System.err.println("Accessed at illegal position out of the document.");
        }
    }
    
	void makeDocumentBySentence()
	{
		doc = new DefaultStyledDocument(sc);
		int offset = 0;
		try{
            String sent;
			for(int i=0;i<text.sentenceNumber;i++)
			{
                
                if(sentenceAttribute[i] != 10 || clicked == false)
                {
                    sent = "("+(text.sentence[i].sentenceID+1)+")"+text.sentence[i].sentenceText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
                }
                else
                    sent = "";

                sentLength[i] = sent.length();
                
				doc.insertString(offset, sent, attr[sentenceAttribute[i]]);
				offset += sent.length();
//				doc.insertString(offset++, "\n", attr[0]);
			}
            
            if(offset == 0)
            {
                if(isMenuInJapanese())
                    sent = inJapanese[3];
                else
                    sent = "No Matched Sentences";
                
                for(int i=0;i<100;i++)
                    sent += " ";
                sent += System.getProperty("line.separator");
                
                doc.insertString(offset, sent, attr[10]);
                offset += sent.length();
            }
            
		}
		catch (BadLocationException ble){
		    System.err.println("Accessed at illegal position out of the document.");
		}
	}
    /////
    void makeDocumentBySentence(int n)
    {
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        try{
            String sent;
            for(int i=0;i<text.sentenceNumber;i++)
            {
                sent = "("+(text.sentence[i].sentenceID+1)+")"+text.sentence[i].sentenceText.replaceAll(System.getProperty("line.separator"),"")+System.getProperty("line.separator");
                sentLength[i] = sent.length();
                
                if(i == n)
                    doc.insertString(offset, sent, attr[1]);         // HighLight by YELLOW
                else
                    doc.insertString(offset, sent, attr[sentenceAttribute[i]]);
                
                offset += sent.length();
            }
        }
        catch (BadLocationException ble){
            System.err.println("Accessed at illegal position out of the document.");
        }
    }
    
	void makeDocumentByWords()
	{
		doc = new DefaultStyledDocument(sc);
		int offset = 0;
		int wordCount = 0;
		try{
			for(int i=0;i<text.sentenceNumber;i++)
			    for(int j=0;j<text.sentence[i].wordNumber;j++,wordCount++)
				{
				    doc.insertString(offset, text.sentence[i].word[j], attr[wordAttribute[wordCount]]);
				    offset += text.sentence[i].word[j].length();					
				}
		}
		catch (BadLocationException ble){
		    System.err.println("Accessed at illegal position out of the document.");
		}
	}
    void makeDocumentByWords(int m, int n)
    {
        doc = new DefaultStyledDocument(sc);
        int offset = 0;
        int wordCount = 0;
        try{
            for(int i=0;i<text.sentenceNumber;i++)
                for(int j=0;j<text.sentence[i].wordNumber;j++,wordCount++)
                {
                    if(i == m && j == n)
                        doc.insertString(offset, text.sentence[i].word[j], attr[1]);         // HighLight by YELLOW
                    else
                        doc.insertString(offset, text.sentence[i].word[j], attr[wordAttribute[wordCount]]);
                    offset += text.sentence[i].word[j].length();
                }
        }
        catch (BadLocationException ble){
            System.err.println("Accessed at illegal position out of the document.");
        }
    }
	
	public void setWordColor()
	{
		doc = new DefaultStyledDocument(sc);
		int offset = 0, position = displayText.indexOf(colorWord), wordLength = colorWord.length();

		try{
		    doc.insertString(offset, displayText, attr[10]);
		}
		catch (BadLocationException ble){
		    System.err.println("Accessed at illegal position out of the document.");
		}		
		
		while (position != -1) 
		{
			doc.setCharacterAttributes(position, wordLength, attr[1], true);
			position = displayText.indexOf(colorWord, position+wordLength);					
		}
	}
    
    public void setWordsColor()
    {
        doc = new DefaultStyledDocument(sc);
        int offset = 0, position = displayText.indexOf(colorWord), wordLength = colorWord.length();
        
        try{
            doc.insertString(offset, displayText, attr[10]);
        }
        catch (BadLocationException ble){
            System.err.println("Accessed at illegal position out of the document.");
        }
        
        MutableAttributeSet attribute = attr[1];
        if(wordsColor >= 0 && wordsColor <= 20)
            attribute = attr[wordsColor];
        
        for(int k=0; k<dictionary.length; k++)
        {
            Pattern pattern = Pattern.compile(dictionary[k]);
            Matcher matcher = pattern.matcher(displayText);
            
            while(matcher.find())
            {
                String matchstr = matcher.group();
                doc.setCharacterAttributes(matcher.start(), matchstr.length(), attribute, true);
            }
        }
    }
	
	//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)
    {
        writeClickLog();
        
        if(focusCancel == false)
            return;
        
        clicked = !clicked;
        System.out.println("Clicked and ReDisplay");
        displayOperations(0);
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    
    public void mousePressed(MouseEvent e)
    {
        writePressLog();
    
        startPosition = text1.getCaretPosition();
        
        if(selectedData == null)
            return;

        startFocusLine = countLine(startPosition);
    }
    
    public int countLine(int position)
    {
        int total = 0;
        for(int i=0;i<selectedData.length;i++)
        {
            total += sentLength[i];
            if(position < total)
                return i;
        }
        return selectedData.length-1;
    }
    
    public void mouseReleased(MouseEvent e)			//get selectedText
    {
        writeReleaseLog();
        
        if(focusCancel == true)
            return;
        
        endPosition = text1.getCaretPosition();
        
        if(text1.getSelectedText() != null && endPosition - startPosition < 10)
        {
            System.out.println("SET MAIN");
            setMainFocusString(text1.getSelectedText());
            executeAllByFocus();
        }
        
        if(selectedData == null)
            return;
        
        if(selectedData.length == text.segmentNumber || selectedData.length == text.sentenceNumber)
        {
            int btn = e.getButton();
            
            if(btn == MouseEvent.BUTTON3)
            {
                for(int i=0;i<selectedData.length;i++)
                    selectedData[i] = false;
            }
            else
            {
                endFocusLine = countLine(endPosition);
                
                if(endFocusLine == startFocusLine && startPosition != endPosition)
                    return;
            
                for(int i=startFocusLine;i<=endFocusLine;i++)
                    selectedData[i] = !selectedData[i];
            }
            displayOperations(37);
        }
    }
}
