/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.utils.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.text.DecimalFormat;

import javax.swing.JComponent;


/**
 * This class is only for tests. It draw a gauge-like of RAM
 * @author chm
 *
 */
public class RamViewer extends JComponent {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1L;
    //public static void main(String[] args) {}
    private Runner currentRunner = null;
    
    public RamViewer() {
        super();
        initComponents();
    }
    
    protected void initComponents() {
        setPreferredSize(new Dimension(100,20));
        currentRunner = new Runner(this);
        currentRunner.start();
    }
    
    
//    public void setVisible(boolean aFlag) {
//        super.setVisible(aFlag);
//        System.out.println("setVisible("+Boolean.toString(aFlag)+")");
//        if(aFlag) {
//            currentRunner = new Runner(this);
//            currentRunner.start();
//        } else {
//            if(currentRunner!=null) {
//                currentRunner.stopIt();
//                currentRunner = null;
//            }
//        }
//    }
    public void paint(Graphics g) {
        if(!isVisible()) return;
        int width = getSize().width;
        int height = getSize().height;
//        Image offscreenImg = createImage(width,height);
        Graphics offG = g;
        if(g==null) return;
        long freeRam = Runtime.getRuntime().freeMemory();
        long totalRam = Runtime.getRuntime().totalMemory();
        //long maxRam = Runtime.getRuntime().maxMemory();
        
        int freeWidth = (int)(width * freeRam / totalRam );
        int totalWidth = /*(int)(width * totalRam / maxRam );*/width-freeWidth;
        offG.setColor(getParent().getBackground());
        offG.fillRect(0,0,width,height);
        offG.setColor(new Color(0xCC0000));
        offG.fillRect(0,0,totalWidth,height);
        offG.setColor(new Color(0x00CC00));
        offG.fillRect(totalWidth,0,freeWidth,height);
        Font f = new Font("Arial",Font.BOLD,12);
        FontMetrics fm = getFontMetrics(f);
        offG.setFont(f);
        DecimalFormat df = new DecimalFormat("0.0' MB'");
        String s = df.format((totalRam-freeRam)/1024.0/1024.0)+" / "+df.format(totalRam/1024.0/1024.0);
        offG.setColor(Color.WHITE);
        offG.drawString(s,(width-fm.stringWidth(s))/2,(int)(height*0.8));
        offG.setColor(Color.BLACK);
        offG.drawRect(0,0,width-1,height-1);
//        g.drawImage(offscreenImg, 0, 0, this);
    }
    protected class Runner extends Thread {
        private RamViewer viewer=null;
        private boolean stop=false;
        public Runner(RamViewer viewer) {
            super();
            this.viewer=viewer;
        }
        @Override
        public void run() {
            setPriority(Thread.MAX_PRIORITY);
            while(!stop) {
                viewer.paint(viewer.getGraphics());
//                System.out.println("paint");
                try { sleep(1000) ; } catch(Throwable t) {
//                    System.out.println("interrupted");
                }
            }
        }
        public void stopIt() { stop=true; }
    }
}
