KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > CPaper


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.awt.*;
17 import java.awt.print.*;
18 import javax.print.attribute.*;
19 import javax.print.attribute.standard.*;
20 import java.util.*;
21
22 import org.compiere.util.*;
23
24 /**
25  * Compiere Paper
26  *
27  * @author Jorg Janke
28  * @version $Id: CPaper.java,v 1.12 2002/09/16 04:03:12 jjanke Exp $
29  */

30 public class CPaper extends Paper
31 {
32     /**
33      * Constructor.
34      * Derive Paper from PageForamt
35      * @param pf PageFormat
36      */

37     public CPaper (PageFormat pf)
38     {
39         super();
40         m_landscape = pf.getOrientation() != PageFormat.PORTRAIT;
41         // try to find MediaSize
42
float x = (float)pf.getWidth();
43         float y = (float)pf.getHeight();
44         MediaSizeName msn = MediaSize.findMedia (x/72, y/72, MediaSize.INCH);
45         MediaSize ms = null;
46         if (msn == null)
47             msn = MediaSize.findMedia (y/72, x/72, MediaSize.INCH); // flip it
48
if (msn != null)
49             ms = MediaSize.getMediaSizeForName(msn);
50         setMediaSize(ms, m_landscape);
51         // set size directly
52
setSize(pf.getWidth(), pf.getHeight());
53         setImageableArea(pf.getImageableX(), pf.getImageableY(),
54             pf.getImageableWidth(), pf.getImageableHeight());
55     } // CPaper
56

57     /**
58      * Constructor.
59      * Get Media Size from Default Language
60      * @param landscape true if landscape, false if portrait
61      */

62     public CPaper (boolean landscape)
63     {
64         this (Language.getLanguage(), landscape);
65     } // CPaper
66

67     /**
68      * Constructor.
69      * Get Media Size from Language,
70      * @param language language to derive media size
71      * @param landscape true if landscape, false if portrait
72      */

73     private CPaper (Language language, boolean landscape)
74     {
75         this (language.getMediaSize(), landscape);
76     } // CPaper
77

78     /**
79      * Detail Constructor 1/2 inch on all sides
80      * @param mediaSize media size
81      * @param landscape true if landscape, false if portrait
82      */

83     private CPaper (MediaSize mediaSize, boolean landscape)
84     {
85         this (mediaSize, landscape, 36, 36, 36, 36);
86     } // CPaper
87

88     /**
89      * Detail Constructor
90      * @param mediaSize media size
91      * @param left x in 1/72 inch
92      * @param top y in 1/72 inch
93      * @param right right x in 1/72
94      * @param bottom bottom y in 1/72
95      * @param landscape true if landscape, false if portrait
96      */

97     public CPaper (MediaSize mediaSize, boolean landscape,
98         double left, double top, double right, double bottom)
99     {
100         super();
101         setMediaSize (mediaSize, landscape);
102         setImageableArea(left, top, getWidth()-left-right, getHeight()-top-bottom);
103     } // CPaper
104

105     /** Media size */
106     private MediaSize m_mediaSize;
107     /** Landscape flag */
108     private boolean m_landscape = false;
109
110
111     /**
112      * Set Media Size
113      * @param mediaSize media size
114      * @param landscape true if landscape, false if portrait
115      */

116     public void setMediaSize (MediaSize mediaSize, boolean landscape)
117     {
118         if (mediaSize == null)
119             throw new IllegalArgumentException JavaDoc("CPaper.setMediaSize - mediaSize is null");
120         m_mediaSize = mediaSize;
121         m_landscape = landscape;
122
123         // Get Sise in Inch * 72
124
double width = m_mediaSize.getX (MediaSize.INCH) * 72;
125         double height = m_mediaSize.getY (MediaSize.INCH) * 72;
126         // Set Size
127
setSize (width, height);
128         Log.trace(Log.l6_Database, "CPaper.setMediaSize " + mediaSize.getMediaSizeName(), m_mediaSize + " - Landscape=" + m_landscape);
129     } // setMediaSize
130

131     /**
132      * Get Media Size
133      * @return media size
134      */

135     public MediaSizeName getMediaSizeName()
136     {
137         return m_mediaSize.getMediaSizeName();
138     } // getMediaSizeName
139

140     /**
141      * Get Media Size
142      * @return media size
143      */

144     public MediaSize getMediaSize()
145     {
146         return m_mediaSize;
147     } // getMediaSize
148

149     /**
150      * Get Printable Media Area
151      * @return Printable Area
152      */

153     public MediaPrintableArea getMediaPrintableArea()
154     {
155         MediaPrintableArea area = new MediaPrintableArea ((float)getImageableX()/72, (float)getImageableY()/72,
156             (float)getImageableWidth()/72, (float)getImageableHeight()/72, MediaPrintableArea.INCH);
157     // Log.trace(Log.l6_Database, "CPaper.getMediaPrintableArea", area.toString(MediaPrintableArea.INCH, "\""));
158
return area;
159     } // getMediaPrintableArea
160

161     /**
162      * Get Printable Media Area
163      * @param area Printable Area
164      */

165     public void setMediaPrintableArea (MediaPrintableArea area)
166     {
167         int inch = MediaPrintableArea.INCH;
168         Log.trace(Log.l6_Database, "CPaper.setMediaPrintableArea", area.toString(inch, "\""));
169         setImageableArea(area.getX(inch)*72, area.getY(inch)*72,
170             area.getWidth(inch)*72, area.getHeight(inch)*72);
171     } // setMediaPrintableArea
172

173     /**
174      * Is Landscape
175      * @return true if landscape
176      */

177     public boolean isLandscape()
178     {
179         return m_landscape;
180     } // isLandscape
181

182     /*************************************************************************/
183
184     /**
185      * Show Dialog and Set Paper
186      * @param job printer job
187      * @return true if changed.
188      */

189     public boolean pageSetupDialog(PrinterJob job)
190     {
191         PrintRequestAttributeSet prats = getPrintRequestAttributeSet();
192         // Page Dialog
193
PageFormat pf = job.pageDialog(prats);
194         setPrintRequestAttributeSet(prats);
195         return true;
196     } // pageSetupDialog
197

198     /**
199      * Return Print Request Attributes
200      * @return PrintRequestAttributeSet
201      */

202     public PrintRequestAttributeSet getPrintRequestAttributeSet()
203     {
204         PrintRequestAttributeSet pratts = new HashPrintRequestAttributeSet();
205         // media-printable-area = (25.4,25.4)->(165.1,228.6)mm - class javax.print.attribute.standard.MediaPrintableArea
206
pratts.add(getMediaPrintableArea());
207         // orientation-requested = landscape - class javax.print.attribute.standard.OrientationRequested
208
if (isLandscape())
209             pratts.add(OrientationRequested.LANDSCAPE);
210         else
211             pratts.add(OrientationRequested.PORTRAIT);
212         // media = na-legal
213
pratts.add(getMediaSizeName());
214
215         return pratts;
216     } // getPrintRequestAttributes
217

218     /**
219      * Set Print Request Attributes
220      * @param prats PrintRequestAttributeSet
221      */

222     public void setPrintRequestAttributeSet (PrintRequestAttributeSet prats)
223     {
224         boolean landscape = m_landscape;
225         MediaSize ms = m_mediaSize;
226         MediaPrintableArea area = getMediaPrintableArea();
227
228         Attribute[] atts = prats.toArray();
229         for (int i = 0; i < atts.length; i++)
230         {
231             if (atts[i] instanceof OrientationRequested)
232             {
233                 OrientationRequested or = (OrientationRequested)atts[i];
234                 if (or.getName().equals(OrientationRequested.PORTRAIT.getName()))
235                     landscape = false;
236                 else
237                     landscape = true;
238             }
239             else if (atts[i] instanceof MediaSizeName)
240             {
241                 MediaSizeName msn = (MediaSizeName)atts[i];
242                 ms = MediaSize.getMediaSizeForName(msn);
243             }
244             else if (atts[i] instanceof MediaPrintableArea)
245             {
246                 area = (MediaPrintableArea)atts[i];
247             }
248             else // unhandeled
249
System.out.println(atts[i].getName() + " = " + atts[i] + " - " + atts[i].getCategory());
250         }
251         //
252
setMediaSize(ms, landscape);
253         setMediaPrintableArea(area);
254     } // getPrintRequestAttributes
255

256
257     /*************************************************************************/
258
259     /**
260      * Get the Page Format for the Papaer
261      * @return Page Format
262      */

263     public PageFormat getPageFormat()
264     {
265         PageFormat pf = new PageFormat();
266         pf.setPaper(this);
267         int orient = PageFormat.PORTRAIT;
268         if (m_landscape)
269             orient = PageFormat.LANDSCAPE;
270         pf.setOrientation(orient);
271         return pf;
272     } // getPageFormat
273

274     /*************************************************************************/
275
276     /**
277      * Get String Representation
278      * @return info
279      */

280     public String JavaDoc toString()
281     {
282         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("CPaper[");
283         sb.append(getWidth()/72).append("x").append(getHeight()/72).append('"')
284             .append(m_landscape ? " Landscape " : " Portrait ")
285             .append("x=").append(getImageableX())
286             .append(",y=").append(getImageableY())
287             .append(" w=").append(getImageableWidth())
288             .append(",h=").append(getImageableHeight())
289             .append("]");
290         return sb.toString();
291     } // toString
292

293     /**
294      * Get "nice" String Representation
295      * @param ctx context
296      * @return info
297      */

298     public String JavaDoc toString (Properties ctx)
299     {
300         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
301         // Print Media size
302
sb.append(m_mediaSize.getMediaSizeName());
303         // Print dimension
304
String JavaDoc name = m_mediaSize.getMediaSizeName().toString();
305         if (!name.startsWith("iso"))
306             sb.append(" - ").append(m_mediaSize.toString(MediaSize.INCH,"\""))
307                 .append(" (").append(getMediaPrintableArea().toString(MediaPrintableArea.INCH,"\""));
308         if (!name.startsWith("na"))
309             sb.append(" - ").append(m_mediaSize.toString(MediaSize.MM,"mm"))
310                 .append(" (").append(getMediaPrintableArea().toString(MediaPrintableArea.MM,"mm"));
311         // Print Orientation
312
sb.append(") - ")
313             .append(Msg.getMsg(ctx, m_landscape ? "Landscape" : "Portrait"));
314         return sb.toString();
315     } // toString
316

317     /**
318      * Equals
319      * @param obj compare
320      * @return true if equal
321      */

322     public boolean equals (Object JavaDoc obj)
323     {
324         if (obj instanceof CPaper)
325         {
326             CPaper cp = (CPaper)obj;
327             if (cp.isLandscape() != m_landscape)
328                 return false;
329             // media size is more descriptive
330
if (getImageableX() == cp.getImageableX() && getImageableY() == cp.getImageableY()
331                 && getImageableWidth() == cp.getImageableWidth() && getImageableHeight() == cp.getImageableHeight())
332                 return true;
333         }
334         return false;
335     } // equals
336

337     /*************************************************************************/
338
339     /**
340      * Get Width in 1/72 inch
341      * @param orientationCorrected correct for orientation
342      * @return width
343      */

344     public double getWidth (boolean orientationCorrected)
345     {
346         if (orientationCorrected && m_landscape)
347             return super.getHeight();
348         return super.getWidth();
349     }
350
351     /**
352      * Get Height in 1/72 inch
353      * @param orientationCorrected correct for orientation
354      * @return height
355      */

356     public double getHeight (boolean orientationCorrected)
357     {
358         if (orientationCorrected && m_landscape)
359             return super.getWidth();
360         return super.getHeight();
361     }
362
363     /**
364      * Get Image Y in 1/72 inch
365      * @param orientationCorrected correct for orientation
366      * @return imagable Y
367      */

368     public double getImageableY (boolean orientationCorrected)
369     {
370         if (orientationCorrected && m_landscape)
371             return super.getImageableX();
372         return super.getImageableY();
373     }
374
375     /**
376      * Get Image X in 1/72 inch
377      * @param orientationCorrected correct for orientation
378      * @return imagable X
379      */

380     public double getImageableX (boolean orientationCorrected)
381     {
382         if (orientationCorrected && m_landscape)
383             return super.getImageableY();
384         return super.getImageableX();
385     }
386
387     /**
388      * Get Image Height in 1/72 inch
389      * @param orientationCorrected correct for orientation
390      * @return imagable height
391      */

392     public double getImageableHeight (boolean orientationCorrected)
393     {
394         if (orientationCorrected && m_landscape)
395             return super.getImageableWidth();
396         return super.getImageableHeight();
397     }
398     /**
399      * Get Image Width in 1/72 inch
400      * @param orientationCorrected correct for orientation
401      * @return imagable width
402      */

403     public double getImageableWidth (boolean orientationCorrected)
404     {
405         if (orientationCorrected && m_landscape)
406             return super.getImageableHeight();
407         return super.getImageableWidth();
408     }
409
410     /**
411      * Get Margin
412      * @param orientationCorrected correct for orientation
413      * @return margin
414      */

415     public Insets getMargin (boolean orientationCorrected)
416     {
417         return new Insets ((int)getImageableY(orientationCorrected), // top
418
(int)getImageableX(orientationCorrected), // left
419
(int)(getHeight(orientationCorrected)-getImageableY(orientationCorrected)-getImageableHeight(orientationCorrected)), // bottom
420
(int)(getWidth(orientationCorrected)-getImageableX(orientationCorrected)-getImageableWidth(orientationCorrected))); // right
421
} // getMargin
422

423 } // CPapaer
424
Popular Tags