com.viaoa.jfc.print.PrintPreviewController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oa Show documentation
Show all versions of oa Show documentation
Object Automation library
The newest version!
/* Copyright 1999-2015 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.jfc.print;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.print.*;
import javax.swing.*;
import com.viaoa.jfc.print.view.*;
// IMPORTANT: need to convert from point to pixel whenever using PageFormat or Paper. For printing, graphics.scale(x,x) is set to make it wysiwyg
/**
* Controller for print preview.
* @author vvia
*
*/
public abstract class PrintPreviewController {
private Window parentWindow;
private PrintPreviewDialog dlgPrintPreview;
private Printable printable;
private PageFormat pageFormat;
private BufferedImage image; // one and only image, that is used by PagePanels.
private String[] scales = new String[] { " 10% ", " 25% ", " 50% ", " 75% ", " 100% " };
private int selectedScale = 4;
public PrintPreviewController() {
}
public void show(Printable printable, String title, PageFormat pageFormat) {
getPrintPreviewDialog().setTitle(title);
clear();
this.pageFormat = pageFormat;
this.printable = printable;
refresh(true);
getPrintPreviewDialog().setVisible(true);
}
public void setParentWindow(Window window) {
parentWindow = window;
}
public Window getParentWindow() {
return parentWindow;
}
private float pointToPixel;
/**
* Convert from Point size to pixel size.
*/
protected float convertPointsToPixels(double pointSize) {
if (pointToPixel == 0.0) {
pointToPixel = (float) (Toolkit.getDefaultToolkit().getScreenResolution() / 72.0);
}
return (float) (pointToPixel * pointSize);
}
/**
* Used to change the PageFormat.
* @see #onPageSetup
*/
public void setPageFormat(PageFormat pageFormat) {
this.pageFormat = pageFormat;
}
protected PrintPreviewDialog getPrintPreviewDialog() {
if (dlgPrintPreview == null) {
dlgPrintPreview = new PrintPreviewDialog(parentWindow, scales) {
public void onClose() {
PrintPreviewController.this.onClose();
}
public void onPageSetup() {
PrintPreviewController.this.onPageSetup();
}
public void onPrint() {
PrintPreviewController.this.onPrint();
}
};
dlgPrintPreview.getScaleComboBox().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/** was: not needed, refresh is ran in a new thread
new Thread() {
public void run() {
String str = dlgPrintPreview.getScaleComboBox().getSelectedItem().toString();
str = str.trim();
if (str.endsWith("%")) str = str.substring(0, str.length()-1);
str = str.trim();
try {
selectedScale = Integer.parseInt(str);
}
catch (NumberFormatException ex) {
return;
}
refresh(false); // resize and repaint
}
}.start();
*/
String str = dlgPrintPreview.getScaleComboBox().getSelectedItem().toString();
str = str.trim();
if (str.endsWith("%")) str = str.substring(0, str.length()-1);
str = str.trim();
try {
selectedScale = Integer.parseInt(str);
}
catch (NumberFormatException ex) {
return;
}
refresh(false); // resize and repaint
}
});
dlgPrintPreview.getScaleComboBox().setSelectedIndex(selectedScale);
Rectangle r;
if (parentWindow == null) r = new Rectangle(20,20, 200, 200);
else r = parentWindow.getBounds();
int x = r.getLocation().x + 20;
int y = r.getLocation().y + 20;
int w = r.getSize().width - 30;
int h = r.getSize().height - 30;
dlgPrintPreview.setBounds(new Rectangle(x, y, w, h));
}
return dlgPrintPreview;
}
protected void clear() {
getPrintPreviewDialog().getPreviewPanel().removeAll();
printable = null;
}
protected int wPage;
protected int hPage;
public void refresh(final boolean bRebuild) {
getPrintPreviewDialog().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
new Thread(new Runnable() {
public void run() {
doRefresh(bRebuild);
getPrintPreviewDialog().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}, "PrintPreview").start();
}
private volatile boolean bDone;
protected void doRefresh(boolean bRebuild) {
if (dlgPrintPreview == null || pageFormat == null) return;
wPage = (int) convertPointsToPixels(pageFormat.getWidth());
hPage = (int) convertPointsToPixels(pageFormat.getHeight());
int w = (int)(wPage * selectedScale/100);
int h = (int)(hPage * selectedScale/100);
if (!bRebuild) {
Component[] comps = getPrintPreviewDialog().getPreviewPanel().getComponents();
for (int i=0; i