1 17 18 19 20 package org.apache.fop.render.awt.viewer; 25 26 import java.awt.BorderLayout ; 28 import java.awt.Dimension ; 29 import java.awt.GridBagConstraints ; 30 import java.awt.GridBagLayout ; 31 import java.awt.Insets ; 32 import java.awt.Point ; 33 import java.awt.Toolkit ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.KeyEvent ; 37 import java.awt.event.WindowAdapter ; 38 import java.awt.event.WindowEvent ; 39 import java.awt.print.PrinterException ; 40 import java.awt.print.PrinterJob ; 41 42 import java.text.DecimalFormat ; 43 44 import javax.swing.ActionMap ; 45 import javax.swing.BorderFactory ; 46 import javax.swing.ButtonGroup ; 47 import javax.swing.InputMap ; 48 import javax.swing.JComboBox ; 49 import javax.swing.JComponent ; 50 import javax.swing.JFrame ; 51 import javax.swing.JLabel ; 52 import javax.swing.JMenu ; 53 import javax.swing.JMenuBar ; 54 import javax.swing.JOptionPane ; 55 import javax.swing.JPanel ; 56 import javax.swing.JRadioButtonMenuItem ; 57 import javax.swing.JToolBar ; 58 import javax.swing.KeyStroke ; 59 import javax.swing.SwingUtilities ; 60 import javax.swing.UIManager ; 61 62 import org.apache.fop.apps.FOUserAgent; 63 import org.apache.fop.apps.FOPException; 64 import org.apache.fop.render.awt.AWTRenderer; 65 66 70 public class PreviewDialog extends JFrame implements StatusListener { 71 72 73 protected Translator translator; 74 75 protected AWTRenderer renderer; 76 77 protected FOUserAgent foUserAgent; 78 82 protected Renderable renderable; 83 84 85 private JComboBox scale; 86 87 88 private JLabel processStatus; 89 90 91 private JLabel infoStatus; 92 93 94 private PreviewPanel previewPanel; 95 96 97 private DecimalFormat percentFormat = new DecimalFormat ("###0.0#"); 98 99 105 public PreviewDialog(FOUserAgent foUserAgent, Renderable renderable) { 106 renderer = (AWTRenderer) foUserAgent.getRendererOverride(); 107 this.foUserAgent = foUserAgent; 108 this.renderable = renderable; 109 translator = new Translator(); 110 111 Command printAction = new Command(translator.getString("Menu.Print"), "Print") { 113 public void doit() { 114 startPrinterJob(true); 115 } 116 }; 117 Command firstPageAction = new Command(translator.getString("Menu.First.page"), 118 "firstpg") { 119 public void doit() { 120 goToFirstPage(); 121 } 122 }; 123 Command previousPageAction = new Command(translator.getString("Menu.Prev.page"), 124 "prevpg") { 125 public void doit() { 126 goToPreviousPage(); 127 } 128 }; 129 Command nextPageAction = new Command(translator.getString("Menu.Next.page"), "nextpg") { 130 public void doit() { 131 goToNextPage(); 132 } 133 134 }; 135 Command lastPageAction = new Command(translator.getString("Menu.Last.page"), "lastpg") { 136 public void doit() { 137 goToLastPage(); 138 } 139 }; 140 Command reloadAction = new Command(translator.getString("Menu.Reload"), "reload") { 141 public void doit() { 142 previewPanel.reload(); 143 } 144 }; 145 Command debugAction = new Command(translator.getString("Menu.Debug"), "debug") { 146 public void doit() { 148 previewPanel.debug(); 149 } 150 }; 151 Command aboutAction = new Command(translator.getString("Menu.About"), "fopLogo") { 152 public void doit() { 153 startHelpAbout(); 154 } 155 }; 156 157 setTitle("FOP: AWT-" + translator.getString("Title.Preview")); 158 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 159 160 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 162 pack(); 164 setSize(screen.width * 61 / 100, screen.height * 9 / 10); 166 167 previewPanel = new PreviewPanel(foUserAgent, renderable, renderer); 169 getContentPane().add(previewPanel, BorderLayout.CENTER); 170 171 InputMap im = previewPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 173 ActionMap am = previewPanel.getActionMap(); 174 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0), "nextPage"); 175 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0), "prevPage"); 176 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0), "firstPage"); 177 im.put(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0), "lastPage"); 178 previewPanel.getActionMap().put("nextPage", nextPageAction); 179 previewPanel.getActionMap().put("prevPage", previousPageAction); 180 previewPanel.getActionMap().put("firstPage", firstPageAction); 181 previewPanel.getActionMap().put("lastPage", lastPageAction); 182 183 scale = new JComboBox (); 185 scale.addItem(translator.getString("Menu.Fit.Window")); 186 scale.addItem(translator.getString("Menu.Fit.Width")); 187 scale.addItem("25%"); 188 scale.addItem("50%"); 189 scale.addItem("75%"); 190 scale.addItem("100%"); 191 scale.addItem("150%"); 192 scale.addItem("200%"); 193 scale.setMaximumSize(new Dimension (80, 24)); 194 scale.setPreferredSize(new Dimension (80, 24)); 195 scale.setSelectedItem("100%"); 196 scale.setEditable(true); 197 scale.addActionListener(new ActionListener () { 198 public void actionPerformed(ActionEvent e) { 199 scaleActionPerformed(e); 200 } 201 }); 202 203 setJMenuBar(setupMenu()); 205 206 JToolBar toolBar = new JToolBar (); 208 toolBar.add(printAction); 209 toolBar.add(reloadAction); 210 toolBar.addSeparator(); 211 toolBar.add(firstPageAction); 212 toolBar.add(previousPageAction); 213 toolBar.add(nextPageAction); 214 toolBar.add(lastPageAction); 215 toolBar.addSeparator(new Dimension (20, 0)); 216 toolBar.add(new JLabel (translator.getString("Menu.Zoom") + " ")); 217 toolBar.add(scale); 218 toolBar.addSeparator(); 219 toolBar.add(debugAction); 220 toolBar.addSeparator(); 221 toolBar.add(aboutAction); 222 getContentPane().add(toolBar, BorderLayout.NORTH); 223 224 JPanel statusBar = new JPanel (); 226 processStatus = new JLabel (); 227 processStatus.setBorder(BorderFactory.createCompoundBorder( 228 BorderFactory.createEtchedBorder(), 229 BorderFactory.createEmptyBorder(0, 3, 0, 0))); 230 infoStatus = new JLabel (); 231 infoStatus.setBorder(BorderFactory.createCompoundBorder( 232 BorderFactory.createEtchedBorder(), 233 BorderFactory.createEmptyBorder(0, 3, 0, 0))); 234 235 statusBar.setLayout(new GridBagLayout ()); 236 237 processStatus.setPreferredSize(new Dimension (200, 21)); 238 processStatus.setMinimumSize(new Dimension (200, 21)); 239 240 infoStatus.setPreferredSize(new Dimension (100, 21)); 241 infoStatus.setMinimumSize(new Dimension (100, 21)); 242 statusBar.add(processStatus, 243 new GridBagConstraints (0, 0, 1, 0, 2.0, 0.0, 244 GridBagConstraints.CENTER, 245 GridBagConstraints.HORIZONTAL, 246 new Insets (0, 0, 0, 3), 0, 0)); 247 statusBar.add(infoStatus, 248 new GridBagConstraints (1, 0, 1, 0, 1.0, 0.0, 249 GridBagConstraints.CENTER, 250 GridBagConstraints.HORIZONTAL, 251 new Insets (0, 0, 0, 0), 0, 0)); 252 getContentPane().add(statusBar, BorderLayout.SOUTH); 253 } 254 255 261 public static PreviewDialog createPreviewDialog(FOUserAgent foUserAgent, 262 Renderable renderable, boolean asMainWindow) { 263 PreviewDialog frame = new PreviewDialog(foUserAgent, renderable); 264 265 if (asMainWindow) { 266 frame.addWindowListener(new WindowAdapter () { 267 public void windowClosed(WindowEvent we) { 268 System.exit(0); 269 } 270 }); 271 } 272 273 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 275 Dimension frameSize = frame.getSize(); 276 if (frameSize.height > screenSize.height) { 277 frameSize.height = screenSize.height; 278 } 279 if (frameSize.width > screenSize.width) { 280 frameSize.width = screenSize.width; 281 } 282 frame.setLocation((screenSize.width - frameSize.width) / 2, 283 (screenSize.height - frameSize.height) / 2); 284 frame.setStatus(frame.translator.getString("Status.Build.FO.tree")); 285 frame.setVisible(true); 286 return frame; 287 } 288 289 293 public PreviewDialog(FOUserAgent foUserAgent) { 294 this(foUserAgent, null); 295 } 296 297 301 private JMenuBar setupMenu() { 302 JMenuBar menuBar = new JMenuBar (); 303 JMenu menu = new JMenu (translator.getString("Menu.File")); 304 menu.setMnemonic(KeyEvent.VK_F); 305 menu.add(new Command(translator.getString("Menu.Print"), KeyEvent.VK_P) { 307 public void doit() { 308 startPrinterJob(true); 309 } 310 }); 311 if (renderable != null) { 313 menu.add(new Command(translator.getString("Menu.Reload"), KeyEvent.VK_R) { 314 public void doit() { 315 reload(); 316 } 317 }); 318 } 319 menu.addSeparator(); 320 menu.add(new Command(translator.getString("Menu.Exit"), KeyEvent.VK_X) { 321 public void doit() { 322 dispose(); 323 } 324 }); 325 menuBar.add(menu); 326 327 menu = new JMenu (translator.getString("Menu.View")); 328 menu.setMnemonic(KeyEvent.VK_V); 329 menu.add(new Command(translator.getString("Menu.First.page"), KeyEvent.VK_F) { 330 public void doit() { 331 goToFirstPage(); 332 } 333 }); 334 menu.add(new Command(translator.getString("Menu.Prev.page"), KeyEvent.VK_P) { 335 public void doit() { 336 goToPreviousPage(); 337 } 338 }); 339 menu.add(new Command(translator.getString("Menu.Next.page"), KeyEvent.VK_N) { 340 public void doit() { 341 goToNextPage(); 342 } 343 }); 344 menu.add(new Command(translator.getString("Menu.Last.page"), KeyEvent.VK_L) { 345 public void doit() { 346 goToLastPage(); 347 } 348 }); 349 menu.add(new Command(translator.getString("Menu.Go.to.Page"), KeyEvent.VK_G) { 350 public void doit() { 351 showGoToPageDialog(); 352 } 353 }); 354 menu.addSeparator(); 355 JMenu subMenu = new JMenu (translator.getString("Menu.Zoom")); 356 subMenu.setMnemonic(KeyEvent.VK_Z); 357 subMenu.add(new Command("25%", 0) { 358 public void doit() { 359 setScale(25.0); 360 } 361 }); 362 subMenu.add(new Command("50%", 0) { 363 public void doit() { 364 setScale(50.0); 365 } 366 }); 367 subMenu.add(new Command("75%", 0) { 368 public void doit() { 369 setScale(75.0); 370 } 371 }); 372 subMenu.add(new Command("100%", 0) { 373 public void doit() { 374 setScale(100.0); 375 } 376 }); 377 subMenu.add(new Command("150%", 0) { 378 public void doit() { 379 setScale(150.0); 380 } 381 }); 382 subMenu.add(new Command("200%", 0) { 383 public void doit() { 384 setScale(200.0); 385 } 386 }); 387 menu.add(subMenu); 388 menu.addSeparator(); 389 menu.add(new Command(translator.getString("Menu.Default.zoom"), KeyEvent.VK_D) { 390 public void doit() { 391 setScale(100.0); 392 } 393 }); 394 menu.add(new Command(translator.getString("Menu.Fit.Window"), KeyEvent.VK_F) { 395 public void doit() { 396 setScaleToFitWindow(); 397 } 398 }); 399 menu.add(new Command(translator.getString("Menu.Fit.Width"), KeyEvent.VK_W) { 400 public void doit() { 401 setScaleToFitWidth(); 402 } 403 }); 404 menu.addSeparator(); 405 406 ButtonGroup group = new ButtonGroup (); 407 JRadioButtonMenuItem single = new JRadioButtonMenuItem ( 408 new Command(translator.getString("Menu.Single"), KeyEvent.VK_S) { 409 public void doit() { 410 previewPanel.setDisplayMode(PreviewPanel.SINGLE); 411 } 412 }); 413 JRadioButtonMenuItem cont = new JRadioButtonMenuItem ( 414 new Command(translator.getString("Menu.Continuous"), KeyEvent.VK_C) { 415 public void doit() { 416 previewPanel.setDisplayMode(PreviewPanel.CONTINUOUS); 417 } 418 }); 419 JRadioButtonMenuItem facing = new JRadioButtonMenuItem ( 420 new Command(translator.getString("Menu.Facing"), 0) { 421 public void doit() { 422 previewPanel.setDisplayMode(PreviewPanel.CONT_FACING); 423 } 424 }); 425 single.setSelected(true); 426 group.add(single); 427 group.add(cont); 428 group.add(facing); 429 menu.add(single); 430 menu.add(cont); 431 menu.add(facing); 432 433 menuBar.add(menu); 434 435 menu = new JMenu (translator.getString("Menu.Help")); 436 menu.setMnemonic(KeyEvent.VK_H); 437 menu.add(new Command(translator.getString("Menu.About"), KeyEvent.VK_A) { 438 public void doit() { 439 startHelpAbout(); 440 } 441 }); 442 menuBar.add(menu); 443 return menuBar; 444 } 445 446 447 public void notifyRendererStopped() { 448 reload(); 449 } 450 451 private void reload() { 452 setStatus(translator.getString("Status.Show")); 453 previewPanel.reload(); 454 } 455 456 460 public void goToPage(int number) { 461 if (number != previewPanel.getPage()) { 462 previewPanel.setPage(number); 463 notifyPageRendered(); 464 } 465 } 466 467 470 public void goToPreviousPage() { 471 int page = previewPanel.getPage(); 472 if (page > 0) { 473 goToPage(page - 1); 474 } 475 } 476 477 480 public void goToNextPage() { 481 int page = previewPanel.getPage(); 482 if (page < renderer.getNumberOfPages() - 1) { 483 goToPage(page + 1); 484 } 485 } 486 487 488 public void goToFirstPage() { 489 goToPage(0); 490 } 491 492 495 public void goToLastPage() { 496 goToPage(renderer.getNumberOfPages() - 1); 497 } 498 499 500 private void startHelpAbout() { 501 PreviewDialogAboutBox dlg = new PreviewDialogAboutBox(this, translator); 502 Dimension dlgSize = dlg.getPreferredSize(); 504 Dimension frmSize = getSize(); 505 Point loc = getLocation(); 506 dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, 507 (frmSize.height - dlgSize.height) / 2 + loc.y); 508 dlg.setVisible(true); 509 } 510 511 514 private void showGoToPageDialog() { 515 int currentPage = previewPanel.getPage(); 516 GoToPageDialog d = new GoToPageDialog(this, 517 translator.getString("Menu.Go.to.Page"), translator); 518 d.setLocation((int)getLocation().getX() + 50, 519 (int)getLocation().getY() + 50); 520 d.setVisible(true); 521 currentPage = d.getPageNumber(); 522 if (currentPage < 1 || currentPage > renderer.getNumberOfPages()) { 523 return; 524 } 525 currentPage--; 526 goToPage(currentPage); 527 } 528 529 530 public void setScale(double scaleFactor) { 531 scale.setSelectedItem(percentFormat.format(scaleFactor) + "%"); 532 previewPanel.setScaleFactor(scaleFactor / 100d); 533 } 534 535 public void setScaleToFitWindow() { 536 try { 537 setScale(previewPanel.getScaleToFitWindow() * 100); 538 } catch (FOPException fopEx) { 539 fopEx.printStackTrace(); 540 } 541 } 542 543 public void setScaleToFitWidth() { 544 try { 545 setScale(previewPanel.getScaleToFitWidth() * 100); 546 } catch (FOPException fopEx) { 547 fopEx.printStackTrace(); 548 } 549 } 550 551 private void scaleActionPerformed(ActionEvent e) { 552 try { 553 int index = scale.getSelectedIndex(); 554 if (index == 0) { 555 setScale(previewPanel.getScaleToFitWindow() * 100); 556 } else if (index == 1) { 557 setScale(previewPanel.getScaleToFitWidth() * 100); 558 } else { 559 String item = (String )scale.getSelectedItem(); 560 setScale(Double.parseDouble(item.substring(0, item.indexOf('%')))); 561 } 562 } catch (FOPException fopEx) { 563 fopEx.printStackTrace(); 564 } 565 } 566 567 568 public void startPrinterJob(boolean showDialog) { 569 PrinterJob pj = PrinterJob.getPrinterJob(); 570 pj.setPageable(renderer); 571 if (!showDialog || pj.printDialog()) { 572 try { 573 pj.print(); 574 } catch (PrinterException e) { 575 e.printStackTrace(); 576 } 577 } 578 } 579 580 584 public void setStatus(String message) { 585 SwingUtilities.invokeLater(new ShowStatus(message)); 586 } 587 588 589 private class ShowStatus implements Runnable { 590 591 592 private String message; 593 594 598 public ShowStatus(String message) { 599 this.message = message; 600 } 601 602 public void run() { 603 processStatus.setText(message.toString()); 604 } 605 } 606 607 610 public void notifyPageRendered() { 611 SwingUtilities.invokeLater(new ShowInfo()); 612 } 613 614 615 private class ShowInfo implements Runnable { 616 617 public void run() { 618 String message = translator.getString("Status.Page") + " " 619 + (previewPanel.getPage() + 1) + " " 620 + translator.getString("Status.of") + " " 621 + (renderer.getNumberOfPages()); 622 infoStatus.setText(message); 623 } 624 } 625 626 630 public void reportException(Exception e) { 631 String msg = translator.getString("Exception.Occured"); 632 setStatus(msg); 633 JOptionPane.showMessageDialog( 634 getContentPane(), 635 "<html><b>" + msg + ":</b><br>" 636 + e.getClass().getName() + "<br>" 637 + e.getMessage() + "</html>", 638 translator.getString("Exception.Error"), 639 JOptionPane.ERROR_MESSAGE 640 ); 641 } 642 } 643 | Popular Tags |