/*
 *  Chart2DActionPrintSingleton, 
 *  singleton action that prints the chart with a dialog.
 *  Copyright (C) 2007 - 2011 Achim Westermann
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library 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
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  If you modify or optimize the code in a useful way please let me know.
 *  Achim.Westermann@gmx.de
 *
 */
package info.monitorenter.gui.chart.events;

import info.monitorenter.gui.chart.Chart2D;

import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.beans.PropertyChangeEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * Singleton Action that prints the current chart by showing a modal print dialog.
 * 

* Only one instance per target component may exist. *

* * @see info.monitorenter.gui.chart.events.Chart2DActionSetCustomGridColor * @author Achim Westermann * @version $Revision: 1.7 $ */ public final class Chart2DActionPrintSingleton extends AChart2DAction { /** Generated serialVersionUID. * */ private static final long serialVersionUID = 3475108617927262279L; /** * Map for instances. */ private static Map instances = new HashMap(); /** * Returns the single instance for the given component or null, if it is not existing. *

* * @param chart * the target the action will work on * @return the single instance for the given component or null. */ public static Chart2DActionPrintSingleton getInstance(final Chart2D chart) { Chart2DActionPrintSingleton result = Chart2DActionPrintSingleton.instances .get(Chart2DActionPrintSingleton.key(chart)); return result; } /** * Returns the single instance for the given component, potentially creating it. *

* If an instance for the given component had been created the description String is ignored. *

* * @param chart * the target the action will work on * @param actionName * the descriptive String that will be displayed by * {@link javax.swing.AbstractButton} subclasses that get this Action * assigned ( {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}). * @return the single instance for the given component. */ public static Chart2DActionPrintSingleton getInstance(final Chart2D chart, final String actionName) { Chart2DActionPrintSingleton result = Chart2DActionPrintSingleton.getInstance(chart); if (result == null) { result = new Chart2DActionPrintSingleton(chart, actionName); Chart2DActionPrintSingleton.instances.put(Chart2DActionPrintSingleton.key(chart), result); } return result; } /** * Creates a key for the component for internal storage. *

* * @param chart * the chart to generate the storage key for. * @return a storage key unique for the given chart instance. */ private static String key(final Chart2D chart) { return chart.getClass().getName() + chart.hashCode(); } /** * This is set to true when this action is triggered internally. *

* This signals the chart that is painting that a print cycle is running triggered by this action * and the chart should be printed on the whole page. *

* Requesting this value has to be synchronized together with the method * {@link Chart2D#paint(java.awt.Graphics)} method and reset to false from there. *

*/ private transient boolean m_printWholePage = false; /** * Create an Action that accesses the trace and identifies itself with the given * action String. *

* * @param chart * the target the action will work on * @param colorName * the descriptive String that will be displayed by * {@link javax.swing.AbstractButton} subclasses that get this Action * assigned ( {@link javax.swing.AbstractButton#setAction(javax.swing.Action)}). */ private Chart2DActionPrintSingleton(final Chart2D chart, final String colorName) { super(chart, colorName); } /** * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(final ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this.m_chart); boolean ok = job.printDialog(); if (ok) { this.m_printWholePage = true; try { job.print(); } catch (PrinterException ex) { /* The job did not successfully complete */ } finally { this.m_printWholePage = false; this.m_chart.resetPrintMode(); } } } /** * Returns true if this action triggered a print request for the corresponding chart. *

* * @return true if this action triggered a print request for the corresponding chart. */ public final boolean isPrintWholePage() { return this.m_printWholePage; } /** * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) */ public void propertyChange(final PropertyChangeEvent evt) { // nop } /** * Resets the printing modus for the corresponding chart triggered by this action. *

* Has to be called by {@link Chart2D#paint(java.awt.Graphics)} when printing was * triggered from here. *

*/ public final void resetPrintWholePage() { this.m_printWholePage = false; } }

Ads help maintain this website.