jdefects.view.dialogs.ChartDisplayDialog.java Source code

Java tutorial

Introduction

Here is the source code for jdefects.view.dialogs.ChartDisplayDialog.java

Source

/* 
 * JDefects - A program to create, manage and display a defects of the personal projects; this program is based on XML files.
 * Copyright (C) 2017 Marco Magliano
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
package jdefects.view.dialogs;

import java.util.HashMap;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import jdefects.LanguageContent;
import jdefects.RepresentationConstants;
import static jdefects.TrackerConstants.EMPTY_STRING;
import jdefects.model.DefectStatus;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * Represent a dialog displaying a chart of the status the all defect stored.
 * 
 * @author Marco Magliano
 */
public class ChartDisplayDialog extends JDialog {

    /* chart panel */
    private JPanel chartPanel;
    /* hashmap dataset */
    private HashMap map;

    /**
     * Creates a dialog representation.
     * 
     * @param parentFrame
     *          dialog's parent frame
     * @param map
     *          the hash map for generates the dataset 
     */
    public ChartDisplayDialog(JFrame parentFrame, HashMap map) {
        super(parentFrame);
        this.map = map;
        this.chartPanel = createChartPanel();

        this.setContentPane(chartPanel);
        this.setModal(true);
        this.setResizable(false);
        this.setTitle(LanguageContent.chart_dialog_title);
        this.setPreferredSize(RepresentationConstants.CHART_DIALOG_SIZE);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }

    /**
     * Generates a panel with the chart
     * @return 
     *      the chart's panel
     */
    private JPanel createChartPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }

    /**
     * Generate the pie chart using JFreeChart library
     * @param dataset
     *         dataset to display in the chart 
     * @return 
     *         the pie chart
     */
    private JFreeChart createChart(PieDataset dataset) {
        JFreeChart chart = ChartFactory.createPieChart(LanguageContent.chart_title, dataset, true, true, false);
        return chart;
    }

    /**
     * Generate the data set for the chart
     * @return 
     *      the dataset generated 
     */
    private PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        String label = EMPTY_STRING;
        for (DefectStatus status : DefectStatus.values()) {
            if (status.equals(DefectStatus.OPEN)) {
                label = LanguageContent.chart_status_labels[1];
            } else if (status.equals(DefectStatus.REOPENED)) {
                label = LanguageContent.chart_status_labels[2];
            } else if (status.equals(DefectStatus.IN_PROGRESS)) {
                label = LanguageContent.chart_status_labels[3];
            } else if (status.equals(DefectStatus.ANALYSIS)) {
                label = LanguageContent.chart_status_labels[4];
            } else if (status.equals(DefectStatus.DEVELOPMENT)) {
                label = LanguageContent.chart_status_labels[5];
            } else if (status.equals(DefectStatus.PRE_INTEGRATION)) {
                label = LanguageContent.chart_status_labels[6];
            } else if (status.equals(DefectStatus.INTEGRATED)) {
                label = LanguageContent.chart_status_labels[7];
            } else if (status.equals(DefectStatus.VALIDATION)) {
                label = LanguageContent.chart_status_labels[8];
            } else if (status.equals(DefectStatus.DEFECT_MONITORING)) {
                label = LanguageContent.chart_status_labels[9];
            } else if (status.equals(DefectStatus.CLOSED)) {
                label = LanguageContent.chart_status_labels[10];
            } else { //dafault: UNKNOWN
                label = LanguageContent.chart_status_labels[0];
            }
            dataset.setValue(label, new Integer((int) map.get(status.name())));
        }
        return dataset;
    }

}