Kbase 18941: Example for Java printing of an Apptivity form contents
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  20/06/1999 |
|
Example for Java printing of an Apptivity form contents
DRAFT COPY - Currently under review and edit.
This document applies to: Apptivity
Version and Release Number: 3.01
Summary:
Example for Java printing of an Apptivity form contents
Step by step details:
Web browser usually allow to print the current web page. One of
the restrictions of Applets by security conventions is printing.
That is why the Apptivity 3.x product currently doesn't provide
no explicit printing tools and documentation. Printing of reports
is very easy to manage by sending the desired output to an html
page and using the AppletContext.showDocument() method to launch
a browser window with the report in it to preview like described
with Progress kbase id # 17509. Although of that already existing
printing solutions and on the other hand restrictions for Applets,
Java printing is very powerfully especially for sending graphical
output to a printer.
With JDK 1.1 printing for an Java application can be done by
creating a java.awt.PrintJob with the
java.awt.Toolkit.getPrintJob method for the actually window
display java.awt.Frame and sending the java.awt.Graphics page,
got with the java.awt.PrintJob.getGraphics() method from
the print job, using for instance the frame's
java.awt.Frame.printAll(java.awt.Graphics page) super method
for all objects of the frame components container window,
to the printer.
Apptivity's IFC form contents is displayed with an extent of the
netscape.application.ContainerView why with Apptivity to be able to
send the components of the container of the actually window frame
to the printer it is additional necessary to get
the java.awt.Frame from the netscape.application.RootView
of the container view with the
netscape.application.AWTCompatibility.awtFrameForRootView method.
Once done this there exists a strong relationship between the
print jobs Graphics page and the RootView, and the Graphics page
can only send all objects to the printer if the Thread responsible
for repainting the screen is not blocked.
The PrintThread.class example below shows one way to do that.
With the Frame's setBounds method it is possible to send only
the informational screen contents to the printer and painting
a at runtime invisible rectangle around the printing area while
design time even allows to predefine that using graphically advantages
of the Apptivity Developer easiest. So in that case
the only thing to attend is making the forms Rect r a member
variable of the form and create the rectangle as last control
that the PrintThread.class can receive the rectangle with it's
constructor :
import progress.apptivity.igui.abContainerView;
import netscape.application.Rect;
import netscape.application.AWTCompatibility;
class PrintThread implements Runnable {
abContainerView m_formContent;
java.awt.Frame frame;
Rect r, rb;
String name;
static boolean preview = false;
//the constructor to receive the name of the print job,
//the progress.apptivity.igui.abContainerView,
// and the the print area surrounding rectangle r
public PrintThread(String name,abContainerView m_formContent,Rect r)
{
this.m_formContent = m_formContent;
this.r = r;
this.name = name;
rb = m_formContent.bounds();
}
public void run()
{
//get the RootView of the form
netscape.application.RootView froot = m_formContent.rootView();
// get the java.awt.Frame from the RootView
frame = AWTCompatibility.awtFrameForRootView(froot);
frame.setBounds(r.x,r.y,r.width,r.height);
frame.show();
if (preview) { pause(); reset() ;return;}
java.awt.Toolkit toolkit = frame.getToolkit();
java.util.Properties properties =
new java.util.Properties();
//create the print job
java.awt.PrintJob pj=toolkit.getPrintJob(
frame,name,properties);
// if user canceled printing
if (pj == null){reset(); return;}
//get the page and it's paper dimension
java.awt.Dimension dim = pj.getPageDimension();
java.awt.Graphics g = pj.getGraphics();
//center the output on the page
int pw = (dim.width>r.width)?(dim.width-r.width)/2:0;
int ph = (dim.height>r.height)?(dim.height-r.height)/2:0;
g.translate(pw,ph);
//avoid trying printing outside the paper
g.setClip(0,0,dim.width,dim.height);
//print all components
frame.printAll(g);
//finish the printing
g.dispose();
pj.end();
pause();
//and set the bounds back to the original
reset();
return;
}
public void pause() {
try {Thread.sleep(5000);} catch (Exception e) {;}}
public void reset() {
frame.setBounds(rb.x,rb.y,rb.width + 17,rb.height);}
}
The implementation of the PrintThread class with an action code behind
a form's print button outside the print area may than look like:
PrintThread rp = new PrintThread("Test", m_formContent, r);
rp.preview = false;
Thread tp = new Thread(rp);
tp.start();
return;
If printing a grid it is also helpfully to use virtual paging
with the abRecordSet.setCurrentPage(int start, int count)
method to predefine the size of the grid and allowing to have
pages together with removing srollbars using its super methods:
.setHasHorizScrollBar(false);
.setHasVertScrollBar(false);
Tests using an A4 paper format for the printer show that
a grid with 33 lines and a height of 8625 and a width
of 7375 looks good if the surrounding rectangle has
an Apptivity height and width of 10125 x 8125.
The above description is based on tests made with
Apptivity 3.01 build 159 and Apptivity 3.1 build 212.
References To Written Documentation or Other
KnowledgeBase Documents:
Progress kbase id # 17509
Apptivity API Reference
O'REILLY Java in a Nutshell
DISCLAIMER
The origins of this information may be internal or
external to Progress Software Corporation. Progress
Software Corporation makes all reasonable efforts
to verify this information. However, the
information provided in this document is for your
information only.
Progress Software Corporation makes no explicit or
implied claims to the validity of this information.
Any trademarks referenced in this document are the
property of their respective owners. Consult your
product manuals for complete trademark information.
Copyright 1999 Progress Software Corporation, Inc.
All Rights Reserved.