package com.tagtraum.perf.gcviewer;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.text.NumberFormat;
import java.util.ResourceBundle;

/**
 *
 * Date: Feb 3, 2002
 * Time: 9:58:00 PM
 * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
 * @version $Id: $
 */
public class ModelPanel extends JPanel {

    private static ResourceBundle localStrings = ResourceBundle.getBundle("com.tagtraum.perf.gcviewer.localStrings");

    private GCModel model;
    private JLabel avgPauseValue;
    private JLabel minPauseValue;
    private JLabel maxPauseValue;
    private JLabel accumPauseValue;
    private JLabel footprintValue;
    private JLabel throughputValue;
    private JLabel totalTimeValue;
    private JLabel freedMemoryValue;
    private JLabel freedMemoryPerMinValue;
    private NumberFormat pauseFormatter;
    private NumberFormat totalTimeFormatter;
    private NumberFormat throughputFormatter;
    private NumberFormat footprintFormatter;
    private NumberFormat freedMemoryPerMinFormatter;

    public ModelPanel() {
        setBorder(new TitledBorder(localStrings.getString("data_panel_title")));
        model = new GCModel();

        pauseFormatter = NumberFormat.getInstance();
        pauseFormatter.setMaximumFractionDigits(5);

        totalTimeFormatter = NumberFormat.getInstance();
        totalTimeFormatter.setMaximumFractionDigits(2);

        throughputFormatter = NumberFormat.getInstance();
        throughputFormatter.setMaximumFractionDigits(2);

        footprintFormatter = NumberFormat.getInstance();
        footprintFormatter.setMaximumFractionDigits(0);

        freedMemoryPerMinFormatter = NumberFormat.getInstance();
        freedMemoryPerMinFormatter.setMaximumFractionDigits(0);

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(0, 3, 0, 3);
        setLayout(layout);

        JLabel accumPauseLabel = new JLabel(localStrings.getString("data_panel_acc_pauses"));
        constraints.gridy = 0;
        constraints.gridx = 0;
        layout.setConstraints(accumPauseLabel, constraints);
        add(accumPauseLabel);
        accumPauseValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(accumPauseValue, constraints);
        add(accumPauseValue);

        JLabel avgPauseLabel = new JLabel(localStrings.getString("data_panel_avg_pause"));
        constraints.gridy = 1;
        constraints.gridx = 0;
        layout.setConstraints(avgPauseLabel, constraints);
        add(avgPauseLabel);
        avgPauseValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(avgPauseValue, constraints);
        add(avgPauseValue);

        JLabel minPauseLabel = new JLabel(localStrings.getString("data_panel_min_pause"));
        constraints.gridy = 2;
        constraints.gridx = 0;
        layout.setConstraints(minPauseLabel, constraints);
        add(minPauseLabel);
        minPauseValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(minPauseValue, constraints);
        add(minPauseValue);

        JLabel maxPauseLabel = new JLabel(localStrings.getString("data_panel_max_pause"));
        constraints.gridy = 3;
        constraints.gridx = 0;
        layout.setConstraints(maxPauseLabel, constraints);
        add(maxPauseLabel);
        maxPauseValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(maxPauseValue, constraints);
        add(maxPauseValue);

        JLabel totalTimeLabel = new JLabel(localStrings.getString("data_panel_total_time"));
        constraints.gridy = 4;
        constraints.gridx = 0;
        layout.setConstraints(totalTimeLabel, constraints);
        add(totalTimeLabel);
        totalTimeValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(totalTimeValue, constraints);
        add(totalTimeValue);

        JLabel footprintLabel = new JLabel(localStrings.getString("data_panel_footprint"));
        constraints.gridy = 5;
        constraints.gridx = 0;
        layout.setConstraints(footprintLabel, constraints);
        add(footprintLabel);
        footprintValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(footprintValue, constraints);
        add(footprintValue);

        JLabel freedMemoryLabel = new JLabel(localStrings.getString("data_panel_freedmemory"));
        constraints.gridy = 6;
        constraints.gridx = 0;
        layout.setConstraints(freedMemoryLabel, constraints);
        add(freedMemoryLabel);
        freedMemoryValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(freedMemoryValue, constraints);
        add(freedMemoryValue);

        JLabel freedMemoryPerMinLabel = new JLabel(localStrings.getString("data_panel_freedmemorypermin"));
        constraints.gridy = 7;
        constraints.gridx = 0;
        layout.setConstraints(freedMemoryPerMinLabel, constraints);
        add(freedMemoryPerMinLabel);
        freedMemoryPerMinValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(freedMemoryPerMinValue, constraints);
        add(freedMemoryPerMinValue);

        JLabel throughputLabel = new JLabel(localStrings.getString("data_panel_throughput"));
        constraints.gridy = 8;
        constraints.gridx = 0;
        layout.setConstraints(throughputLabel, constraints);
        add(throughputLabel);
        throughputValue = new JLabel();
        constraints.gridx = 1;
        layout.setConstraints(throughputValue, constraints);
        add(throughputValue);
    }

    public void setModel(GCModel model) {
        this.model = model;
        avgPauseValue.setText(pauseFormatter.format(model.getAvgPause()) + "s");
        minPauseValue.setText(pauseFormatter.format(model.getMinPause()) + "s");
        maxPauseValue.setText(pauseFormatter.format(model.getMaxPause()) + "s");
        accumPauseValue.setText(totalTimeFormatter.format(model.getPauseTime()) + "s");
        footprintValue.setText(footprintFormatter.format(model.getFootprint()) + "K");
        freedMemoryValue.setText(footprintFormatter.format(model.getFreedMemory()) + "K");
        if (model.hasCorrectTimestamp()) {
            throughputValue.setText(throughputFormatter.format(model.getThroughput()) + "%");
            totalTimeValue.setText(totalTimeFormatter.format(model.getRunningTime()) + "s");
            freedMemoryPerMinValue.setText(freedMemoryPerMinFormatter.format(model.getFreedMemory()/model.getRunningTime()*60.0) + "K/min");
        } else {
            throughputValue.setText("n.a.");
            totalTimeValue.setText("n.a.");
            freedMemoryPerMinValue.setText("n.a.");
        }
        repaint();
    }
}
