View Javadoc

1   package org.apache.commons.jelly.eclipse.ui;
2   
3   import java.util.MissingResourceException;
4   import java.util.ResourceBundle;
5   
6   import org.eclipse.core.runtime.Status;
7   import org.eclipse.jface.dialogs.ErrorDialog;
8   import org.eclipse.ui.plugin.AbstractUIPlugin;
9   import org.osgi.framework.BundleContext;
10  
11  /***
12   * The main plugin class to be used in the desktop.
13   * @author Jürgen Mayrbäurl
14   * @since 0.9
15   */
16  public class JellyLauncherPlugin extends AbstractUIPlugin 
17  {
18  	//The shared instance.
19  	private static JellyLauncherPlugin plugin;
20  	
21  	//Resource bundle.
22  	private ResourceBundle resourceBundle;
23   
24  	/***
25  	 * Set <code>doAddProjectClassPathEntries</code> to true, if you want to
26  	 * have the USER_CLASS Runtime class path entries of the project (the Jelly
27  	 * script belongs to) added automatically to the classpath
28  	 * @since 0.9.4 (Before project's class path entries were added automatically. Unfortunately this caused a lot of problems
29  	 */
30  	private boolean doAddProjectClassPathEntries = false;
31  	
32  	/***
33  	 * The constructor.
34  	 */
35  	public JellyLauncherPlugin() {
36  		super();
37  		plugin = this;
38  		try {
39  			resourceBundle = ResourceBundle.getBundle("org.apache.commons.jelly.eclipse.ui.JellyLauncherPluginResources");
40  		} catch (MissingResourceException x) {
41  			resourceBundle = null;
42  		}
43  	}
44  
45  	/***
46  	 * This method is called upon plug-in activation
47  	 */
48  	public void start(BundleContext context) throws Exception {
49  		super.start(context);
50  	}
51  
52  	/***
53  	 * This method is called when the plug-in is stopped
54  	 */
55  	public void stop(BundleContext context) throws Exception {
56  		super.stop(context);
57  	}
58  
59  	/***
60  	 * Returns the shared instance.
61  	 */
62  	public static JellyLauncherPlugin getDefault() {
63  		return plugin;
64  	}
65  
66  	/***
67  	 * Returns the string from the plugin's resource bundle,
68  	 * or 'key' if not found.
69  	 */
70  	public static String getResourceString(String key) {
71  		ResourceBundle bundle = JellyLauncherPlugin.getDefault().getResourceBundle();
72  		try {
73  			return (bundle != null) ? bundle.getString(key) : key;
74  		} catch (MissingResourceException e) {
75  			return key;
76  		}
77  	}
78  
79  	/***
80  	 * Returns the plugin's resource bundle,
81  	 */
82  	public ResourceBundle getResourceBundle() {
83  		return resourceBundle;
84  	}
85  	
86  	/***
87  	 * Shows an Eclipse <code>ErrorDialog</code> for the error code and exception and
88  	 * logs the error message to the standard logger of the plugin
89  	 * @param errCode the error code
90  	 * @param exception the exception that occured
91  	 */
92  	public void showErrorForException(int errCode, Throwable exception)
93  	{
94  		// Create the error message
95  		String theMessage = JellyLauncherPlugin.getResourceString(
96  								JellyLauncherPlugin.getErrorMessageKey(errCode));
97  		
98  		// Create the Eclipse status object
99  		Status aStatus = new Status(Status.ERROR, JellyLauncherPlugin.getDefault().getBundle().getSymbolicName(),
100 									errCode, theMessage, exception);
101 		
102 		// Log the error status to the default logger of the plugin
103 		JellyLauncherPlugin.getDefault().getLog().log(aStatus);
104 		
105 		// Show the error dialog
106 		ErrorDialog.openError(null, JellyLauncherPlugin.getResourceString(JellyLaunchConfigurationConstants.ERROR_MSG_DIALOG_TITLE_KEY),
107 									null, aStatus);
108 	}
109 	
110 	/***
111 	 * Shows an error dialog with the message "Feature not implemented yet". Error 
112 	 * message will be logged to the standard logger of the plugin 
113 	 */
114 	public void showNotImplemented()
115 	{
116 		// Create the error message
117 		String theMessage = JellyLauncherPlugin.getResourceString(
118 					JellyLauncherPlugin.getErrorMessageKey(
119 							JellyLaunchConfigurationConstants.ERR_NOT_IMPLEMENTED_YET));		
120 		
121 		// Create the Eclipse status object
122 		Status aStatus = new Status(Status.ERROR, JellyLauncherPlugin.getDefault().getBundle().getSymbolicName(),
123 					JellyLaunchConfigurationConstants.ERR_NOT_IMPLEMENTED_YET, theMessage,null);
124 		
125 		// Log the error status to the default logger of the plugin
126 		JellyLauncherPlugin.getDefault().getLog().log(aStatus);
127 		
128 		// Show the error dialog
129 		ErrorDialog.openError(null, 
130 							JellyLauncherPlugin.getResourceString(JellyLaunchConfigurationConstants.ERROR_MSG_DIALOG_TITLE_KEY), 
131 							null, aStatus);
132 	}
133 	
134 	/***
135 	 * Returns the properties key string for the error message
136 	 * @param errorCode the error code
137 	 * @return property key string
138 	 */
139 	private static String getErrorMessageKey(int errorCode)
140 	{
141 		return "jelly.launch.messages.error." + String.valueOf(errorCode);
142 	}
143 	/***
144 	 * @return Returns the doAddProjectClassPathEntries.
145 	 */
146 	public boolean isDoAddProjectClassPathEntries() {
147 		return doAddProjectClassPathEntries;
148 	}
149 	/***
150 	 * @param doAddProjectClassPathEntries The doAddProjectClassPathEntries to set.
151 	 */
152 	public void setDoAddProjectClassPathEntries(
153 			boolean doAddProjectClassPathEntries) {
154 		this.doAddProjectClassPathEntries = doAddProjectClassPathEntries;
155 	}
156 }