Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/swing/PrintingStatus.java
+++ new/src/share/classes/sun/swing/PrintingStatus.java
1 1 /*
2 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package sun.swing;
26 26
27 27 import javax.swing.*;
28 28 import java.awt.*;
29 29 import java.awt.event.ActionEvent;
30 30 import java.awt.event.WindowAdapter;
31 31 import java.awt.event.WindowEvent;
32 32 import java.awt.print.PageFormat;
33 33 import java.awt.print.Printable;
34 34 import java.awt.print.PrinterException;
35 35 import java.awt.print.PrinterJob;
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
36 36 import java.text.MessageFormat;
37 37 import java.util.concurrent.atomic.AtomicBoolean;
38 38 import java.lang.reflect.InvocationTargetException;
39 39
40 40 /**
41 41 * The {@code PrintingStatus} provides a dialog that displays progress
42 42 * of the printing job and provides a way to abort it
43 43 * <p/>
44 44 * Methods of these class are thread safe, although most Swing methods
45 45 * are not. Please see
46 - * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
46 + * <A HREF="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
47 47 * in Swing</A> for more information.
48 48 *
49 49 * @author Alexander Potochkin
50 50 * @since 1.6
51 51 */
52 52
53 53 public class PrintingStatus {
54 54
55 55 private final PrinterJob job;
56 56 private final Component parent;
57 57 private JDialog abortDialog;
58 58
59 59 private JButton abortButton;
60 60 private JLabel statusLabel;
61 61 private MessageFormat statusFormat;
62 62 private final AtomicBoolean isAborted = new AtomicBoolean(false);
63 63
64 64 // the action that will abort printing
65 65 private final Action abortAction = new AbstractAction() {
66 66 public void actionPerformed(ActionEvent ae) {
67 67 if (!isAborted.get()) {
68 68 isAborted.set(true);
69 69
70 70 // update the status abortDialog to indicate aborting
71 71 abortButton.setEnabled(false);
72 72 abortDialog.setTitle(
73 73 UIManager.getString("PrintingDialog.titleAbortingText"));
74 74 statusLabel.setText(
75 75 UIManager.getString("PrintingDialog.contentAbortingText"));
76 76
77 77 // cancel the PrinterJob
78 78 job.cancel();
79 79 }
80 80 }
81 81 };
82 82
83 83 private final WindowAdapter closeListener = new WindowAdapter() {
84 84 public void windowClosing(WindowEvent we) {
85 85 abortAction.actionPerformed(null);
86 86 }
87 87 };
88 88
89 89 /**
90 90 * Creates PrintingStatus instance
91 91 *
92 92 * @param parent a <code>Component</code> object to be used
93 93 * as parent component for PrintingStatus dialog
94 94 * @param job a <code>PrinterJob</code> object to be cancelled
95 95 * using this <code>PrintingStatus</code> dialog
96 96 * @return a <code>PrintingStatus</code> object
97 97 */
98 98 public static PrintingStatus
99 99 createPrintingStatus(Component parent, PrinterJob job) {
100 100 return new PrintingStatus(parent, job);
101 101 }
102 102
103 103 protected PrintingStatus(Component parent, PrinterJob job) {
104 104 this.job = job;
105 105 this.parent = parent;
106 106 }
107 107
108 108 private void init() {
109 109 // prepare the status JOptionPane
110 110 String progressTitle =
111 111 UIManager.getString("PrintingDialog.titleProgressText");
112 112
113 113 String dialogInitialContent =
114 114 UIManager.getString("PrintingDialog.contentInitialText");
115 115
116 116 // this one's a MessageFormat since it must include the page
117 117 // number in its text
118 118 statusFormat = new MessageFormat(
119 119 UIManager.getString("PrintingDialog.contentProgressText"));
120 120
121 121 String abortText =
122 122 UIManager.getString("PrintingDialog.abortButtonText");
123 123 String abortTooltip =
124 124 UIManager.getString("PrintingDialog.abortButtonToolTipText");
125 125 int abortMnemonic =
126 126 getInt("PrintingDialog.abortButtonMnemonic", -1);
127 127 int abortMnemonicIndex =
128 128 getInt("PrintingDialog.abortButtonDisplayedMnemonicIndex", -1);
129 129
130 130 abortButton = new JButton(abortText);
131 131 abortButton.addActionListener(abortAction);
132 132
133 133 abortButton.setToolTipText(abortTooltip);
134 134 if (abortMnemonic != -1) {
135 135 abortButton.setMnemonic(abortMnemonic);
136 136 }
137 137 if (abortMnemonicIndex != -1) {
138 138 abortButton.setDisplayedMnemonicIndex(abortMnemonicIndex);
139 139 }
140 140 statusLabel = new JLabel(dialogInitialContent);
141 141 JOptionPane abortPane = new JOptionPane(statusLabel,
142 142 JOptionPane.INFORMATION_MESSAGE,
143 143 JOptionPane.DEFAULT_OPTION,
144 144 null, new Object[]{abortButton},
145 145 abortButton);
146 146 abortPane.getActionMap().put("close", abortAction);
147 147
148 148 // The dialog should be centered over the viewport if the table is in one
149 149 if (parent != null && parent.getParent() instanceof JViewport) {
150 150 abortDialog =
151 151 abortPane.createDialog(parent.getParent(), progressTitle);
152 152 } else {
153 153 abortDialog = abortPane.createDialog(parent, progressTitle);
154 154 }
155 155 // clicking the X button should not hide the dialog
156 156 abortDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
157 157 abortDialog.addWindowListener(closeListener);
158 158 }
159 159
160 160 /**
161 161 * Shows PrintingStatus dialog.
162 162 * if dialog is modal this method returns only
163 163 * after <code>dispose()</code> was called otherwise returns immediately
164 164 *
165 165 * @param isModal <code>true</code> this dialog should be modal;
166 166 * <code>false</code> otherwise.
167 167 * @see #dispose
168 168 */
169 169 public void showModal(final boolean isModal) {
170 170 if (SwingUtilities.isEventDispatchThread()) {
171 171 showModalOnEDT(isModal);
172 172 } else {
173 173 try {
174 174 SwingUtilities.invokeAndWait(new Runnable() {
175 175 public void run() {
176 176 showModalOnEDT(isModal);
177 177 }
178 178 });
179 179 } catch(InterruptedException e) {
180 180 throw new RuntimeException(e);
181 181 } catch(InvocationTargetException e) {
182 182 Throwable cause = e.getCause();
183 183 if (cause instanceof RuntimeException) {
184 184 throw (RuntimeException) cause;
185 185 } else if (cause instanceof Error) {
186 186 throw (Error) cause;
187 187 } else {
188 188 throw new RuntimeException(cause);
189 189 }
190 190 }
191 191 }
192 192 }
193 193
194 194 /**
195 195 * The EDT part of the showModal method.
196 196 *
197 197 * This method is to be called on the EDT only.
198 198 */
199 199 private void showModalOnEDT(boolean isModal) {
200 200 assert SwingUtilities.isEventDispatchThread();
201 201 init();
202 202 abortDialog.setModal(isModal);
203 203 abortDialog.setVisible(true);
204 204 }
205 205
206 206 /**
207 207 * Disposes modal PrintingStatus dialog
208 208 *
209 209 * @see #showModal(boolean)
210 210 */
211 211 public void dispose() {
212 212 if (SwingUtilities.isEventDispatchThread()) {
213 213 disposeOnEDT();
214 214 } else {
215 215 SwingUtilities.invokeLater(new Runnable() {
216 216 public void run() {
217 217 disposeOnEDT();
218 218 }
219 219 });
220 220 }
221 221 }
222 222
223 223 /**
224 224 * The EDT part of the dispose method.
225 225 *
226 226 * This method is to be called on the EDT only.
227 227 */
228 228 private void disposeOnEDT() {
229 229 assert SwingUtilities.isEventDispatchThread();
230 230 if (abortDialog != null) {
231 231 abortDialog.removeWindowListener(closeListener);
232 232 abortDialog.dispose();
233 233 abortDialog = null;
234 234 }
235 235 }
236 236
237 237 /**
238 238 * Returns whether the printng was aborted using this PrintingStatus
239 239 *
240 240 * @return whether the printng was aborted using this PrintingStatus
241 241 */
242 242 public boolean isAborted() {
243 243 return isAborted.get();
244 244 }
245 245
246 246 /**
247 247 * Returns printable which is used to track the current page being
248 248 * printed in this PrintingStatus
249 249 *
250 250 * @param printable to be used to create notification printable
251 251 * @return printable which is used to track the current page being
252 252 * printed in this PrintingStatus
253 253 * @throws NullPointerException if <code>printable</code> is <code>null</code>
254 254 */
255 255 public Printable createNotificationPrintable(Printable printable) {
256 256 return new NotificationPrintable(printable);
257 257 }
258 258
259 259 private class NotificationPrintable implements Printable {
260 260 private final Printable printDelegatee;
261 261
262 262 public NotificationPrintable(Printable delegatee) {
263 263 if (delegatee == null) {
264 264 throw new NullPointerException("Printable is null");
265 265 }
266 266 this.printDelegatee = delegatee;
267 267 }
268 268
269 269 public int print(final Graphics graphics,
270 270 final PageFormat pageFormat, final int pageIndex)
271 271 throws PrinterException {
272 272
273 273 final int retVal =
274 274 printDelegatee.print(graphics, pageFormat, pageIndex);
275 275 if (retVal != NO_SUCH_PAGE && !isAborted()) {
276 276 if (SwingUtilities.isEventDispatchThread()) {
277 277 updateStatusOnEDT(pageIndex);
278 278 } else {
279 279 SwingUtilities.invokeLater(new Runnable() {
280 280 public void run() {
281 281 updateStatusOnEDT(pageIndex);
282 282 }
283 283 });
284 284 }
285 285 }
286 286 return retVal;
287 287 }
288 288
289 289 /**
290 290 * The EDT part of the print method.
291 291 *
292 292 * This method is to be called on the EDT only.
293 293 */
294 294 private void updateStatusOnEDT(int pageIndex) {
295 295 assert SwingUtilities.isEventDispatchThread();
296 296 Object[] pageNumber = new Object[]{
297 297 new Integer(pageIndex + 1)};
298 298 statusLabel.setText(statusFormat.format(pageNumber));
299 299 }
300 300 }
301 301
302 302 /**
303 303 * Duplicated from UIManager to make it visible
304 304 */
305 305 static int getInt(Object key, int defaultValue) {
306 306 Object value = UIManager.get(key);
307 307 if (value instanceof Integer) {
308 308 return ((Integer) value).intValue();
309 309 }
310 310 if (value instanceof String) {
311 311 try {
312 312 return Integer.parseInt((String) value);
313 313 } catch(NumberFormatException nfe) {
314 314 }
315 315 }
316 316 return defaultValue;
317 317 }
318 318 }
↓ open down ↓ |
262 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX