
import java.awt.Color;
import java.text.DecimalFormat;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BatteryPanel.java
 *
 * Created on 24-08-2011, 07:39:03 PM
 */

/**
 * Clase que crea el panel de informacion de la bateria
 * @author Manuel
 */
public class BatteryPanel extends javax.swing.JPanel {

    /** Crea panel de bateria */
    public BatteryPanel() {
        initComponents();
    }

    /** Metodo llamado por el constructor para inicializar el panel.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TitleText = new javax.swing.JLabel();
        StateLabel = new javax.swing.JLabel();
        StateText = new javax.swing.JLabel();
        PowerLabel = new javax.swing.JLabel();
        PowerText = new javax.swing.JLabel();
        PowerPercentLabel = new javax.swing.JLabel();
        PowerPercentText = new javax.swing.JLabel();
        BatteryLevel = new javax.swing.JProgressBar();

        setBackground(new java.awt.Color(255, 255, 255));
        setMaximumSize(new java.awt.Dimension(260, 130));
        setMinimumSize(new java.awt.Dimension(260, 130));
        setPreferredSize(new java.awt.Dimension(260, 130));
        setLayout(null);

        TitleText.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        TitleText.setText("Baterías");
        add(TitleText);
        TitleText.setBounds(80, 10, 80, 22);

        StateLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        StateLabel.setText("Estado Bateria:");
        add(StateLabel);
        StateLabel.setBounds(10, 92, 82, 15);

        StateText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        StateText.setForeground(new java.awt.Color(0, 140, 0));
        StateText.setText("Conectada");
        StateText.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        StateText.setMaximumSize(new java.awt.Dimension(70, 14));
        add(StateText);
        StateText.setBounds(130, 92, 80, 15);

        PowerLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        PowerLabel.setText("Potencia Entregada:");
        add(PowerLabel);
        PowerLabel.setBounds(10, 50, 111, 15);

        PowerText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        PowerText.setText("0.0 [W]");
        add(PowerText);
        PowerText.setBounds(130, 50, 70, 15);

        PowerPercentLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        PowerPercentLabel.setText("% Potencia Total:");
        add(PowerPercentLabel);
        PowerPercentLabel.setBounds(10, 71, 99, 15);

        PowerPercentText.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        PowerPercentText.setText("0.0 %");
        add(PowerPercentText);
        PowerPercentText.setBounds(130, 71, 70, 15);

        BatteryLevel.setForeground(new java.awt.Color(48, 184, 3));
        BatteryLevel.setOrientation(1);
        BatteryLevel.setValue(80);
        BatteryLevel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        BatteryLevel.setMaximumSize(new java.awt.Dimension(17, 10));
        BatteryLevel.setName("BatteryLevel"); // NOI18N
        BatteryLevel.setPreferredSize(new java.awt.Dimension(17, 10));
        BatteryLevel.setRequestFocusEnabled(false);
        BatteryLevel.setStringPainted(true);
        add(BatteryLevel);
        BatteryLevel.setBounds(220, 20, 30, 90);
    }// </editor-fold>//GEN-END:initComponents

	/**
	* Actualiza valores mostrados por pantalla.
	* @param power Potencia entregada o recibida por la bateria. 
	* @param percent Porcentaje de potencia entregado por la bateria.
	* @param conected Estado de conexion de bateria.
	* @param level Nivel de carga bateria.
	*/	
	public void updateValues(float power, float percent, boolean conected, int level) {
        // Estado
        if (conected == false) {
            StateText.setText("Desconectada");
            StateText.setForeground(Color.red);
        }
        else {
            StateText.setText("Conectada");
            StateText.setForeground(new java.awt.Color(0, 140, 0)); //green
        }

        // Nivel de carga
        BatteryLevel.setValue(level);

        // Potencia
        if (power < 0) {
            PowerLabel.setText("Potencia Recibida: ");
            PowerText.setForeground(new java.awt.Color(0, 140, 0)); //green
        }
        else {
            PowerLabel.setText("Potencia Entregada:");
            PowerText.setForeground(Color.BLACK);
        }
        PowerText.setText( Math.abs(power) + " [W]");

        // Porcentaje potencia
        if (power > 0) {
            DecimalFormat format = new DecimalFormat("###.00");
            PowerPercentText.setText( format.format(percent) + " %");
        }
        else
            PowerPercentText.setText( 0.0f + " %");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar BatteryLevel;
    private javax.swing.JLabel PowerLabel;
    private javax.swing.JLabel PowerPercentLabel;
    private javax.swing.JLabel PowerPercentText;
    private javax.swing.JLabel PowerText;
    private javax.swing.JLabel StateLabel;
    private javax.swing.JLabel StateText;
    private javax.swing.JLabel TitleText;
    // End of variables declaration//GEN-END:variables

    
}
