FileDocCategorySizeDatePackage
PageAttributes.javaAPI DocJava SE 6 API49239Tue Jun 10 00:25:18 BST 2008java.awt

PageAttributes

public final class PageAttributes extends Object implements Cloneable
A set of attributes which control the output of a printed page.

Instances of this class control the color state, paper size (media type), orientation, logical origin, print quality, and resolution of every page which uses the instance. Attribute names are compliant with the Internet Printing Protocol (IPP) 1.1 where possible. Attribute values are partially compliant where possible.

To use a method which takes an inner class type, pass a reference to one of the constant fields of the inner class. Client code cannot create new instances of the inner class types because none of those classes has a public constructor. For example, to set the color state to monochrome, use the following code:

import java.awt.PageAttributes;

public class MonochromeExample {
public void setMonochrome(PageAttributes pageAttributes) {
pageAttributes.setColor(PageAttributes.ColorType.MONOCHROME);
}
}

Every IPP attribute which supports an attributeName-default value has a corresponding setattributeNameToDefault method. Default value fields are not provided.

version
1.9, 04/07/06
author
David Mendenhall
since
1.3

Fields Summary
private ColorType
color
private MediaType
media
private OrientationRequestedType
orientationRequested
private OriginType
origin
private PrintQualityType
printQuality
private int[]
printerResolution
Constructors Summary
public PageAttributes()
Constructs a PageAttributes instance with default values for every attribute.

        setColor(ColorType.MONOCHROME);
	setMediaToDefault();
	setOrientationRequestedToDefault();
	setOrigin(OriginType.PHYSICAL);
	setPrintQualityToDefault();
	setPrinterResolutionToDefault();
    
public PageAttributes(PageAttributes obj)
Constructs a PageAttributes instance which is a copy of the supplied PageAttributes.

param
obj the PageAttributes to copy.

        set(obj);
    
public PageAttributes(ColorType color, MediaType media, OrientationRequestedType orientationRequested, OriginType origin, PrintQualityType printQuality, int[] printerResolution)
Constructs a PageAttributes instance with the specified values for every attribute.

param
color ColorType.COLOR or ColorType.MONOCHROME.
param
media one of the constant fields of the MediaType class.
param
orientationRequested OrientationRequestedType.PORTRAIT or OrientationRequestedType.LANDSCAPE.
param
origin OriginType.PHYSICAL or OriginType.PRINTABLE
param
printQuality PrintQualityType.DRAFT, PrintQualityType.NORMAL, or PrintQualityType.HIGH
param
printerResolution an integer array of 3 elements. The first element must be greater than 0. The second element must be must be greater than 0. The third element must be either 3 or 4.
throws
IllegalArgumentException if one or more of the above conditions is violated.

        setColor(color);
	setMedia(media);
	setOrientationRequested(orientationRequested);
	setOrigin(origin);
	setPrintQuality(printQuality);
	setPrinterResolution(printerResolution);
    
Methods Summary
public java.lang.Objectclone()
Creates and returns a copy of this PageAttributes.

return
the newly created copy. It is safe to cast this Object into a PageAttributes.

        try {
	    return super.clone();
	} catch (CloneNotSupportedException e) {
	    // Since we implement Cloneable, this should never happen
	    throw new InternalError();
	}
    
public booleanequals(java.lang.Object obj)
Determines whether two PageAttributes are equal to each other.

Two PageAttributes are equal if and only if each of their attributes are equal. Attributes of enumeration type are equal if and only if the fields refer to the same unique enumeration object. This means that an aliased media is equal to its underlying unique media. Printer resolutions are equal if and only if the feed resolution, cross feed resolution, and units are equal.

param
obj the object whose equality will be checked.
return
whether obj is equal to this PageAttribute according to the above criteria.

        if (!(obj instanceof PageAttributes)) {
	    return false;
	}

	PageAttributes rhs = (PageAttributes)obj;

	return (color == rhs.color &&
		media == rhs.media &&
		orientationRequested == rhs.orientationRequested &&
		origin == rhs.origin &&
		printQuality == rhs.printQuality &&
		printerResolution[0] == rhs.printerResolution[0] &&
		printerResolution[1] == rhs.printerResolution[1] &&
		printerResolution[2] == rhs.printerResolution[2]);
    
public java.awt.PageAttributes$ColorTypegetColor()
Returns whether pages using these attributes will be rendered in color or monochrome. This attribute is updated to the value chosen by the user.

return
ColorType.COLOR or ColorType.MONOCHROME.

        return color;
    
public java.awt.PageAttributes$MediaTypegetMedia()
Returns the paper size for pages using these attributes. This attribute is updated to the value chosen by the user.

return
one of the constant fields of the MediaType class.

        return media;
    
public java.awt.PageAttributes$OrientationRequestedTypegetOrientationRequested()
Returns the print orientation for pages using these attributes. This attribute is updated to the value chosen by the user.

return
OrientationRequestedType.PORTRAIT or OrientationRequestedType.LANDSCAPE.

        return orientationRequested;
    
public java.awt.PageAttributes$OriginTypegetOrigin()
Returns whether drawing at (0, 0) to pages using these attributes draws at the upper-left corner of the physical page, or at the upper-left corner of the printable area. (Note that these locations could be equivalent.) This attribute cannot be modified by, and is not subject to any limitations of, the implementation or the target printer.

return
OriginType.PHYSICAL or OriginType.PRINTABLE

        return origin;
    
public java.awt.PageAttributes$PrintQualityTypegetPrintQuality()
Returns the print quality for pages using these attributes. This attribute is updated to the value chosen by the user.

return
PrintQualityType.DRAFT, PrintQualityType.NORMAL, or PrintQualityType.HIGH

        return printQuality;
    
public int[]getPrinterResolution()
Returns the print resolution for pages using these attributes. Index 0 of the array specifies the cross feed direction resolution (typically the horizontal resolution). Index 1 of the array specifies the feed direction resolution (typically the vertical resolution). Index 2 of the array specifies whether the resolutions are in dots per inch or dots per centimeter. 3 denotes dots per inch. 4 denotes dots per centimeter.

return
an integer array of 3 elements. The first element must be greater than 0. The second element must be must be greater than 0. The third element must be either 3 or 4.

        // Return a copy because otherwise client code could circumvent the
        // the checks made in setPrinterResolution by modifying the
        // returned array.
        int[] copy = new int[3];
	copy[0] = printerResolution[0];
	copy[1] = printerResolution[1];
	copy[2] = printerResolution[2];
        return copy;
    
public inthashCode()
Returns a hash code value for this PageAttributes.

return
the hash code.

        return (color.hashCode() << 31 ^
		media.hashCode() << 24 ^
		orientationRequested.hashCode() << 23 ^
		origin.hashCode() << 22 ^
		printQuality.hashCode() << 20 ^
		printerResolution[2] >> 2 << 19 ^
		printerResolution[1] << 10 ^
		printerResolution[0]);
    
public voidset(java.awt.PageAttributes obj)
Sets all of the attributes of this PageAttributes to the same values as the attributes of obj.

param
obj the PageAttributes to copy.

        color = obj.color;
	media = obj.media;
	orientationRequested = obj.orientationRequested;
	origin = obj.origin;
	printQuality = obj.printQuality;
	// okay because we never modify the contents of printerResolution
	printerResolution = obj.printerResolution;
    
public voidsetColor(java.awt.PageAttributes$ColorType color)
Specifies whether pages using these attributes will be rendered in color or monochrome. Not specifying this attribute is equivalent to specifying ColorType.MONOCHROME.

param
color ColorType.COLOR or ColorType.MONOCHROME.
throws
IllegalArgumentException if color is null.

        if (color == null) {
	    throw new IllegalArgumentException("Invalid value for attribute "+
					       "color");
	}
        this.color = color;
    
public voidsetMedia(java.awt.PageAttributes$MediaType media)
Specifies the desired paper size for pages using these attributes. The actual paper size will be determined by the limitations of the target printer. If an exact match cannot be found, an implementation will choose the closest possible match. Not specifying this attribute is equivalent to specifying the default size for the default locale. The default size for locales in the United States and Canada is MediaType.NA_LETTER. The default size for all other locales is MediaType.ISO_A4.

param
media one of the constant fields of the MediaType class.
throws
IllegalArgumentException if media is null.

        if (media == null) {
	    throw new IllegalArgumentException("Invalid value for attribute "+
					       "media");
	}
        this.media = media;
    
public voidsetMediaToDefault()
Sets the paper size for pages using these attributes to the default size for the default locale. The default size for locales in the United States and Canada is MediaType.NA_LETTER. The default size for all other locales is MediaType.ISO_A4.

	String defaultCountry = Locale.getDefault().getCountry();
	if (defaultCountry != null &&
	    (defaultCountry.equals(Locale.US.getCountry()) ||
	     defaultCountry.equals(Locale.CANADA.getCountry()))) {
	    setMedia(MediaType.NA_LETTER);
	} else {
	    setMedia(MediaType.ISO_A4);
	}
    
public voidsetOrientationRequested(java.awt.PageAttributes$OrientationRequestedType orientationRequested)
Specifies the print orientation for pages using these attributes. Not specifying the property is equivalent to specifying OrientationRequestedType.PORTRAIT.

param
orientationRequested OrientationRequestedType.PORTRAIT or OrientationRequestedType.LANDSCAPE.
throws
IllegalArgumentException if orientationRequested is null.

        if (orientationRequested == null) {
	    throw new IllegalArgumentException("Invalid value for attribute "+
					       "orientationRequested");
	}
        this.orientationRequested = orientationRequested;
    
public voidsetOrientationRequested(int orientationRequested)
Specifies the print orientation for pages using these attributes. Specifying 3 denotes portrait. Specifying 4 denotes landscape. Specifying any other value will generate an IllegalArgumentException. Not specifying the property is equivalent to calling setOrientationRequested(OrientationRequestedType.PORTRAIT).

param
orientationRequested 3 or 4
throws
IllegalArgumentException if orientationRequested is not 3 or 4

        switch (orientationRequested) {
	  case 3:
	    setOrientationRequested(OrientationRequestedType.PORTRAIT);
	    break;
	  case 4:
	    setOrientationRequested(OrientationRequestedType.LANDSCAPE);
	    break;
	  default:
	    // This will throw an IllegalArgumentException
	    setOrientationRequested(null);
	    break;
	}
    
public voidsetOrientationRequestedToDefault()
Sets the print orientation for pages using these attributes to the default. The default orientation is portrait.

        setOrientationRequested(OrientationRequestedType.PORTRAIT);
    
public voidsetOrigin(java.awt.PageAttributes$OriginType origin)
Specifies whether drawing at (0, 0) to pages using these attributes draws at the upper-left corner of the physical page, or at the upper-left corner of the printable area. (Note that these locations could be equivalent.) Not specifying the property is equivalent to specifying OriginType.PHYSICAL.

param
origin OriginType.PHYSICAL or OriginType.PRINTABLE
throws
IllegalArgumentException if origin is null.

        if (origin == null) {
	    throw new IllegalArgumentException("Invalid value for attribute "+
					       "origin");
	}
        this.origin = origin;
    
public voidsetPrintQuality(java.awt.PageAttributes$PrintQualityType printQuality)
Specifies the print quality for pages using these attributes. Not specifying the property is equivalent to specifying PrintQualityType.NORMAL.

param
printQuality PrintQualityType.DRAFT, PrintQualityType.NORMAL, or PrintQualityType.HIGH
throws
IllegalArgumentException if printQuality is null.

        if (printQuality == null) {
	    throw new IllegalArgumentException("Invalid value for attribute "+
					       "printQuality");
	}
        this.printQuality = printQuality;
    
public voidsetPrintQuality(int printQuality)
Specifies the print quality for pages using these attributes. Specifying 3 denotes draft. Specifying 4 denotes normal. Specifying 5 denotes high. Specifying any other value will generate an IllegalArgumentException. Not specifying the property is equivalent to calling setPrintQuality(PrintQualityType.NORMAL).

param
printQuality 3, 4, or 5
throws
IllegalArgumentException if printQuality is not 3 , 4, or 5

        switch (printQuality) {
	  case 3:
	    setPrintQuality(PrintQualityType.DRAFT);
	    break;
	  case 4:
	    setPrintQuality(PrintQualityType.NORMAL);
	    break;
	  case 5:
	    setPrintQuality(PrintQualityType.HIGH);
	    break;
	  default:
	    // This will throw an IllegalArgumentException
	    setPrintQuality(null);
	    break;
	}
    
public voidsetPrintQualityToDefault()
Sets the print quality for pages using these attributes to the default. The default print quality is normal.

        setPrintQuality(PrintQualityType.NORMAL);
    
public voidsetPrinterResolution(int[] printerResolution)
Specifies the desired print resolution for pages using these attributes. The actual resolution will be determined by the limitations of the implementation and the target printer. Index 0 of the array specifies the cross feed direction resolution (typically the horizontal resolution). Index 1 of the array specifies the feed direction resolution (typically the vertical resolution). Index 2 of the array specifies whether the resolutions are in dots per inch or dots per centimeter. 3 denotes dots per inch. 4 denotes dots per centimeter. Note that the 1.1 printing implementation (Toolkit.getPrintJob) requires that the feed and cross feed resolutions be the same. Not specifying the property is equivalent to calling setPrinterResolution(72).

param
printerResolution an integer array of 3 elements. The first element must be greater than 0. The second element must be must be greater than 0. The third element must be either 3 or 4.
throws
IllegalArgumentException if one or more of the above conditions is violated.

        if (printerResolution == null ||
	    printerResolution.length != 3 ||
	    printerResolution[0] <= 0 ||
	    printerResolution[1] <= 0 ||
	    (printerResolution[2] != 3 && printerResolution[2] != 4)) {
	    throw new IllegalArgumentException("Invalid value for attribute "+
					       "printerResolution");
	}
        // Store a copy because otherwise client code could circumvent the
        // the checks made above by holding a reference to the array and
	// modifying it after calling setPrinterResolution.
	int[] copy = new int[3];
	copy[0] = printerResolution[0];
	copy[1] = printerResolution[1];
	copy[2] = printerResolution[2];
	this.printerResolution = copy;
    
public voidsetPrinterResolution(int printerResolution)
Specifies the desired cross feed and feed print resolutions in dots per inch for pages using these attributes. The same value is used for both resolutions. The actual resolutions will be determined by the limitations of the implementation and the target printer. Not specifying the property is equivalent to specifying 72.

param
printerResolution an integer greater than 0.
throws
IllegalArgumentException if printerResolution is less than or equal to 0.

        setPrinterResolution(new int[] { printerResolution, printerResolution,
					 3 } );
    
public voidsetPrinterResolutionToDefault()
Sets the printer resolution for pages using these attributes to the default. The default is 72 dpi for both the feed and cross feed resolutions.

        setPrinterResolution(72);
    
public java.lang.StringtoString()
Returns a string representation of this PageAttributes.

return
the string representation.

        // int[] printerResolution = getPrinterResolution();
        return "color=" + getColor() + ",media=" + getMedia() +
	    ",orientation-requested=" + getOrientationRequested() +
	    ",origin=" + getOrigin() + ",print-quality=" + getPrintQuality() +
	    ",printer-resolution=[" + printerResolution[0] + "," +
	    printerResolution[1] + "," + printerResolution[2] + "]";