KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > apps > AWTStarter


1 /*
2  * $Id: AWTStarter.java,v 1.9.2.6 2003/02/25 10:18:31 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.apps;
52 /*
53  * originally contributed by
54  * Juergen Verwohlt: Juergen.Verwohlt@jCatalog.com,
55  * Rainer Steinkuhle: Rainer.Steinkuhle@jCatalog.com,
56  * Stanislav Gorkhover: Stanislav.Gorkhover@jCatalog.com
57  * Modified to use streaming API by Mark Lillywhite, mark-fop@inomial.com
58  */

59 import org.apache.fop.viewer.*;
60 import org.apache.fop.render.awt.*;
61 import java.awt.event.WindowAdapter JavaDoc;
62 import java.awt.event.WindowEvent JavaDoc;
63
64
65 import javax.swing.UIManager JavaDoc;
66 import java.awt.*;
67
68 // SAX
69
import org.xml.sax.XMLReader JavaDoc;
70
71 // Avalon
72
import org.apache.avalon.framework.logger.ConsoleLogger;
73
74 // Java
75
import java.io.InputStream JavaDoc;
76 import java.net.URL JavaDoc;
77
78 /**
79  * initialize AWT previewer
80  */

81 public class AWTStarter extends CommandLineStarter {
82
83     PreviewDialog frame;
84     AWTRenderer renderer;
85     protected Driver driver;
86     protected XMLReader JavaDoc parser;
87     public static final String JavaDoc TRANSLATION_PATH =
88         "/org/apache/fop/viewer/resources/";
89
90     private Translator resource;
91
92     public AWTStarter(CommandLineOptions commandLineOptions)
93     throws FOPException {
94         super(commandLineOptions);
95         init();
96     }
97
98     private void init() throws FOPException {
99         try {
100             UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel JavaDoc());
101         } catch (Exception JavaDoc e) {
102             e.printStackTrace();
103         }
104
105         String JavaDoc language = commandLineOptions.getLanguage();
106
107         if (language == null) {
108             try {
109                 language = System.getProperty("user.language");
110             } catch(SecurityException JavaDoc se) {
111                 // if this is running in a secure place
112
}
113         }
114         resource = getResourceBundle(TRANSLATION_PATH + "resources."
115                                      + language);
116
117         UserMessage.setTranslator(getResourceBundle(TRANSLATION_PATH
118                                   + "messages."
119                                   + language));
120
121         resource.setMissingEmphasized(false);
122         renderer = new AWTRenderer(resource);
123         frame = createPreviewDialog(renderer, resource);
124         renderer.setProgressListener(frame);
125         renderer.setComponent(frame);
126         driver = new Driver();
127         driver.setLogger(new ConsoleLogger(ConsoleLogger.LEVEL_INFO));
128         if (errorDump) {
129             driver.setErrorDump(true);
130         }
131         driver.setRenderer(renderer);
132         // init parser
133
frame.progress(resource.getString("Init parser") + " ...");
134         parser = inputHandler.getParser();
135         if (parser == null) {
136             throw new FOPException("Unable to create SAX parser");
137         }
138     }
139
140
141     public void run() throws FOPException {
142         driver.reset();
143         try {
144             // build FO tree: time
145
frame.progress(resource.getString("Build FO tree") + " ...");
146             driver.render(parser, inputHandler.getInputSource());
147
148             frame.progress(resource.getString("Show"));
149             frame.showPage();
150
151         } catch (Exception JavaDoc e) {
152             frame.reportException(e);
153             if (e instanceof FOPException) {
154                 throw (FOPException)e;
155             }
156             throw new FOPException(e);
157         }
158
159     }
160
161     protected PreviewDialog createPreviewDialog(AWTRenderer renderer,
162             Translator res) {
163         PreviewDialog frame = new PreviewDialog(this, renderer, res);
164         frame.validate();
165         frame.addWindowListener(new WindowAdapter JavaDoc() {
166                 public void windowClosed(WindowEvent JavaDoc we) {
167                     System.exit(0);
168                 }
169             });
170
171         // center window
172
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
173         Dimension frameSize = frame.getSize();
174         if (frameSize.height > screenSize.height)
175             frameSize.height = screenSize.height;
176         if (frameSize.width > screenSize.width)
177             frameSize.width = screenSize.width;
178         frame.setLocation((screenSize.width - frameSize.width) / 2,
179                           (screenSize.height - frameSize.height) / 2);
180         frame.setVisible(true);
181         return frame;
182     }
183
184
185
186     private SecureResourceBundle getResourceBundle(String JavaDoc path) {
187         InputStream JavaDoc in = null;
188
189         try {
190             URL JavaDoc url = getClass().getResource(path);
191
192             /* The following code was added by Alex Alishevskikh [alex@openmechanics.net]
193                to fix for crashes on machines with unsupported user languages */

194         if (url == null) {
195                 // if the given resource file not found, the english resource uses as default
196
path = path.substring(0, path.lastIndexOf(".")) + ".en";
197                 url = getClass().getResource(path);
198         }
199
200             in = url.openStream();
201         } catch (Exception JavaDoc ex) {
202             log.error("Can't find URL to: <" + path + "> "
203                                  + ex.getMessage(), ex);
204         }
205         return new SecureResourceBundle(in);
206     }
207
208 }
209
210
211
212
Popular Tags