KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > platform > DefaultPlatform


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.platform;
35
36 import edu.rice.cs.drjava.DrJava;
37 import edu.rice.cs.drjava.config.Configuration;
38 import edu.rice.cs.drjava.config.FileOption;
39 import edu.rice.cs.drjava.config.OptionConstants;
40 import edu.rice.cs.util.ArgumentTokenizer;
41 import edu.rice.cs.util.StringOps;
42
43 import javax.swing.*;
44 import java.io.File JavaDoc;
45 import java.lang.reflect.Method JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.util.List JavaDoc;
48
49 /** Default platform-neutral implementation of PlatformSupport. Most implementations
50   * will extend this class to inherit default behaviors.
51   * @version $Id: DefaultPlatform.java 4011 2006-09-22 15:31:42Z rcartwright $
52   */

53 class DefaultPlatform implements PlatformSupport {
54   /** Singleton instance. */
55   public static DefaultPlatform ONLY = new DefaultPlatform();
56
57   /** Private constructor for singleton pattern. */
58   protected DefaultPlatform() { }
59
60   /** Utility method to determine if the current Swing look and feel is the platform-specific look and feel for the
61     * client platform.
62     * @return true if current Swing look and feel is the system look and feel
63     */

64   public boolean isUsingSystemLAF() {
65     String JavaDoc sysLAF = UIManager.getSystemLookAndFeelClassName();
66     String JavaDoc curLAF = UIManager.getLookAndFeel().getClass().getName();
67     return (sysLAF.equals(curLAF));
68   }
69   
70   /** Hook for performing general UI setup. Called before all other UI setup is done. The default implementation
71     * does nothing.
72     */

73   public void beforeUISetup() { }
74
75   /** Hook for performing general UI setup. Called after all other UI setup is done. The default implementation
76     * does nothing.
77     *
78     * @param about the Action associated with openning the About dialog
79     * @param prefs the Action associated with openning the Preferences dialog
80     * @param quit the Action associated with quitting the DrJava application
81     */

82   public void afterUISetup(Action about, Action prefs, Action quit) { }
83
84   /** Returns whether this is a Mac OS X platform. */
85   public boolean isMacPlatform() { return false; }
86
87   /** Returns whether this is a Windows platform. */
88   public boolean isWindowsPlatform() { return false; }
89
90   /** Returns the current Java specification version. */
91   public String JavaDoc getJavaSpecVersion() {
92     return System.getProperty("java.specification.version");
93   }
94
95   /** Returns true if the classpath's tools.jar is from version 1.3. */
96   public boolean has13ToolsJar() {
97     // Javadoc's Main class should not have an execute(String[]) method.
98
try {
99       Class JavaDoc<?> main = Class.forName("com.sun.tools.javadoc.Main");
100       return !_javadocMainHasExecuteMethod(main);
101     }
102     catch (Throwable JavaDoc t) { return false; }
103   }
104
105   /** Returns true if the classpath's tools.jar is from version 1.4. */
106   public boolean has14ToolsJar() {
107     // Javadoc's Main class should have an execute(String[]) method.
108
try {
109       Class JavaDoc<?> main = Class.forName("com.sun.tools.javadoc.Main");
110       return _javadocMainHasExecuteMethod(main);
111     }
112     catch (Throwable JavaDoc t) { return false; }
113   }
114
115   /** Returns true if the given class object for com.sun.tools.javadoc.Main
116    * has an execute(String[]) method. If so, that means we have a 1.4
117    * version of tools.jar.
118    *
119    * @param main Class object for com.sun.tools.javadoc.Main
120    */

121   private boolean _javadocMainHasExecuteMethod(Class JavaDoc main) {
122     try {
123       @SuppressWarnings JavaDoc("unchecked") Class JavaDoc<String JavaDoc[]>[] arr = new Class JavaDoc[]{String JavaDoc[].class};
124       main.getMethod("execute", arr);
125       return true;
126     }
127     catch (Throwable JavaDoc t) { return false; }
128   }
129
130   /** Utility method for opening a URL in a browser in a platform-specific way.
131     * The default implementation uses Runtime.exec to execute a command specified
132     * in Preferences. Platform implementations should attempt the default method
133     * first, then try to use a "default browser", if such a thing exists on the
134     * specific platform.
135     *
136     * @param address the URL to open
137     * @return true if the URL was successfully handled, false otherwise
138     */

139   public boolean openURL(URL JavaDoc address) {
140     // Get the two config options.
141
Configuration config = DrJava.getConfig();
142     File JavaDoc exe = config.getSetting(OptionConstants.BROWSER_FILE);
143     String JavaDoc command = config.getSetting(OptionConstants.BROWSER_STRING);
144
145     // Check for empty settings.
146
if ((exe == FileOption.NULL_FILE) && (command.equals(""))) {
147       // If the user hasn't specified anything, don't try to run it.
148
return false;
149     }
150     else {
151       String JavaDoc addr = address.toString();
152       if (command.equals("")) {
153         // If there is no command, simply use the URL.
154
command = addr;
155       }
156       else {
157         // Otherwise, replace any <URL> tags in the command with the address.
158
String JavaDoc tag = "<URL>";
159         if (command.indexOf(tag) != -1) {
160           command = StringOps.replace(command, tag, addr);
161         }
162         else {
163           // No <URL> specified, so tack it onto the end.
164
command = command + " " + addr;
165         }
166       }
167
168       // Build a string array of command and arguments.
169
List JavaDoc<String JavaDoc> args = ArgumentTokenizer.tokenize(command);
170
171       // Prepend the file only if it exists.
172
if (exe != FileOption.NULL_FILE) args.add(0, exe.getAbsolutePath());
173
174       // Call the command.
175
try {
176         // Process proc =
177
Runtime.getRuntime().exec(args.toArray(new String JavaDoc[args.size()]));
178       }
179       catch (Throwable JavaDoc t) {
180         // If there was any kind of problem, ignore it and report failure.
181
return false;
182       }
183     }
184
185     // Otherwise, trust that it worked.
186
return true;
187   }
188 }
189
Popular Tags