KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > buildpath > ClasspathVariableMarkerResolutionGenerator


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.jdt.internal.junit.buildpath;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.Path;
19
20 import org.eclipse.core.resources.IMarker;
21
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.operation.IRunnableContext;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27
28 import org.eclipse.ui.IMarkerResolution;
29 import org.eclipse.ui.IMarkerResolution2;
30 import org.eclipse.ui.IMarkerResolutionGenerator2;
31
32 import org.eclipse.jdt.core.CorrectionEngine;
33 import org.eclipse.jdt.core.IClasspathEntry;
34 import org.eclipse.jdt.core.IJavaModelMarker;
35 import org.eclipse.jdt.core.IJavaModelStatusConstants;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.JavaCore;
38 import org.eclipse.jdt.core.JavaModelException;
39
40 import org.eclipse.jdt.internal.ui.JavaPluginImages;
41 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
42
43 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
44
45 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
46
47 public class ClasspathVariableMarkerResolutionGenerator implements IMarkerResolutionGenerator2 {
48
49     private final static IMarkerResolution[] NO_RESOLUTION = new IMarkerResolution[0];
50
51     /*
52      * @see org.eclipse.ui.IMarkerResolutionGenerator2#hasResolutions(org.eclipse.core.resources.IMarker)
53      */

54     public boolean hasResolutions(IMarker marker) {
55         int id = marker.getAttribute(IJavaModelMarker.ID, -1);
56         if (id == IJavaModelStatusConstants.DEPRECATED_VARIABLE) {
57             String JavaDoc[] arguments= CorrectionEngine.getProblemArguments(marker);
58             if (arguments == null || arguments.length == 0)
59                 return false;
60             if (arguments[0].startsWith(JUnitPlugin.JUNIT_HOME + IPath.SEPARATOR)
61                     || arguments[0].startsWith(JUnitPlugin.JUNIT_SRC_HOME + IPath.SEPARATOR))
62                 return true;
63         }
64         return false;
65     }
66     
67     /*
68      * @see org.eclipse.ui.IMarkerResolutionGenerator#getResolutions(org.eclipse.core.resources.IMarker)
69      */

70     public IMarkerResolution[] getResolutions(IMarker marker) {
71         if (!hasResolutions(marker)) {
72             return NO_RESOLUTION;
73         }
74         
75         String JavaDoc[] arguments= CorrectionEngine.getProblemArguments(marker);
76         final IPath path= new Path(arguments[0]);
77         final IJavaProject project= getJavaProject(marker);
78         
79         return new IMarkerResolution2[] {
80                 new IMarkerResolution2() {
81                     public Image getImage() {
82                         return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
83                     }
84         
85                     public String JavaDoc getLabel() {
86                         return JUnitMessages.ClasspathVariableMarkerResolutionGenerator_use_JUnit3;
87                     }
88         
89                     public String JavaDoc getDescription() {
90                         return JUnitMessages.ClasspathVariableMarkerResolutionGenerator_use_JUnit3_desc;
91                     }
92         
93                     public void run(IMarker nonsenseArgument) {
94                         IClasspathEntry[] entries;
95                         try {
96                             entries= project.getRawClasspath();
97                             int idx= indexOfClasspath(entries, path);
98                             if (idx == -1) {
99                                 return;
100                             }
101                             entries[idx]= BuildPathSupport.getJUnit3ClasspathEntry();
102                             
103                             Shell shell= JUnitPlugin.getActiveWorkbenchShell();
104                             setClasspath(shell, project, entries, new BusyIndicatorRunnableContext());
105                             
106                         } catch (JavaModelException e) {
107                             JUnitPlugin.log(e);
108                         }
109                     }
110                 }
111         };
112     }
113
114     private IJavaProject getJavaProject(IMarker marker) {
115         return JavaCore.create(marker.getResource().getProject());
116     }
117
118     private int indexOfClasspath(IClasspathEntry[] entries, IPath path) {
119         for (int i= 0; i < entries.length; i++) {
120             IClasspathEntry curr= entries[i];
121             if (curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE && curr.getPath().equals(path)) {
122                 return i;
123             }
124         }
125         return -1;
126     }
127     
128     private static void setClasspath(Shell shell, final IJavaProject project, final IClasspathEntry[] entries, IRunnableContext context) throws JavaModelException {
129         /*
130          * @see org.eclipse.jdt.internal.junit.ui.JUnitAddLibraryProposal#addToClasspath()
131          */

132         try {
133             context.run(true, false, new IRunnableWithProgress() {
134                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
135                     try {
136                         project.setRawClasspath(entries, monitor);
137                     } catch (JavaModelException e) {
138                         throw new InvocationTargetException JavaDoc(e);
139                     }
140                 }
141             });
142         } catch (InvocationTargetException JavaDoc e) {
143             JUnitPlugin.log(e);
144         } catch (InterruptedException JavaDoc e) {
145             // that's OK
146
}
147
148     }
149     
150 }
151
Popular Tags