KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > runner > Application


1 /*
2  * $Id: Application.java,v 1.3 2004/07/28 00:51:45 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.runner;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Image JavaDoc;
13 import java.awt.Window JavaDoc;
14
15 import java.beans.Expression JavaDoc;
16
17 import java.net.URL JavaDoc;
18
19 import java.util.logging.Level JavaDoc;
20 import java.util.logging.Logger JavaDoc;
21
22 import javax.swing.JApplet JavaDoc;
23 import javax.swing.JComponent JavaDoc;
24 import javax.swing.JFrame JavaDoc;
25 import javax.swing.JOptionPane JavaDoc;
26 import javax.swing.JRootPane JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29
30 import net.openmarkup.ObjectRealizer;
31 import net.openmarkup.Scribe;
32 import net.openmarkup.Vocabulary;
33
34 // XXX This class should be renamed to JDNCLoader or something
35

36 /**
37  * <p>
38  * Application class which instantiates its user interface based on the
39  * XML configuration file specified as the command line parameter.
40  * <p>
41  * The configuration file must adhere to the JDNC schema:<br>
42  * TBD
43  * </p>
44  *
45  * @author Amy Fowler
46  * @author Ramesh Gupta
47  */

48 public class Application {
49
50     private static ObjectRealizer realizer;
51
52     private static Vocabulary vocabulary;
53
54     /**
55      * Constructs the application to use the object realizer
56      * vocabulary.
57      */

58     public Application(Vocabulary vocabulary) {
59     this.vocabulary = vocabulary;
60     }
61
62     /**
63      * Creates a JDNC application from the url. The logging level is
64      * set at the default - which is Level.WARNING.
65      *
66      * @param url a URL which represents a jdnc application
67      */

68     public Application(URL JavaDoc url) {
69     this(url, null);
70     }
71
72     /**
73      * Creates a JDNC application from the url.
74      * <p>
75      * The level parameter must correspond to a string value
76      * of <code>java.util.logging.Level</code> constants.
77      * May be upper or lower case. If null then the level
78      * will be set to Level.WARNING.
79      *
80      * @param url a URL which represents a jdnc application
81      * @param level the default level to set the logger
82      */

83     public Application(URL JavaDoc url, Level JavaDoc level) {
84     setLogLevel(level);
85     try {
86         initUI(realizeObject(url));
87     } catch (Exception JavaDoc ex) {
88         Scribe.getLogger().log(Level.SEVERE, "Exception loading: " +
89                    url, ex);
90     }
91     }
92
93     /**
94      * Creates a JDNC application from the url. The logging level is
95      * set at the default - which is Level.WARNING
96      */

97     public Application(String JavaDoc configuration) {
98     this(configuration, null);
99     }
100
101     /**
102      * Main constructor to use for a stand alone application.
103      * <p>
104      * The level parameter must correspond to a string value
105      * of <code>java.util.logging.Level</code> constants.
106      * May be upper or lower case. If null then the level
107      * will be set to Level.WARNING.
108      *
109      * @param configuration A JDNC configuration file.
110      * @param level a value to set the default logging level or null
111      * @see java.util.logging.Level
112      */

113     public Application(String JavaDoc configuration, String JavaDoc level) {
114     setLogLevel(level);
115     try {
116         initUI(realizeObject(configuration));
117     } catch (Exception JavaDoc ex) {
118         Scribe.getLogger().log(Level.SEVERE, "Exception loading: " +
119                    configuration, ex);
120     }
121     }
122
123     private void initUI(final JComponent JavaDoc ui) {
124         SwingUtilities.invokeLater(new Runnable JavaDoc() {
125             public void run() {
126                 // App temporarily stashed in client properties if created already
127
org.jdesktop.swing.Application app = getApplication(ui);
128                 JDNCFrame frame = new JDNCFrame(app);
129         if (ui instanceof JRootPane JavaDoc) {
130                     frame.replaceRootPane((JRootPane JavaDoc)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 JavaDoc ui) {
141     if (ui == null) {
142         return null;
143     }
144         // App temporarily stashed in clientproperties if created already
145
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 JavaDoc realizeObject(URL JavaDoc config) throws Exception JavaDoc {
153     Scribe.getLogger().info("Loading: " + config);
154     ObjectRealizer realizer = Application.getObjectRealizer();
155     Object JavaDoc object = null;
156     if (config != null) {
157         object = realizer.getObject(config);
158     }
159     return validateObject(object, config.toExternalForm());
160     }
161
162     static JComponent JavaDoc realizeObject(String JavaDoc configuration) throws Exception JavaDoc {
163     Scribe.getLogger().info("Loading: " + configuration);
164     ObjectRealizer realizer = Application.getObjectRealizer();
165     Object JavaDoc object = null;
166     if (configuration != null) {
167         object = realizer.getObject(configuration);
168     }
169     return validateObject(object, configuration);
170     }
171
172     // Set the level of the logger.
173
static void setLogLevel(Level JavaDoc level) {
174     Scribe.getLogger().setLevel(level == null ? Level.WARNING : level);
175     }
176
177     static void setLogLevel(String JavaDoc level) {
178     Level JavaDoc lvl = null;
179     if (level != null) {
180         try {
181         lvl = Level.parse(level.toUpperCase());
182         } catch (Exception JavaDoc ex) {
183         // level is invalid, ignore
184
}
185     }
186     setLogLevel(lvl);
187     }
188
189     /**
190      * Checks the Object to ensure that it is not null and it must be
191      * a JComponent.
192      */

193     static JComponent JavaDoc validateObject(Object JavaDoc object, String JavaDoc configuration) {
194         if (object == null || ! (object instanceof JComponent JavaDoc)) {
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 JavaDoc)object;
202     }
203
204
205     /**
206      * This method looks up the ObjectRealizer implementation from the
207      * system property openmarkup.realizer.impl.
208      * <p>
209      * Also, there should be a similar mechanism in which to specify the
210      * vocabulary for the object realizer.
211      * <p>
212      * This method should be consolidated with markup.RealizationUtils.getObjectRealizer
213      */

214     public static ObjectRealizer getObjectRealizer() {
215     if (realizer == null) {
216         // The OR implementation should be looked up in System.properties.
217
String JavaDoc 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 JavaDoc ex) {
224         // Access to properties are not allowed. For example, in sandboxed
225
// applications.
226
realizerImplName = "org.jdesktop.openmarkup.ri.ObjectRealizerImpl";
227         }
228         
229         Object JavaDoc or = null;
230         try {
231         Class JavaDoc clz = Class.forName(realizerImplName);
232         or = clz.newInstance();
233         
234         // First use getInstance (if it exists) to get the actual
235
// object realizer instance.
236
realizer = (ObjectRealizer)(new Expression JavaDoc(or, "getInstance",
237                                new Object JavaDoc[0])).getValue();
238         } catch (Exception JavaDoc ex) {
239         // getInstance doesn't exist.
240
if (or != null && or instanceof ObjectRealizer) {
241             realizer = (ObjectRealizer)or;
242         }
243         else {
244             throw new RuntimeException JavaDoc("Cannot find ObjectRealizer: " +
245                            realizerImplName, ex);
246         }
247         }
248
249         // XXX - this is also a potential circular dependency
250
if (vocabulary == null) {
251         try {
252             Class JavaDoc cls = Class.forName("org.jdesktop.jdnc.markup.ElementTypes");
253             Vocabulary vb = (Vocabulary)cls.newInstance();
254             vocabulary = (Vocabulary)(new Expression JavaDoc(vb,
255                      "get", new Object JavaDoc[0])).getValue();
256         } catch (Exception JavaDoc ex) {
257             throw new RuntimeException JavaDoc("Error no suitable Vocabulary", ex);
258
259         }
260         }
261         realizer.add(vocabulary);
262     }
263     return realizer;
264     }
265
266     /**
267      * XXX this should be replaced by org.jdesktop.swing.JXFrame
268      */

269     private class JDNCFrame extends JFrame JavaDoc {
270
271     public JDNCFrame(org.jdesktop.swing.Application app) {
272         super(app.getTitle());
273         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
274         Image JavaDoc iconImage = app.getTitleBarImage();
275         if (iconImage != null) {
276         setIconImage(iconImage);
277         }
278         app.registerWindow(this);
279     }
280
281         public void replaceRootPane(JRootPane JavaDoc rootPane) {
282             // setRootPane is protected on JFrame
283
setRootPane(rootPane);
284         }
285     }
286
287     public static void main(String JavaDoc[] 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 JavaDoc 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