1 7 8 package org.jdesktop.jdnc.runner; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Component ; 12 import java.awt.Image ; 13 import java.awt.Window ; 14 15 import java.beans.Expression ; 16 17 import java.net.URL ; 18 19 import java.util.logging.Level ; 20 import java.util.logging.Logger ; 21 22 import javax.swing.JApplet ; 23 import javax.swing.JComponent ; 24 import javax.swing.JFrame ; 25 import javax.swing.JOptionPane ; 26 import javax.swing.JRootPane ; 27 import javax.swing.SwingUtilities ; 28 import javax.swing.UIManager ; 29 30 import net.openmarkup.ObjectRealizer; 31 import net.openmarkup.Scribe; 32 import net.openmarkup.Vocabulary; 33 34 36 48 public class Application { 49 50 private static ObjectRealizer realizer; 51 52 private static Vocabulary vocabulary; 53 54 58 public Application(Vocabulary vocabulary) { 59 this.vocabulary = vocabulary; 60 } 61 62 68 public Application(URL url) { 69 this(url, null); 70 } 71 72 83 public Application(URL url, Level level) { 84 setLogLevel(level); 85 try { 86 initUI(realizeObject(url)); 87 } catch (Exception ex) { 88 Scribe.getLogger().log(Level.SEVERE, "Exception loading: " + 89 url, ex); 90 } 91 } 92 93 97 public Application(String configuration) { 98 this(configuration, null); 99 } 100 101 113 public Application(String configuration, String level) { 114 setLogLevel(level); 115 try { 116 initUI(realizeObject(configuration)); 117 } catch (Exception ex) { 118 Scribe.getLogger().log(Level.SEVERE, "Exception loading: " + 119 configuration, ex); 120 } 121 } 122 123 private void initUI(final JComponent ui) { 124 SwingUtilities.invokeLater(new Runnable () { 125 public void run() { 126 org.jdesktop.swing.Application app = getApplication(ui); 128 JDNCFrame frame = new JDNCFrame(app); 129 if (ui instanceof JRootPane ) { 130 frame.replaceRootPane((JRootPane )ui); 131 } else { 132 frame.getContentPane().add(BorderLayout.CENTER, ui); 133 } 134 frame.pack(); 135 frame.setVisible(true); 136 } 137 }); 138 } 139 140 static org.jdesktop.swing.Application getApplication(JComponent ui) { 141 if (ui == null) { 142 return null; 143 } 144 org.jdesktop.swing.Application app = (org.jdesktop.swing.Application) ui.getClientProperty("Application"); 146 if (app == null) { 147 app = org.jdesktop.swing.Application.getInstance(); 148 } 149 return app; 150 } 151 152 static JComponent realizeObject(URL config) throws Exception { 153 Scribe.getLogger().info("Loading: " + config); 154 ObjectRealizer realizer = Application.getObjectRealizer(); 155 Object object = null; 156 if (config != null) { 157 object = realizer.getObject(config); 158 } 159 return validateObject(object, config.toExternalForm()); 160 } 161 162 static JComponent realizeObject(String configuration) throws Exception { 163 Scribe.getLogger().info("Loading: " + configuration); 164 ObjectRealizer realizer = Application.getObjectRealizer(); 165 Object object = null; 166 if (configuration != null) { 167 object = realizer.getObject(configuration); 168 } 169 return validateObject(object, configuration); 170 } 171 172 static void setLogLevel(Level level) { 174 Scribe.getLogger().setLevel(level == null ? Level.WARNING : level); 175 } 176 177 static void setLogLevel(String level) { 178 Level lvl = null; 179 if (level != null) { 180 try { 181 lvl = Level.parse(level.toUpperCase()); 182 } catch (Exception ex) { 183 } 185 } 186 setLogLevel(lvl); 187 } 188 189 193 static JComponent validateObject(Object object, String configuration) { 194 if (object == null || ! (object instanceof JComponent )) { 195 Scribe.getLogger().severe("Unable to realize the user interface\n" + 196 " from configuration URL:\n" + 197 configuration + "\n" + 198 "Please verify configuration file\n" + 199 "adheres to JDNC schema."); 200 } 201 return (JComponent )object; 202 } 203 204 205 214 public static ObjectRealizer getObjectRealizer() { 215 if (realizer == null) { 216 String realizerImplName = null; 218 try { 219 realizerImplName = System.getProperty("openmarkup.realizer.impl"); 220 if (realizerImplName == null) { 221 realizerImplName = "org.jdesktop.openmarkup.ri.ObjectRealizerImpl"; 222 } 223 } catch (SecurityException ex) { 224 realizerImplName = "org.jdesktop.openmarkup.ri.ObjectRealizerImpl"; 227 } 228 229 Object or = null; 230 try { 231 Class clz = Class.forName(realizerImplName); 232 or = clz.newInstance(); 233 234 realizer = (ObjectRealizer)(new Expression (or, "getInstance", 237 new Object [0])).getValue(); 238 } catch (Exception ex) { 239 if (or != null && or instanceof ObjectRealizer) { 241 realizer = (ObjectRealizer)or; 242 } 243 else { 244 throw new RuntimeException ("Cannot find ObjectRealizer: " + 245 realizerImplName, ex); 246 } 247 } 248 249 if (vocabulary == null) { 251 try { 252 Class cls = Class.forName("org.jdesktop.jdnc.markup.ElementTypes"); 253 Vocabulary vb = (Vocabulary)cls.newInstance(); 254 vocabulary = (Vocabulary)(new Expression (vb, 255 "get", new Object [0])).getValue(); 256 } catch (Exception ex) { 257 throw new RuntimeException ("Error no suitable Vocabulary", ex); 258 259 } 260 } 261 realizer.add(vocabulary); 262 } 263 return realizer; 264 } 265 266 269 private class JDNCFrame extends JFrame { 270 271 public JDNCFrame(org.jdesktop.swing.Application app) { 272 super(app.getTitle()); 273 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 274 Image iconImage = app.getTitleBarImage(); 275 if (iconImage != null) { 276 setIconImage(iconImage); 277 } 278 app.registerWindow(this); 279 } 280 281 public void replaceRootPane(JRootPane rootPane) { 282 setRootPane(rootPane); 284 } 285 } 286 287 public static void main(String [] args) { 288 if (args.length <= 0) { 289 System.err.println( 290 "XML configuration URL must be specified on command line"); 291 System.exit(-1); 292 } 293 try { 294 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 295 } 296 catch(Exception e) { 297 e.printStackTrace(); 298 } 299 300 Application application; 301 if (args.length == 2) { 302 application = new Application(args[0], args[1]); 303 } else { 304 application = new Application(args[0]); 305 } 306 } 307 } 308 | Popular Tags |