1 51 package org.apache.fop.apps; 52 59 import org.apache.fop.viewer.*; 60 import org.apache.fop.render.awt.*; 61 import java.awt.event.WindowAdapter ; 62 import java.awt.event.WindowEvent ; 63 64 65 import javax.swing.UIManager ; 66 import java.awt.*; 67 68 import org.xml.sax.XMLReader ; 70 71 import org.apache.avalon.framework.logger.ConsoleLogger; 73 74 import java.io.InputStream ; 76 import java.net.URL ; 77 78 81 public class AWTStarter extends CommandLineStarter { 82 83 PreviewDialog frame; 84 AWTRenderer renderer; 85 protected Driver driver; 86 protected XMLReader parser; 87 public static final String 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 ()); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 } 104 105 String language = commandLineOptions.getLanguage(); 106 107 if (language == null) { 108 try { 109 language = System.getProperty("user.language"); 110 } catch(SecurityException se) { 111 } 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 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 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 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 () { 166 public void windowClosed(WindowEvent we) { 167 System.exit(0); 168 } 169 }); 170 171 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 path) { 187 InputStream in = null; 188 189 try { 190 URL url = getClass().getResource(path); 191 192 194 if (url == null) { 195 path = path.substring(0, path.lastIndexOf(".")) + ".en"; 197 url = getClass().getResource(path); 198 } 199 200 in = url.openStream(); 201 } catch (Exception 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 |