Source code: com/port80/eclipse/util/UtilPlugin.java
1 package com.port80.eclipse.util;
2
3 import java.util.MissingResourceException;
4 import java.util.ResourceBundle;
5
6 import org.eclipse.core.resources.IMarker;
7 import org.eclipse.core.resources.IResource;
8 import org.eclipse.core.resources.IWorkspace;
9 import org.eclipse.core.resources.ResourcesPlugin;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IPluginDescriptor;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
15 import org.eclipse.ui.plugin.AbstractUIPlugin;
16
17 /**
18 * The main plugin class to be used in the desktop.
19 */
20 public class UtilPlugin extends AbstractUIPlugin {
21
22 ////////////////////////////////////////////////////////////////////////
23
24 private static final String NAME = "UtilPlugin";
25 private static final boolean DEBUG = false;
26
27 ////////////////////////////////////////////////////////////////////////
28
29 //The shared instance.
30 private static UtilPlugin plugin;
31 //Resource bundle.
32 private ResourceBundle resourceBundle;
33
34 ////////////////////////////////////////////////////////////////////////
35
36 /**
37 * The constructor.
38 */
39 public UtilPlugin(IPluginDescriptor descriptor) {
40 super(descriptor);
41 plugin = this;
42 try {
43 resourceBundle = ResourceBundle.getBundle("com.port80.eclipse.util.messages");
44 } catch (MissingResourceException x) {
45 resourceBundle = null;
46 }
47 getPreferenceStore();
48 savePluginPreferences();
49 }
50
51 ////////////////////////////////////////////////////////////////////////
52
53 /**
54 * Returns the shared instance.
55 */
56 public static UtilPlugin getDefault() {
57 return plugin;
58 }
59
60 /**
61 * Returns the workspace instance.
62 */
63 public static IWorkspace getWorkspace() {
64 return ResourcesPlugin.getWorkspace();
65 }
66
67 /**
68 * Returns the string from the plugin's resource bundle,
69 * or 'key' if not found.
70 */
71 public static String getResourceString(String key) {
72 ResourceBundle bundle = UtilPlugin.getDefault().getResourceBundle();
73 try {
74 return bundle.getString(key);
75 } catch (MissingResourceException e) {
76 return key;
77 }
78 }
79
80 /**
81 * Returns the plugin's resource bundle,
82 */
83 public ResourceBundle getResourceBundle() {
84 return resourceBundle;
85 }
86
87 ////////////////////////////////////////////////////////////////////////
88
89 public static String getPluginId() {
90 return getDefault().getDescriptor().getUniqueIdentifier();
91 }
92
93 /**
94 * Returns the string from the plugin's resource bundle,
95 * or 'key' if not found.
96 */
97 public static String getString(String key) {
98 try {
99 return getDefault().getResourceBundle().getString(key);
100 } catch (MissingResourceException e) {
101 return key;
102 }
103 }
104
105 // Messages ////////////////////////////////////////////////////////////
106 //
107
108 public static void log(IStatus status) {
109 getDefault().getLog().log(status);
110 }
111
112 public static void log(String message) {
113 log(message, null);
114 }
115
116 public static void log(String message, Exception e) {
117 //For 2.1.0
118 //log(new Status(IStatus.ERROR, getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, e));
119 log(new Status(IStatus.ERROR, getPluginId(), 1, message, e));
120 }
121
122 // IMarkerFactory interface /////////////////////////////////////////
123
124 /**
125 * Create an error marker.
126 *
127 * @param resource Resource to attach the marker to.
128 * @param marker_type Marker id.
129 * @param start Start offset (0-based) of error range.
130 * @param end End offset (0-based) of error range.
131 * @param line Line (1-based) of error.
132 */
133 public static void errorMarker(
134 String message,
135 Exception e,
136 IResource resource,
137 String marker_type,
138 int start,
139 int end,
140 int line) {
141 //
142 if (e != null) {
143 String s = e.getMessage();
144 message += ": " + s;
145 EditorsPlugin.log(e);
146 }
147 if (resource == null || marker_type == null) {
148 System.err.println("ERROR: " + message);
149 return;
150 }
151 // if (message != null) {
152 // int index = message.indexOf('\n');
153 // if (index > 0)
154 // message = message.substring(0, index);
155 // }
156 createMarker(resource, marker_type, IMarker.SEVERITY_ERROR, message, start, end, line);
157 }
158
159 /**
160 * Create a warning marker.
161 *
162 * @param resource Resource to attach the marker to.
163 * @param marker_type Marker id.
164 * @param start Start offset (0-based) of error range.
165 * @param end End offset (0-based) of error range.
166 * @param line Line (1-based) of error.
167 */
168 public static void warnMarker(
169 String message,
170 Exception e,
171 IResource resource,
172 String marker_type,
173 int start,
174 int end,
175 int line) {
176 //
177 if (e != null) {
178 String s = e.getMessage();
179 if (s != null)
180 message += ": " + s;
181 else
182 EditorsPlugin.log(e);
183 }
184 if (resource == null || marker_type == null) {
185 System.err.println("WARN: " + message);
186 return;
187 }
188 // if (message != null) {
189 // int index = message.indexOf('\n');
190 // if (index > 0)
191 // message = message.substring(0, index);
192 // }
193 createMarker(resource, marker_type, IMarker.SEVERITY_WARNING, message, start, end, line);
194 }
195
196 /**
197 * Create an info marker.
198 *
199 * @param resource Resource to attach the marker to.
200 * @param marker_type Marker id.
201 * @param start Start offset (0-based) of error range.
202 * @param end End offset (0-based) of error range.
203 * @param line Line (1-based) of error.
204 */
205 public static void infoMarker(
206 String message,
207 Exception e,
208 IResource resource,
209 String marker_type,
210 int start,
211 int end,
212 int line) {
213 //
214 if (e != null) {
215 String s = e.getMessage();
216 if (s != null)
217 message += ": " + s;
218 else
219 EditorsPlugin.log(e);
220 }
221 if (resource == null || marker_type == null) {
222 System.err.println("INFO: " + message);
223 return;
224 }
225 // if (message != null) {
226 // int index = message.indexOf('\n');
227 // if (index > 0)
228 // message = message.substring(0, index);
229 // }
230 createMarker(resource, marker_type, IMarker.SEVERITY_INFO, message, start, end, line);
231 }
232
233 ////////////////////////////////////////////////////////////////////////
234
235 public static void createMarker(
236 IResource resource,
237 String kind,
238 int severity,
239 String message,
240 int start,
241 int end,
242 int line) {
243 //
244 IMarker marker;
245 try {
246 marker = resource.createMarker(kind);
247 marker.setAttributes(
248 new String[] {
249 IMarker.SEVERITY,
250 IMarker.MESSAGE,
251 IMarker.LOCATION,
252 IMarker.LINE_NUMBER,
253 IMarker.CHAR_START,
254 IMarker.CHAR_END },
255 new Object[] {
256 new Integer(severity),
257 message,
258 resource.getFullPath().toString(),
259 new Integer(line),
260 new Integer(start),
261 new Integer(end)});
262 } catch (CoreException ex) {
263 log(NAME + ".createMarker(): Unable to create marker");
264 return;
265 }
266 }
267
268 public static void deleteMarkers(IResource resource, String type) {
269 if (resource == null)
270 return;
271 try {
272 resource.deleteMarkers(type, true, IResource.DEPTH_INFINITE);
273 } catch (CoreException e) {
274 if (DEBUG)
275 System.err.println(
276 NAME
277 + ".deleteProblemMarkers(): resource.deleteMarkers() error: resource="
278 + resource.getFullPath());
279 }
280 }
281
282 public static IMarker[] getMarkers(IResource resource, String type) {
283 if (resource != null) {
284 try {
285 return resource.findMarkers(type, true, IResource.DEPTH_INFINITE);
286 } catch (CoreException e) {
287 }
288 }
289 return new IMarker[0];
290 }
291
292 ////////////////////////////////////////////////////////////////////////
293
294 }