1 package net.suberic.pooka.gui; 2 3 import javax.swing.*; 4 import javax.print.*; 5 import javax.print.event.*; 6 import java.awt.event.ActionListener ; 7 import java.awt.event.ActionEvent ; 8 9 import net.suberic.pooka.*; 10 11 14 public class MessagePrinterDisplay implements PrintJobListener { 15 16 public static int RENDERING = 0; 17 public static int PAGINATING = 5; 18 public static int PRINTING = 10; 19 20 public static int CANCELED = -5; 21 public static int FAILED = -10; 22 23 MessagePrinter mPrinter = null; 24 DocPrintJob mJob = null; 25 26 int mStatus = RENDERING; 27 28 int mCurrentPage = 0; 29 30 int mPageCount = 0; 31 32 String mCurrentDoc = ""; 33 34 String mTitle; 35 36 JTextPane mDisplayPane = null; 37 JButton mOkButton = null; 38 JButton mCancelButton = null; 39 40 boolean mInternal = false; 41 42 JDialog mDialog = null; 43 JInternalFrame mDialogFrame = null; 44 45 Object mSource; 46 47 51 public MessagePrinterDisplay(MessagePrinter pPrinter, DocPrintJob pJob, Object pSource) { 52 mPrinter = pPrinter; 53 mJob = pJob; 54 mSource = pSource; 55 mInternal = checkInternal(); 56 mPrinter.setDisplay(this); 57 } 58 59 62 public void updateDisplayPane() { 63 StringBuffer displayMessage = new StringBuffer (); 64 displayMessage.append(getStatusString() + ": "); 65 displayMessage.append(mCurrentDoc); 66 displayMessage.append("\r\n\r\n"); 67 if (getStatus() > RENDERING) { 68 displayMessage.append(Pooka.getProperty("PrinterDisplay.page", "Page")); 69 displayMessage.append(" "); 70 displayMessage.append(mCurrentPage); 71 if (getStatus() > PAGINATING) { 72 displayMessage.append(" "); 73 displayMessage.append(Pooka.getProperty("PrinterDisplay.of", "of")); 74 displayMessage.append(" "); 75 displayMessage.append(mPageCount); 76 } 77 displayMessage.append("\r\n"); 78 } 79 80 final String msg = displayMessage.toString(); 81 if (SwingUtilities.isEventDispatchThread()) { 82 mDisplayPane.setText(msg); 83 mDisplayPane.repaint(); 84 } else { 85 SwingUtilities.invokeLater(new Runnable () { 86 public void run() { 87 mDisplayPane.setText(msg); 88 mDisplayPane.repaint(); 89 } 90 }); 91 } 92 } 93 94 97 public void setCurrentPage(int pCurrentPage) { 98 mCurrentPage = pCurrentPage; 99 updateDisplayPane(); 100 } 101 102 105 public void setPageCount(int pPageCount) { 106 mPageCount = pPageCount; 107 updateDisplayPane(); 108 } 109 110 113 public int getStatus() { 114 return mStatus; 115 } 116 117 120 public void setStatus(int pStatus) { 121 mStatus = pStatus; 122 updateDisplayPane(); 123 } 124 125 128 private boolean checkInternal() { 129 if (mSource instanceof JComponent) { 130 PookaUIFactory uiFactory = Pooka.getUIFactory(); 131 if (uiFactory instanceof PookaDesktopPaneUIFactory) { 132 JComponent sourceComponent = (JComponent) mSource; 133 if (((PookaDesktopPaneUIFactory) uiFactory).isInMainFrame(sourceComponent)) 134 return true; 135 } 136 } 137 138 return false; 139 140 } 141 142 145 public void show() { 146 mDisplayPane = new JTextPane(); 147 mDisplayPane.setBorder(BorderFactory.createEtchedBorder()); 148 JLabel jl = new JLabel(); 149 mDisplayPane.setBackground(jl.getBackground()); 150 mDisplayPane.setFont(jl.getFont()); 151 java.awt.Insets newMargin = new java.awt.Insets (10,10,10,10); 152 mDisplayPane.setMargin(newMargin); 153 154 if (mInternal) { 155 mDialogFrame = new JInternalFrame("Printing", true, false, false, true); 156 mDialogFrame.getContentPane().setLayout(new BoxLayout(mDialogFrame.getContentPane(), BoxLayout.Y_AXIS)); 157 158 mDialogFrame.getContentPane().add(mDisplayPane); 159 if (mJob instanceof CancelablePrintJob) { 160 Box buttonBox = createButtonBox(); 161 mDialogFrame.getContentPane().add(buttonBox); 162 } 163 updateDisplayPane(); 164 mDialogFrame.pack(); 165 166 mDialogFrame.setSize(Math.max(mDialogFrame.getPreferredSize().width, 300), Math.max(mDialogFrame.getPreferredSize().height, 200)); 167 168 MessagePanel mp = ((PookaDesktopPaneUIFactory)Pooka.getUIFactory()).getMessagePanel(); 169 mp.add(mDialogFrame); 170 mDialogFrame.setLocation(mp.getNewWindowLocation(mDialogFrame, true)); 171 mDialogFrame.setVisible(true); 172 173 } else { 174 if (mSource instanceof JComponent && SwingUtilities.getWindowAncestor((JComponent) mSource) instanceof java.awt.Frame ) { 175 mDialog = new JDialog((java.awt.Frame ) SwingUtilities.getWindowAncestor((JComponent) mSource)); 176 } else { 177 mDialog = new JDialog(); 178 } 179 mDialog.getContentPane().setLayout(new BoxLayout(mDialog.getContentPane(), BoxLayout.Y_AXIS)); 180 181 mDialog.getContentPane().add(mDisplayPane); 182 183 if (mJob instanceof CancelablePrintJob) { 184 Box buttonBox = createButtonBox(); 185 mDialog.getContentPane().add(buttonBox); 186 } 187 updateDisplayPane(); 188 mDialog.pack(); 189 mDialog.setSize(Math.max(mDialog.getPreferredSize().width, 300), Math.max(mDialog.getPreferredSize().height, 200)); 190 mDialog.setVisible(true); 191 } 192 193 } 194 195 198 public void cancel() { 199 if (mJob instanceof CancelablePrintJob) { 200 try { 201 ((CancelablePrintJob) mJob).cancel(); 202 } catch (PrintException e) { 203 showError(Pooka.getProperty("PrintDisplay.message.errorCanceling", "Error canceling job: "), e); 204 } 205 } 206 } 207 208 211 Box createButtonBox() { 212 Box returnValue = new Box(BoxLayout.X_AXIS); 213 JButton cancelButton = new JButton(Pooka.getProperty("button.cancel", "Cancel")); 214 returnValue.add(Box.createHorizontalGlue()); 215 returnValue.add(cancelButton); 216 returnValue.add(Box.createHorizontalGlue()); 217 cancelButton.addActionListener(new ActionListener () { 218 public void actionPerformed(ActionEvent e) { 219 cancel(); 220 } 221 }); 222 223 return returnValue; 224 225 } 226 227 230 public void dispose() { 231 if (SwingUtilities.isEventDispatchThread()) { 232 if (mInternal) { 233 try { 234 mDialogFrame.setClosed(true); 235 } catch (java.beans.PropertyVetoException e) { 236 } 237 } else { 238 mDialog.dispose(); 239 } 240 } else { 241 SwingUtilities.invokeLater(new Runnable () { 242 public void run() { 243 if (mInternal) { 244 try { 245 mDialogFrame.setClosed(true); 246 } catch (java.beans.PropertyVetoException e) { 247 } 248 } else { 249 mDialog.dispose(); 250 } 251 } 252 }); 253 } 254 } 255 256 259 public String getStatusString() { 260 if (mStatus == RENDERING) { 261 return Pooka.getProperty("PrintDisplay.status.rendering", "Rendering"); 262 } else if (mStatus == PAGINATING) { 263 return Pooka.getProperty("PrintDisplay.status.paginating", "Paginating"); 264 } else { 265 return Pooka.getProperty("PrintDisplay.status.printing", "Printing"); 266 } 267 } 268 269 272 public void showError(String text) { 273 Pooka.getUIFactory().showError(text); 274 } 275 276 279 public void showError(String text, Exception e) { 280 Pooka.getUIFactory().showError(text, e); 281 } 282 283 285 public void printDataTransferCompleted(PrintJobEvent pje) { 286 } 288 289 public void printJobCompleted(PrintJobEvent pje) { 290 } 292 293 public void printJobCanceled(PrintJobEvent pje) { 294 setStatus(CANCELED); 295 showError(Pooka.getProperty("PrintDisplay.message.canceled", "Canceled.")); 296 dispose(); 297 } 298 299 public void printJobFailed(PrintJobEvent pje) { 300 if (getStatus() > CANCELED) { 301 setStatus(FAILED); 302 showError(Pooka.getProperty("PrintDisplay.message.failed", "Failed.")); 303 dispose(); 304 } 305 } 306 307 public void printJobNoMoreEvents(PrintJobEvent pje) { 308 dispose(); 309 } 310 311 public void printJobRequiresAttention(PrintJobEvent pje) { 312 showError(Pooka.getProperty("PrintDisplay.message.needsAttention", "Needs attention.")); 313 } 314 315 } 316 | Popular Tags |