KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > awt > viewer > PreviewDialog


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: PreviewDialog.java 473976 2006-11-12 15:38:45Z jeremias $ */
19
20 // Originally contributed by:
21
// Juergen Verwohlt: Juergen.Verwohlt@jCatalog.com,
22
// Rainer Steinkuhle: Rainer.Steinkuhle@jCatalog.com,
23
// Stanislav Gorkhover: Stanislav.Gorkhover@jCatalog.com
24
package org.apache.fop.render.awt.viewer;
25
26 // Java
27
import java.awt.BorderLayout JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.GridBagConstraints JavaDoc;
30 import java.awt.GridBagLayout JavaDoc;
31 import java.awt.Insets JavaDoc;
32 import java.awt.Point JavaDoc;
33 import java.awt.Toolkit JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.KeyEvent JavaDoc;
37 import java.awt.event.WindowAdapter JavaDoc;
38 import java.awt.event.WindowEvent JavaDoc;
39 import java.awt.print.PrinterException JavaDoc;
40 import java.awt.print.PrinterJob JavaDoc;
41
42 import java.text.DecimalFormat JavaDoc;
43
44 import javax.swing.ActionMap JavaDoc;
45 import javax.swing.BorderFactory JavaDoc;
46 import javax.swing.ButtonGroup JavaDoc;
47 import javax.swing.InputMap JavaDoc;
48 import javax.swing.JComboBox JavaDoc;
49 import javax.swing.JComponent JavaDoc;
50 import javax.swing.JFrame JavaDoc;
51 import javax.swing.JLabel JavaDoc;
52 import javax.swing.JMenu JavaDoc;
53 import javax.swing.JMenuBar JavaDoc;
54 import javax.swing.JOptionPane JavaDoc;
55 import javax.swing.JPanel JavaDoc;
56 import javax.swing.JRadioButtonMenuItem JavaDoc;
57 import javax.swing.JToolBar JavaDoc;
58 import javax.swing.KeyStroke JavaDoc;
59 import javax.swing.SwingUtilities JavaDoc;
60 import javax.swing.UIManager JavaDoc;
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 /**
67  * AWT Viewer main window.
68  * Surrounds a PreviewPanel with a bunch of pretty buttons and controls.
69  */

70 public class PreviewDialog extends JFrame JavaDoc implements StatusListener {
71
72     /** The Translator for localization */
73     protected Translator translator;
74     /** The AWT renderer */
75     protected AWTRenderer renderer;
76     /** The FOUserAgent associated with this window */
77     protected FOUserAgent foUserAgent;
78     /**
79      * Renderable instance that can be used to reload and re-render a document after
80      * modifications.
81      */

82     protected Renderable renderable;
83
84     /** The JCombobox to rescale the rendered page view */
85     private JComboBox JavaDoc scale;
86
87     /** The JLabel for the process status bar */
88     private JLabel JavaDoc processStatus;
89
90     /** The JLabel information status bar */
91     private JLabel JavaDoc infoStatus;
92
93     /** The main display area */
94     private PreviewPanel previewPanel;
95
96     /** Formats the text in the scale combobox. */
97     private DecimalFormat JavaDoc percentFormat = new DecimalFormat JavaDoc("###0.0#");
98
99     /**
100      * Creates a new PreviewDialog that uses the given renderer.
101      * @param foUserAgent the user agent
102      * @param renderable the Renderable instance that is used to reload/re-render a document
103      * after modifications.
104      */

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         //Commands aka Actions
112
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             // TODO use Translator
147
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         //Sets size to be 61%x90% of the screen size
161
Dimension JavaDoc screen = Toolkit.getDefaultToolkit().getScreenSize();
162         // Needed due to bug in Sun's JVM 1.5 (6429775)
163
pack();
164         //Rather frivolous size - fits A4 page width in 1024x768 screen on my desktop
165
setSize(screen.width * 61 / 100, screen.height * 9 / 10);
166
167         //Page view stuff
168
previewPanel = new PreviewPanel(foUserAgent, renderable, renderer);
169         getContentPane().add(previewPanel, BorderLayout.CENTER);
170
171         // Keyboard shortcuts - pgup/pgdn
172
InputMap JavaDoc im = previewPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
173         ActionMap JavaDoc 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         //Scaling combobox
184
scale = new JComboBox JavaDoc();
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 JavaDoc(80, 24));
194         scale.setPreferredSize(new Dimension JavaDoc(80, 24));
195         scale.setSelectedItem("100%");
196         scale.setEditable(true);
197         scale.addActionListener(new ActionListener JavaDoc() {
198             public void actionPerformed(ActionEvent JavaDoc e) {
199                 scaleActionPerformed(e);
200             }
201         });
202
203         //Menu
204
setJMenuBar(setupMenu());
205
206         // Toolbar
207
JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
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 JavaDoc(20, 0));
216         toolBar.add(new JLabel JavaDoc(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         // Status bar
225
JPanel JavaDoc statusBar = new JPanel JavaDoc();
226         processStatus = new JLabel JavaDoc();
227         processStatus.setBorder(BorderFactory.createCompoundBorder(
228                 BorderFactory.createEtchedBorder(),
229                 BorderFactory.createEmptyBorder(0, 3, 0, 0)));
230         infoStatus = new JLabel JavaDoc();
231         infoStatus.setBorder(BorderFactory.createCompoundBorder(
232                 BorderFactory.createEtchedBorder(),
233                 BorderFactory.createEmptyBorder(0, 3, 0, 0)));
234
235         statusBar.setLayout(new GridBagLayout JavaDoc());
236
237         processStatus.setPreferredSize(new Dimension JavaDoc(200, 21));
238         processStatus.setMinimumSize(new Dimension JavaDoc(200, 21));
239
240         infoStatus.setPreferredSize(new Dimension JavaDoc(100, 21));
241         infoStatus.setMinimumSize(new Dimension JavaDoc(100, 21));
242         statusBar.add(processStatus,
243                       new GridBagConstraints JavaDoc(0, 0, 1, 0, 2.0, 0.0,
244                                              GridBagConstraints.CENTER,
245                                              GridBagConstraints.HORIZONTAL,
246                                              new Insets JavaDoc(0, 0, 0, 3), 0, 0));
247         statusBar.add(infoStatus,
248                       new GridBagConstraints JavaDoc(1, 0, 1, 0, 1.0, 0.0,
249                                              GridBagConstraints.CENTER,
250                                              GridBagConstraints.HORIZONTAL,
251                                              new Insets JavaDoc(0, 0, 0, 0), 0, 0));
252         getContentPane().add(statusBar, BorderLayout.SOUTH);
253     }
254     
255     /**
256      * Creates and initialize the AWT Viewer main window.
257      * @param foUserAgent the FO user agent
258      * @param renderable the target for the rendering
259      * @return the newly initialized preview dialog
260      */

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 JavaDoc() {
267                 public void windowClosed(WindowEvent JavaDoc we) {
268                     System.exit(0);
269                 }
270             });
271         }
272
273         // Centers the window
274
Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
275         Dimension JavaDoc 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     /**
290      * Creates a new PreviewDialog that uses the given renderer.
291      * @param foUserAgent the user agent
292      */

293     public PreviewDialog(FOUserAgent foUserAgent) {
294         this(foUserAgent, null);
295     }
296     
297     /**
298      * Creates a new menubar to be shown in this window.
299      * @return the newly created menubar
300      */

301     private JMenuBar JavaDoc setupMenu() {
302         JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
303         JMenu JavaDoc menu = new JMenu JavaDoc(translator.getString("Menu.File"));
304         menu.setMnemonic(KeyEvent.VK_F);
305         //Adds mostly the same actions, but without icons
306
menu.add(new Command(translator.getString("Menu.Print"), KeyEvent.VK_P) {
307             public void doit() {
308                 startPrinterJob(true);
309             }
310         });
311         // inputHandler must be set to allow reloading
312
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 JavaDoc(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 JavaDoc subMenu = new JMenu JavaDoc(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 JavaDoc group = new ButtonGroup JavaDoc();
407         JRadioButtonMenuItem JavaDoc single = new JRadioButtonMenuItem JavaDoc(
408                 new Command(translator.getString("Menu.Single"), KeyEvent.VK_S) {
409             public void doit() {
410                 previewPanel.setDisplayMode(PreviewPanel.SINGLE);
411             }
412         });
413         JRadioButtonMenuItem JavaDoc cont = new JRadioButtonMenuItem JavaDoc(
414                 new Command(translator.getString("Menu.Continuous"), KeyEvent.VK_C) {
415             public void doit() {
416                 previewPanel.setDisplayMode(PreviewPanel.CONTINUOUS);
417             }
418         });
419         JRadioButtonMenuItem JavaDoc facing = new JRadioButtonMenuItem JavaDoc(
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 JavaDoc(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     /** @see org.apache.fop.render.awt.viewer.StatusListener#notifyRendererStopped() */
447     public void notifyRendererStopped() {
448         reload();
449     }
450
451     private void reload() {
452         setStatus(translator.getString("Status.Show"));
453         previewPanel.reload();
454     }
455     
456     /**
457      * Changes the current visible page
458      * @param number the page number to go to
459      */

460     public void goToPage(int number) {
461         if (number != previewPanel.getPage()) {
462             previewPanel.setPage(number);
463             notifyPageRendered();
464         }
465     }
466
467     /**
468      * Shows the previous page.
469      */

470     public void goToPreviousPage() {
471         int page = previewPanel.getPage();
472         if (page > 0) {
473             goToPage(page - 1);
474         }
475     }
476
477     /**
478      * Shows the next page.
479      */

480     public void goToNextPage() {
481         int page = previewPanel.getPage();
482         if (page < renderer.getNumberOfPages() - 1) {
483             goToPage(page + 1);
484         }
485     }
486
487     /** Shows the first page. */
488     public void goToFirstPage() {
489         goToPage(0);
490     }
491
492     /**
493      * Shows the last page.
494      */

495     public void goToLastPage() {
496         goToPage(renderer.getNumberOfPages() - 1);
497     }
498
499     /** Shows the About box */
500     private void startHelpAbout() {
501         PreviewDialogAboutBox dlg = new PreviewDialogAboutBox(this, translator);
502         //Centers the box
503
Dimension JavaDoc dlgSize = dlg.getPreferredSize();
504         Dimension JavaDoc frmSize = getSize();
505         Point JavaDoc 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     /**
512      * Shows "go to page" dialog and then goes to the selected page
513      */

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     /** Scales page image */
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 JavaDoc 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 JavaDoc item = (String JavaDoc)scale.getSelectedItem();
560                 setScale(Double.parseDouble(item.substring(0, item.indexOf('%'))));
561             }
562         } catch (FOPException fopEx) {
563             fopEx.printStackTrace();
564         }
565     }
566
567     /** Prints the document */
568     public void startPrinterJob(boolean showDialog) {
569         PrinterJob JavaDoc pj = PrinterJob.getPrinterJob();
570         pj.setPageable(renderer);
571         if (!showDialog || pj.printDialog()) {
572             try {
573                 pj.print();
574             } catch (PrinterException JavaDoc e) {
575                 e.printStackTrace();
576             }
577         }
578     }
579
580     /**
581      * Sets message to be shown in the status bar in a thread safe way.
582      * @param message the message
583      */

584     public void setStatus(String JavaDoc message) {
585         SwingUtilities.invokeLater(new ShowStatus(message));
586     }
587
588     /** This class is used to show status in a thread safe way. */
589     private class ShowStatus implements Runnable JavaDoc {
590
591         /** The message to display */
592         private String JavaDoc message;
593
594         /**
595          * Constructs ShowStatus thread
596          * @param message message to display
597          */

598         public ShowStatus(String JavaDoc message) {
599             this.message = message;
600         }
601
602         public void run() {
603             processStatus.setText(message.toString());
604         }
605     }
606
607     /**
608      * Updates the message to be shown in the info bar in a thread safe way.
609      */

610     public void notifyPageRendered() {
611         SwingUtilities.invokeLater(new ShowInfo());
612     }
613
614     /** This class is used to show info in a thread safe way. */
615     private class ShowInfo implements Runnable JavaDoc {
616
617         public void run() {
618             String JavaDoc 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     /**
627      * Opens standard Swing error dialog box and reports given exception details.
628      * @param e the Exception
629      */

630     public void reportException(Exception JavaDoc e) {
631         String JavaDoc 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