View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package net.sf.tm.plugin;
34  
35  import java.net.MalformedURLException;
36  import java.net.URL;
37  import java.text.MessageFormat;
38  import java.util.MissingResourceException;
39  import java.util.ResourceBundle;
40  import java.util.StringTokenizer;
41  
42  import org.apache.commons.lang.StringUtils;
43  import org.apache.commons.lang.exception.ExceptionUtils;
44  import org.apache.commons.lang.exception.Nestable;
45  import org.eclipse.core.resources.IWorkspace;
46  import org.eclipse.core.resources.ResourcesPlugin;
47  import org.eclipse.core.runtime.IPluginDescriptor;
48  import org.eclipse.core.runtime.IStatus;
49  import org.eclipse.core.runtime.MultiStatus;
50  import org.eclipse.core.runtime.Status;
51  import org.eclipse.jface.dialogs.ErrorDialog;
52  import org.eclipse.jface.dialogs.MessageDialog;
53  import org.eclipse.jface.preference.IPreferenceStore;
54  import org.eclipse.jface.resource.ImageDescriptor;
55  import org.eclipse.swt.widgets.Shell;
56  import org.eclipse.ui.plugin.AbstractUIPlugin;
57  
58  /***
59   * Main plugin class. Provides some access methods to the Eclipse environment
60   * (e.g. resources, workspace).
61   * 
62   * @author Andreas Pataki
63   * @version $Id: ContextManagerPlugin.java,v 1.10 2004/02/23 19:18:13 apataki Exp $
64   */
65  public class ContextManagerPlugin extends AbstractUIPlugin {
66  
67      /***
68       * Instance of the plugin. If another class must call a plugin method
69       * this instance can be retrieved by calling the {@link getDefault()}
70       * method.
71       */
72      private static ContextManagerPlugin plugin;
73  
74      /***
75       * Resources for localization
76       */
77      private ResourceBundle resourceBundle;
78  
79      /***
80       * Constructor
81       * 
82       * @param descriptor plugin informations
83       */
84      public ContextManagerPlugin(IPluginDescriptor descriptor) {
85          super(descriptor);
86          plugin = this;
87          try {
88              resourceBundle =
89                  ResourceBundle.getBundle("net.sf.tm.plugin.PluginResources");
90          } catch (MissingResourceException x) {
91              resourceBundle = null;
92          }
93      }
94  
95      /***
96       * @return Returns the shared instance.
97       */
98      public static ContextManagerPlugin getDefault() {
99          return plugin;
100     }
101 
102     /***
103      * @return the active workbench shell instance
104      */
105     public Shell getActiveWorkbenchShell() {
106         return getWorkbench().getActiveWorkbenchWindow().getShell();
107     }
108 
109     /***
110      * @return the workspace instance.
111      */
112     public IWorkspace getWorkspace() {
113         return ResourcesPlugin.getWorkspace();
114     }
115 
116     /***
117      * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
118      */
119     protected void initializeDefaultPreferences(IPreferenceStore store) {
120         super.initializeDefaultPreferences(store);
121 
122         store.setDefault(
123             PreferenceConstants.PREF_AUTOUPDATE,
124             PreferenceConstants.PREF_AUTOUPDATE_DEFAULT);
125         store.setDefault(
126             PreferenceConstants.PREF_UPDATE_INTERVAL,
127             PreferenceConstants.PREF_INTERVAL_DEFAULT);
128     }
129 
130     /***
131      * @return Returns the plugin's resource bundle,
132      */
133     public ResourceBundle getResourceBundle() {
134         return resourceBundle;
135     }
136 
137     /***
138      * Displays an error message box. The stacktrace of the provided exception
139      * is displayed in the details part of the dialog.
140      * 
141      * @param msg localized exception message.
142      * @param exception catched exception
143      */
144     public void showError(String msg, Throwable exception) {
145         String errorReason = getErrorReason(exception);
146 
147         MultiStatus status =
148             new MultiStatus(
149                 getDescriptor().getUniqueIdentifier(),
150                 IStatus.ERROR,
151                 errorReason,
152                 exception);
153 
154         if (exception != null) {
155 
156             // convert stacktrace to a string
157             String stacktrace = ExceptionUtils.getFullStackTrace(exception);
158             stacktrace = StringUtils.replace(stacktrace, "\t", "    ");
159 
160             // append each stacktrace line as a status object
161             StringTokenizer st = new StringTokenizer(stacktrace, "\r\n");
162             while (st.hasMoreTokens()) {
163                 status.add(
164                     new Status(
165                         IStatus.ERROR,
166                         getDescriptor().getUniqueIdentifier(),
167                         IStatus.OK,
168                         st.nextToken(),
169                         null));
170             }
171         }
172 
173         ErrorDialog.openError(
174             getActiveWorkbenchShell(),
175             getLocalizedString("error.title"),
176             msg,
177             status);
178 
179     }
180 
181     /***
182      * Displays an error message box. 
183      * 
184      * @param msg localized exception message.
185      */
186     public void showError(String msg) {
187         MessageDialog.openError(
188             getActiveWorkbenchShell(),
189             getLocalizedString("error.title"),
190             msg);
191     }
192 
193     /***
194      * Computes the error reason text depending on the provided exception.
195      * 
196      * @param exception The exception
197      * @return The computed localized text.
198      */
199     private String getErrorReason(Throwable exception) {
200         String errorReason;
201         if (exception != null) {
202             if (exception instanceof Nestable
203                 && ((Nestable) exception).getCause() != null) {
204                 // nestable exception -> get reason from nested exception
205                 errorReason =
206                     ((Nestable) exception).getCause().getClass().getName();
207             } else {
208                 // normal exception
209                 errorReason = exception.getClass().getName();
210             }
211         } else {
212             // no exception provided
213             errorReason = getLocalizedString("error.unknown");
214         }
215         return errorReason;
216     }
217 
218     /***
219      * Localizes a given String using the current locale.
220      * 
221      * @param key The key of the text that should be translated
222      * @return localized String
223      */
224     public String getLocalizedString(String key) {
225         try {
226             return getResourceBundle().getString(key);
227         } catch (MissingResourceException e) {
228             return key;
229         }
230     }
231 
232     /***
233      * Localizes a given String using the current locale.
234      * 
235      * @param key The key of the text that should be translated
236      * @param arg Argument which is inserted into the message at the {0} pos
237      * @return localized String
238      */
239     public String getLocalizedString(String key, Object arg) {
240         return getLocalizedString(key, new Object[] { arg });
241     }
242 
243     /***
244      * Localizes a given String using the current locale.
245      * 
246      * @param key The key of the text that should be translated
247      * @param args The arguments which should be inserted in the message.
248      * @return localized String
249      */
250     public String getLocalizedString(String key, Object[] args) {
251         String text = getLocalizedString(key);
252         if(!key.equals(text)) {
253             MessageFormat mf = new MessageFormat(text);
254             return mf.format(args);
255         }
256         return text;
257     }
258 
259     /***
260      * Generates an Image descriptor. The name of the image must be provided
261      * relative to the plugin install directory.
262      * 
263      * @param name path to the image, relative to the plugin directory
264      * @return The image descriptor for the image 
265      */
266     public ImageDescriptor getImageDescriptor(String name) {
267         try {
268             URL installURL = getDefault().getDescriptor().getInstallURL();
269             URL url = new URL(installURL, name);
270             return ImageDescriptor.createFromURL(url);
271         } catch (MalformedURLException _ex) {
272             return ImageDescriptor.getMissingImageDescriptor();
273         }
274     }
275 
276 }