1 11 package org.eclipse.jdt.internal.junit.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 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 60 public int getRelevance() { 61 return fRelevance; 62 } 63 64 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 int offset= fContext.getSelectionOffset(); 82 int length= fContext.getSelectionLength(); 83 String 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 } 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 newEntries= new ArrayList (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; } else if (path.matchingFirstSegments(entry.getPath()) > 0) { 103 if (!added) { 104 curr= entry; 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; 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 try { 132 context.run(true, false, new IRunnableWithProgress() { 133 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 134 try { 135 project.setRawClasspath(newCPEntries, monitor); 136 } catch (JavaModelException e) { 137 throw new InvocationTargetException (e); 138 } 139 } 140 }); 141 return true; 142 } catch (InvocationTargetException e) { 143 Throwable 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 e) { 149 return false; 150 } 151 152 } 153 154 157 public Point getSelection(IDocument document) { 158 return new Point(fContext.getSelectionOffset(), fContext.getSelectionLength()); 159 } 160 161 164 public String getAdditionalProposalInfo() { 165 if (fIsJunit4) { 166 return JUnitMessages.JUnitAddLibraryProposal_junit4_info; 167 } 168 return JUnitMessages.JUnitAddLibraryProposal_info; 169 } 170 171 174 public String getDisplayString() { 175 if (fIsJunit4) { 176 return JUnitMessages.JUnitAddLibraryProposa_junit4_label; 177 } 178 return JUnitMessages.JUnitAddLibraryProposal_label; 179 } 180 181 184 public Image getImage() { 185 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_LIBRARY); 186 } 187 188 191 public IContextInformation getContextInformation() { 192 return null; 193 } 194 } 195 | Popular Tags |