KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > PrintSettings


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.text;
20
21 import org.openide.options.ContextSystemOption;
22 import org.openide.util.HelpCtx;
23 import org.openide.util.NbBundle;
24
25 import java.awt.Font JavaDoc;
26 import java.awt.print.PageFormat JavaDoc;
27 import java.awt.print.Paper JavaDoc;
28 import java.awt.print.PrinterJob JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.ObjectOutput JavaDoc;
33
34 /** Settings for output window.
35 *
36 * @author Ales Novak
37 */

38 public final class PrintSettings extends ContextSystemOption {
39     // final because overrides Externalizable methods
40

41     /** serialVersionUID */
42     static final long serialVersionUID = -9102470021814206818L;
43
44     /** Constant for center position of page header. */
45     public static final int CENTER = 0x1;
46
47     /** Constant for right position of page header. */
48     public static final int RIGHT = 0x2;
49
50     /** Constant for left position of page header. */
51     public static final int LEFT = 0x0;
52
53     /** Property name of the wrap property */
54     public static final String JavaDoc PROP_PAGE_FORMAT = "pageFormat"; // NOI18N
55

56     /** Property name of the wrap property */
57     public static final String JavaDoc PROP_WRAP = "wrap"; // NOI18N
58

59     /** Property name of the header format property */
60     public static final String JavaDoc PROP_HEADER_FORMAT = "headerFormat"; // NOI18N
61

62     /** Property name of the footer format property */
63     public static final String JavaDoc PROP_FOOTER_FORMAT = "footerFormat"; // NOI18N
64

65     /** Property name of the header font property */
66     public static final String JavaDoc PROP_HEADER_FONT = "headerFont"; // NOI18N
67

68     /** Property name of the footer font property */
69     public static final String JavaDoc PROP_FOOTER_FONT = "footerFont"; // NOI18N
70

71     /** Property name of the header alignment property */
72     public static final String JavaDoc PROP_HEADER_ALIGNMENT = "headerAlignment"; // NOI18N
73

74     /** Property name of the footer alignment property */
75     public static final String JavaDoc PROP_FOOTER_ALIGNMENT = "footerAlignment"; // NOI18N
76

77     /** Property name of the line ascent correction property */
78     public static final String JavaDoc PROP_LINE_ASCENT_CORRECTION = "lineAscentCorrection"; // NOI18N
79
private static final String JavaDoc HELP_ID = "editing.printing"; // !!! NOI18N
80

81     /** PageFormat */
82     private static PageFormat JavaDoc pageFormat;
83
84     /** Wrap lines? */
85     private static boolean wrap = true;
86
87     /** Header format - see MessageFormat */
88     private static String JavaDoc headerFormat;
89
90     /** Footer format - see MessageFormat */
91     private static String JavaDoc footerFormat;
92
93     /** Header font */
94     private static Font JavaDoc headerFont;
95
96     /** Footer font */
97     private static Font JavaDoc footerFont;
98
99     /** Header alignment */
100     private static int headerAlignment = CENTER;
101
102     /** Footer alignment */
103     private static int footerAlignment = CENTER;
104
105     /** Line ascent correction parameter. Ranged from 0 to infinity.*/
106     private static float lineAscentCorrection = 1.0f;
107
108     /** Externalizes this SystemOption
109     * @param obtos
110     * @exception IOException
111     */

112     public void writeExternal(ObjectOutput JavaDoc obtos) throws IOException JavaDoc {
113         super.writeExternal(obtos);
114         obtos.writeBoolean(wrap);
115         obtos.writeObject(headerFormat);
116         obtos.writeObject(footerFormat);
117         obtos.writeObject(headerFont);
118         obtos.writeObject(footerFont);
119         obtos.writeInt(headerAlignment);
120         obtos.writeInt(footerAlignment);
121         externalizePageFormat(pageFormat, obtos);
122     }
123
124     /** Deexetrnalizes this SystemOption
125     * @param obtis
126     * @exception IOException
127     * @exception ClassNotFoundException
128     */

129     public void readExternal(ObjectInput JavaDoc obtis) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
130         super.readExternal(obtis);
131         wrap = obtis.readBoolean();
132         headerFormat = (String JavaDoc) obtis.readObject();
133         footerFormat = (String JavaDoc) obtis.readObject();
134         headerFont = (Font JavaDoc) obtis.readObject();
135         footerFont = (Font JavaDoc) obtis.readObject();
136         headerAlignment = obtis.readInt();
137         footerAlignment = obtis.readInt();
138         pageFormat = internalizePageFormat(obtis);
139     }
140
141     /** Writes a PageFormat instance into ObjectOutput
142     * @param pf
143     * @param obtos
144     */

145     private static void externalizePageFormat(PageFormat JavaDoc pf, ObjectOutput JavaDoc obtos)
146     throws IOException JavaDoc {
147         if (pf == null) {
148             obtos.writeInt(PageFormat.LANDSCAPE ^ PageFormat.REVERSE_LANDSCAPE ^ PageFormat.PORTRAIT);
149
150             return;
151         }
152
153         obtos.writeInt(pf.getOrientation());
154
155         Paper JavaDoc paper = pf.getPaper();
156
157         // paper size
158
obtos.writeDouble(paper.getWidth());
159         obtos.writeDouble(paper.getHeight());
160
161         // paper imageable area
162
obtos.writeDouble(paper.getImageableX());
163         obtos.writeDouble(paper.getImageableY());
164         obtos.writeDouble(paper.getImageableWidth());
165         obtos.writeDouble(paper.getImageableHeight());
166     }
167
168     /** Reads a PageFormat instance from ObjectInput
169     * @param obtis
170     * @return deserialized PageFormat instance
171     */

172     private static PageFormat JavaDoc internalizePageFormat(ObjectInput JavaDoc obtis)
173     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
174         PageFormat JavaDoc pf = new PageFormat JavaDoc();
175         Paper JavaDoc paper = pf.getPaper();
176         int etc = obtis.readInt();
177
178         if (etc == (PageFormat.LANDSCAPE ^ PageFormat.REVERSE_LANDSCAPE ^ PageFormat.PORTRAIT)) {
179             return null;
180         }
181
182         pf.setOrientation(etc);
183
184         // paper size
185
paper.setSize(obtis.readDouble(), obtis.readDouble());
186
187         // imageable
188
paper.setImageableArea(obtis.readDouble(), obtis.readDouble(), obtis.readDouble(), obtis.readDouble());
189         pf.setPaper(paper);
190
191         return pf;
192     }
193
194     public String JavaDoc displayName() {
195         return NbBundle.getMessage(PrintSettings.class, "CTL_Print_settings");
196     }
197
198     public HelpCtx getHelpCtx() {
199         return new HelpCtx(HELP_ID);
200     }
201
202     /** @return an instance of PageFormat
203     * The returned page format is either previously set by
204     * PageSetupAction or is acquired as a default PageFormat
205     * from supported PrinterJob
206     */

207     public static PageFormat JavaDoc getPageFormat(PrinterJob JavaDoc pj) {
208         if (pageFormat == null) {
209             pageFormat = pj.defaultPage();
210         }
211
212         return pageFormat;
213     }
214
215     /** @deprecated Use {@link #getPageFormat(PrinterJob)} instead. */
216     @Deprecated JavaDoc
217     public PageFormat JavaDoc getPageFormat() {
218         if (pageFormat == null) {
219             PrinterJob JavaDoc pj = PrinterJob.getPrinterJob();
220             pageFormat = pj.defaultPage(new PageFormat JavaDoc());
221             pj.cancel();
222         }
223
224         return pageFormat;
225     }
226
227     /** sets page format */
228     public void setPageFormat(PageFormat JavaDoc pf) {
229         if (pf == null) {
230             return;
231         }
232
233         if (pf.equals(pageFormat)) {
234             return;
235         }
236
237         PageFormat JavaDoc old = pageFormat;
238         pageFormat = pf;
239         firePropertyChange(PROP_PAGE_FORMAT, old, pageFormat);
240     }
241
242     public boolean getWrap() {
243         return wrap;
244     }
245
246     public void setWrap(boolean b) {
247         if (wrap == b) {
248             return;
249         }
250
251         wrap = b;
252         firePropertyChange(PROP_WRAP, (b ? Boolean.FALSE : Boolean.TRUE), (b ? Boolean.TRUE : Boolean.FALSE));
253     }
254
255     public String JavaDoc getHeaderFormat() {
256         if (headerFormat == null) {
257             headerFormat = NbBundle.getMessage(PrintSettings.class, "CTL_Header_format");
258         }
259
260         return headerFormat;
261     }
262
263     public void setHeaderFormat(String JavaDoc s) {
264         if (s == null) {
265             return;
266         }
267
268         if (s.equals(headerFormat)) {
269             return;
270         }
271
272         String JavaDoc of = headerFormat;
273         headerFormat = s;
274         firePropertyChange(PROP_HEADER_FORMAT, of, headerFormat);
275     }
276
277     public String JavaDoc getFooterFormat() {
278         if (footerFormat == null) {
279             footerFormat = NbBundle.getMessage(PrintSettings.class, "CTL_Footer_format");
280         }
281
282         return footerFormat;
283     }
284
285     public void setFooterFormat(String JavaDoc s) {
286         if (s == null) {
287             return;
288         }
289
290         if (s.equals(footerFormat)) {
291             return;
292         }
293
294         String JavaDoc of = footerFormat;
295         footerFormat = s;
296         firePropertyChange(PROP_FOOTER_FORMAT, of, footerFormat);
297     }
298
299     public Font JavaDoc getHeaderFont() {
300         if (headerFont == null) {
301             headerFont = new Font JavaDoc("Monospaced", java.awt.Font.PLAIN, 6); // NOI18N
302
}
303
304         return headerFont;
305     }
306
307     public void setHeaderFont(Font JavaDoc f) {
308         if (f == null) {
309             return;
310         }
311
312         if (f.equals(headerFont)) {
313             return;
314         }
315
316         Font JavaDoc old = headerFont;
317         headerFont = f;
318         firePropertyChange(PROP_HEADER_FONT, old, headerFont);
319     }
320
321     public Font JavaDoc getFooterFont() {
322         if (footerFont == null) {
323             footerFont = getHeaderFont();
324         }
325
326         return footerFont;
327     }
328
329     public void setFooterFont(Font JavaDoc f) {
330         if (f == null) {
331             return;
332         }
333
334         if (f.equals(footerFont)) {
335             return;
336         }
337
338         Font JavaDoc old = headerFont;
339         footerFont = f;
340         firePropertyChange(PROP_FOOTER_FONT, old, footerFont);
341     }
342
343     public int getHeaderAlignment() {
344         return headerAlignment;
345     }
346
347     public void setHeaderAlignment(int alignment) {
348         if (alignment == headerAlignment) {
349             return;
350         }
351
352         if ((alignment != LEFT) && (alignment != CENTER) && (alignment != RIGHT)) {
353             throw new IllegalArgumentException JavaDoc();
354         }
355
356         int old = headerAlignment;
357         headerAlignment = alignment;
358         firePropertyChange(PROP_HEADER_ALIGNMENT, new Integer JavaDoc(old), new Integer JavaDoc(headerAlignment));
359     }
360
361     public int getFooterAlignment() {
362         return footerAlignment;
363     }
364
365     public void setFooterAlignment(int alignment) {
366         if (alignment == footerAlignment) {
367             return;
368         }
369
370         if ((alignment != LEFT) && (alignment != CENTER) && (alignment != RIGHT)) {
371             throw new IllegalArgumentException JavaDoc();
372         }
373
374         int old = footerAlignment;
375         footerAlignment = alignment;
376         firePropertyChange(PROP_FOOTER_ALIGNMENT, new Integer JavaDoc(old), new Integer JavaDoc(footerAlignment));
377     }
378
379     /** Getter for lineAscentCorrection property. */
380     public float getLineAscentCorrection() {
381         return lineAscentCorrection;
382     }
383
384     /** Setter for lineAscentCorrection property.
385     * @param correction the correction
386     * @exception IllegalArgumentException if <tt>correction</tt> is less than 0.
387     */

388     public void setLineAscentCorrection(float correction) {
389         if (correction == lineAscentCorrection) {
390             return;
391         } else if (correction < 0) {
392             throw new IllegalArgumentException JavaDoc();
393         }
394
395         float old = lineAscentCorrection;
396         lineAscentCorrection = correction;
397         firePropertyChange(PROP_LINE_ASCENT_CORRECTION, new Float JavaDoc(old), new Float JavaDoc(lineAscentCorrection));
398     }
399
400     /** Property editor for alignment properties */
401     public static class AlignmentEditor extends java.beans.PropertyEditorSupport JavaDoc {
402         private String JavaDoc sCENTER;
403         private String JavaDoc sRIGHT;
404         private String JavaDoc sLEFT;
405         private String JavaDoc[] tags = new String JavaDoc[] {
406                 sLEFT = NbBundle.getMessage(PrintSettings.class, "CTL_LEFT"),
407                 sCENTER = NbBundle.getMessage(PrintSettings.class, "CTL_CENTER"),
408                 sRIGHT = NbBundle.getMessage(PrintSettings.class, "CTL_RIGHT")
409             };
410
411         public String JavaDoc[] getTags() {
412             return tags;
413         }
414
415         public String JavaDoc getAsText() {
416             return tags[((Integer JavaDoc) getValue()).intValue()];
417         }
418
419         public void setAsText(String JavaDoc s) {
420             if (s.equals(sLEFT)) {
421                 setValue(new Integer JavaDoc(0));
422             } else if (s.equals(sCENTER)) {
423                 setValue(new Integer JavaDoc(1));
424             } else {
425                 setValue(new Integer JavaDoc(2));
426             }
427         }
428     }
429
430     /** Property editor for PageFormat instances */
431     public static class PageFormatEditor extends java.beans.PropertyEditorSupport JavaDoc {
432         /** No text */
433         public String JavaDoc getAsText() {
434             return null;
435         }
436
437         /* @return <tt>true</tt> */
438         public boolean supportsCustomEditor() {
439             return true;
440         }
441
442         /**
443         * @return <tt>null</tt> Shows pageDialog, however.
444         */

445         public java.awt.Component JavaDoc getCustomEditor() {
446             PageFormat JavaDoc pf = (PageFormat JavaDoc) getValue();
447             PrinterJob JavaDoc pj = PrinterJob.getPrinterJob();
448             PageFormat JavaDoc npf = pj.pageDialog(pf);
449
450             //setValue(npf);
451
PrintSettings.findObject(PrintSettings.class).setPageFormat((PageFormat JavaDoc) npf.clone());
452             pj.cancel();
453
454             return null;
455         }
456     }
457 }
458
Popular Tags