* @param visibleRect the view area visible within the viewport * @param orientation either <code>SwingConstants.VERTICAL</code> or * <code>SwingConstants.HORIZONTAL</code> * @param direction less than zero to scroll up/left, greater than * zero for down/right * @return the "unit" increment for scrolling in the specified direction * @exception IllegalArgumentException for an invalid orientation * @see JScrollBar#setUnitIncrement */ public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { switch(orientation) { case SwingConstants.VERTICAL: return visibleRect.height / 10; case SwingConstants.HORIZONTAL: return visibleRect.width / 10; default: throw new IllegalArgumentException("Invalid orientation: " + orientation); } } /** * Components that display logical rows or columns should compute * the scroll increment that will completely expose one block * of rows or columns, depending on the value of orientation. * <p> * The default implementation of this is to simply return the visible * area. Subclasses will likely be able to provide a much more * reasonable value. * * @param visibleRect the view area visible within the viewport * @param orientation either <code>SwingConstants.VERTICAL</code> or * <code>SwingConstants.HORIZONTAL</code> * @param direction less than zero to scroll up/left, greater than zero * for down/right * @return the "block" increment for scrolling in the specified direction * @exception IllegalArgumentException for an invalid orientation * @see JScrollBar#setBlockIncrement */ public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { switch(orientation) { case SwingConstants.VERTICAL: return visibleRect.height; case SwingConstants.HORIZONTAL: return visibleRect.width; default: throw new IllegalArgumentException("Invalid orientation: " + orientation); } } /** * Returns true if a viewport should always force the width of this * <code>Scrollable</code> to match the width of the viewport. * For example a normal text view that supported line wrapping * would return true here, since it would be undesirable for * wrapped lines to disappear beyond the right * edge of the viewport. Note that returning true for a * <code>Scrollable</code> whose ancestor is a <code>JScrollPane</code> * effectively disables horizontal scrolling. * <p> * Scrolling containers, like <code>JViewport</code>, * will use this method each time they are validated. * * @return true if a viewport should force the <code>Scrollable</code>s * width to match its own */ public boolean getScrollableTracksViewportWidth() { Container parent = SwingUtilities.getUnwrappedParent(this); if (parent instanceof JViewport) { return parent.getWidth() > getPreferredSize().width; } return false; } /** * Returns true if a viewport should always force the height of this * <code>Scrollable</code> to match the height of the viewport. * For example a columnar text view that flowed text in left to * right columns could effectively disable vertical scrolling by * returning true here. * <p> * Scrolling containers, like <code>JViewport</code>, * will use this method each time they are validated. * * @return true if a viewport should force the Scrollables height * to match its own */ public boolean getScrollableTracksViewportHeight() { Container parent = SwingUtilities.getUnwrappedParent(this); if (parent instanceof JViewport) { return parent.getHeight() > getPreferredSize().height; } return false; } //////////////////// Printing Support////////////////// /** * A convenience print method that displays a print dialog, and then * prints this {@code JTextComponent} in <i>interactive</i> mode with no * header or footer text. Note: this method * blocks until printing is done. * <p> * Note: In <i>headless</i> mode, no dialogs will be shown. * * <p> This method calls the full featured * {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * print} method to perform printing. * @return {@code true}, unless printing is canceled by the user * @throws PrinterException if an error in the print system causes the job * to be aborted * @throws SecurityException if this thread is not allowed to * initiate a print job request * * @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * * @since 1.6 */ public boolean print() throws PrinterException { return print(null, null, true, null, null, true); } /** * A convenience print method that displays a print dialog, and then * prints this {@code JTextComponent} in <i>interactive</i> mode with * the specified header and footer text. Note: this method * blocks until printing is done. * <p> * Note: In <i>headless</i> mode, no dialogs will be shown. * * <p> This method calls the full featured * {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * print} method to perform printing. * @param headerFormat the text, in {@code MessageFormat}, to be * used as the header, or {@code null} for no header * @param footerFormat the text, in {@code MessageFormat}, to be * used as the footer, or {@code null} for no footer * @return {@code true}, unless printing is canceled by the user * @throws PrinterException if an error in the print system causes the job * to be aborted * @throws SecurityException if this thread is not allowed to * initiate a print job request * * @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * @see java.text.MessageFormat * @since 1.6 */ public boolean print(final MessageFormat headerFormat, final MessageFormat footerFormat) throws PrinterException { return print(headerFormat, footerFormat, true, null, null, true); } /** * Prints the content of this {@code JTextComponent}. Note: this method * blocks until printing is done. * * <p> * Page header and footer text can be added to the output by providing * {@code MessageFormat} arguments. The printing code requests * {@code Strings} from the formats, providing a single item which may be * included in the formatted string: an {@code Integer} representing the * current page number. * * <p> * {@code showPrintDialog boolean} parameter allows you to specify whether * a print dialog is displayed to the user. When it is, the user * may use the dialog to change printing attributes or even cancel the * print. * * <p> * {@code service} allows you to provide the initial * {@code PrintService} for the print dialog, or to specify * {@code PrintService} to print to when the dialog is not shown. * * <p> * {@code attributes} can be used to provide the * initial values for the print dialog, or to supply any needed * attributes when the dialog is not shown. {@code attributes} can * be used to control how the job will print, for example * <i>duplex</i> or <i>single-sided</i>. * * <p> * {@code interactive boolean} parameter allows you to specify * whether to perform printing in <i>interactive</i> * mode. If {@code true}, a progress dialog, with an abort option, * is displayed for the duration of printing. This dialog is * <i>modal</i> when {@code print} is invoked on the <i>Event Dispatch * Thread</i> and <i>non-modal</i> otherwise. <b>Warning</b>: * calling this method on the <i>Event Dispatch Thread</i> with {@code * interactive false} blocks <i>all</i> events, including repaints, from * being processed until printing is complete. It is only * recommended when printing from an application with no * visible GUI. * * <p> * Note: In <i>headless</i> mode, {@code showPrintDialog} and * {@code interactive} parameters are ignored and no dialogs are * shown. * * <p> * This method ensures the {@code document} is not mutated during printing. * To indicate it visually, {@code setEnabled(false)} is set for the * duration of printing. * * <p> * This method uses {@link #getPrintable} to render document content. * * <p> * This method is thread-safe, although most Swing methods are not. Please * see <A * HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html"> * How to Use Threads</A> for more information. * * <p> * <b>Sample Usage</b>. This code snippet shows a cross-platform print * dialog and then prints the {@code JTextComponent} in <i>interactive</i> mode * unless the user cancels the dialog: * * <pre> * textComponent.print(new MessageFormat("My text component header"), * new MessageFormat("Footer. Page - {0}"), true, null, null, true); * </pre> * <p> * Executing this code off the <i>Event Dispatch Thread</i> * performs printing on the <i>background</i>. * The following pattern might be used for <i>background</i> * printing: * <pre> * FutureTask<Boolean> future = * new FutureTask<Boolean>( * new Callable<Boolean>() { * public Boolean call() { * return textComponent.print(.....); * } * }); * executor.execute(future); * </pre> * * @param headerFormat the text, in {@code MessageFormat}, to be * used as the header, or {@code null} for no header * @param footerFormat the text, in {@code MessageFormat}, to be * used as the footer, or {@code null} for no footer * @param showPrintDialog {@code true} to display a print dialog, * {@code false} otherwise * @param service initial {@code PrintService}, or {@code null} for the * default * @param attributes the job attributes to be applied to the print job, or * {@code null} for none * @param interactive whether to print in an interactive mode * @return {@code true}, unless printing is canceled by the user * @throws PrinterException if an error in the print system causes the job * to be aborted * @throws SecurityException if this thread is not allowed to * initiate a print job request * * @see #getPrintable * @see java.text.MessageFormat * @see java.awt.GraphicsEnvironment#isHeadless * @see java.util.concurrent.FutureTask * * @since 1.6 */ public boolean print(final MessageFormat headerFormat, final MessageFormat footerFormat, final boolean showPrintDialog, final PrintService service, final PrintRequestAttributeSet attributes, final boolean interactive) throws PrinterException { final PrinterJob job = PrinterJob.getPrinterJob(); final Printable printable; final PrintingStatus printingStatus; final boolean isHeadless = GraphicsEnvironment.isHeadless(); final boolean isEventDispatchThread = SwingUtilities.isEventDispatchThread(); final Printable textPrintable = getPrintable(headerFormat, footerFormat); if (interactive && ! isHeadless) { printingStatus = PrintingStatus.createPrintingStatus(this, job); printable = printingStatus.createNotificationPrintable(textPrintable); } else { printingStatus = null; printable = textPrintable; } if (service != null) { job.setPrintService(service); } job.setPrintable(printable); final PrintRequestAttributeSet attr = (attributes == null) ? new HashPrintRequestAttributeSet() : attributes; if (showPrintDialog && ! isHeadless && ! job.printDialog(attr)) { return false; } /* * there are three cases for printing: * 1. print non interactively (! interactive || isHeadless) * 2. print interactively off EDT * 3. print interactively on EDT * * 1 and 2 prints on the current thread (3 prints on another thread) * 2 and 3 deal with PrintingStatusDialog */ final Callable<Object> doPrint = new Callable<Object>() { public Object call() throws Exception { try { job.print(attr); } finally { if (printingStatus != null) { printingStatus.dispose(); } } return null; } }; final FutureTask<Object> futurePrinting = new FutureTask<Object>(doPrint); final Runnable runnablePrinting = new Runnable() { public void run() { //disable component boolean wasEnabled = false; if (isEventDispatchThread) { if (isEnabled()) { wasEnabled = true; setEnabled(false); } } else { try { wasEnabled = SwingUtilities2.submit( new Callable<Boolean>() { public Boolean call() throws Exception { boolean rv = isEnabled(); if (rv) { setEnabled(false); } return rv; } }).get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof Error) { throw (Error) cause; } if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } throw new AssertionError(cause); } } getDocument().render(futurePrinting); //enable component if (wasEnabled) { if (isEventDispatchThread) { setEnabled(true); } else { try { SwingUtilities2.submit( new Runnable() { public void run() { setEnabled(true); } }, null).get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof Error) { throw (Error) cause; } if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } throw new AssertionError(cause); } } } } }; if (! interactive || isHeadless) { runnablePrinting.run(); } else { if (isEventDispatchThread) { (new Thread(runnablePrinting)).start(); printingStatus.showModal(true); } else { printingStatus.showModal(false); runnablePrinting.run(); } } //the printing is done successfully or otherwise. //dialog is hidden if needed. try { futurePrinting.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof PrinterAbortException) { if (printingStatus != null && printingStatus.isAborted()) { return false; } else { throw (PrinterAbortException) cause; } } else if (cause instanceof PrinterException) { throw (PrinterException) cause; } else if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else if (cause instanceof Error) { throw (Error) cause; } else { throw new AssertionError(cause); } } return true; } /** * Returns a {@code Printable} to use for printing the content of this * {@code JTextComponent}. The returned {@code Printable} prints * the document as it looks on the screen except being reformatted * to fit the paper. * The returned {@code Printable} can be wrapped inside another * {@code Printable} in order to create complex reports and * documents. * * * <p> * The returned {@code Printable} shares the {@code document} with this * {@code JTextComponent}. It is the responsibility of the developer to * ensure that the {@code document} is not mutated while this {@code Printable} * is used. Printing behavior is undefined when the {@code document} is * mutated during printing. * * <p> * Page header and footer text can be added to the output by providing * {@code MessageFormat} arguments. The printing code requests * {@code Strings} from the formats, providing a single item which may be * included in the formatted string: an {@code Integer} representing the * current page number. * * <p> * The returned {@code Printable} when printed, formats the * document content appropriately for the page size. For correct * line wrapping the {@code imageable width} of all pages must be the * same. See {@link java.awt.print.PageFormat#getImageableWidth}. * * <p> * This method is thread-safe, although most Swing methods are not. Please * see <A * HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html"> * How to Use Threads</A> for more information. * * <p> * The returned {@code Printable} can be printed on any thread. * * <p> * This implementation returned {@code Printable} performs all painting on * the <i>Event Dispatch Thread</i>, regardless of what thread it is * used on. * * @param headerFormat the text, in {@code MessageFormat}, to be * used as the header, or {@code null} for no header * @param footerFormat the text, in {@code MessageFormat}, to be * used as the footer, or {@code null} for no footer * @return a {@code Printable} for use in printing content of this * {@code JTextComponent} * * * @see java.awt.print.Printable * @see java.awt.print.PageFormat * @see javax.swing.text.Document#render(java.lang.Runnable) * * @since 1.6 */ public Printable getPrintable(final MessageFormat headerFormat, final MessageFormat footerFormat) {Lines 2001–2500 of 5073. Previous lines Load more
* @param visibleRect the view area visible within the viewport * @param orientation either <code>SwingConstants.VERTICAL</code> or * <code>SwingConstants.HORIZONTAL</code> * @param direction less than zero to scroll up/left, greater than * zero for down/right * @return the "unit" increment for scrolling in the specified direction * @exception IllegalArgumentException for an invalid orientation * @see JScrollBar#setUnitIncrement */ public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { switch(orientation) { case SwingConstants.VERTICAL: return visibleRect.height / 10; case SwingConstants.HORIZONTAL: return visibleRect.width / 10; default: throw new IllegalArgumentException("Invalid orientation: " + orientation); } } /** * Components that display logical rows or columns should compute * the scroll increment that will completely expose one block * of rows or columns, depending on the value of orientation. * <p> * The default implementation of this is to simply return the visible * area. Subclasses will likely be able to provide a much more * reasonable value. * * @param visibleRect the view area visible within the viewport * @param orientation either <code>SwingConstants.VERTICAL</code> or * <code>SwingConstants.HORIZONTAL</code> * @param direction less than zero to scroll up/left, greater than zero * for down/right * @return the "block" increment for scrolling in the specified direction * @exception IllegalArgumentException for an invalid orientation * @see JScrollBar#setBlockIncrement */ public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { switch(orientation) { case SwingConstants.VERTICAL: return visibleRect.height; case SwingConstants.HORIZONTAL: return visibleRect.width; default: throw new IllegalArgumentException("Invalid orientation: " + orientation); } } /** * Returns true if a viewport should always force the width of this * <code>Scrollable</code> to match the width of the viewport. * For example a normal text view that supported line wrapping * would return true here, since it would be undesirable for * wrapped lines to disappear beyond the right * edge of the viewport. Note that returning true for a * <code>Scrollable</code> whose ancestor is a <code>JScrollPane</code> * effectively disables horizontal scrolling. * <p> * Scrolling containers, like <code>JViewport</code>, * will use this method each time they are validated. * * @return true if a viewport should force the <code>Scrollable</code>s * width to match its own */ public boolean getScrollableTracksViewportWidth() { Container parent = SwingUtilities.getUnwrappedParent(this); if (parent instanceof JViewport) { return parent.getWidth() > getPreferredSize().width; } return false; } /** * Returns true if a viewport should always force the height of this * <code>Scrollable</code> to match the height of the viewport. * For example a columnar text view that flowed text in left to * right columns could effectively disable vertical scrolling by * returning true here. * <p> * Scrolling containers, like <code>JViewport</code>, * will use this method each time they are validated. * * @return true if a viewport should force the Scrollables height * to match its own */ public boolean getScrollableTracksViewportHeight() { Container parent = SwingUtilities.getUnwrappedParent(this); if (parent instanceof JViewport) { return parent.getHeight() > getPreferredSize().height; } return false; } ////////////////// // Printing Support ////////////////// /** * A convenience print method that displays a print dialog, and then * prints this {@code JTextComponent} in <i>interactive</i> mode with no * header or footer text. Note: this method * blocks until printing is done. * <p> * Note: In <i>headless</i> mode, no dialogs will be shown. * * <p> This method calls the full featured * {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * print} method to perform printing. * @return {@code true}, unless printing is canceled by the user * @throws PrinterException if an error in the print system causes the job * to be aborted * @throws SecurityException if this thread is not allowed to * initiate a print job request * * @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * * @since 1.6 */ public boolean print() throws PrinterException { return print(null, null, true, null, null, true); } /** * A convenience print method that displays a print dialog, and then * prints this {@code JTextComponent} in <i>interactive</i> mode with * the specified header and footer text. Note: this method * blocks until printing is done. * <p> * Note: In <i>headless</i> mode, no dialogs will be shown. * * <p> This method calls the full featured * {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * print} method to perform printing. * @param headerFormat the text, in {@code MessageFormat}, to be * used as the header, or {@code null} for no header * @param footerFormat the text, in {@code MessageFormat}, to be * used as the footer, or {@code null} for no footer * @return {@code true}, unless printing is canceled by the user * @throws PrinterException if an error in the print system causes the job * to be aborted * @throws SecurityException if this thread is not allowed to * initiate a print job request * * @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) * @see java.text.MessageFormat * @since 1.6 */ public boolean print(final MessageFormat headerFormat, final MessageFormat footerFormat) throws PrinterException { return print(headerFormat, footerFormat, true, null, null, true); } /** * Prints the content of this {@code JTextComponent}. Note: this method * blocks until printing is done. * * <p> * Page header and footer text can be added to the output by providing * {@code MessageFormat} arguments. The printing code requests * {@code Strings} from the formats, providing a single item which may be * included in the formatted string: an {@code Integer} representing the * current page number. * * <p> * {@code showPrintDialog boolean} parameter allows you to specify whether * a print dialog is displayed to the user. When it is, the user * may use the dialog to change printing attributes or even cancel the * print. * * <p> * {@code service} allows you to provide the initial * {@code PrintService} for the print dialog, or to specify * {@code PrintService} to print to when the dialog is not shown. * * <p> * {@code attributes} can be used to provide the * initial values for the print dialog, or to supply any needed * attributes when the dialog is not shown. {@code attributes} can * be used to control how the job will print, for example * <i>duplex</i> or <i>single-sided</i>. * * <p> * {@code interactive boolean} parameter allows you to specify * whether to perform printing in <i>interactive</i> * mode. If {@code true}, a progress dialog, with an abort option, * is displayed for the duration of printing. This dialog is * <i>modal</i> when {@code print} is invoked on the <i>Event Dispatch * Thread</i> and <i>non-modal</i> otherwise. <b>Warning</b>: * calling this method on the <i>Event Dispatch Thread</i> with {@code * interactive false} blocks <i>all</i> events, including repaints, from * being processed until printing is complete. It is only * recommended when printing from an application with no * visible GUI. * * <p> * Note: In <i>headless</i> mode, {@code showPrintDialog} and * {@code interactive} parameters are ignored and no dialogs are * shown. * * <p> * This method ensures the {@code document} is not mutated during printing. * To indicate it visually, {@code setEnabled(false)} is set for the * duration of printing. * * <p> * This method uses {@link #getPrintable} to render document content. * * <p> * This method is thread-safe, although most Swing methods are not. Please * see <A * HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html"> * How to Use Threads</A> for more information. * * <p> * <b>Sample Usage</b>. This code snippet shows a cross-platform print * dialog and then prints the {@code JTextComponent} in <i>interactive</i> mode * unless the user cancels the dialog: * * <pre> * textComponent.print(new MessageFormat("My text component header"), * new MessageFormat("Footer. Page - {0}"), true, null, null, true); * </pre> * <p> * Executing this code off the <i>Event Dispatch Thread</i> * performs printing on the <i>background</i>. * The following pattern might be used for <i>background</i> * printing: * <pre> * FutureTask<Boolean> future = * new FutureTask<Boolean>( * new Callable<Boolean>() { * public Boolean call() { * return textComponent.print(.....); * } * }); * executor.execute(future); * </pre> * * @param headerFormat the text, in {@code MessageFormat}, to be * used as the header, or {@code null} for no header * @param footerFormat the text, in {@code MessageFormat}, to be * used as the footer, or {@code null} for no footer * @param showPrintDialog {@code true} to display a print dialog, * {@code false} otherwise * @param service initial {@code PrintService}, or {@code null} for the * default * @param attributes the job attributes to be applied to the print job, or * {@code null} for none * @param interactive whether to print in an interactive mode * @return {@code true}, unless printing is canceled by the user * @throws PrinterException if an error in the print system causes the job * to be aborted * @throws SecurityException if this thread is not allowed to * initiate a print job request * * @see #getPrintable * @see java.text.MessageFormat * @see java.awt.GraphicsEnvironment#isHeadless * @see java.util.concurrent.FutureTask * * @since 1.6 */ public boolean print(final MessageFormat headerFormat, final MessageFormat footerFormat, final boolean showPrintDialog, final PrintService service, final PrintRequestAttributeSet attributes, final boolean interactive) throws PrinterException { final PrinterJob job = PrinterJob.getPrinterJob(); final Printable printable; final PrintingStatus printingStatus; final boolean isHeadless = GraphicsEnvironment.isHeadless(); final boolean isEventDispatchThread = SwingUtilities.isEventDispatchThread(); final Printable textPrintable = getPrintable(headerFormat, footerFormat); if (interactive && ! isHeadless) { printingStatus = PrintingStatus.createPrintingStatus(this, job); printable = printingStatus.createNotificationPrintable(textPrintable); } else { printingStatus = null; printable = textPrintable; } if (service != null) { job.setPrintService(service); } job.setPrintable(printable); final PrintRequestAttributeSet attr = (attributes == null) ? new HashPrintRequestAttributeSet() : attributes; if (showPrintDialog && ! isHeadless && ! job.printDialog(attr)) { return false; } /* * there are three cases for printing: * 1. print non interactively (! interactive || isHeadless) * 2. print interactively off EDT * 3. print interactively on EDT * * 1 and 2 prints on the current thread (3 prints on another thread) * 2 and 3 deal with PrintingStatusDialog */ final Callable<Object> doPrint = new Callable<Object>() { public Object call() throws Exception { try { job.print(attr); } finally { if (printingStatus != null) { printingStatus.dispose(); } } return null; } }; final FutureTask<Object> futurePrinting = new FutureTask<Object>(doPrint); final Runnable runnablePrinting = new Runnable() { public void run() { //disable component boolean wasEnabled = false; if (isEventDispatchThread) { if (isEnabled()) { wasEnabled = true; setEnabled(false); } } else { try { wasEnabled = SwingUtilities2.submit( new Callable<Boolean>() { public Boolean call() throws Exception { boolean rv = isEnabled(); if (rv) { setEnabled(false); } return rv; } }).get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof Error) { throw (Error) cause; } if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } throw new AssertionError(cause); } } getDocument().render(futurePrinting); //enable component if (wasEnabled) { if (isEventDispatchThread) { setEnabled(true); } else { try { SwingUtilities2.submit( new Runnable() { public void run() { setEnabled(true); } }, null).get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof Error) { throw (Error) cause; } if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } throw new AssertionError(cause); } } } } }; if (! interactive || isHeadless) { runnablePrinting.run(); } else { if (isEventDispatchThread) { (new Thread(runnablePrinting)).start(); printingStatus.showModal(true); } else { printingStatus.showModal(false); runnablePrinting.run(); } } //the printing is done successfully or otherwise. //dialog is hidden if needed. try { futurePrinting.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof PrinterAbortException) { if (printingStatus != null && printingStatus.isAborted()) { return false; } else { throw (PrinterAbortException) cause; } } else if (cause instanceof PrinterException) { throw (PrinterException) cause; } else if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else if (cause instanceof Error) { throw (Error) cause; } else { throw new AssertionError(cause); } } return true; } /** * Returns a {@code Printable} to use for printing the content of this * {@code JTextComponent}. The returned {@code Printable} prints * the document as it looks on the screen except being reformatted * to fit the paper. * The returned {@code Printable} can be wrapped inside another * {@code Printable} in order to create complex reports and * documents. * * * <p> * The returned {@code Printable} shares the {@code document} with this * {@code JTextComponent}. It is the responsibility of the developer to * ensure that the {@code document} is not mutated while this {@code Printable} * is used. Printing behavior is undefined when the {@code document} is * mutated during printing. * * <p> * Page header and footer text can be added to the output by providing * {@code MessageFormat} arguments. The printing code requests * {@code Strings} from the formats, providing a single item which may be * included in the formatted string: an {@code Integer} representing the * current page number. * * <p> * The returned {@code Printable} when printed, formats the * document content appropriately for the page size. For correct * line wrapping the {@code imageable width} of all pages must be the * same. See {@link java.awt.print.PageFormat#getImageableWidth}. * * <p> * This method is thread-safe, although most Swing methods are not. Please * see <A * HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html"> * How to Use Threads</A> for more information. * * <p> * The returned {@code Printable} can be printed on any thread. * * <p> * This implementation returned {@code Printable} performs all painting on * the <i>Event Dispatch Thread</i>, regardless of what thread it is * used on. * * @param headerFormat the text, in {@code MessageFormat}, to be * used as the header, or {@code null} for no header * @param footerFormat the text, in {@code MessageFormat}, to be * used as the footer, or {@code null} for no footer * @return a {@code Printable} for use in printing content of this * {@code JTextComponent} * * * @see java.awt.print.Printable * @see java.awt.print.PageFormat * @see javax.swing.text.Document#render(java.lang.Runnable) * * @since 1.6 */ public Printable getPrintable(final MessageFormat headerFormat, final MessageFormat footerFormat) {