KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > DrJavaRoot


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava;
35
36 import java.io.*;
37 import java.net.URL JavaDoc;
38 import java.net.URLClassLoader JavaDoc;
39 import java.net.MalformedURLException JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42 import java.util.jar.JarFile JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import javax.swing.*;
45
46 import edu.rice.cs.util.FileOpenSelector;
47 import edu.rice.cs.util.UnexpectedException;
48 import edu.rice.cs.util.OutputStreamRedirector;
49 import edu.rice.cs.util.newjvm.ExecJVM;
50 import edu.rice.cs.util.classloader.ToolsJarClassLoader;
51 import edu.rice.cs.util.swing.Utilities;
52
53 import edu.rice.cs.drjava.ui.MainFrame;
54 import edu.rice.cs.drjava.ui.SplashScreen;
55 import edu.rice.cs.drjava.ui.ClassPathFilter;
56 import edu.rice.cs.drjava.ui.DrJavaErrorWindow;
57 import edu.rice.cs.drjava.ui.DrJavaErrorHandler;
58 import edu.rice.cs.drjava.ui.SimpleInteractionsWindow;
59 import edu.rice.cs.drjava.model.*;
60 import edu.rice.cs.drjava.model.compiler.*;
61 import edu.rice.cs.drjava.platform.PlatformFactory;
62 import edu.rice.cs.drjava.config.FileConfiguration;
63 import edu.rice.cs.drjava.config.*;
64
65 import static edu.rice.cs.drjava.config.OptionConstants.*;
66
67 /** Main class for DrJava.
68  * @version $Id: DrJavaRoot.java 4085 2007-01-24 06:27:10Z c45207 $
69  */

70 public class DrJavaRoot {
71   
72   /* Constants for language levels */
73   public static final int FULL_JAVA = 0;
74   public static final int ELEMENTARY_LEVEL = 1;
75   public static final int INTERMEDIATE_LEVEL = 2;
76   public static final int ADVANCED_LEVEL = 3;
77   public static final String JavaDoc[] LANGUAGE_LEVEL_EXTENSIONS = new String JavaDoc[] {"java", "dj0", "dj1", "dj2"};
78   
79   /** Class to probe to see if the debugger is available */
80   public static final String JavaDoc TEST_DEBUGGER_CLASS = "com.sun.jdi.Bootstrap";
81
82   private static final PrintStream _consoleOut = System.out;
83   private static final PrintStream _consoleErr = System.err;
84   
85 // /** This field is only used in the instance of this class in the Interpreter JVM. */
86
// private static PreventExitSecurityManager _manager = null;
87

88   private static boolean _attemptingAugmentedClassPath = false;
89   private static SimpleInteractionsWindow _debugConsole = null;
90   
91   /* Config objects can't be public static final, since we have to delay construction until we know the
92    * config file's location. (Might be specified on command line.) Instead, use accessor methods to
93    * prevent others from assigning new values. */

94   
95   public static void main(final String JavaDoc[] args) {
96     // Platform-specific UI setup.
97
PlatformFactory.ONLY.beforeUISetup();
98
99 // Utilities.show("DrJavaRoot started with args = " + Arrays.toString(args));
100
// let DrJava class handle command line arguments
101
if (!DrJava.handleCommandLineArgs(args)) {
102       System.exit(0);
103     }
104
105     String JavaDoc[] filesToOpen = DrJava.getFilesToOpen();
106     final int numFiles = filesToOpen.length;
107       
108     /* files to open held in filesToOpen[0:numFiles-1] which may be an initial segment of filesToOpen */
109     
110     /* In some unit test cases, creating a MainFrame in the main thread generated index out of bounds exceptions. It appear that this
111      * creation process generates some swing events that are processed by the event thread. Hence we need to create the MainFrame in
112      * the event thread.
113      */

114 // Utilities.invokeAndWait(new Runnable() {
115
// public void run() {
116
try {
117           String JavaDoc configLAFName = DrJava.getConfig().getSetting(LOOK_AND_FEEL);
118           String JavaDoc currLAFName = UIManager.getLookAndFeel().getClass().getName();
119           if (! configLAFName.equals(currLAFName)) UIManager.setLookAndFeel(configLAFName);
120           
121           // The MainFrame *must* be constructed after the compiler setup process has
122
// occurred; otherwise, the list of compilers in the UI will be wrong.
123

124 // Utilities.showDebug("Creating MainFrame");
125

126           final MainFrame mf = new MainFrame();
127           
128 // Utilities.showDebug("MainFrame created");
129

130           // Make sure all uncaught exceptions are shown in an DrJavaErrorHandler
131
DrJavaErrorWindow.setFrame(mf);
132           System.setProperty("sun.awt.exception.handler", "edu.rice.cs.drjava.ui.DrJavaErrorHandler");
133           
134           _openCommandLineFiles(mf, filesToOpen, numFiles);
135           
136           /* This call on invokeLater only runs in the main thread, so we use SwingUtilities rather than Utilities.
137            * We use invokeLater here ensure all files have finished loading and added to the fileview before the MainFrame
138            * is set visible. When this was not done, we occasionally encountered a NullPointerException on start up when
139            * specifying a file (ex: java -jar drjava.jar somefile.java)
140            */

141           SwingUtilities.invokeLater(new Runnable JavaDoc(){ public void run(){ mf.setVisible(true); } });
142           
143           // redirect stdout to DrJava's console
144
System.setOut(new PrintStream(new OutputStreamRedirector() {
145             public void print(String JavaDoc s) { mf.getModel().systemOutPrint(s); }
146           }));
147           
148           // redirect stderr to DrJava's console
149
System.setErr(new PrintStream(new OutputStreamRedirector() {
150             public void print(String JavaDoc s) { mf.getModel().systemErrPrint(s); }
151           }));
152           
153 // Utilities.showDebug("showDebugConsole flag = " + DrJava.getShowDebugConsole());
154
// Show debug console if enabled
155
if (DrJava.getShowDebugConsole()) showDrJavaDebugConsole(mf);
156         }
157         catch (Throwable JavaDoc t) {
158           // Show any errors to the real System.err and in an DrJavaErrorHandler
159
_consoleErr.println(t.getClass().getName() + ": " + t.getMessage());
160           t.printStackTrace(_consoleErr);
161           System.out.println("error thrown");
162           new DrJavaErrorHandler().handle(t);
163         }
164 // }
165
// });
166
}
167
168   /** Handle the list of files specified on the command line. Feature request #509701.
169    * If file exists, open it in DrJava. Otherwise, ignore it.
170    * Is there a better way to handle nonexistent files? Dialog box, maybe?
171    */

172   static void openCommandLineFiles(final MainFrame mf, final String JavaDoc[] filesToOpen) {
173     openCommandLineFiles(mf, filesToOpen, filesToOpen.length);
174   }
175   
176   /** Handle the list of files specified on the command line. Feature request #509701. If the final element in
177     * filesToOpen is a pathSeparator, it opens the debug console. If file exists, open it in DrJava. Otherwise, ignore
178     * it. Is there a better way to handle nonexistent files? Dialog box, maybe?
179     * Why the wait?
180     */

181   static void openCommandLineFiles(final MainFrame mf, final String JavaDoc[] filesToOpen, final int len) {
182     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _openCommandLineFiles(mf, filesToOpen, len); }});
183   }
184       
185   private static void _openCommandLineFiles(MainFrame mf, String JavaDoc[] filesToOpen, int len) {
186 // Utilities.showDebug("Files to open: " + Arrays.toString(filesToOpen));
187
for (int i = 0; i < len; i++) {
188       String JavaDoc currFileName = filesToOpen[i];
189       boolean isProjectFile = currFileName.endsWith(".pjt");
190       final File JavaDoc file = new File JavaDoc(currFileName).getAbsoluteFile();
191       FileOpenSelector command = new FileOpenSelector() {
192         public File JavaDoc[] getFiles() { return new File JavaDoc[] {file}; }
193       };
194       try {
195         if (isProjectFile) mf.openProject(command);
196         else mf.getModel().openFile(command);
197       }
198
199       catch (FileNotFoundException ex) {
200         // TODO: show a dialog? (file not found)
201
}
202       catch (SecurityException JavaDoc se) {
203         // TODO: show a dialog? (file not found)
204
}
205       catch (AlreadyOpenException aoe) {
206         // This explicitly does nothing to ignore duplicate files.
207
}
208       catch (FileMovedException aoe) {
209         // This explicitly does nothing to ignore duplicate files.
210
}
211       catch (IOException ex) {
212         // TODO: show a dialog? (file not found)
213
}
214       catch (Exception JavaDoc ex) { throw new UnexpectedException(ex); }
215     }
216   }
217
218   /** Shows a separate interactions window with a reference to DrJava's MainFrame defined as "mainFrame".
219    * Useful for debugging DrJava.
220    * @param mf MainFrame to define in the new window
221    */

222   public static void showDrJavaDebugConsole(MainFrame mf) {
223     if (_debugConsole == null) {
224       _debugConsole = new SimpleInteractionsWindow("DrJava Debug Console") {
225         protected void close() {
226           dispose();
227           _debugConsole = null;
228         }
229       };
230       _debugConsole.defineConstant("mainFrame", mf);
231       _debugConsole.defineConstant("model", mf.getModel());
232       _debugConsole.defineConstant("config", DrJava.getConfig());
233       _debugConsole.setInterpreterPrivateAccessible(true);
234       _debugConsole.setVisible(true);
235     }
236     else _debugConsole.toFront();
237   }
238
239   /** Get the actual System.err stream.
240    * @return System.err
241    */

242   public static PrintStream consoleErr() { return _consoleErr; }
243
244   /** Get the actual System.out stream.
245     * @return System.out
246    */

247   public static PrintStream consoleOut() { return _consoleOut; }
248   
249 }
250
251
Popular Tags