1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 21 import org.eclipse.core.resources.ResourcesPlugin; 22 23 import org.eclipse.swt.widgets.Shell; 24 25 import org.eclipse.jface.dialogs.MessageDialog; 26 27 import org.eclipse.jdt.core.ClasspathContainerInitializer; 28 import org.eclipse.jdt.core.IClasspathAttribute; 29 import org.eclipse.jdt.core.IClasspathContainer; 30 import org.eclipse.jdt.core.IClasspathEntry; 31 import org.eclipse.jdt.core.IJavaModel; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.JavaCore; 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.internal.corext.util.Messages; 37 38 import org.eclipse.jdt.ui.JavaUI; 39 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 42 43 46 public class BuildPathSupport { 47 48 public static final String JRE_PREF_PAGE_ID= "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"; 50 51 private BuildPathSupport() { 52 super(); 53 } 54 55 63 public static String getDeprecationMessage(String variableName) { 64 String deprecationMessage= JavaCore.getClasspathVariableDeprecationMessage(variableName); 65 if (deprecationMessage == null ) 66 return null; 67 else 68 return Messages.format(NewWizardMessages.BuildPathSupport_deprecated, 69 new Object [] {variableName, deprecationMessage}); 70 } 71 72 77 public static IPath guessSourceAttachment(CPListElement elem) { 78 if (elem.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 79 return null; 80 } 81 IJavaProject currProject= elem.getJavaProject(); try { 83 IJavaModel jmodel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 85 IJavaProject[] jprojects= jmodel.getJavaProjects(); 86 for (int i= 0; i < jprojects.length; i++) { 87 IJavaProject curr= jprojects[i]; 88 if (!curr.equals(currProject)) { 89 IClasspathEntry[] entries= curr.getRawClasspath(); 90 for (int k= 0; k < entries.length; k++) { 91 IClasspathEntry entry= entries[k]; 92 if (entry.getEntryKind() == elem.getEntryKind() 93 && entry.getPath().equals(elem.getPath())) { 94 IPath attachPath= entry.getSourceAttachmentPath(); 95 if (attachPath != null && !attachPath.isEmpty()) { 96 return attachPath; 97 } 98 } 99 } 100 } 101 } 102 } catch (JavaModelException e) { 103 JavaPlugin.log(e.getStatus()); 104 } 105 return null; 106 } 107 108 113 public static String guessJavadocLocation(CPListElement elem) { 114 if (elem.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 115 return null; 116 } 117 IJavaProject currProject= elem.getJavaProject(); try { 119 IJavaModel jmodel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 121 IJavaProject[] jprojects= jmodel.getJavaProjects(); 122 for (int i= 0; i < jprojects.length; i++) { 123 IJavaProject curr= jprojects[i]; 124 if (!curr.equals(currProject)) { 125 IClasspathEntry[] entries= curr.getRawClasspath(); 126 for (int k= 0; k < entries.length; k++) { 127 IClasspathEntry entry= entries[k]; 128 if (entry.getEntryKind() == elem.getEntryKind() && entry.getPath().equals(elem.getPath())) { 129 IClasspathAttribute[] attributes= entry.getExtraAttributes(); 130 for (int n= 0; n < attributes.length; n++) { 131 IClasspathAttribute attrib= attributes[n]; 132 if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) { 133 return attrib.getValue(); 134 } 135 } 136 } 137 } 138 } 139 } 140 } catch (JavaModelException e) { 141 JavaPlugin.log(e.getStatus()); 142 } 143 return null; 144 } 145 146 private static class UpdatedClasspathContainer implements IClasspathContainer { 147 148 private IClasspathEntry[] fNewEntries; 149 private IClasspathContainer fOriginal; 150 151 public UpdatedClasspathContainer(IClasspathContainer original, IClasspathEntry[] newEntries) { 152 fNewEntries= newEntries; 153 fOriginal= original; 154 } 155 156 public IClasspathEntry[] getClasspathEntries() { 157 return fNewEntries; 158 } 159 160 public String getDescription() { 161 return fOriginal.getDescription(); 162 } 163 164 public int getKind() { 165 return fOriginal.getKind(); 166 } 167 168 public IPath getPath() { 169 return fOriginal.getPath(); 170 } 171 } 172 173 183 public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, String [] changedAttributes, IJavaProject jproject, IPath containerPath, IProgressMonitor monitor) throws CoreException { 184 if (containerPath != null) { 185 updateContainerClasspath(jproject, containerPath, newEntry, changedAttributes, monitor); 186 } else { 187 updateProjectClasspath(shell, jproject, newEntry, changedAttributes, monitor); 188 } 189 } 190 191 192 201 public static void modifyClasspathEntry(Shell shell, IClasspathEntry newEntry, IJavaProject jproject, IPath containerPath, IProgressMonitor monitor) throws CoreException { 202 modifyClasspathEntry(shell, newEntry, null, jproject, containerPath, monitor); 203 } 204 205 private static void updateContainerClasspath(IJavaProject jproject, IPath containerPath, IClasspathEntry newEntry, String [] changedAttributes, IProgressMonitor monitor) throws CoreException { 206 IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject); 207 if (container == null) { 208 throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, "Container " + containerPath + " cannot be resolved", null)); } 210 IClasspathEntry[] entries= container.getClasspathEntries(); 211 IClasspathEntry[] newEntries= new IClasspathEntry[entries.length]; 212 for (int i= 0; i < entries.length; i++) { 213 IClasspathEntry curr= entries[i]; 214 if (curr.getEntryKind() == newEntry.getEntryKind() && curr.getPath().equals(newEntry.getPath())) { 215 newEntries[i]= getUpdatedEntry(curr, newEntry, changedAttributes, jproject); 216 } else { 217 newEntries[i]= curr; 218 } 219 } 220 requestContainerUpdate(jproject, container, newEntries); 221 monitor.worked(1); 222 } 223 224 private static IClasspathEntry getUpdatedEntry(IClasspathEntry currEntry, IClasspathEntry updatedEntry, String [] updatedAttributes, IJavaProject jproject) { 225 if (updatedAttributes == null) { 226 return updatedEntry; } 228 CPListElement currElem= CPListElement.createFromExisting(currEntry, jproject); 229 CPListElement newElem= CPListElement.createFromExisting(updatedEntry, jproject); 230 for (int i= 0; i < updatedAttributes.length; i++) { 231 String attrib= updatedAttributes[i]; 232 currElem.setAttribute(attrib, newElem.getAttribute(attrib)); 233 } 234 return currElem.getClasspathEntry(); 235 } 236 237 244 public static void requestContainerUpdate(IJavaProject jproject, IClasspathContainer container, IClasspathEntry[] newEntries) throws CoreException { 245 IPath containerPath= container.getPath(); 246 IClasspathContainer updatedContainer= new UpdatedClasspathContainer(container, newEntries); 247 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0)); 248 if (initializer != null) { 249 initializer.requestClasspathContainerUpdate(containerPath, jproject, updatedContainer); 250 } 251 } 252 253 private static void updateProjectClasspath(Shell shell, IJavaProject jproject, IClasspathEntry newEntry, String [] changedAttributes, IProgressMonitor monitor) throws JavaModelException { 254 IClasspathEntry[] oldClasspath= jproject.getRawClasspath(); 255 int nEntries= oldClasspath.length; 256 ArrayList newEntries= new ArrayList (nEntries + 1); 257 int entryKind= newEntry.getEntryKind(); 258 IPath jarPath= newEntry.getPath(); 259 boolean found= false; 260 for (int i= 0; i < nEntries; i++) { 261 IClasspathEntry curr= oldClasspath[i]; 262 if (curr.getEntryKind() == entryKind && curr.getPath().equals(jarPath)) { 263 newEntries.add(getUpdatedEntry(curr, newEntry, changedAttributes, jproject)); 265 found= true; 266 } else { 267 newEntries.add(curr); 268 } 269 } 270 if (!found) { 271 if (!putJarOnClasspathDialog(shell)) { 272 return; 273 } 274 newEntries.add(newEntry); 276 } 277 IClasspathEntry[] newClasspath= (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]); 278 jproject.setRawClasspath(newClasspath, monitor); 279 } 280 281 private static boolean putJarOnClasspathDialog(final Shell shell) { 282 if (shell == null) { 283 return false; 284 } 285 286 final boolean[] result= new boolean[1]; 287 shell.getDisplay().syncExec(new Runnable () { 288 public void run() { 289 String title= NewWizardMessages.BuildPathSupport_putoncpdialog_title; 290 String message= NewWizardMessages.BuildPathSupport_putoncpdialog_message; 291 result[0]= MessageDialog.openQuestion(shell, title, message); 292 } 293 }); 294 return result[0]; 295 } 296 } 297 | Popular Tags |