Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Printing on MacOSX (Rowan Christmas)



>What are the issues with printing support in the java runtime on OSX...

I have had printing issues for over a year. I have provided examples
(Problem ID: 3426772 on Apple Bug Reporting - reported last September) of
printing problems that we have experienced. The only response I have been
able to get from Apple is that my particular problem won't be fixed for the
release of Panther. This was a pretty big problem, since when you usually
print, you would like to have your margins set correctly. The problem I was
having was that we could not set the margins and have them print correctly
on the Apple platform (Jaguar or Panther). We have since released our Java
application, but the Apple platform still can not print correctly.

Here is a copy of the defect that I submitted to Apple (with an example
piece of source code). If you have any ideas on how to get the margins to
work, please send me any ideas...

Problem ID: 3426772 9/18/2003

* NOTES
I am using a JEditorPane as the object I am printing from.
I am including sample code with this defect to demonstrate the problem.
Line 113 in the sample code reads:
fp.formatPageFormat(fp.setMargins("0.6","0.5","2.18","1.0"));
You can set the left,right,top, bottom margins (in inches) on this line.
The 2.18 top margin setting is what is failing on the print out. It seems
if you stick to around 1 inch margins, things will work better, but our
company letterhead doesn't like a 1" margin.

We did set up a print preview object to preview the printout, and the
preview works fine and has not been added to this example.

When running the testSwing Application, enter enough text into the
JEditfield pane to cover several pages and then hit the print button below
the pane to print the text. We are using an HP LaserJet 4050 printer.

Code:
//
// testSwing.java
// testSwing
//
// Created by Steve Aguirre on Wed Sep 17 2003.
// Copyright (c) 2003 Intercontinental Hotels Group. All rights reserved.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.TextAction;
import javax.swing.text.*;
import com.apple.eawt.*;
import testSwing.*;

public class testSwing {
public static void main(String args[]) {
final String[] osName = new String[]{""};
osName[0] = System.getProperty("os.name");
if (osName[0].startsWith("Mac OS X") ) {
// set mac specific properties

System.setProperty("com.apple.mrj.application.apple.menu.about.name","IHG
Defect Test");
System.setProperty("apple.awt.brushMetalLook", "false");
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
new testSwingWindow();
}
}

class testSwingWindow extends JFrame {

private Application fApplication = new Application();
protected Action newAction, openAction, closeAction, saveAction,
saveAsAction,
undoAction, cutAction, copyAction, pasteAction, clearAction,
selectAllAction;
static final JMenuBar mainMenuBar = new JMenuBar();
protected JMenu fileMenu, editMenu;
private JEditorPane previewEditPane = null;
private JScrollPane previewScrollPane= null;

public testSwingWindow() {
setTitle("Defect Test");
this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().setBackground(Color.red);
this.setSize(750,450);
this.setLocation(10,10);
JButton b = new JButton("Print");
addListener(b);
b.setBackground(new Color(0x00, 0x00, 0x00, 0x00));
this.getContentPane().add(b, BorderLayout.SOUTH);
createActions();
addMenus();

// add editor
previewScrollPane = new JScrollPane();
previewEditPane = new JEditorPane();
previewScrollPane.setPreferredSize(new Dimension(540, 450-80));
previewEditPane.setEditable(true);
previewEditPane.setContentType("text/rtf");
previewEditPane.setText("Test Text\nEnter enough text to cover several
pages into this field");

previewScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTA
L_SCROLLBAR_AS_NEEDED);

previewScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SC
ROLLBAR_AS_NEEDED);
previewScrollPane.setViewportView(previewEditPane);
previewScrollPane.setBounds(2,35,540, 450-80);
previewScrollPane.setVisible(true);
this.getContentPane().add(previewScrollPane, BorderLayout.CENTER);
Toolkit.getDefaultToolkit();

fApplication.setEnabledPreferencesMenu(true);
fApplication.addApplicationListener(new
com.apple.eawt.ApplicationAdapter() {
public void handleAbout(ApplicationEvent e) {
}
public void handleOpenApplication(ApplicationEvent e) {
System.out.println("handleOpenApplication...");
}
public void handleOpenFile(ApplicationEvent e) {
System.out.println("handleOpenFile...");
}
public void handlePreferences(ApplicationEvent e) {
}
public void handlePrintFile(ApplicationEvent e) {
System.out.println("handlePrintFile...");
}
public void handleQuit(ApplicationEvent e) {
quit(e);
}
});
setVisible(true);
}
public void addListener(JButton b) {
b.addMouseListener(new java.awt.event.MouseListener() {
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me) {};
public void mouseEntered(MouseEvent me) {};
public void mouseClicked(MouseEvent me) {evaluatePop(me);};
private void evaluatePop(MouseEvent me) {
FormatPrinter fp = new FormatPrinter(previewEditPane);
// set left,right,top,bottom margins
fp.formatPageFormat(fp.setMargins("0.6","0.5","2.18","1.0"));
fp.printReport();
};
});
}

public void quit(ApplicationEvent e) {
System.exit(0);
}

public void createActions() {
int shortcutKeyMask =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

//Create actions that can be used by menus, buttons, toolbars, etc.
newAction = new newActionClass( "newItem",
KeyStroke.getKeyStroke(KeyEvent.VK_N,
shortcutKeyMask) );
openAction = new openActionClass( "openItem",
KeyStroke.getKeyStroke(KeyEvent.VK_O,
shortcutKeyMask) );
closeAction = new closeActionClass( "closeItem",

KeyStroke.getKeyStroke(KeyEvent.VK_W, shortcutKeyMask) );
saveAction = new saveActionClass( "saveItem",
KeyStroke.getKeyStroke(KeyEvent.VK_S,
shortcutKeyMask) );
saveAsAction = new saveAsActionClass( "saveAsItem");

undoAction = new undoActionClass( "undoItem",
KeyStroke.getKeyStroke(KeyEvent.VK_Z,
shortcutKeyMask) );
cutAction = new cutActionClass( "Cut",
KeyStroke.getKeyStroke(KeyEvent.VK_X,
shortcutKeyMask) );
copyAction = new copyActionClass( "Copy",
KeyStroke.getKeyStroke(KeyEvent.VK_C,
shortcutKeyMask) );
pasteAction = new pasteActionClass( "Paste",

KeyStroke.getKeyStroke(KeyEvent.VK_V, shortcutKeyMask) );
clearAction = new clearActionClass( "clearItem");
selectAllAction = new selectAllActionClass( "Select All",
KeyStroke.getKeyStroke(KeyEvent.VK_A, shortcutKeyMask) );
}

public void addMenus() {
fileMenu = new JMenu("File");
fileMenu.add(new JMenuItem(newAction));
fileMenu.add(new JMenuItem(openAction));
fileMenu.add(new JMenuItem(closeAction));
fileMenu.add(new JMenuItem(saveAction));
fileMenu.add(new JMenuItem(saveAsAction));
mainMenuBar.add(fileMenu);
editMenu = new JMenu("Edit");
editMenu.add(new JMenuItem(undoAction));
editMenu.addSeparator();
JMenuItem cutMenu = new JMenuItem("Cut");
JMenuItem copyMenu = new JMenuItem("Copy");
JMenuItem pasteMenu = new JMenuItem("Paste");
JMenuItem selectAllMenu = new JMenuItem("Select All");
cutMenu.setAction(new DefaultEditorKit.CutAction());
copyMenu.setAction(new DefaultEditorKit.CopyAction());
pasteMenu.setAction(new DefaultEditorKit.PasteAction());
selectAllMenu.setAction(new ActionSelectAll());
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
editMenu.add(new JMenuItem(clearAction));
editMenu.addSeparator();
editMenu.add(selectAllMenu);
mainMenuBar.add(editMenu);
setJMenuBar (mainMenuBar);
}

public class newActionClass extends AbstractAction {
public newActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("New...");
}
}

public class openActionClass extends AbstractAction {
public openActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Open...");
}
}
public class closeActionClass extends AbstractAction {
public closeActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Close...");
}
}

public class saveActionClass extends AbstractAction {
public saveActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Save...");
}
}

public class saveAsActionClass extends AbstractAction {
public saveAsActionClass(String text) {
super(text);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Save As...");
}
}

public class undoActionClass extends AbstractAction {
public undoActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Undo...");
}
}

public class cutActionClass extends AbstractAction {
public cutActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Cut...");
}
}

public class copyActionClass extends AbstractAction {
public copyActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Copy...");
}
}

public class pasteActionClass extends AbstractAction {
public pasteActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Paste...");
}
}

public class clearActionClass extends AbstractAction {
public clearActionClass(String text) {
super(text);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Clear...");
}
}

public class selectAllActionClass extends AbstractAction {
public selectAllActionClass(String text, KeyStroke shortcut) {
super(text);
putValue(ACCELERATOR_KEY, shortcut);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Select All...");
}
}
}
/*
* Select the entire document
* @see DefaultEditorKit
*/
class ActionSelectAll extends TextAction {
ActionSelectAll() {
super(DefaultEditorKit.selectAllAction);
}
/** The operation to perform when this action is triggered. */
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
if (target != null) {
Document doc = target.getDocument();
target.setCaretPosition(0);
target.moveCaretPosition(doc.getLength());
}
}
}

/**
* <p>Title: FormatPrinter</p>
* <p>Description: Prints Books and objects to printer</p>
* <p>Copyright: Copyright (c) 2003 Intercontinental Hotels Group</p>
* <p>Company: Intercontinental Hotels Group</p>
* @author Steve Aguirre
* @version 1.0
*/

class FormatPrinter implements Printable
{
PageFormat format;
PrinterJob printJob;
JEditorPane previewEditPane = null;

public FormatPrinter(JEditorPane _previewEditPane) {
printJob=null;
previewEditPane = _previewEditPane;
// set initial margins
format = formatPage();
}

/** initialize paper margins */
public PageFormat formatPage()
{
// default page margins
double marginX = 43.2; // 0.6" left margin
double marginY = 54.0; // 0.75" top margin
double marginW = 530.80; // 0.25" right margin
double marginH = 680.4; // 0.8" bottom margin
PageFormat pf = new PageFormat();
Paper paper = new Paper();
paper.setImageableArea(marginX, marginY, marginW, marginH);
pf.setPaper(paper);

return pf;
}
/** set provided paper margins */
public void formatPageFormat(PageFormat pf)
{
format = pf;
}

/** get current PageFormat */
public PageFormat getPageFormat()
{
return format;
}

/** initialize paper margins
* used to set margins manually from leftMargin,
rightMargin,topMargin,bottomMargin
* provided strings contain a number (in inches) for each margin i.e. 0.5
for a 1/2 margin
*
*/

public PageFormat setMargins(String leftMargin, String rightMargin,String
topMargin,String bottomMargin)
{
Double ml,mr,mt,mb;
double mx,my,mw,mh;
try {
ml = new Double(leftMargin);
mr = new Double(rightMargin);
mt = new Double(topMargin);
mb = new Double(bottomMargin);
mx = ml.doubleValue() * 72.0;
mw = format.getWidth() - (mr.doubleValue()*72.0)-mx;
my = mt.doubleValue() * 72.0;
mh = format.getHeight() - (mb.doubleValue()*72.0)-my;
}
catch (NumberFormatException ex) {
mx = 43.2;
my = 54.0;
mw = 530.80;
mh = 680.4;
}
PageFormat pf = new PageFormat();
Paper paper = new Paper();
paper.setImageableArea(mx, my, mw, mh);
pf.setPaper(paper);

return pf;
}

/**
* Determin number of pages in letter
* @param mFormat
* @param height
* @return
*/
protected int getNumberOfPages(PageFormat mFormat, float height) {
int mNumPagesY = (int) ((height + mFormat.getImageableHeight() - 1)/
mFormat.getImageableHeight());
return mNumPagesY;
}
/**
* Interface Method Implementation for Printable.
*
* This method is called by a PrinterJob object for each page that needs
to be printed.
*
* @param a_graphicsContext an graphics context to which the component to
be printed should paint itself.
* @param a_pageFormat an object representing the printable context of the
page on the printer.
* @param a_nPageIndex the current page that the PrinterJob is trying to
print.
* @return NO_SUCH_PAGE if the case report does not extend to
a_nPageIndex. PAGE_EXISTS if the case report does extend to a_nPageIndex and
the component was sucessfully painted into the graphics context.
* @exception PrinterException not thrown by this code. May be thrown in
methods called by this method.
*/
public int print(Graphics a_graphicsContext, PageFormat a_pageFormat, int
a_nPageIndex) throws PrinterException
{
Graphics2D g2 = (Graphics2D)a_graphicsContext;

double dImagableX = a_pageFormat.getImageableX();
double dImagableY = a_pageFormat.getImageableY();
double dImagableWidth = a_pageFormat.getImageableWidth();
double dImagableHeight = a_pageFormat.getImageableHeight();
double theight = a_pageFormat.getHeight();
double twide = a_pageFormat.getWidth();
Rectangle componentBounds = previewEditPane.getBounds();
if (a_nPageIndex > 0 && componentBounds.height < (dImagableHeight *
a_nPageIndex))
{
return NO_SUCH_PAGE;
}

g2.translate(dImagableX, dImagableY - dImagableHeight * a_nPageIndex);
// apply the margin settings
g2.setClip(0,(int)(dImagableHeight *
a_nPageIndex),(int)dImagableWidth,(int)(dImagableHeight));
boolean bWasDoubleBuffered = previewEditPane.isDoubleBuffered();
previewEditPane.setDoubleBuffered(false);
previewEditPane.paint(g2);
previewEditPane.setDoubleBuffered(bWasDoubleBuffered);
return PAGE_EXISTS;

}
/**
* Print report
* @return true if print was successfull
*/
public boolean printReport() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
boolean success = false;
Rectangle componentBounds = previewEditPane.getBounds();
if (getNumberOfPages(format,componentBounds.height) < 1) {
return success;
}
printJob.setPrintable(this,format);
try
{
printJob.print();
success = true;
}
catch(PrinterException pe)
{
JOptionPane.showMessageDialog(null, "Error printing: " +
pe.toString(), "Print Error", JOptionPane.ERROR_MESSAGE);
}
catch(java.lang.OutOfMemoryError ome) {
JOptionPane.showMessageDialog(null, "Out of memory. Printing too many
letters at one time.","Print Error", JOptionPane.ERROR_MESSAGE);
}

printJob = null;
format = null;
return success;
}
}



****************************************************************************

This email and any attachments hereto contain confidential and/or privileged
information. This information is intended solely for the individual or
entity named herein. If the reader of this email is not the intended
recipient, you are hereby notified that any use, disclosure, storage,
dissemination, or duplication of this communication is strictly
prohibited.If you have received this communication in error, please notify
the sender immediately by telephone - +65 63956166, or by return e-mail, and
delete the message from your computer. As the intended recipient, you
acknowledge that emails transmitted over the World Wide Web may be subject
to hacking or unauthorised interferences. If in doubt as to the form in
which this message was originally transmitted, please verify directly with
the writer.
***************************************************************************
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.