📄 jfpageformat.java
字号:
/**
* $Id:JFPageFormat.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.decorate;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.awt.Color;
import java.awt.Font;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.Dimension;
import java.awt.Toolkit;
import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.JFVersion;
/**
* Page format class encapsulates java.awt.print.PageFormat and some storage methods from abstractObject.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFPageFormat extends AbstractObject{
//*****************************************************************
// XML Tags
//*****************************************************************
/**
* A XML string tag represents a page format.
*/
public static final String XML_PAGEFORMAT ="PageFormat";
/**
* A XML string tag represents the page width.
*/
public static final String XML_WIDTH ="pageWidth";
/**
* A XML string tag represents the page height
*/
public static final String XML_HEIGHT ="pageHeight";
/**
* A XML string tag represents the left margin of a page.
*/
public static final String XML_LEFTMARGIN ="leftMargin";
/**
* A XML string tag represents the right margin of a page.
*/
public static final String XML_RIGHTMARGIN ="rightMargin";
/**
* A XML string tag represents the top margin of a page.
*/
public static final String XML_TOPMARGIN ="topMargin";
/**
* A XML string tag represents the bottom margin of a page.
*/
public static final String XML_BOTTOMMARGIN ="bottomMargin";
/**
* A XML string tag represents the orientation of a page.
*/
public static final String XML_ORIENTATION ="orientation";
/** Page types*/
public static final int PAGETYPE_A0 =1;
public static final int PAGETYPE_A1 =2;
public static final int PAGETYPE_A2 =3;
public static final int PAGETYPE_A3 =4;
public static final int PAGETYPE_A4 =5;
public static final int PAGETYPE_A4SMALL =6;
public static final int PAGETYPE_A5 =7;
public static final int PAGETYPE_B0 =20;
public static final int PAGETYPE_B1 =21;
public static final int PAGETYPE_B2 =22;
public static final int PAGETYPE_B3 =23;
public static final int PAGETYPE_B4 =24;
public static final int PAGETYPE_B5 =25;
public static final int PAGETYPE_CUSTOM =50;
/** screen resolution, pixel per inch */
public static final int INCH_SCREEN = Toolkit.getDefaultToolkit().getScreenResolution();
public static final int INCH = 72;
/** page size for each page type, unit in pixels */
public static final Dimension PAGESIZE_A0 =new Dimension((int)(33.11 * INCH),(int)(46.81 * INCH));
public static final Dimension PAGESIZE_A1 =new Dimension((int)(23.39 * INCH),(int)(33.11 * INCH));
public static final Dimension PAGESIZE_A2 =new Dimension((int)(16.54 * INCH),(int)(23.39 * INCH));
public static final Dimension PAGESIZE_A3 =new Dimension((int)(11.69 * INCH),(int)(16.54 * INCH));
public static final Dimension PAGESIZE_A4 =new Dimension((int)(8.27 * INCH),(int)(11.69 * INCH));
public static final Dimension PAGESIZE_A4SMALL =new Dimension((int)(8.27 * INCH),(int)(11.22 * INCH));
public static final Dimension PAGESIZE_A5 =new Dimension((int)(5.83 * INCH),(int)(8.27 * INCH));
public static final Dimension PAGESIZE_B0 =new Dimension((int)(39.37 * INCH),(int)(55.67 * INCH));
public static final Dimension PAGESIZE_B1 =new Dimension((int)(27.83 * INCH),(int)(39.37 * INCH));
public static final Dimension PAGESIZE_B2 =new Dimension((int)(19.68 * INCH),(int)(27.83 * INCH));
public static final Dimension PAGESIZE_B3 =new Dimension((int)(13.90 * INCH),(int)(19.68 * INCH));
public static final Dimension PAGESIZE_B4 =new Dimension((int)(9.84 * INCH),(int)(13.90 * INCH));
public static final Dimension PAGESIZE_B5 =new Dimension((int)(6.93 * INCH),(int)(9.84 * INCH));
public static final Dimension PAGESIZE_DEFAULT=new Dimension(PAGESIZE_A4);
/** A page dimension is a 72 times value of an inch, but on screen that would be another times, e.g. 96 */
public static Dimension getScreenDimension(Dimension d){
double w =d.getWidth();
double h =d.getHeight();
return new Dimension((int)(w/INCH*INCH_SCREEN),(int)(h/INCH*INCH_SCREEN));
}
/** A page size is a 72 times value of an inch, but on screen that would be another times, e.g. 96 */
public static int getScreenSize(int size){
double newSize=size;
return (int)(newSize/INCH*INCH_SCREEN);
}
/** page width in pixels. */
private double m_width=PAGESIZE_DEFAULT.getWidth();
public double getWidth(){
return m_width;
}
public void setWidth(double width){
m_width =width;
}
/** page height in pixels. */
private double m_height=PAGESIZE_DEFAULT.getHeight();
public double getHeight(){
return m_height;
}
public void setHeight(double height){
m_height =height;
}
/** left marin of page in pixels. */
private double m_leftMargin=INCH/4;
public double getLeftMargin(){
return m_leftMargin;
}
public void setLeftMargin(double leftMargin){
m_leftMargin =leftMargin;
}
/** right marin of page in pixels. */
private double m_rightMargin=INCH/4;
public double getRightMargin(){
return m_rightMargin;
}
public void setRightMargin(double rightMargin){
m_rightMargin =rightMargin;
}
/** top marin of page in pixels. */
private double m_topMargin=INCH/4;
public double getTopMargin(){
return m_topMargin;
}
public void setTopMargin(double topMargin){
m_topMargin =topMargin;
}
/** bottom marin of page in pixels. */
private double m_bottomMargin=INCH/4;
public double getBottomMargin(){
return m_bottomMargin;
}
public void setBottomMargin(double bottomMargin){
m_bottomMargin =bottomMargin;
}
/** orientation of page*/
private int m_orientation=PageFormat.PORTRAIT;
public int getOrientation(){
return m_orientation;
}
public void setOrientation(int orientation){
m_orientation =orientation;
}
/** a page format object*/
private PageFormat m_pageFormat=new PageFormat();
public PageFormat getPageFormat(){
//directly change the paper of a PageForamt will not affect the paper of this PageFormat,
//so we create a new Paper object then use setPaper() method.
Paper p =new Paper();
p.setSize(m_width,m_height);
p.setImageableArea(m_leftMargin,m_topMargin,m_width-m_leftMargin-m_rightMargin,m_height-m_topMargin-m_bottomMargin);
m_pageFormat.setOrientation(m_orientation);
m_pageFormat.setPaper(p);
setPageFormat(m_pageFormat);
return m_pageFormat;
}
public void setPageFormat(PageFormat pageFormat){
Paper p =pageFormat.getPaper();
m_leftMargin =p.getImageableX();
m_topMargin =p.getImageableY();
m_rightMargin =p.getWidth() - p.getImageableWidth() - m_leftMargin;
m_bottomMargin =p.getHeight() - p.getImageableHeight() - m_topMargin;
m_height =p.getHeight();
m_width =p.getWidth();
m_orientation =pageFormat.getOrientation();
}
/**
* Constructor for JFPageFormat
*/
public JFPageFormat(){
setObjectType(ShapeConst.DECORATETYPE_PAGEFORMAT);
setXMLTag(XML_PAGEFORMAT);
}
/**
* Convert this object to String
*
* @return An string represents the content of the object
*
*/
public String toString(){
StringBuffer buf =new StringBuffer();
buf.append(super.toString());
buf.append(";leftMargin="); buf.append(m_leftMargin);
buf.append(";topMargin="); buf.append(m_topMargin);
buf.append(";rightMargin="); buf.append(m_rightMargin);
buf.append(";bottomMargin="); buf.append(m_bottomMargin);
buf.append(";height="); buf.append(m_height);
buf.append(";width="); buf.append(m_width);
buf.append(";orientation="); buf.append(m_orientation);
return buf.toString();
}
/**
* Creates a new AbstractObject of the same class and with the same contents as this object.
* This method implements the method defined in AbstractObject.
*
* @return A clone of this class.
*
*/
protected AbstractObject cloneMe() throws CloneNotSupportedException{
return new JFPageFormat();
}
/**
* Creates a new object of the same class and with the same contents as this object.
*
* @return A clone of this instance.
*
*/
public Object clone() throws CloneNotSupportedException{
try{
JFPageFormat pageFormat =(JFPageFormat) super.clone();
pageFormat.m_leftMargin =this.m_leftMargin;
pageFormat.m_rightMargin =this.m_rightMargin;
pageFormat.m_topMargin =this.m_topMargin;
pageFormat.m_bottomMargin =this.m_bottomMargin;
pageFormat.m_height =this.m_height;
pageFormat.m_width =this.m_width;
pageFormat.m_orientation =this.m_orientation;
return pageFormat;
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
return super.hashCode() ^
(int)m_leftMargin ^
(int)m_rightMargin ^
(int)m_topMargin ^
(int)m_bottomMargin ^
(int)m_height ^
(int)m_width ^
m_orientation
;
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (!super.equals(obj))
return false;
if (obj == this)
return true;
if (!(obj instanceof JFPageFormat))
return false;
JFPageFormat pageFormat= (JFPageFormat)obj;
return ((int)m_leftMargin==(int)pageFormat.m_leftMargin) &&
((int)m_rightMargin==(int)pageFormat.m_rightMargin) &&
((int)m_topMargin==(int)pageFormat.m_topMargin) &&
((int)m_bottomMargin==(int)pageFormat.m_bottomMargin) &&
((int)m_height==(int)pageFormat.m_height) &&
((int)m_width==(int)pageFormat.m_width) &&
(m_orientation==pageFormat.m_orientation)
;
}
/**
* Append necessary xml child for current element,
* this method will be called internally by toDOM.
*
* @param element A XML element to append child xml nodes
* @param version A file version notification so this object can obey the rules to save data.
*
*/
protected void appendChildToDOM(Element element,JFVersion version){
if (element==null)
return;
super.appendChildToDOM(element,version);
element.addChild(new Element(XML_LEFTMARGIN, m_leftMargin));
element.addChild(new Element(XML_RIGHTMARGIN, m_rightMargin));
element.addChild(new Element(XML_TOPMARGIN, m_topMargin));
element.addChild(new Element(XML_BOTTOMMARGIN, m_bottomMargin));
element.addChild(new Element(XML_HEIGHT, m_height));
element.addChild(new Element(XML_WIDTH, m_width));
element.addChild(new Element(XML_ORIENTATION, m_orientation));
}
/**
* Extract needed xml child from current element,
* this method will be called internally by fromDOM.
*
* @param element An element used to extract needed xml child
* @param version A file version notification so this object can obey the rules to fetch data.
*
*/
protected void extractChildFromDOM(Element element,JFVersion version){
if (element==null)
return;
super.extractChildFromDOM(element,version);
m_leftMargin =Element.getDoubleValue(element.getChild(XML_LEFTMARGIN));
m_rightMargin =Element.getDoubleValue(element.getChild(XML_RIGHTMARGIN));
m_topMargin =Element.getDoubleValue(element.getChild(XML_TOPMARGIN));
m_bottomMargin =Element.getDoubleValue(element.getChild(XML_BOTTOMMARGIN));
m_height =Element.getDoubleValue(element.getChild(XML_HEIGHT));
m_width =Element.getDoubleValue(element.getChild(XML_WIDTH));
m_orientation =Element.getIntValue(element.getChild(XML_ORIENTATION));
}
/**
* Save this object to a binary stream <br>
*
* @param stream An binary output stream
* @param version A file version notification so this object can obey the rules to save data.
*
* @exception java.io.IOException
*
*/
public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
super.saveToStream(stream,version);
stream.writeDouble(m_leftMargin);
stream.writeDouble(m_rightMargin);
stream.writeDouble(m_topMargin);
stream.writeDouble(m_bottomMargin);
stream.writeDouble(m_height);
stream.writeDouble(m_width);
stream.writeInt(m_orientation);
}
/**
* Load object data from a binary stream <br>
*
* @param stream An binary input stream
*
* @param skipHead Skip head 'TYPE' check, an shape object should always
* has its own shape-type stored, if this shape-type has already been readed,
* this loadFromStream should/could not read the type anymore.
*
* @param version A file version notification so this object can obey the rules to fetch data.
* @exception java.io.IOException
*
*/
public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
super.loadFromStream(stream,skipHead,version);
m_leftMargin =stream.readDouble();
m_rightMargin =stream.readDouble();
m_topMargin =stream.readDouble();
m_bottomMargin =stream.readDouble();
m_height =stream.readDouble();
m_width =stream.readDouble();
m_orientation =stream.readInt();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -