/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program 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 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.guillc.panel;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.logging.Logger;
import org.geoforge.guillc.button.GfrBcnPrintPartPanel;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
public class GfrPnlPrintableSctAwt extends PnlAbs implements
ActionListener,
Printable
{
// ----
// begin: instantiate logger for this class
final static private Logger _LOGGER_ = Logger.getLogger(GfrPnlPrintableSctAwt.class.getName());
static
{
GfrPnlPrintableSctAwt._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
public GfrPnlPrintableSctAwt()
{
super();
}
@Override
public void actionPerformed(ActionEvent e)
{
Object objSource = e.getSource();
if (objSource instanceof GfrBcnPrintPartPanel)
{
_doPrintComponentAwt_();
return;
}
String str = "!! uncaught objSource.getClass().getName()" + objSource.getClass().getName();
GfrPnlPrintableSctAwt._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
}
@Override
public int print(Graphics g, PageFormat pft, int intPageIndex)
throws PrinterException
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black); //set default foreground color to black
//for faster printing, turn off double buffering
//RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double dblPanelWidth = d.width; //width in pixels
double dblPanelHeight = d.height; //height in pixels
double dblPageHeight = pft.getImageableHeight(); //height of printer page
double dblPageWidth = pft.getImageableWidth(); //width of printer page
double dblScale = dblPageWidth/dblPanelWidth;
int intPageNb = (int)Math.ceil(dblScale * dblPanelHeight / dblPageHeight);
//make sure not print empty pages
if(intPageIndex >= intPageNb)
{
return Printable.NO_SUCH_PAGE;
}
//shift Graphic to line up with beginning of print-imageable region
g2.translate(pft.getImageableX(), pft.getImageableY());
//shift Graphic to line up with beginning of next page to print
g2.translate(0f, -intPageIndex*dblPageHeight);
//dblScale the page so the width fits...
g2.scale(dblScale, dblScale);
this.paint(g2); //repaint the page for printing
return Printable.PAGE_EXISTS;
}
private boolean _doPrintComponentAwt_()
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean blnOk = true;
if (job.printDialog())
{
try
{
job.print();
setCursor(Cursor.getDefaultCursor());
}
catch (Exception exc)
{
setCursor(Cursor.getDefaultCursor());
exc.printStackTrace();
// TODO: show warning dialog
blnOk = false;
}
}
else
setCursor(Cursor.getDefaultCursor());
// else the user has cancelled the printing (AS FOR INFO)
return blnOk;
}
@Override
public boolean init()
{
if (! super.init())
return false;
return true;
}
@Override
public void destroy()
{
super.destroy();
}
@Override
public void loadTransient() throws Exception
{
// TODO: loadTransient();
}
@Override
public void releaseTransient() throws Exception {
// TODO: releaseTransient();
}
}