KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > ForrestPlugin


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.eclipse;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 import org.apache.forrest.eclipse.preference.ForrestPreferences;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.ui.plugin.AbstractUIPlugin;
29
30 /**
31  * The main plugin class to be used in the desktop.
32  */

33 public class ForrestPlugin extends AbstractUIPlugin {
34     public final static String JavaDoc ID = "org.apache.forrest.eclipse";
35     //The shared instance.
36
private static ForrestPlugin plugin;
37
38     //Resource bundle.
39
private ResourceBundle JavaDoc resourceBundle;
40
41     /**
42      * The constructor.
43      *
44      */

45     public ForrestPlugin() {
46         super();
47         plugin = this;
48         try {
49             resourceBundle = ResourceBundle
50                     .getBundle("org.apache.forrest.ForrestPluginResources"); //$NON-NLS-1$
51
} catch (MissingResourceException JavaDoc x) {
52             resourceBundle = null;
53         }
54     }
55
56     /**
57      * Returns the shared instance.
58      *
59      * @return
60      */

61     public static ForrestPlugin getDefault() {
62         return plugin;
63     }
64
65     /**
66      * Returns the string from the plugin's resource bundle, or 'key' if not
67      * found.
68      *
69      * @param key
70      * @return
71      */

72     public static String JavaDoc getResourceString(String JavaDoc key) {
73         ResourceBundle JavaDoc bundle = ForrestPlugin.getDefault().getResourceBundle();
74         try {
75             return (bundle != null) ? bundle.getString(key) : key;
76         } catch (MissingResourceException JavaDoc e) {
77             return key;
78         }
79     }
80
81     /**
82      * Returns the plugin's resource bundle,
83      *
84      * @return
85      */

86     public ResourceBundle JavaDoc getResourceBundle() {
87         return resourceBundle;
88     }
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
92      */

93     protected void initializeDefaultPreferences(IPreferenceStore store) {
94         super.initializeDefaultPreferences(store);
95         
96        HashMap JavaDoc envVariables = new HashMap JavaDoc();
97        BufferedReader JavaDoc reader = null;
98
99        //"env" works on Linux & Unix Variants but if we're on Windows,
100
//use "cmd /c set".
101
String JavaDoc envCommand = "env";
102        if (System.getProperty("os.name").toLowerCase().startsWith("win"))
103          envCommand = "cmd /c set";
104
105        try {
106          //First we launch the command and attach a Reader to the Output
107
Process JavaDoc p = Runtime.getRuntime().exec(envCommand);
108          reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
109
110          //Now we parse the output, filling up the Hashtable
111
String JavaDoc theLine = null;
112          while ( (theLine = reader.readLine()) != null) {
113            int equalsIndex = theLine.indexOf("=");
114            if (equalsIndex < 0)
115             continue;
116
117            String JavaDoc name = theLine.substring(0,equalsIndex);
118            String JavaDoc value = "";
119            //Test for an empty value such as "FOO="
120
if ((equalsIndex + 1) < theLine.length())
121              value = theLine.substring(equalsIndex+1, theLine.length());
122            envVariables.put(name, value);
123          }
124        }
125        catch (IOException JavaDoc e) {
126         // FIXME: Handle this error
127
e.printStackTrace();
128        }
129
130         store = getPreferenceStore();
131         if (envVariables.containsKey(ForrestPreferences.FORREST_HOME)) {
132             store.setDefault(ForrestPreferences.FORREST_HOME, (String JavaDoc)envVariables.get(ForrestPreferences.FORREST_HOME));
133         }
134     }
135     
136     /**
137      * Set the preference value for Forrest Home.
138      * @param forrestHome
139      */

140     public void setForrestHome(String JavaDoc forrestHome) {
141         IPreferenceStore store = getPreferenceStore();
142         store.setDefault(ForrestPreferences.FORREST_HOME, forrestHome);
143     }
144 }
145
Popular Tags