1 package jimm.datavision.layout.swing; 2 import jimm.datavision.*; 3 import jimm.datavision.field.*; 4 import jimm.datavision.layout.LayoutEngine; 5 import jimm.datavision.gui.ExportWin; 6 import jimm.datavision.gui.MenuUtils; 7 import jimm.datavision.gui.StatusDialog; 8 import jimm.util.I18N; 9 import java.awt.*; 10 import java.awt.print.*; 11 import java.awt.event.*; 12 import javax.swing.*; 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 16 26 public class SwingLE extends LayoutEngine { 27 28 protected static final Dimension WINDOW_START_SIZE = new Dimension(600, 400); 29 protected static final String PRINT_ICON = "images/Print16.gif"; 30 protected static final String EXPORT_ICON = "images/Export16.gif"; 31 protected static final String FIRST_ICON = "images/Home16.gif"; 32 protected static final String PREV_ICON = "images/Back16.gif"; 33 protected static final String NEXT_ICON = "images/Forward16.gif"; 34 protected static final String LAST_ICON = "images/Down16.gif"; 35 36 protected JFrame frame; 37 protected Dimension pageDim; 38 protected JPanel cardPanel; 39 protected JScrollPane scroller; 40 protected int displayPageNum; protected ArrayList pageContents; 42 protected SwingPageContents pageBeingBuilt; 43 protected JLabel pageCountLabel; 44 protected Action printAction; 45 protected Action closeAction; 46 protected Action exportAction; 47 protected Action goFirstAction; 48 protected Action goPrevAction; 49 protected Action goNextAction; 50 protected Action goLastAction; 51 52 55 public SwingLE() { 56 super(); 57 pageContents = new ArrayList (); 58 } 59 60 public void cancel() { 61 super.cancel(); 62 close(); 63 } 64 65 public JFrame getJFrame() { return frame; } 66 67 70 protected void doStart() { 71 pageDim = new Dimension((int)pageWidth(), (int)pageHeight()); 72 frame = new JFrame(report.getTitle()); 73 makeActions(); 74 makeMenu(frame); 75 frame.getContentPane().add(makeToolbar(), BorderLayout.NORTH); 76 77 cardPanel = new JPanel(); 79 CardLayout cardLayout = new CardLayout(0, 0); 80 cardPanel.setLayout(cardLayout); 81 82 cardPanel.setPreferredSize(pageDim); 84 85 JPanel blankPage = new JPanel(); 88 blankPage.setBackground(Color.white); 89 blankPage.setPreferredSize(pageDim); 90 cardPanel.add(blankPage, "blank page"); 91 cardLayout.show(cardPanel, "blank page"); 92 93 scroller = new JScrollPane(cardPanel); 95 scroller.setPreferredSize(WINDOW_START_SIZE); 96 frame.getContentPane().add(scroller, BorderLayout.CENTER); 97 98 displayPageNum = 0; 99 100 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 101 frame.addWindowListener(new WindowAdapter() { 102 public void windowClosing(WindowEvent e) { 103 close(); 104 } 105 }); 106 107 frame.pack(); 108 frame.setVisible(true); 109 } 110 111 114 protected void doEnd() { 115 printAction.setEnabled(true); 116 117 if (pageContents.size() > 1) pageBeingBuilt.prebuildPage(); 119 120 if (displayPageNum == 0) 123 displayPage(1); 124 } 125 126 129 protected void makeActions() { 130 URL url = getClass().getClassLoader().getResource(PRINT_ICON); 131 String str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_print"); 132 printAction = new AbstractAction(str, new ImageIcon(url, str)) { 133 public void actionPerformed(ActionEvent e) { printReport(); } 134 }; 135 printAction.putValue(Action.SHORT_DESCRIPTION, str); 136 printAction.setEnabled(false); 137 138 str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_close"); 139 closeAction = new AbstractAction(str) { 140 public void actionPerformed(ActionEvent e) { close(); } 141 }; 142 143 url = getClass().getClassLoader().getResource(EXPORT_ICON); 144 str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_export"); 145 exportAction = new AbstractAction(str, new ImageIcon(url, str)) { 146 public void actionPerformed(ActionEvent e) { export(); } 147 }; 148 exportAction.putValue(Action.SHORT_DESCRIPTION, str); 149 150 url = getClass().getClassLoader().getResource(FIRST_ICON); 151 str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_first_page"); 152 goFirstAction = new AbstractAction(str, new ImageIcon(url, str)) { 153 public void actionPerformed(ActionEvent e) { displayFirstPage(); } 154 }; 155 goFirstAction.putValue(Action.SHORT_DESCRIPTION, str); 156 goFirstAction.setEnabled(false); 157 158 url = getClass().getClassLoader().getResource(PREV_ICON); 159 str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_previous_page"); 160 goPrevAction = new AbstractAction(str, new ImageIcon(url, str)) { 161 public void actionPerformed(ActionEvent e) { displayPrevPage(); } 162 }; 163 goPrevAction.putValue(Action.SHORT_DESCRIPTION, str); 164 goPrevAction.setEnabled(false); 165 166 url = getClass().getClassLoader().getResource(NEXT_ICON); 167 str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_next_page"); 168 goNextAction = new AbstractAction(str, new ImageIcon(url, str)) { 169 public void actionPerformed(ActionEvent e) { displayNextPage(); } 170 }; 171 goNextAction.putValue(Action.SHORT_DESCRIPTION, str); 172 goNextAction.setEnabled(false); 173 174 url = getClass().getClassLoader().getResource(LAST_ICON); 175 str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_last_page"); 176 goLastAction = new AbstractAction(str, new ImageIcon(url, str)) { 177 public void actionPerformed(ActionEvent e) { displayLastPage(); } 178 }; 179 goLastAction.putValue(Action.SHORT_DESCRIPTION, str); 180 goLastAction.setEnabled(false); 181 } 182 183 188 protected void makeMenu(JFrame frame) { 189 JMenuBar menuBar = new JMenuBar(); 190 frame.setJMenuBar(menuBar); 191 192 JMenu menu = MenuUtils.readMenu("SwingLE.menu_file"); 194 menuBar.add(menu); 195 196 MenuUtils.addToMenu(menu, printAction, "SwingLE.action_print"); 197 menu.addSeparator(); 198 MenuUtils.addToMenu(menu, closeAction, "SwingLE.action_close"); 199 200 menu = MenuUtils.readMenu("SwingLE.menu_view"); 202 menuBar.add(menu); 203 204 MenuUtils.addToMenu(menu, goFirstAction, "SwingLE.action_first_page"); 205 MenuUtils.addToMenu(menu, goPrevAction, "SwingLE.action_previous_page"); 206 MenuUtils.addToMenu(menu, goNextAction, "SwingLE.action_next_page"); 207 MenuUtils.addToMenu(menu, goLastAction, "SwingLE.action_last_page"); 208 209 menu = MenuUtils.readMenu("SwingLE.menu_report"); 211 menuBar.add(menu); 212 213 MenuUtils.addToMenu(menu, exportAction, "SwingLE.action_export"); 214 } 215 216 219 protected JToolBar makeToolbar() { 220 JToolBar bar = new JToolBar(javax.swing.SwingConstants.HORIZONTAL); 221 bar.add(printAction); 222 bar.addSeparator(); 223 bar.add(exportAction); 224 bar.addSeparator(); 225 bar.add(goFirstAction); 226 bar.add(goPrevAction); 227 bar.add(goNextAction); 228 bar.add(goLastAction); 229 pageCountLabel = new JLabel(I18N.get("SwingLE.loading_first_page")); 230 bar.addSeparator(); 231 bar.add(pageCountLabel); 232 return bar; 233 } 234 235 238 protected void doStartPage() { 239 int pageNum = pageContents.size() + 1; 240 pageBeingBuilt = new SwingPageContents(cardPanel, pageNum, pageDim); 241 pageContents.add(pageBeingBuilt); 242 } 243 244 249 protected void doEndPage() { 250 int numPages = pageContents.size(); 251 if (numPages == 1) { pageBeingBuilt.prebuildPage(); 254 pageCountLabel.setText(I18N.get("SwingLE.building_first_page")); 255 } 256 257 if (((SwingPageContents)pageContents.get(0)).isPageBuilt() 260 && displayPageNum == 0) 261 displayPage(1); else { 263 updatePageCountLabel(); 264 updateNavActions(); 265 } 266 } 267 268 271 public void close() { 272 if (frame != null) { 273 frame.setVisible(false); 274 frame.dispose(); 275 } 276 wantsMoreData = false; 278 pageContents = null; 280 pageBeingBuilt = null; 281 } 282 283 286 protected void export() { 287 new ExportWin(frame, report); 288 } 289 290 294 protected void updateNavActions() { 295 int numPages = pageContents.size(); 296 297 boolean canGoBack = numPages > 0 && displayPageNum > 1; 298 goFirstAction.setEnabled(canGoBack); 299 goPrevAction.setEnabled(canGoBack); 300 301 boolean canGoForward = numPages > 0 && displayPageNum < numPages; 302 goNextAction.setEnabled(canGoForward); 303 goLastAction.setEnabled(canGoForward); 304 } 305 306 310 protected void updatePageCountLabel() { 311 if (displayPageNum > 0) 312 pageCountLabel.setText(I18N.get("SwingLE.page") 313 + ' ' + displayPageNum + ' ' 314 + I18N.get("SwingLE.of") 315 + ' ' + pageContents.size()); 316 } 317 318 319 protected void displayFirstPage() { 320 if (pageContents.size() > 0) 321 displayPage(1); 322 } 323 324 325 protected void displayNextPage() { 326 if (pageContents.size() > displayPageNum) 327 displayPage(displayPageNum + 1); 328 } 329 330 331 protected void displayPrevPage() { 332 if (displayPageNum > 1) 333 displayPage(displayPageNum - 1); 334 } 335 336 337 protected void displayLastPage() { 338 int numPages = pageContents.size(); 339 if (numPages > 0) 340 displayPage(numPages); 341 } 342 343 351 protected void displayPage(int num) { 352 if (num == displayPageNum) 353 return; 354 355 SwingPageContents contents = (SwingPageContents)pageContents.get(num - 1); 357 if (!contents.isPageBuilt()) 358 pageCountLabel.setText(I18N.get("SwingLE.building_page") 359 + ' ' + num + "..."); 360 361 int vertPosition = scroller.getVerticalScrollBar().getValue(); 362 contents.showPage(); scroller.getVerticalScrollBar().setValue(vertPosition); 364 365 int numPages = pageContents.size(); 368 if (numPages > num) { contents = (SwingPageContents)pageContents.get(num); 370 contents.prebuildPage(); 371 } 372 373 if (num > 1) { 377 contents = (SwingPageContents)pageContents.get(num - 2); 378 contents.prebuildPage(); 379 } 380 381 displayPageNum = num; 382 updatePageCountLabel(); 383 updateNavActions(); 384 385 for (int i = 1; i < (numPages - 1); ++i) { 389 if (i < (displayPageNum - 2) || i > (displayPageNum)) { 390 contents = (SwingPageContents)pageContents.get(i); 391 contents.forgetPage(); 392 } 393 } 394 } 395 396 401 protected void doOutputField(Field field) { 402 if (!field.isVisible()) 403 return; 404 405 String fieldAsString = field.toString(); 406 if (fieldAsString == null || fieldAsString.length() == 0) { 407 return; 410 } 411 412 jimm.datavision.field.Rectangle bounds = field.getBounds(); 413 414 int y = (currentSection.getArea().getArea() == SectionArea.PAGE_FOOTER) 416 ? (int)(pageHeight() - currentSection.getOutputHeight() + bounds.y) 417 : (int)(pageHeightUsed + bounds.y); 418 java.awt.Rectangle fieldBounds = 419 new java.awt.Rectangle ((int)bounds.x, y, (int)bounds.width, 420 (int)field.getOutputHeight()); 421 pageBeingBuilt.add(field, fieldAsString, fieldBounds); 422 423 } 426 427 protected void doOutputImage(ImageField image) { 428 doOutputField(image); 429 } 430 431 436 protected void doOutputLine(Line line) {} 437 438 441 public void printReport() { 442 final PrinterJob printJob = PrinterJob.getPrinterJob(); 443 PageFormat format = report.getPaperFormat().getPageFormat(); 444 SwingPrintBook book = new SwingPrintBook(pageContents, format); 445 printJob.setPageable(book); 446 447 if (printJob.printDialog()) { 448 final StatusDialog statusDialog = 449 new StatusDialog(frame, 450 I18N.get("SwingLE.print_report_title"), 451 true, 452 I18N.get("SwingLE.print_report_status")); 453 book.setStatusDialog(statusDialog); 454 455 new Thread (new Runnable () { 456 public void run() { 457 try { 458 printJob.print(); 459 } 460 catch (UserCancellationException uce) { 461 printJob.cancel(); 462 } 463 catch (Exception e) { 464 ErrorHandler.error(e); 465 } 466 finally { 467 if (statusDialog != null) 468 statusDialog.dispose(); 469 } 470 } 471 }).start(); 472 } 473 } 474 475 } 476 | Popular Tags |