Source code: com/port80/eclipse/builder/BuilderPlugin.java
1 package com.port80.eclipse.builder;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.HashMap;
5 import java.util.MissingResourceException;
6 import java.util.ResourceBundle;
7
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IWorkspace;
10 import org.eclipse.core.resources.IncrementalProjectBuilder;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IPluginDescriptor;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
16 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
17 import org.eclipse.jface.operation.IRunnableWithProgress;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.plugin.AbstractUIPlugin;
20
21 import com.port80.eclipse.util.UtilPlugin;
22
23 /**
24 * The main plugin class to be used in the desktop.
25 */
26 public class BuilderPlugin extends AbstractUIPlugin {
27
28 ////////////////////////////////////////////////////////////////////////
29
30 public static final String NAME = "BuilderPlugin";
31 public static final String ID = "com.port80.eclipse.builder.BuilderPlugin";
32 public static final String BUILD_PROBLEM_MARKER = ID + ".blacksun_build_problem";
33
34 //The shared instance.
35 private static BuilderPlugin plugin;
36
37 ////////////////////////////////////////////////////////////////////////
38
39 //Resource bundle.
40 private ResourceBundle resourceBundle;
41
42 ////////////////////////////////////////////////////////////////////////
43
44 /**
45 * The constructor.
46 */
47 public BuilderPlugin(IPluginDescriptor descriptor) {
48 super(descriptor);
49 plugin = this;
50 try {
51 resourceBundle = ResourceBundle.getBundle("com.port80.eclipse.builder.messages");
52 } catch (MissingResourceException x) {
53 resourceBundle = null;
54 }
55 }
56
57 ////////////////////////////////////////////////////////////////////////
58
59 /**
60 * Returns the shared instance.
61 */
62 public static BuilderPlugin getDefault() {
63 return plugin;
64 }
65
66 /**
67 * Returns the workspace instance.
68 */
69 public static IWorkspace getWorkspace() {
70 return ResourcesPlugin.getWorkspace();
71 }
72
73 /**
74 * Returns the string from the plugin's resource bundle,
75 * or 'key' if not found.
76 */
77 public static String getString(String key) {
78 ResourceBundle bundle = BuilderPlugin.getDefault().getResourceBundle();
79 try {
80 return bundle.getString(key);
81 } catch (MissingResourceException e) {
82 return key;
83 }
84 }
85
86 public static void error(String message, Exception e, IProject project) {
87 UtilPlugin.errorMarker(message, e, project, BUILD_PROBLEM_MARKER, 0, 0, 0);
88 }
89
90 ////////////////////////////////////////////////////////////////////////
91
92 public static void build(final IProject project, final String builder_name, Shell shell) {
93 ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
94 try {
95 dialog.run(true, true, new IRunnableWithProgress() {
96 public void run(IProgressMonitor monitor) throws InvocationTargetException {
97 build(project, builder_name, monitor);
98 }
99 });
100 } catch (InterruptedException e) {
101 // cancelled by user
102 System.err.println("Build cancelled.");
103 } catch (InvocationTargetException e) {
104 String message =
105 BuilderPlugin.getString(
106 "Builder.error.message"
107 + ": builder="
108 + builder_name
109 + ", project="
110 + project.getName());
111 String title = BuilderPlugin.getString("Builder.error.title");
112 ExceptionHandler.handle(e, shell, title, message);
113 // BuilderPlugin.error(message, e, project);
114 }
115 }
116
117 static void build(IProject project, String builder_name, IProgressMonitor monitor)
118 throws InvocationTargetException {
119 try {
120 project.build(IncrementalProjectBuilder.FULL_BUILD, builder_name, new HashMap(), monitor);
121 } catch (CoreException e) {
122 throw new InvocationTargetException(e);
123 }
124
125 }
126
127 ////////////////////////////////////////////////////////////////////////
128
129 /**
130 * Returns the plugin's resource bundle,
131 */
132 public ResourceBundle getResourceBundle() {
133 return resourceBundle;
134 }
135
136 ////////////////////////////////////////////////////////////////////////
137
138 }