

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

package source;

import java.io.*;

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

public class PopUpForProcessing extends JFrame
{
	ProcessingPanel processingPanel;
	Container pane = getContentPane();
	
	String windowName = "PROCESS";
	
	PopUpForProcessing(int X, int Y, int width, int height, String processName, Color barColor, String absolutePath, String fontName, String windowTitle)
	{
		super(windowTitle);
		pane.setLayout(new BorderLayout());
		
		setBounds(X, Y, width, height);
//		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		setAlwaysOnTop(true);
		pane.setBackground(Color.white);
//		setUndecorated(true);
		
		processingPanel = new ProcessingPanel(X, Y, width, height, processName, barColor, absolutePath, fontName);
//		this.addWindowListener(new MyWindowListener());
		pane.add(processingPanel);		
		setVisible(true);
		
		setPercent();
	}
	/*
	class MyWindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) 
		{
			//System.out.println("windowClosing");
			
			settingPanel.closeWindow();
		}
	}*/
	
	public void setPercent()
	{
		processingPanel.setPercent();
	}	
	
	public void setPercent(int percent)
	{
		processingPanel.setPercent(percent);
	}	
}

class ProcessingPanel extends JPanel
{
	int X,Y,width,height;
	
	private int percent;
	int xmargin,ymargin;
	String processName;
	Color barColor;
	String fontName;
	
	String inJapanese[];
	
	ProcessingPanel(int X, int Y, int width, int height, String processName, Color barColor, String absolutePath, String fontName)
	{
		this.X = X;
		this.Y = Y;
		this.width = width;
		this.height = height;
		this.processName = processName;
		this.barColor = barColor;
		this.fontName = fontName;
		
		inJapanese = FILEIO.TextFileAllReadCodeArray(new File(absolutePath+"source"+File.separator+"japaneseText"+File.separator+"JapaneseHints.txt"));		
		
		percent = 10;
		xmargin = 20;
		ymargin = 200;
	}
	
	public void setPercent()
	{
		paintImmediately(0,0,width,height);	
		System.out.println("START PROCESS ------------" + processName);
	}	
	
	public void setPercent(int percent)
	{
		this.percent = percent;
		paintImmediately(0,0,width,height);	
		System.out.print("o");		
//		System.out.println("CONTINUING PROCESS ------------" + processName);
//		repaint();
	}
	
	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);		
		
		drawBackground(g2);		
		drawBar(g2);
		drawMessages(g2);
		drawAdditionalMessages(g2);		
	}
	
	public void drawBackground(Graphics2D g2)
	{		
		g2.setColor(new Color(0,128,255));
		g2.fillRect(0,0, width, height);
	}
					   
	public void drawBar(Graphics2D g2)
	{
		g2.setColor(Color.black);
		g2.fillRect(xmargin/2,ymargin/2 - 50, width-xmargin, height-ymargin);
		
		g2.setColor(barColor);
		g2.fillRect(xmargin/2,ymargin/2 - 50, (int)((width-xmargin)*percent/100), height-ymargin);
	}
	
	public void drawMessages(Graphics2D g2)
	{
		g2.setColor(Color.white);
		g2.setFont(new Font(fontName, Font.BOLD, 14));
		g2.drawString(processName, xmargin/2, 20);
		g2.drawString(percent + " %", width/2, height-ymargin/2 - 20);
	}
	
	public void drawAdditionalMessages(Graphics2D g2)
	{
		int i;
		g2.setColor(Color.white);
		g2.setFont(new Font(fontName, Font.BOLD, 14));
		for(i=0;i<(int)(inJapanese[0].length()/30);i++)
			g2.drawString(inJapanese[0].substring(i*30,(i+1)*30), 20, height-ymargin/2 + 20+20*i);
		g2.drawString(inJapanese[0].substring(i*30,inJapanese[0].length()), 20, height-ymargin/2 + 20+20*i);		
	}	
	
	public void update(Graphics g)		//Avoid from blinking
	{
		paintComponent(g);
	}		
	
}
