gg.view.movementsbalances.GraphMovementsBalancesTopComponent.java Source code

Java tutorial

Introduction

Here is the source code for gg.view.movementsbalances.GraphMovementsBalancesTopComponent.java

Source

/*
 * GraphMovementsBalancesTopComponent.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.movementsbalances;

import gg.db.datamodel.SearchCriteria;
import gg.db.entities.Account;
import gg.db.entities.MoneyContainer;
import gg.utilities.Utilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.math.BigDecimal;
import java.math.RoundingMode;
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.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.LookupEvent;
import org.openide.util.LookupListener;

/**
 * Top component which displays a graph showing the accounts' movements evolution over time.
 */
@ConvertAsProperties(dtd = "-//gg.view.movementsbalances//GraphMovementsBalances//EN", autostore = false)
public final class GraphMovementsBalancesTopComponent extends TopComponent implements LookupListener {

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

    /** Creates a new instance of GraphMovementsBalancesTopComponent */
    public GraphMovementsBalancesTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(GraphMovementsBalancesTopComponent.class,
                "CTL_GraphMovementsBalancesTopComponent"));
        setToolTipText(NbBundle.getMessage(GraphMovementsBalancesTopComponent.class,
                "HINT_GraphMovementsBalancesTopComponent"));
        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() {

        jPanelMovementsBalances = new javax.swing.JPanel();

        jPanelMovementsBalances.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)
                        .addGroup(layout
                                .createSequentialGroup().addContainerGap().addComponent(jPanelMovementsBalances,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                                .addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout
                                .createSequentialGroup().addContainerGap().addComponent(jPanelMovementsBalances,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                                .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents
     // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JPanel jPanelMovementsBalances;
    // 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 GraphMovementsBalancesTopComponent getDefault() {
        if (instance == null) {
            instance = new GraphMovementsBalancesTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the GraphMovementsBalancesTopComponent instance. Never call {@link #getDefault} directly!
     * @return GraphMovementsBalancesTopComponent instance
     */
    public static synchronized GraphMovementsBalancesTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(GraphMovementsBalancesTopComponent.class.getName()).warning("Cannot find "
                    + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof GraphMovementsBalancesTopComponent) {
            return (GraphMovementsBalancesTopComponent) win;
        }
        Logger.getLogger(GraphMovementsBalancesTopComponent.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) {
        GraphMovementsBalancesTopComponent singleton = GraphMovementsBalancesTopComponent.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 Movements' 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 movements' balances table top component
            result = WindowManager.getDefault().findTopComponent("MovementsBalancesTopComponent").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()) {
            // Get the currency/account movements balances by search criteria
            @SuppressWarnings("unchecked")
            Map<MoneyContainer, Map<SearchCriteria, BigDecimal>> balances = (Map<MoneyContainer, Map<SearchCriteria, BigDecimal>>) instances
                    .iterator().next();

            // Display the content of the table in the graph
            displayData(balances);
        }
    }

    /**
     * Displays the accounts' movements balances by period
     * @param searchFilters Search filter objects (one per period) for which the movements balances are wanted
     */
    private void displayData(Map<MoneyContainer, Map<SearchCriteria, BigDecimal>> balances) {
        log.info("Movements' balances graph computed and displayed");

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

        // Create the dataset (that will contain the movements' balances)
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

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

        // Chart color
        chart.setBackgroundPaint(jPanelMovementsBalances.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 (MoneyContainer moneyContainer : balances.keySet()) {
            if (moneyContainer instanceof Account) {

                SortedSet<SearchCriteria> sortedSearchFilters = new TreeSet<SearchCriteria>(
                        balances.get(moneyContainer).keySet());

                for (SearchCriteria searchCriteria : sortedSearchFilters) {
                    BigDecimal accountBalance = balances.get(moneyContainer).get(searchCriteria);
                    accountBalance = accountBalance.setScale(2, RoundingMode.HALF_EVEN);

                    dataset.addValue(accountBalance, moneyContainer.toString(), 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
        jPanelMovementsBalances.removeAll();
        jPanelMovementsBalances.add(chartPanel, BorderLayout.CENTER);
        jPanelMovementsBalances.updateUI();

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