KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.awt.*;
23 import java.awt.font.*;
24 import java.awt.geom.*;
25 import java.awt.print.*;
26 import java.text.*;
27 import java.util.*;
28 import java.util.List JavaDoc;
29 import javax.swing.text.*;
30 import org.openide.*;
31 import org.openide.util.Exceptions;
32 import org.openide.util.NbBundle;
33
34
35 /** The class creates from an instance of AttributedCharacterIterator
36  * a java.awt.print.Pageable object.
37  *
38  * @author Ales Novak
39  */

40 final class DefaultPrintable extends Object JavaDoc implements Printable {
41     /** Number of args. */
42     private static final int ARG_SIZE = 3;
43
44     /** Options */
45     private static PrintSettings printSettings;
46
47     /** Font for CancellationDialog */
48     private static Font fontInstance;
49
50     /**
51      * for each item is created new LineBreakMeasurer
52      */

53     private AttributedCharacterIterator[] styledTexts;
54
55     /** expected page */
56
57     // private int pageno;
58

59     /** Start page */
60     private int startPage = -1;
61
62     /** created text layouts */
63     private List JavaDoc<TextLayout> textLayouts;
64
65     /** page indices to textLayouts list */
66     private int[] pageIndices;
67
68     /** pageIndices size */
69     private int pageIndicesSize;
70
71     /** iterator over textLayouts */
72     private int currentLayout;
73
74     /** curent styledText entry */
75     private int currentStyledText;
76
77     /** current LineBreakMeasurer */
78     private LineBreakMeasurer lineBreakMeasurer;
79
80     /** maximal page */
81     private int maxPage;
82
83     /** text layouts that starts new line (those that were not created thanks to wrapping) */
84     private List JavaDoc<TextLayout> startLayouts;
85
86     /** page to line indexes (page 5 starts with line 112) */
87     private int[] lineIndices; // pageIndicesSize
88

89     /** Arguments for page. */
90     private Object JavaDoc[] pageArgs;
91
92     /** Header of each page. */
93     private MessageFormat header;
94
95     /** Should be header printed? */
96     private boolean printHeader;
97
98     /** Bottom of each page. */
99     private MessageFormat footer;
100
101     /** Should be footer printed? */
102     private boolean printFooter;
103
104     /** CancellationPanel */
105     private CancellationPanel cancellationPanel;
106
107     /** Dialog */
108     private Dialog cancellationDialog;
109
110     /**
111     * @param attrs an AttributedCharacterIterator
112     * @param filename
113     */

114     private DefaultPrintable(AttributedCharacterIterator[] iter, String JavaDoc filename) {
115         if ((iter == null) || (iter.length == 0)) {
116             throw new IllegalArgumentException JavaDoc();
117         }
118
119         if (printSettings == null) {
120             printSettings = PrintSettings.findObject(PrintSettings.class, true);
121         }
122
123         // bugfix for sun.awt.Bidi line 250
124
replaceEmptyIterators(iter);
125
126         styledTexts = iter;
127
128         // pageno = 0;
129
textLayouts = new ArrayList<TextLayout>(100); // 100 lines
130
pageIndices = new int[50];
131         pageIndicesSize = 0;
132         currentLayout = 0;
133         currentStyledText = 0;
134         lineBreakMeasurer = null;
135         maxPage = Integer.MAX_VALUE;
136
137         startLayouts = new ArrayList<TextLayout>(10); // 10 lines
138
lineIndices = new int[pageIndices.length];
139
140         pageArgs = new Object JavaDoc[ARG_SIZE];
141         pageArgs[2] = filename;
142         pageArgs[1] = new Date(System.currentTimeMillis());
143
144         header = new MessageFormat(getHeaderFormat());
145         printHeader = !getHeaderFormat().equals(""); // NOI18N
146
footer = new MessageFormat(getFooterFormat());
147         printFooter = !getFooterFormat().equals(""); // NOI18N
148
}
149
150     /**
151     * @param doc printed document
152     */

153     public DefaultPrintable(Document doc) {
154         this(getIterators(doc), getFilename(doc));
155     }
156
157     /**
158      * Prints a page.
159      *
160      * @param g a Graphics
161      * @param pf a PageFormat
162      * @param pageNo Which page?
163      */

164     public int print(Graphics g, PageFormat pf, int pageNo)
165     throws PrinterException {
166         boolean processDummy = false;
167
168         if (startPage == -1) {
169             processDummy = true;
170             startPage = pageNo;
171         }
172
173         if (processDummy) {
174             for (int i = 0; i < startPage; i++) {
175                 // XXX #21245 Processes dummy pages (first pages to not print).
176
// PENDING shuold be made better+faster way to skip
177
// processing of such pages (then this - hot fix).
178
printImpl(g, pf, i, false);
179             }
180         }
181
182         return printImpl(g, pf, pageNo, true);
183     }
184
185     private int printImpl(Graphics g, PageFormat pf, int pageNo, boolean print)
186     throws PrinterException {
187         if (pageNo > maxPage) {
188             closeDialog();
189
190             return Printable.NO_SUCH_PAGE;
191         } else if (pageNo < 0) {
192             closeDialog();
193             throw new IllegalArgumentException JavaDoc("Illegal page number=" + pageNo); // NOI18N
194
}
195
196         // stop if cancelled
197
if ((g instanceof PrinterGraphics) && isCancelled(((PrinterGraphics) g).getPrinterJob())) {
198             closeDialog();
199             throw new PrinterAbortException();
200         }
201
202         if ((cancellationPanel == null) && (g instanceof PrinterGraphics)) {
203             // [TODO] - commented out since the awt API does not allow proper handling
204
// of the dialog - e.g when am I to close it?
205
PrinterJob pJob = ((PrinterGraphics) g).getPrinterJob();
206             createCancellationPanel(pJob);
207         }
208
209         if (cancellationPanel != null) {
210             int pageNumber = (print ? pageNo : startPage);
211             cancellationPanel.setPageno(pageNumber);
212             packDialog();
213         }
214
215         // line numbers init
216
int startLine = 0;
217         int correction = 3; // magic - take pencil, paper, and start measuring.
218

219         // if (lineNumbers()) { // not used
220

221         /*
222         lineNo = Integer.toString(startLine = page2Line(pageNo));
223         // correction may not be ok
224         if (startLine < 100) {
225           lineNo = lineNo + " ";
226         } else {
227           lineNo = lineNo + " ";
228         }
229         correction = g.getFontMetrics().stringWidth(lineNo);
230         */

231
232         // }
233
g.setColor(Color.black);
234
235         final Graphics2D graphics = (Graphics2D) g;
236         final Point2D.Float pen = new Point2D.Float(getImageableXPatch(pf), getImageableYPatch(pf));
237
238         /* System.out.println("PEN IS: " + pen);
239             pen = new Point2D.Float((float) pf.getImageableWidth(),
240                                     (float) pf.getImageableHeight()
241                                    );
242             System.out.println("END IS: " + pen);
243             Paper paper = pf.getPaper();
244             System.out.println("DEF IS: " + paper.getHeight() + ", " + paper.getWidth());
245         */

246         // header & footer init
247
pageArgs[0] = new Integer JavaDoc(pageNo + 1); // pages numbered from 1
248

249         float pageBreakCorrection = 0.0F;
250         TextLayout headerString = null;
251         TextLayout footerString = null;
252
253         if (printHeader) {
254             headerString = new TextLayout(header.format(pageArgs), getHeaderFont(), graphics.getFontRenderContext());
255
256             pageBreakCorrection += (headerString.getAscent() +
257             ((headerString.getDescent() + headerString.getLeading()) * 2));
258         }
259
260         if (printFooter) {
261             footerString = new TextLayout(footer.format(pageArgs), getFooterFont(), graphics.getFontRenderContext());
262
263             pageBreakCorrection += ((footerString.getAscent() * 2) + footerString.getDescent() +
264             footerString.getLeading());
265         }
266
267         // for now suppose that getImageableWidthPatch(pf) is always
268
// the same during the same print job
269
final float wrappingWidth = (wrap() ? ((float) pf.getImageableWidth() - correction) : Float.MAX_VALUE);
270         final float pageBreak = (((float) pf.getImageableHeight()) + ((float) pf.getImageableY())) -
271             pageBreakCorrection;
272         final FontRenderContext frCtx = graphics.getFontRenderContext();
273
274         boolean pageExists = false;
275
276         // page rendering
277
for (
278             TextLayout layout = layoutForPage(pageNo, wrappingWidth, frCtx); (pen.y < pageBreak);
279                 layout = nextLayout(wrappingWidth, frCtx)
280         ) {
281             if (layout == null) {
282                 maxPage = pageNo;
283
284                 break;
285             }
286
287             if (!pageExists) {
288                 // draw header
289
if (printHeader && (headerString != null)) {
290                     pen.y += headerString.getAscent();
291
292                     float center = computeStart(
293                             headerString.getBounds(), (float) pf.getImageableWidth(), getHeaderAlignment()
294                         );
295                     float dx = (headerString.isLeftToRight() ? center : (wrappingWidth - headerString.getAdvance() -
296                         center));
297
298                     if (print) {
299                         headerString.draw(graphics, pen.x + dx, pen.y);
300                     }
301
302                     pen.y += ((headerString.getDescent() + headerString.getLeading()) * 2);
303                 }
304
305                 pageExists = true;
306             }
307
308             pen.y += (layout.getAscent() * getLineAscentCorrection());
309
310             // line number handling
311
// if (lineNumbers() && isNewline(layout, startLine)) {
312

313             /*
314             lineNo = Integer.toString(++startLine); // + 1 -> lines starts from 1
315
316             if (startLine < 100) {
317               lineNo = lineNo + " ";
318             } else {
319               lineNo = lineNo + " ";
320             }
321
322             // graphics.drawString(lineNo, (int) pen.x, (int) pen.y);
323             TextLayout tl = new TextLayout(lineNo, lineNumbersFont(), graphics.getFontRenderContext());
324             tl.draw(graphics, pen.x, pen.y);
325             */

326
327             // }
328
float dx = (layout.isLeftToRight() ? 0 : (wrappingWidth - layout.getAdvance()));
329
330             if (print) {
331                 layout.draw(graphics, correction + pen.x + dx, pen.y);
332             }
333
334             pen.y += ((layout.getDescent() + layout.getLeading()) * getLineAscentCorrection());
335         }
336
337         // draw footer
338
if (printFooter && pageExists && (footerString != null)) {
339             pen.y = pageBreak;
340             pen.y += (footerString.getAscent() * 2);
341
342             float center = computeStart(footerString.getBounds(), (float) pf.getImageableWidth(), getFooterAlignment());
343             float dx = (footerString.isLeftToRight() ? (0 + center) : (wrappingWidth - footerString.getAdvance() -
344                 center));
345
346             if (print) {
347                 footerString.draw(graphics, pen.x + dx, pen.y);
348             }
349         }
350
351         // stop if cancelled
352
if ((g instanceof PrinterGraphics) && isCancelled(((PrinterGraphics) g).getPrinterJob())) {
353             closeDialog();
354             throw new PrinterAbortException();
355         }
356
357         // at least one layout draw?
358
if (!pageExists) {
359             closeDialog();
360
361             return Printable.NO_SUCH_PAGE;
362         } else {
363             return Printable.PAGE_EXISTS;
364         }
365     }
366
367     /* for following two methods:
368     * windows page setup dialog behaves incorrectly for LANDSCAPE format
369     * the x coordinate is influenced by RIGHT margin instead of LEFT margin
370     * the y coordinate ... BOTTOM instead of TOP
371     * #3732
372     */

373
374     /** Patch for a bug in the PageFormat class
375     * @param pf PageFormat
376     * @return imageable x coordination for this page format
377     */

378     private float getImageableXPatch(PageFormat pf) {
379         if (pf.getOrientation() == PageFormat.LANDSCAPE) {
380             double ret = pf.getPaper().getHeight() - (pf.getImageableX() + pf.getImageableWidth());
381
382             return (float) Math.round(ret);
383         } else {
384             return (float) pf.getImageableX();
385         }
386     }
387
388     /** Patch for a bug in the PageFormat class
389     * @param pf PageFormat
390     * @return imageable y coordination for this page format
391     */

392     private float getImageableYPatch(PageFormat pf) {
393         if (pf.getOrientation() == PageFormat.LANDSCAPE) {
394             double ret = pf.getPaper().getWidth() - (pf.getImageableY() + pf.getImageableHeight());
395
396             return (float) Math.round(ret);
397         } else {
398             return (float) pf.getImageableY();
399         }
400     }
401
402     /** Translates given page number to line number.
403     * @param pageNo
404     * @return number of first line on the page
405     * /
406     private int page2Line(int pageNo) {
407         if (pageNo == 0) {
408             return 0;
409         } else {
410             return (pageNo == pageIndicesSize ? Math.max(startLayouts.size() - 1, 0) : lineIndices[pageNo]);
411         }
412     }
413      */

414     /**
415     * @param tl a TextLayout
416     * @param currentLine
417     * @return <tt>true</tt> iff <tt>tl</tt> is a TextLayout that does not represent wrapped line
418     */

419     private boolean isNewline(TextLayout tl, int currentLine) {
420         if (currentLine >= startLayouts.size()) {
421             return false; // wrapping appeared
422
} else {
423             return startLayouts.get(currentLine) == tl;
424         }
425     }
426
427     /** Computes alignment for a TextLayout with given bounds on the page with given width
428     * and for given alignment policy.
429     *
430     * @param rect Bounds of a TextLayout
431     * @param width page width
432     * @param alignment one of @see PageSettings#LEFT @see PageSettings#CENTER @see PageSettings#RIGHT
433     */

434     private static float computeStart(Rectangle2D rect, float width, int alignment) {
435         float x;
436
437         if (rect instanceof Rectangle2D.Float) {
438             x = ((Rectangle2D.Float) rect).width;
439         } else {
440             x = (float) ((Rectangle2D.Double) rect).width;
441         }
442
443         if (x >= width) {
444             return 0;
445         }
446
447         switch (alignment) {
448         case PrintSettings.LEFT:
449             return 0;
450
451         case PrintSettings.RIGHT:
452             return (width - x);
453
454         default:
455             return (width - x) / 2;
456         }
457     }
458
459     /**
460     * @param wrappingWidth width of the layout
461     * @param frCtx for possible new instance of LineBreakMeasurer
462     * @return next TextLayout that is to be rendered
463     */

464     private TextLayout nextLayout(float wrappingWidth, FontRenderContext frc) {
465         TextLayout l;
466
467         if (currentLayout == textLayouts.size()) {
468             LineBreakMeasurer old = lineBreakMeasurer;
469             LineBreakMeasurer measurer = getMeasurer(frc);
470
471             if (measurer == null) {
472                 return null;
473             }
474
475             l = measurer.nextLayout(wrappingWidth);
476             textLayouts.add(l);
477
478             if (old != measurer) { // new line
479
startLayouts.add(l);
480             }
481         } else {
482             l = textLayouts.get(currentLayout);
483         }
484
485         currentLayout++; // advance to next
486

487         return l;
488     }
489
490     /** Sets @see #currentLayout variable then calls nextLayout.
491     * @param pageNo searched page
492     * @param wrappingWidth width of the layout
493     * @param frCtx for possible new instance of LineBreakMeasurer
494     * @return next TextLayout that is to be rendered
495     */

496     private TextLayout layoutForPage(int pageNo, float wrappingWidth, FontRenderContext frc) {
497         if (pageNo > (pageIndicesSize + 1)) {
498             throw new IllegalArgumentException JavaDoc(
499                 "Page number " + pageNo // NOI18N
500
+" is bigger than array size " + (pageIndicesSize + 1)
501             ); // NOI18N
502
}
503
504         // first request for a page // pageNo==3 -> fourth page to print
505
if (pageNo == pageIndicesSize) {
506             // small array?
507
if (pageIndicesSize >= pageIndices.length) {
508                 pageIndices = increaseArray(pageIndices);
509                 lineIndices = increaseArray(lineIndices);
510             }
511
512             // layouts for given page starts at:
513
pageIndices[pageIndicesSize] = Math.max(textLayouts.size() - 1, 0);
514
515             // remember - in the for loop above last layout is not printed
516
// if page breaks
517
lineIndices[pageIndicesSize++] = Math.max(startLayouts.size() - 1, 0);
518         }
519
520         currentLayout = pageIndices[pageNo]; // set the iterator
521

522         return nextLayout(wrappingWidth, frc); // iterate
523
}
524
525     /** Called only if new TextLayouts are in need.
526     * @param frc is used for possible new LineBreakMeasurer instance
527     * @return current LineBreakMeasurer or <tt>null</tt> if no is available.
528     */

529     private LineBreakMeasurer getMeasurer(FontRenderContext frc) {
530         if (lineBreakMeasurer == null) { // first page to print
531
lineBreakMeasurer = new LineBreakMeasurer(styledTexts[currentStyledText], frc);
532
533             // no layouts available in this measurer?
534
} else if (lineBreakMeasurer.getPosition() >= styledTexts[currentStyledText].getEndIndex()) {
535             // next measurer is not available?
536
if (currentStyledText == (styledTexts.length - 1)) {
537                 return null; // everything is printed
538
} else { // use next styledTexts entry
539
lineBreakMeasurer = new LineBreakMeasurer(styledTexts[++currentStyledText], frc);
540             }
541         }
542
543         return lineBreakMeasurer;
544     }
545
546     // ------------------ options -----------------
547

548     /** @return true iff wrapping is on*/
549     private static boolean wrap() {
550         return printSettings.getWrap();
551     }
552
553     /** @return String describing header */
554     private static String JavaDoc getHeaderFormat() {
555         return printSettings.getHeaderFormat();
556     }
557
558     /** @return String describing footer */
559     private static String JavaDoc getFooterFormat() {
560         return printSettings.getFooterFormat();
561     }
562
563     /** @return font for header */
564     private static Font getHeaderFont() {
565         return printSettings.getHeaderFont();
566     }
567
568     /** @return font for footer */
569     private static Font getFooterFont() {
570         return printSettings.getFooterFont();
571     }
572
573     /** @return an alignment constant for footer */
574     private static int getFooterAlignment() {
575         return printSettings.getFooterAlignment();
576     }
577
578     /** @return an alignment constant for header */
579     private static int getHeaderAlignment() {
580         return printSettings.getHeaderAlignment();
581     }
582
583     /** @return a line ascent correction */
584     private static float getLineAscentCorrection() {
585         return printSettings.getLineAscentCorrection();
586     }
587
588     /** @return false */
589     private static boolean lineNumbers() {
590         return false;
591     }
592
593     // not used
594
private static Font lineNumbersFont() {
595         return new Font("Courier", java.awt.Font.PLAIN, 6); // NOI18N
596
}
597
598     // ----------------- options end --------------
599

600     /** Creates new AttributedCharacterIterator for plain text.
601      *
602      * @return an AttributedCharacterIterator
603      */

604     private static AttributedCharacterIterator[] getIterators(Document doc) {
605         if (doc instanceof NbDocument.Printable) {
606             return ((NbDocument.Printable) doc).createPrintIterators();
607         }
608
609         // load options
610
java.awt.Font JavaDoc f = new java.awt.Font JavaDoc("Courier", java.awt.Font.PLAIN, 8); // NOI18N
611

612         AttributedCharacters achs = null;
613         char[] chars = null;
614         List JavaDoc<AttributedCharacterIterator> iterators = new ArrayList<AttributedCharacterIterator>(300);
615
616         try {
617             String JavaDoc document = doc.getText(0, doc.getLength());
618
619             // now chars are filled from the document
620
int firstCharInDoc = 0;
621
622             for (int i = 0; i < document.length(); i++) { // search for new lines
623

624                 if (document.charAt(i) == '\n') {
625                     chars = new char[i - firstCharInDoc + 1];
626                     document.getChars(firstCharInDoc, chars.length + firstCharInDoc, chars, 0);
627                     achs = new AttributedCharacters();
628                     achs.append(chars, f, Color.black);
629                     iterators.add(achs.iterator()); // new iterator for new line
630
firstCharInDoc = i + 1;
631                 }
632             }
633         } catch (BadLocationException e) {
634             Exceptions.printStackTrace(e);
635         }
636
637         AttributedCharacterIterator[] iters = new AttributedCharacterIterator[iterators.size()];
638         iterators.toArray(iters);
639
640         return iters;
641     }
642
643     /**
644     * @param doc
645     * @return filename from which the document is loaded.
646     */

647     private static String JavaDoc getFilename(Document doc) {
648         String JavaDoc ret = (String JavaDoc) doc.getProperty(javax.swing.text.Document.TitleProperty);
649
650         return ((ret == null) ? "UNKNOWN" : ret); // NOI18N
651
}
652
653     /** Doubles given array. The old one is then copied into the new one.
654     * @return new int array
655     */

656     private static int[] increaseArray(int[] old) {
657         int[] ret = new int[2 * old.length];
658         System.arraycopy(old, 0, ret, 0, old.length);
659
660         return ret;
661     }
662
663     // ------------------ cancellation dialog ---------------------------
664

665     /** Creates cancellation dialog.
666     * @param job PrinterJob
667     */

668     private void createCancellationPanel(final PrinterJob job) {
669         cancellationPanel = new CancellationPanel(job);
670
671         DialogDescriptor ddesc = new DialogDescriptor(
672                 cancellationPanel, NbBundle.getMessage(PrintSettings.class, "CTL_Print_cancellation"), false,
673                 new Object JavaDoc[] { NbBundle.getMessage(PrintSettings.class, "CTL_Cancel") },
674                 NbBundle.getMessage(PrintSettings.class, "CTL_Cancel"), DialogDescriptor.BOTTOM_ALIGN, null,
675                 new java.awt.event.ActionListener JavaDoc() {
676                     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
677                         setCancelled(job);
678                         closeDialog();
679                     }
680                 }
681             );
682         setDialog(DialogDisplayer.getDefault().createDialog(ddesc));
683     }
684
685     /** Closes cancellationDialog */
686     void closeDialog() {
687         if (cancellationDialog != null) {
688             cancellationDialog.setVisible(false);
689             cancellationDialog.dispose();
690         }
691     }
692
693     /** @param <tt>d</tt> New value of <tt>cancellationDialog</tt>*/
694     void setDialog(Dialog d) {
695         d.setVisible(true);
696         d.pack();
697         cancellationDialog = d;
698     }
699
700     /** packs the dialog */
701     void packDialog() {
702         if (cancellationDialog != null) {
703             cancellationDialog.pack();
704         }
705     }
706
707     /** Marks this job as cancelled.
708     * @param <tt>job</tt>
709     */

710     void setCancelled(PrinterJob job) {
711         job.cancel();
712     }
713
714     /** @return <tt>true</tt> iff the job was cancelled */
715     boolean isCancelled(PrinterJob job) {
716         return job.isCancelled();
717     }
718
719     /** Replaces an empty iterator by an iterator conatining one space. */
720     private static void replaceEmptyIterators(AttributedCharacterIterator[] iters) {
721         for (int i = 0; i < iters.length; i++) {
722             AttributedCharacterIterator achit = iters[i];
723
724             if (achit.getBeginIndex() == achit.getEndIndex()) {
725                 AttributedCharacters at = new AttributedCharacters();
726                 at.append(' ', getFontInstance(), Color.white);
727                 iters[i] = at.iterator();
728             }
729         }
730     }
731
732     /** @return cached font instance */
733     static Font getFontInstance() {
734         if (fontInstance == null) {
735             fontInstance = new java.awt.Font JavaDoc("Dialog", java.awt.Font.PLAIN, 14); // NOI18N
736
}
737
738         return fontInstance;
739     }
740
741     /** CancellationPanel class allows user to cancel current PrinterJob */
742     static final class CancellationPanel extends javax.swing.JPanel JavaDoc {
743         static final long serialVersionUID = -6419253408585188541L;
744
745         /** Label indicating progress. */
746         private final javax.swing.JLabel JavaDoc printProgress;
747
748         /** Format of displayed text. */
749         private final MessageFormat format;
750
751         /** Parameters for <tt>format</tt>. */
752         private final Object JavaDoc[] msgParams;
753
754         /**
755         * @param <tt>job</tt> PrinterJob
756         * @exception IllegalArgumentException is thrown if <tt>job</tt> is <tt>null</tt>.
757         */

758         public CancellationPanel(PrinterJob job) {
759             if (job == null) {
760                 throw new IllegalArgumentException JavaDoc();
761             }
762
763             format = new MessageFormat(NbBundle.getMessage(PrintSettings.class, "CTL_Print_progress"));
764             msgParams = new Object JavaDoc[1];
765
766             setLayout(new java.awt.BorderLayout JavaDoc());
767             setBorder(new javax.swing.border.EmptyBorder JavaDoc(12, 12, 0, 12));
768             printProgress = new javax.swing.JLabel JavaDoc();
769             printProgress.setHorizontalAlignment(javax.swing.JLabel.CENTER);
770             add(printProgress);
771         }
772
773         /** Advances progress.
774         * @param <tt>pageno</tt> Page number that was printed.
775         */

776         public void setPageno(int pageno) {
777             msgParams[0] = new Integer JavaDoc(pageno + 1);
778             printProgress.setText(format.format(msgParams));
779             getAccessibleContext().setAccessibleDescription(printProgress.getText());
780         }
781     }
782 }
783
Popular Tags