KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > SelectSystemLibraryQuickFix


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.debug.ui.launcher;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jdt.core.IClasspathEntry;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
26 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
27 import org.eclipse.jdt.launching.JavaRuntime;
28 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.ui.PlatformUI;
31
32 import com.ibm.icu.text.MessageFormat;
33
34 /**
35  * Quick fix to select an alternate JRE for a project.
36  */

37 public class SelectSystemLibraryQuickFix extends JREResolution {
38     
39     private IPath fOldPath;
40     private IJavaProject fProject;
41     
42     public SelectSystemLibraryQuickFix(IPath oldPath, IJavaProject project) {
43         fOldPath = oldPath;
44         fProject = project;
45     }
46
47     /**
48      * @see org.eclipse.ui.IMarkerResolution#run(org.eclipse.core.resources.IMarker)
49      */

50     public void run(IMarker marker) {
51         try {
52             handleContainerResolutionError(fOldPath, fProject);
53         } catch (CoreException e) {
54             JDIDebugUIPlugin.statusDialog(LauncherMessages.JREContainerResolution_Unable_to_update_classpath_1, e.getStatus());
55         }
56     }
57     
58     protected void handleContainerResolutionError(final IPath oldPath, final IJavaProject project) throws CoreException {
59         
60         String JavaDoc lib = oldPath.segment(0);
61         IPath initialPath = null;
62         if (JavaRuntime.JRELIB_VARIABLE.equals(lib)) {
63             initialPath = JavaRuntime.newDefaultJREContainerPath();
64         } else if (JavaRuntime.JRE_CONTAINER.equals(lib)) {
65             initialPath = oldPath;
66         }
67         IClasspathEntry initialEntry = JavaCore.newContainerEntry(initialPath);
68         final IClasspathEntry containerEntry = BuildPathDialogAccess.configureContainerEntry(JDIDebugUIPlugin.getActiveWorkbenchShell(), initialEntry, project, new IClasspathEntry[]{});
69         if (containerEntry == null || containerEntry.getPath().equals(oldPath)) {
70             return;
71         }
72
73         IRunnableWithProgress runnable = new IRunnableWithProgress() {
74             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
75                 try {
76                     IPath newPath = containerEntry.getPath();
77                     IClasspathEntry[] classpath = project.getRawClasspath();
78                     for (int i = 0; i < classpath.length; i++) {
79                         if (classpath[i].getPath().equals(oldPath)) {
80                             classpath[i] = JavaCore.newContainerEntry(newPath, classpath[i].isExported());
81                             break;
82                         }
83                     }
84                     project.setRawClasspath(classpath, monitor);
85                 //JavaCore.setClasspathContainer(oldPath, new IJavaProject[] {project}, new IClasspathContainer[] {new JREContainer(vm, newBinding, project)}, monitor);
86
} catch (CoreException e) {
87                     throw new InvocationTargetException JavaDoc(e);
88                 }
89             }
90         };
91         
92         try {
93             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
94         } catch (InvocationTargetException JavaDoc e) {
95             if (e.getTargetException() instanceof CoreException) {
96                 throw (CoreException)e.getTargetException();
97             }
98             throw new CoreException(new Status(IStatus.ERROR,
99                 JDIDebugUIPlugin.getUniqueIdentifier(),
100                 IJavaDebugUIConstants.INTERNAL_ERROR,
101                 LauncherMessages.JREContainerResolution_An_exception_occurred_while_updating_the_classpath__1, e.getTargetException()));
102         } catch (InterruptedException JavaDoc e) {
103             // cancelled
104
}
105     }
106     /**
107      * @see org.eclipse.ui.IMarkerResolution#getLabel()
108      */

109     public String JavaDoc getLabel() {
110         return MessageFormat.format(LauncherMessages.JREContainerResolution_Select_a_system_library_to_use_when_building__0__2, new String JavaDoc[]{fProject.getElementName()});
111     }
112
113 }
114
Popular Tags