gg.view.categoriesbalances.GraphCategoriesBalancesTopComponent.java Source code

Java tutorial

Introduction

Here is the source code for gg.view.categoriesbalances.GraphCategoriesBalancesTopComponent.java

Source

/*
 * GraphCategoriesBalancesTopComponent.java
 *
 * Copyright (C) 2009 Francois Duchemin
 *
 * This file is part of GrisbiGraphs.
 *
 * GrisbiGraphs 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.
 *
 * GrisbiGraphs 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 GrisbiGraphs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gg.view.categoriesbalances;

import gg.db.datamodel.SearchCriteria;
import gg.db.entities.Category;
import gg.utilities.Utilities;
import gg.wallet.Wallet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.ShapeUtilities;
import org.openide.util.LookupEvent;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;

/**
 * Top component which displays a graph showing the categories/sub-categories balances evolution over time.
 */
@ConvertAsProperties(dtd = "-//gg.view.categoriesbalances//GraphCategoriesBalances//EN", autostore = false)
public final class GraphCategoriesBalancesTopComponent extends TopComponent implements LookupListener {

    /** Singleton instance of the topcomponent */
    private static GraphCategoriesBalancesTopComponent instance;
    /** Path to the icon used by the component and its open action */
    private static final String ICON_PATH = "gg/resources/icons/GraphCategoriesBalances.png";
    /** ID of the component */
    private static final String PREFERRED_ID = "GraphCategoriesBalancesTopComponent";
    /** Result for the lookup listener */
    private Lookup.Result result = null;
    /** Logger */
    private Logger log = Logger.getLogger(this.getClass().getName());

    /** Creates a new instance of GraphCategoriesBalancesTopComponent */
    public GraphCategoriesBalancesTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(GraphCategoriesBalancesTopComponent.class,
                "CTL_GraphCategoriesBalancesTopComponent"));
        setToolTipText(NbBundle.getMessage(GraphCategoriesBalancesTopComponent.class,
                "HINT_GraphCategoriesBalancesTopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelCategoriesBalances = new javax.swing.JPanel();

        jPanelCategoriesBalances.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        jPanelCategoriesBalances, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanelCategoriesBalances, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE));
    }// </editor-fold>//GEN-END:initComponents
     // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JPanel jPanelCategoriesBalances;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     * @return Default instance
     */
    public static synchronized GraphCategoriesBalancesTopComponent getDefault() {
        if (instance == null) {
            instance = new GraphCategoriesBalancesTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the GraphCategoriesBalancesTopComponent instance. Never call {@link #getDefault} directly!
     * @return GraphCategoriesBalancesTopComponent instance
     */
    public static synchronized GraphCategoriesBalancesTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(GraphCategoriesBalancesTopComponent.class.getName()).warning("Cannot find "
                    + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof GraphCategoriesBalancesTopComponent) {
            return (GraphCategoriesBalancesTopComponent) win;
        }
        Logger.getLogger(GraphCategoriesBalancesTopComponent.class.getName())
                .warning("There seem to be multiple components with the '" + PREFERRED_ID
                        + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    /**
     * Gets the persistence type
     * @return Persistence type
     */
    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    /**
     * Saves properties
     * @param p Properties to save
     */
    public void writeProperties(java.util.Properties p) {
        p.setProperty("version", "1.0");
    }

    /**
     * Reads properties
     * @param p properties to save
     * @return TopComponent with loaded properties
     */
    public Object readProperties(java.util.Properties p) {
        GraphCategoriesBalancesTopComponent singleton = GraphCategoriesBalancesTopComponent.getDefault();
        singleton.readPropertiesImpl(p);
        return singleton;
    }

    /**
     * Reads properties
     * @param p Properties to read
     */
    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
    }

    /**
     * Gets the topcomponent's ID
     * @return Topcomponent's ID
     */
    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    /**
     * Registers a lookup listener on the Categories' balances table topcomponent
     * when the topcomponent is activated so the updating the table automatically updates the graph
     */
    @Override
    public void componentOpened() {
        if (result == null) {
            // Register lookup listener on the categories' balance table top component
            result = WindowManager.getDefault().findTopComponent("CategoriesBalancesTopComponent").getLookup()
                    .lookupResult(Map.class);
            result.addLookupListener(this);
            result.allInstances();

            // Display the graph
            resultChanged(null);
        }
    }

    /** Unregisters the lookup listener when the topcomponent is closed */
    @Override
    public void componentClosed() {
        if (result != null) {
            result.removeLookupListener(this);
            result = null;
        }
    }

    /** Called when the lookup content is changed (content of table changed)*/
    @Override
    public void resultChanged(LookupEvent ev) {
        Collection instances = result.allInstances();
        if (!instances.isEmpty()) {
            @SuppressWarnings("unchecked")
            Map<Long, Map<SearchCriteria, BigDecimal>> balances = (Map<Long, Map<SearchCriteria, BigDecimal>>) instances
                    .iterator().next();
            displayData(balances);
        }
    }

    /**
     * Displays the categories/sub-categories' balances by period
     * @param balances Categories' balances
     */
    private void displayData(Map<Long, Map<SearchCriteria, BigDecimal>> balances) {
        log.info("Categories' balances graph computed and displayed");

        // Display hourglass cursor
        Utilities.changeCursorWaitStatus(true);

        // Create the dataset (that will contain the categories/sub-categories' balances)
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        // Create an empty chart
        JFreeChart chart = ChartFactory.createLineChart("", // chart title
                "", // x axis label
                NbBundle.getMessage(GraphCategoriesBalancesTopComponent.class,
                        "GraphAccountsBalancesTopComponent.Amount"), // y axis label
                dataset, // data displayed in the chart
                PlotOrientation.VERTICAL, true, // include legend
                true, // tooltips
                false // urls
        );

        // Chart color
        chart.setBackgroundPaint(jPanelCategoriesBalances.getBackground());
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);

        // Grid lines
        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);

        // Set the orientation of the categories on the domain axis (X axis)
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

        // Add the series on the chart for each category/sub-category displayed in the table
        for (Long categoryId : balances.keySet()) {
            Category category = Wallet.getInstance().getCategoriesWithId().get(categoryId);
            assert (category != null);

            SortedSet<SearchCriteria> sortedSearchCriteria = new TreeSet<SearchCriteria>(
                    balances.get(categoryId).keySet());
            for (SearchCriteria searchCriteria : sortedSearchCriteria) {

                if ((!searchCriteria.hasCategoriesFilter() && category.isTopCategory()
                        && !category.getSystemProperty()) || searchCriteria.getCategories().contains(category)) {

                    BigDecimal balance = new BigDecimal(0);
                    if (balances.get(categoryId) != null && balances.get(categoryId).get(searchCriteria) != null) {
                        balance = balances.get(categoryId).get(searchCriteria);
                    }

                    dataset.addValue(balance, category.getName(), searchCriteria.getPeriod());
                }
            }
        }

        // Series' shapes
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        for (int i = 0; i < dataset.getRowCount(); i++) {
            renderer.setSeriesShapesVisible(i, true);
            renderer.setSeriesShape(i, ShapeUtilities.createDiamond(2F));
        }
        // Set the scale of the chart
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
        rangeAxis.setAutoRangeIncludesZero(false);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // Create the chart panel that contains the chart
        ChartPanel chartPanel = new ChartPanel(chart);

        // Display the chart
        jPanelCategoriesBalances.removeAll();
        jPanelCategoriesBalances.add(chartPanel, BorderLayout.CENTER);
        jPanelCategoriesBalances.updateUI();

        // Display normal cursor
        Utilities.changeCursorWaitStatus(false);
    }
}