

//
// Core Program for TETDM 
// PopUpForShrinkInformation.java Version 1.00
// Copyright(C) 2018- TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//

package 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 javax.swing.plaf.ComponentUI;


public class PopUpForShrinkInformation extends JFrame
{
    public ShrinkInformationPanel shrinkInformationPanel;
    Container pane = getContentPane();

    String absolutePath;
    String inJapanese[];

    Control control;
    
        public JButton invertion;
    public JButton undo, redo;
    

    
    PopUpForShrinkInformation(int X, int Y, int width, int height, int fontSize, String fontName, String title, String absolutePath, Control control)
    {
        super(title);
        pane.setLayout(new BorderLayout());
        
        this.addWindowListener(new MyWindowListener());
        
        this.control = control;
        this.absolutePath = absolutePath;
        readJapanese();
        
        setBounds(X, Y, width, height);
        setBackground(new Color(0xff, 0xf8, 0xdc));
        setAlwaysOnTop(true);

        shrinkInformationPanel = new ShrinkInformationPanel(fontSize, fontName, control.sp1.inJapanese);
        pane.add(shrinkInformationPanel);
        setVisible(true);
    }
    
    public void readJapanese()
    {
        String filename = absolutePath+"source"+File.separator+"japaneseText"+File.separator+"shrinkJapanese.txt";
        inJapanese = FILEIO.TextFileAllReadCodeArray(new File(filename));
    }
    
    void resetPanel()
    {
        shrinkInformationPanel.resetPanel();
    }
    
    void moveY(int y)
    {
         setBounds(getX(), getY()+y, getWidth(), getHeight());
    }
    
    void reSize(int baseHeight, int shrinkCount, int fontSize)
    {
        int x = getWidth();
        setSize(x, baseHeight+(shrinkCount+1)*fontSize);
    }
    
    public boolean reviseData(String toolName, String condition, int objectType, String example, int shrunkNumber, int maxDataNumber, ShrinkData shrinkData[], int shrinkCount)
    {
        String objectWord="";
        
        if(objectType == 1)//Segment
            objectWord = inJapanese[2];
        else if(objectType == 2)//Segment including Sentences
            objectWord = inJapanese[3];
        else if(objectType == 3)//Segment including Keywords
            objectWord = inJapanese[4];
        
        shrinkData[shrinkCount] = new ShrinkData(toolName, condition, objectWord, example, shrunkNumber, control.text.focus.getFocusSegments());
        shrinkInformationPanel.createPanel(shrinkData, shrinkCount, true);
        
        
        if(shrunkNumber == 0)
            return false;
        
        if(shrinkCount > 0)
        {
            if(shrinkData[shrinkCount-1].shrunkNumber == shrunkNumber)
                return false;
            else
                return true;
        }
        else if(shrunkNumber == maxDataNumber)
            return false;
        else
            return true;
    }
    
    public void reviseInvertionData(int shrunkNumber, ShrinkData shrinkData[], int shrinkCount)
    {
        shrinkData[shrinkCount].createInvertionData(shrunkNumber, control.text.focus.getFocusSegments());
        shrinkInformationPanel.createPanel(shrinkData, shrinkCount, true);
    }
    
    public void recallData(ShrinkData shrinkData[], int shrinkCount)
    {
        shrinkInformationPanel.createPanel(shrinkData, shrinkCount, true);
    }
    
    class ShrinkInformationPanel extends JPanel implements ActionListener
    {
        JTextArea text1;
        JPanel southPanel;
//        public JButton invertion;
        
        ShrinkInformationPanel(int fontSize, String fontName, boolean japanese)
        {
            setLayout(new BorderLayout());
            

            setBackground(new Color(0xff, 0xf8, 0xdc));
            text1 = new JTextArea();

            text1.setFont(new Font(fontName, Font.BOLD, fontSize));
            text1.setForeground(Color.black);
            text1.setBackground(new Color(0xff, 0xf8, 0xdc));
            text1.setEditable(false);
            text1.setCaretPosition(0);
            add(text1);
            
            southPanel = new JPanel(new GridLayout(1,3));
            
            if(japanese)
                invertion = new JButton(inJapanese[9]);
            else
                invertion = new JButton("focus invertion");
            invertion.setBackground(Color.YELLOW);
            invertion.setOpaque(true);
            invertion.addActionListener(this);
            
            undo = new JButton("<<");
            undo.setBackground(Color.YELLOW);
            undo.setOpaque(true);
            undo.addActionListener(this);
            
            redo = new JButton(">>");
            redo.setBackground(Color.YELLOW);
            redo.setOpaque(true);
            redo.addActionListener(this);
            
            southPanel.add(undo);
            southPanel.add(redo);
            southPanel.add(invertion);
            
            add(southPanel, BorderLayout.SOUTH);
            
            undo.setEnabled(false);
            redo.setEnabled(false);
            invertion.setEnabled(false);
        }
        

        
        void createPanel(ShrinkData shrinkData[], int shrinkCount, boolean japanese)
        {
            String displayMessage = "";
            String space = inJapanese[0];
            
            if(japanese == true)
            {
                for(int i=0;i<shrinkCount;i++)
                    displayMessage += shrinkData[i].message.replace(" ", space)+System.getProperty("line.separator");
                displayMessage += inJapanese[1]+" ";
                displayMessage += shrinkData[shrinkCount].message.replace(" ", space);
            }
            else
            {
                for(int i=0;i<shrinkCount;i++)
                    displayMessage += shrinkData[i].message+System.getProperty("line.separator");
                displayMessage += "-> ";
                displayMessage += shrinkData[shrinkCount].message;
            }
            
            text1.setText(displayMessage);
        }
        
        void resetPanel()
        {
            text1.setText("");
        }

        //Action listener
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == invertion)
            {
                control.focus.setInvertionCondition();
                control.setDisplayAllByFocus();
                control.resetSelectPanelDataByFocusAll();
            }
            
            if(e.getSource() == undo)
            {
                if(control.focus.shrinkCount == 0)
                    return;
                control.focus.shrinkCount--;
                if(control.focus.shrinkCount == 0)
                    undo.setEnabled(false);
                redo.setEnabled(true);
                
                control.focus.setCondition();
                
//                control.setDisplayAllByFocus();
//                control.resetSelectPanelDataByFocusAll();
            }
            
            if(e.getSource() == redo)
            {
                if(control.focus.shrinkCount == control.focus.shrinkCurrentMax)
                    return;
                control.focus.shrinkCount++;
                if(control.focus.shrinkCount == control.focus.shrinkCurrentMax)
                    redo.setEnabled(false);
                undo.setEnabled(true);
                
                control.focus.setCondition();
                
//                control.setDisplayAllByFocus();
//                control.resetSelectPanelDataByFocusAll();
            }
        }
    
    }
    
    
    
    
    class MyWindowListener extends WindowAdapter {
        
        public void windowClosing(WindowEvent e) // x button
        {
            control.btd3.setSelected(false);
        }
        
        public void windowClosed(WindowEvent e)
        {
            control.btd3.setSelected(false);
        }
    }
}





