 /*
 * Copyright (C) 7-avr-2004  Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */
package jxtray.swing;

import javax.swing.JPanel;
import javax.swing.BorderFactory;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GradientPaint;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.BorderLayout;

 

public class MemoryViewer extends JPanel implements Runnable
{
	private Thread _thread = null;
	private boolean _stop = true;
	private final int _DELAY = 1000;
	private long _max_use=0;
	private long _current_use=0;
	private long _total=0;
	private Runtime _runTime=null;
	private GradientPaint painter = null;
	private Graphics2D g2 = null;
	private long width =0;
	private long height = 0;
	
	public MemoryViewer	()
	{
		super();
		_runTime=Runtime.getRuntime();
		_total = _runTime.totalMemory();
		this.setLayout(new BorderLayout());
		this.add(new JxtJLabel("Runtime memory viewer"),BorderLayout.NORTH);
		this.setBorder(BorderFactory.createRaisedBevelBorder());
	}
	
	public void start()
	{
		try
		{
			
			if ( _thread == null )
			{
				_stop=false;
				_thread = new Thread(this);
				_thread.start();
				this.setEnabled(true);
			}
		}
		catch ( Exception e )
		{
			_stop=true;
		}
	}
	
	
	public void stop()
	{
		try
		{
			_stop=true;
			_thread.stop();
			_thread = null;
		}
		catch ( Exception e )
		{
			_thread=null;
		}
		
		this.setEnabled(false);
	}
	
	public void run()
	{
		try
		{
			width = this.getWidth();
			height = this.getHeight();
			painter = new GradientPaint(0, 0,new Color(54,80,125),width, height,new Color(149,165,193));
			//painter = new GradientPaint(0, 0,Color.RED,width, height, Color.WHITE);

			while ( !_stop )
			{
				_current_use =_total  - _runTime.freeMemory();
				if ( _current_use > _max_use )
					_max_use = _current_use;
				this.repaint();
				Thread.sleep(_DELAY);
			}
		}
		catch ( Exception e )
		{
		}
	}
	
	public void paint(Graphics g)
	{
		if ( !_stop)
		{
			g2 = (Graphics2D) g;
			g2.setPaint(painter);
			g2.fill(new Rectangle(0,0,(int)width,(int)height));
		}
		else
		{
			g.setColor(Color.LIGHT_GRAY);
			g.fillRect(0,00,this.getWidth(),this.getHeight());
		}
		
			g.setColor(Color.WHITE);
			g.drawString("Runtime memory viewer",1,20);
			g.drawString(_current_use+" used",1,40);
			g.drawString(_max_use+" max used",1,60);
			g.drawString(_total+" total",1,80);
			
	}
}
