KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > build > BaseBuildAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.build;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.ant.internal.ui.IAntUIConstants;
19 import org.eclipse.ant.internal.ui.launchConfigurations.AntLaunchShortcut;
20 import org.eclipse.ant.internal.ui.launchConfigurations.IAntLaunchConfigurationConstants;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IWorkspaceRunnable;
26 import org.eclipse.core.resources.IncrementalProjectBuilder;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.Preferences;
30 import org.eclipse.debug.core.ILaunchConfiguration;
31 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
32 import org.eclipse.jdt.core.IJavaProject;
33 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
35 import org.eclipse.jdt.launching.JavaRuntime;
36 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
37 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
38 import org.eclipse.jface.action.IAction;
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.operation.IRunnableWithProgress;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.pde.core.plugin.TargetPlatform;
44 import org.eclipse.pde.internal.build.IXMLConstants;
45 import org.eclipse.pde.internal.core.exports.BuildUtilities;
46 import org.eclipse.pde.internal.core.natures.PDE;
47 import org.eclipse.pde.internal.ui.PDEPlugin;
48 import org.eclipse.pde.internal.ui.PDEUIMessages;
49 import org.eclipse.ui.IObjectActionDelegate;
50 import org.eclipse.ui.IWorkbenchPart;
51 import org.eclipse.ui.PlatformUI;
52
53 public abstract class BaseBuildAction implements IObjectActionDelegate{
54
55     protected IFile fManifestFile;
56
57     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
58     }
59
60     public void run(IAction action) {
61         if (!fManifestFile.exists())
62             return;
63
64         IRunnableWithProgress op = new IRunnableWithProgress() {
65             public void run(IProgressMonitor monitor) {
66                 IWorkspaceRunnable wop = new IWorkspaceRunnable() {
67                     public void run(IProgressMonitor monitor)
68                             throws CoreException {
69                         try {
70                             doBuild(monitor);
71                         } catch (InvocationTargetException JavaDoc e) {
72                             PDEPlugin.logException(e);
73                         }
74                     }
75                 };
76                 try {
77                     PDEPlugin.getWorkspace().run(wop, monitor);
78                 } catch (CoreException e) {
79                     PDEPlugin.logException(e);
80                 }
81             }
82         };
83         try {
84             PlatformUI.getWorkbench().getProgressService().runInUI(
85                     PDEPlugin.getActiveWorkbenchWindow(), op,
86                     PDEPlugin.getWorkspace().getRoot());
87         } catch (InterruptedException JavaDoc e) {
88         } catch (InvocationTargetException JavaDoc e) {
89             PDEPlugin.logException(e);
90         }
91
92     }
93
94     public void selectionChanged(IAction action, ISelection selection) {
95         if (selection instanceof IStructuredSelection) {
96             Object JavaDoc obj = ((IStructuredSelection) selection).getFirstElement();
97             if (obj != null && obj instanceof IFile) {
98                 this.fManifestFile = (IFile) obj;
99             }
100         }
101
102     }
103
104     private void doBuild(IProgressMonitor monitor) throws CoreException,
105             InvocationTargetException JavaDoc {
106         monitor.beginTask(
107                 PDEUIMessages.BuildAction_Validate, 4);
108         if (!ensureValid(fManifestFile, monitor)) {
109             monitor.done();
110             return;
111         }
112         monitor.worked(1);
113         monitor
114                 .setTaskName(PDEUIMessages.BuildAction_Generate);
115         makeScripts(monitor);
116         monitor.worked(1);
117         monitor.setTaskName(PDEUIMessages.BuildAction_Update);
118         refreshLocal(monitor);
119         monitor.worked(1);
120         IProject project = fManifestFile.getProject();
121         IFile generatedFile = (IFile) project.findMember("build.xml"); //$NON-NLS-1$
122
if (generatedFile != null)
123             setDefaultValues(generatedFile);
124         monitor.worked(1);
125
126     }
127
128     protected abstract void makeScripts(IProgressMonitor monitor)
129             throws InvocationTargetException JavaDoc, CoreException;
130
131     public static boolean ensureValid(IFile file, IProgressMonitor monitor)
132             throws CoreException {
133         // Force the build if autobuild is off
134
IProject project = file.getProject();
135         if (!project.getWorkspace().isAutoBuilding()) {
136             String JavaDoc builderID = "feature.xml".equals(file.getName()) ? PDE.FEATURE_BUILDER_ID : PDE.MANIFEST_BUILDER_ID; //$NON-NLS-1$
137
project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, builderID, null, monitor);
138         }
139
140         if (hasErrors(file)) {
141             // There are errors against this file - abort
142
MessageDialog
143                     .openError(
144                             null,
145                             PDEUIMessages.BuildAction_ErrorDialog_Title,
146                             PDEUIMessages.BuildAction_ErrorDialog_Message);
147             return false;
148         }
149         return true;
150     }
151
152     public static boolean hasErrors(IFile file) throws CoreException {
153         IMarker[] markers = file.findMarkers(IMarker.PROBLEM, true,
154                 IResource.DEPTH_ZERO);
155         for (int i = 0; i < markers.length; i++) {
156             Object JavaDoc att = markers[i].getAttribute(IMarker.SEVERITY);
157             if (att != null && att instanceof Integer JavaDoc) {
158                 if (((Integer JavaDoc) att).intValue() == IMarker.SEVERITY_ERROR)
159                     return true;
160             }
161         }
162         return false;
163     }
164
165     protected void refreshLocal(IProgressMonitor monitor) throws CoreException {
166         IProject project = fManifestFile.getProject();
167         project.refreshLocal(IResource.DEPTH_ONE, monitor);
168         IFile file = project.getFile("dev.properties"); //$NON-NLS-1$
169
if (file.exists())
170             file.delete(true, false, monitor);
171         project.refreshLocal(IResource.DEPTH_ONE, monitor);
172     }
173
174     public static void setDefaultValues(IFile generatedFile) {
175         try {
176             List JavaDoc configs = AntLaunchShortcut
177                     .findExistingLaunchConfigurations(generatedFile);
178             ILaunchConfigurationWorkingCopy launchCopy;
179             if (configs.size() == 0) {
180                 ILaunchConfiguration config = AntLaunchShortcut
181                         .createDefaultLaunchConfiguration(generatedFile);
182                 launchCopy = config.getWorkingCopy();
183             } else {
184                 launchCopy = ((ILaunchConfiguration) configs.get(0))
185                         .getWorkingCopy();
186             }
187             if (launchCopy == null)
188                 return;
189
190             Map JavaDoc properties = new HashMap JavaDoc();
191             properties = launchCopy.getAttribute(
192                     IAntLaunchConfigurationConstants.ATTR_ANT_PROPERTIES,
193                     properties);
194             properties.put(IXMLConstants.PROPERTY_BASE_WS, TargetPlatform.getWS());
195             properties.put(IXMLConstants.PROPERTY_BASE_OS, TargetPlatform.getOS());
196             properties.put(IXMLConstants.PROPERTY_BASE_ARCH, TargetPlatform.getOSArch());
197             properties.put(IXMLConstants.PROPERTY_BASE_NL, TargetPlatform.getNL());
198             properties.put("eclipse.running", "true"); //$NON-NLS-1$ //$NON-NLS-2$
199

200             properties.put(IXMLConstants.PROPERTY_JAVAC_FAIL_ON_ERROR, "false"); //$NON-NLS-1$
201
properties.put(IXMLConstants.PROPERTY_JAVAC_DEBUG_INFO, "on"); //$NON-NLS-1$
202
properties.put(IXMLConstants.PROPERTY_JAVAC_VERBOSE, "false"); //$NON-NLS-1$
203

204             IProject project = generatedFile.getProject();
205             if (!project.hasNature(JavaCore.NATURE_ID)) {
206                 Preferences pref = JavaCore.getPlugin().getPluginPreferences();
207                 properties.put(IXMLConstants.PROPERTY_JAVAC_SOURCE, pref.getString(JavaCore.COMPILER_SOURCE));
208                 properties.put(IXMLConstants.PROPERTY_JAVAC_TARGET, pref.getString(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM));
209             } else {
210                 IJavaProject jProject = JavaCore.create(project);
211                 properties.put(IXMLConstants.PROPERTY_JAVAC_SOURCE, jProject.getOption(JavaCore.COMPILER_SOURCE, true));
212                 properties.put(IXMLConstants.PROPERTY_JAVAC_TARGET, jProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true));
213             }
214             properties.put(IXMLConstants.PROPERTY_BOOTCLASSPATH, BuildUtilities.getBootClasspath());
215             IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
216             IExecutionEnvironment[] envs = manager.getExecutionEnvironments();
217             for (int i = 0; i < envs.length; i++) {
218                 String JavaDoc id = envs[i].getId();
219                 if (id != null)
220                     properties.put(id, BuildUtilities.getBootClasspath(id));
221             }
222             
223             launchCopy.setAttribute(
224                     IAntLaunchConfigurationConstants.ATTR_ANT_PROPERTIES,
225                     properties);
226             launchCopy.setAttribute(
227                     IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME,
228                     (String JavaDoc) null);
229             launchCopy.setAttribute(
230                     IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE,
231                     (String JavaDoc) null);
232             launchCopy.setAttribute(
233                     IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
234                     (String JavaDoc) null);
235             launchCopy.setAttribute(
236                     IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
237                     (String JavaDoc) null);
238             launchCopy.setAttribute(
239                     IAntUIConstants.ATTR_DEFAULT_VM_INSTALL,
240                     (String JavaDoc) null);
241             launchCopy.doSave();
242         } catch (CoreException e) {
243         }
244     }
245     
246 }
247
Popular Tags