KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > JUnitAddLibraryProposal


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.junit.ui;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.operation.IRunnableContext;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.contentassist.IContextInformation;
31
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.JavaModelException;
35
36 import org.eclipse.jdt.ui.ISharedImages;
37 import org.eclipse.jdt.ui.JavaUI;
38 import org.eclipse.jdt.ui.text.java.IInvocationContext;
39 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
40
41 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
42
43 import org.eclipse.jdt.internal.junit.buildpath.BuildPathSupport;
44
45 public final class JUnitAddLibraryProposal implements IJavaCompletionProposal {
46     
47     private final IInvocationContext fContext;
48     private final boolean fIsJunit4;
49     private final int fRelevance;
50     
51     public JUnitAddLibraryProposal(boolean isJunit4, IInvocationContext context, int relevance) {
52         fIsJunit4= isJunit4;
53         fContext= context;
54         fRelevance= relevance;
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
59      */

60     public int getRelevance() {
61         return fRelevance;
62     }
63     
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
66      */

67     public void apply(IDocument document) {
68         IJavaProject project= fContext.getCompilationUnit().getJavaProject();
69         Shell shell= JUnitPlugin.getActiveWorkbenchShell();
70         try {
71             IClasspathEntry entry= null;
72             if (fIsJunit4) {
73                 entry= BuildPathSupport.getJUnit4ClasspathEntry();
74             } else {
75                 entry= BuildPathSupport.getJUnit3ClasspathEntry();
76             }
77             if (entry != null) {
78                 addToClasspath(shell, project, entry, new BusyIndicatorRunnableContext());
79             }
80             // force a reconcile
81
int offset= fContext.getSelectionOffset();
82             int length= fContext.getSelectionLength();
83             String JavaDoc s= document.get(offset, length);
84             document.replace(offset, length, s);
85         } catch (CoreException e) {
86             ErrorDialog.openError(shell, JUnitMessages.JUnitAddLibraryProposal_title, JUnitMessages.JUnitAddLibraryProposal_cannotAdd, e.getStatus());
87         } catch (BadLocationException e) {
88             //ignore
89
}
90     }
91     
92     private static boolean addToClasspath(Shell shell, final IJavaProject project, IClasspathEntry entry, IRunnableContext context) throws JavaModelException {
93         IClasspathEntry[] oldEntries= project.getRawClasspath();
94         ArrayList JavaDoc newEntries= new ArrayList JavaDoc(oldEntries.length + 1);
95         boolean added= false;
96         for (int i= 0; i < oldEntries.length; i++) {
97             IClasspathEntry curr= oldEntries[i];
98             if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
99                 IPath path= curr.getPath();
100                 if (path.equals(entry.getPath())) {
101                     return true; // already on build path
102
} else if (path.matchingFirstSegments(entry.getPath()) > 0) {
103                     if (!added) {
104                         curr= entry; // replace
105
added= true;
106                     } else {
107                         curr= null;
108                     }
109                 }
110             } else if (curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
111                 IPath path= curr.getPath();
112                 if (path.segmentCount() > 0 && JUnitPlugin.JUNIT_HOME.equals(path.segment(0))) {
113                     if (!added) {
114                         curr= entry; // replace
115
added= true;
116                     } else {
117                         curr= null;
118                     }
119                 }
120             }
121             if (curr != null) {
122                 newEntries.add(curr);
123             }
124         }
125         if (!added) {
126             newEntries.add(entry);
127         }
128         
129         final IClasspathEntry[] newCPEntries= (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
130         // fix for 64974 OCE in New JUnit Test Case wizard while workspace is locked [JUnit]
131
try {
132             context.run(true, false, new IRunnableWithProgress() {
133                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
134                     try {
135                         project.setRawClasspath(newCPEntries, monitor);
136                     } catch (JavaModelException e) {
137                         throw new InvocationTargetException JavaDoc(e);
138                     }
139                 }
140             });
141             return true;
142         } catch (InvocationTargetException JavaDoc e) {
143             Throwable JavaDoc t = e.getTargetException();
144             if (t instanceof CoreException) {
145                 ErrorDialog.openError(shell, JUnitMessages.JUnitAddLibraryProposal_title, JUnitMessages.JUnitAddLibraryProposal_cannotAdd, ((CoreException)t).getStatus());
146             }
147             return false;
148         } catch (InterruptedException JavaDoc e) {
149             return false;
150         }
151
152     }
153     
154     /* (non-Javadoc)
155      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
156      */

157     public Point getSelection(IDocument document) {
158         return new Point(fContext.getSelectionOffset(), fContext.getSelectionLength());
159     }
160     
161     /* (non-Javadoc)
162      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
163      */

164     public String JavaDoc getAdditionalProposalInfo() {
165         if (fIsJunit4) {
166             return JUnitMessages.JUnitAddLibraryProposal_junit4_info;
167         }
168         return JUnitMessages.JUnitAddLibraryProposal_info;
169     }
170     
171     /* (non-Javadoc)
172      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
173      */

174     public String JavaDoc getDisplayString() {
175         if (fIsJunit4) {
176             return JUnitMessages.JUnitAddLibraryProposa_junit4_label;
177         }
178         return JUnitMessages.JUnitAddLibraryProposal_label;
179     }
180     
181     /* (non-Javadoc)
182      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
183      */

184     public Image getImage() {
185         return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_LIBRARY);
186     }
187     
188     /* (non-Javadoc)
189      * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
190      */

191     public IContextInformation getContextInformation() {
192         return null;
193     }
194 }
195
Popular Tags