| 
| PrintBigFigureOperation [message #53366] | Fri, 10 January 2003 09:16  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: sven.e-sven.net 
 Already posted it in another thread. I'd like to get some feedback on
 it. I think the translating of the figure and it's childs is not the
 best way to do this, but it works.
 
 Sven\ 8)
 
 PS: The Question Window in the run method doesn't belong there. Is there
 a way to display the number of pages in the PrintDialog?
 
 ------------------
 import java.util.List;
 
 import org.eclipse.draw2d.IFigure;
 import org.eclipse.draw2d.PrintFigureOperation;
 import org.eclipse.draw2d.PrinterGraphics;
 import org.eclipse.draw2d.geometry.Point;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.swt.printing.Printer;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.PlatformUI;
 
 /**
 * Druckt eine IFigure über mehrere Seiten.
 * @author Sven Müller
 */
 public class PrintBigFigureOperation extends PrintFigureOperation {
 
 private double scale;
 
 public PrintBigFigureOperation(Printer printer, IFigure figure) {
 super(printer, figure);
 scale = getPrinter().getDPI().x / Display.getDefault().getDPI().x;
 }
 
 /**
 * @see org.eclipse.draw2d.PrintOperation#printPages()
 */
 protected void printPages() {
 IFigure figure = getPrintSource();
 boolean nextPage = true;
 Point offset = figure.getBounds().getLocation().getCopy();
 int x =
 (int) (figure.getBounds().getSize().width
 / (getPrintRegion().width / scale));
 int y =
 (int) (figure.getBounds().getSize().height
 / (getPrintRegion().height / scale));
 int memX1 = x;
 int memX2 = offset.x;
 PrinterGraphics g = getFreshPrinterGraphics();
 List childs = figure.getChildren();
 while (nextPage) {
 g.pushState();
 getPrinter().startPage();
 g.setForegroundColor(figure.getForegroundColor());
 g.setBackgroundColor(figure.getBackgroundColor());
 g.setFont(figure.getFont());
 g.scale(scale);
 figure.translate(offset.x, offset.y);
 for (int i = 0; i < childs.size(); i++)
 ((IFigure) childs.get(i)).translate(-offset.x, -offset.y);
 g.translate(figure.getBounds().getLocation().getCopy().negat e());
 g.clipRect(figure.getBounds());
 figure.paint(g);
 getPrinter().endPage();
 g.restoreState();
 if (x > 0) {
 x--;
 offset.x = (int) (getPrintRegion().width / scale);
 } else if (y > 0) {
 y--;
 x = memX1;
 figure.translate(-x * offset.x, 0);
 for (int i = 0; i < childs.size(); i++)
 ((IFigure) childs.get(i)).translate(x * offset.x, 0);
 offset.x = memX2;
 offset.y = (int) (getPrintRegion().height / scale);
 } else {
 nextPage = false;
 }
 }
 }
 /**
 * @see org.eclipse.draw2d.PrintOperation#run(String)
 */
 public void run(String jobName) {
 if (MessageDialog
 .openQuestion(
 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l(),
 "Question...",
 "Really print "
 + getNumofPages()
 + " pages on "
 + getPrinter().getPrinterData().name
 + "?"))
 super.run(jobName);
 }
 
 private int getNumofPages() {
 double x =
 getPrintSource().getBounds().getSize().width
 / (getPrintRegion().width / scale);
 double y =
 getPrintSource().getBounds().getSize().height
 / (getPrintRegion().height / scale);
 return (int)
 (((x > (int) x) ? (int) x + 1 : (int) x)
 * ((y > (int) y) ? (int) y + 1 : (int) y));
 }
 }
 |  |  |  | 
|  | 
| 
| Re: PrintBigFigureOperation [message #53499 is a reply to message #53473] | Fri, 10 January 2003 11:11  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: sven.e-sven.net 
 You are right it's easier and faster to simply translate the PG.
 
 Why move the world beneath your feet if walking is so easy? ;)
 
 Sven\ 8)
 
 
 Here's a better impl.:
 ------------
 import org.eclipse.draw2d.IFigure;
 import org.eclipse.draw2d.PrintFigureOperation;
 import org.eclipse.draw2d.PrinterGraphics;
 import org.eclipse.draw2d.geometry.Point;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.swt.printing.Printer;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.PlatformUI;
 
 /**
 * Druckt eine IFigure über mehrere Seiten.
 * @author Sven Müller
 */
 public class PrintBigFigureOperation extends PrintFigureOperation {
 
 private double scale;
 
 public PrintBigFigureOperation(Printer printer, IFigure figure) {
 super(printer, figure);
 scale = getPrinter().getDPI().x / Display.getDefault().getDPI().x;
 }
 
 /**
 * @see org.eclipse.draw2d.PrintOperation#printPages()
 */
 protected void printPages() {
 IFigure figure = getPrintSource();
 boolean nextPage = true;
 Point offset = figure.getBounds().getLocation();
 int x =
 (int) (getPrintSource().getBounds().getSize().width
 / (getPrintRegion().width / scale));
 int y =
 (int) (getPrintSource().getBounds().getSize().height
 / (getPrintRegion().height / scale));
 PrinterGraphics g = getFreshPrinterGraphics();
 g.setForegroundColor(figure.getForegroundColor());
 g.setBackgroundColor(figure.getBackgroundColor());
 g.setFont(figure.getFont());
 
 for (int i = 0; i < getNumofPages(); i++) {
 g.pushState();
 getPrinter().startPage();
 g.scale(scale);
 g.translate(-offset.x, -offset.y);
 g.clipRect(figure.getBounds());
 figure.paint(g);
 getPrinter().endPage();
 g.restoreState();
 if (x > 0) {
 x--;
 offset.x += (int) (getPrintRegion().width / scale);
 } else if (y > 0) {
 y--;
 offset.y += (int) (getPrintRegion().height / scale);
 offset.x = figure.getBounds().getLocation().x;
 x =
 (int) (getPrintSource().getBounds().getSize().width
 / (getPrintRegion().width / scale));
 }
 }
 }
 /**
 * @see org.eclipse.draw2d.PrintOperation#run(String)
 */
 public void run(String jobName) {
 if (MessageDialog
 .openQuestion(
 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l(),
 "Question...",
 "Really print "
 + getNumofPages()
 + " pages on "
 + getPrinter().getPrinterData().name
 + "?"))
 super.run(jobName);
 }
 /**
 * Berechnet die Seiten die nötig sind um die {@link IFigure IFigure}
 auf dem gewählten {@link Printer Printer} auszugeben
 * @return int
 */
 private int getNumofPages() {
 double x =
 getPrintSource().getBounds().getSize().width
 / (getPrintRegion().width / scale);
 double y =
 getPrintSource().getBounds().getSize().height
 / (getPrintRegion().height / scale);
 return ((x > (int) x) ? (int) x + 1 : (int) x)
 * ((y > (int) y) ? (int) y + 1 : (int) y);
 }
 }
 |  |  |  | 
Powered by 
FUDForum. Page generated in 0.05916 seconds