1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.Path; 22 23 import org.eclipse.core.resources.IMarker; 24 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.swt.widgets.Shell; 27 28 import org.eclipse.jface.operation.IRunnableContext; 29 import org.eclipse.jface.operation.IRunnableWithProgress; 30 31 import org.eclipse.ui.IMarkerResolution; 32 import org.eclipse.ui.IMarkerResolution2; 33 import org.eclipse.ui.IMarkerResolutionGenerator; 34 import org.eclipse.ui.IMarkerResolutionGenerator2; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.dialogs.PreferencesUtil; 37 38 import org.eclipse.jdt.core.CorrectionEngine; 39 import org.eclipse.jdt.core.IClasspathEntry; 40 import org.eclipse.jdt.core.IJavaModelMarker; 41 import org.eclipse.jdt.core.IJavaModelStatusConstants; 42 import org.eclipse.jdt.core.IJavaProject; 43 import org.eclipse.jdt.core.JavaCore; 44 import org.eclipse.jdt.core.JavaModelException; 45 46 import org.eclipse.jdt.internal.corext.util.Messages; 47 48 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess; 49 50 import org.eclipse.jdt.internal.ui.JavaPlugin; 51 import org.eclipse.jdt.internal.ui.JavaPluginImages; 52 import org.eclipse.jdt.internal.ui.preferences.BuildPathsPropertyPage; 53 import org.eclipse.jdt.internal.ui.preferences.UserLibraryPreferencePage; 54 import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; 55 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 56 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 57 58 public class UserLibraryMarkerResolutionGenerator implements IMarkerResolutionGenerator, IMarkerResolutionGenerator2 { 59 60 private final static IMarkerResolution[] NO_RESOLUTION = new IMarkerResolution[0]; 61 62 65 public boolean hasResolutions(IMarker marker) { 66 int id= marker.getAttribute(IJavaModelMarker.ID, -1); 67 if (id == IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND 68 || id == IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND 69 || id == IJavaModelStatusConstants.INVALID_CP_CONTAINER_ENTRY 70 || id == IJavaModelStatusConstants.DEPRECATED_VARIABLE 71 || id == IJavaModelStatusConstants.INVALID_CLASSPATH) { 72 return true; 73 } 74 return false; 75 } 76 77 80 public IMarkerResolution[] getResolutions(IMarker marker) { 81 final Shell shell= JavaPlugin.getActiveWorkbenchShell(); 82 if (!hasResolutions(marker) || shell == null) { 83 return NO_RESOLUTION; 84 } 85 86 ArrayList resolutions= new ArrayList (); 87 88 final IJavaProject project= getJavaProject(marker); 89 90 int id= marker.getAttribute(IJavaModelMarker.ID, -1); 91 if (id == IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND) { 92 String [] arguments= CorrectionEngine.getProblemArguments(marker); 93 final IPath path= new Path(arguments[0]); 94 95 if (path.segment(0).equals(JavaCore.USER_LIBRARY_CONTAINER_ID)) { 96 String label= NewWizardMessages.UserLibraryMarkerResolutionGenerator_changetouserlib_label; 97 Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME); 98 resolutions.add(new UserLibraryMarkerResolution(label, image) { 99 public void run(IMarker m) { 100 changeToExistingLibrary(shell, path, false, project); 101 } 102 }); 103 if (path.segmentCount() == 2) { 104 String label2= Messages.format(NewWizardMessages.UserLibraryMarkerResolutionGenerator_createuserlib_label, path.segment(1)); 105 Image image2= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD); 106 resolutions.add(new UserLibraryMarkerResolution(label2, image2) { 107 public void run(IMarker m) { 108 createUserLibrary(shell, path, project); 109 } 110 }); 111 } 112 } 113 String label= NewWizardMessages.UserLibraryMarkerResolutionGenerator_changetoother; 114 Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME); 115 resolutions.add(new UserLibraryMarkerResolution(label, image) { 116 public void run(IMarker m) { 117 changeToExistingLibrary(shell, path, true, project); 118 } 119 }); 120 } 121 122 if (project != null) { 123 resolutions.add(new OpenBuildPathMarkerResolution(project)); 124 } 125 126 return (IMarkerResolution[]) resolutions.toArray(new IMarkerResolution[resolutions.size()]); 127 } 128 129 protected void changeToExistingLibrary(Shell shell, IPath path, boolean isNew, final IJavaProject project) { 130 try { 131 IClasspathEntry[] entries= project.getRawClasspath(); 132 int idx= indexOfClasspath(entries, path); 133 if (idx == -1) { 134 return; 135 } 136 IClasspathEntry[] res; 137 if (isNew) { 138 res= BuildPathDialogAccess.chooseContainerEntries(shell, project, entries); 139 if (res == null) { 140 return; 141 } 142 } else { 143 IClasspathEntry resEntry= BuildPathDialogAccess.configureContainerEntry(shell, entries[idx], project, entries); 144 if (resEntry == null) { 145 return; 146 } 147 res= new IClasspathEntry[] { resEntry }; 148 } 149 final IClasspathEntry[] newEntries= new IClasspathEntry[entries.length - 1 + res.length]; 150 System.arraycopy(entries, 0, newEntries, 0, idx); 151 System.arraycopy(res, 0, newEntries, idx, res.length); 152 System.arraycopy(entries, idx + 1, newEntries, idx + res.length, entries.length - idx - 1); 153 154 IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow(); 155 if (context == null) { 156 context= PlatformUI.getWorkbench().getProgressService(); 157 } 158 context.run(true, true, new IRunnableWithProgress() { 159 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 160 try { 161 project.setRawClasspath(newEntries, project.getOutputLocation(), monitor); 162 } catch (CoreException e) { 163 throw new InvocationTargetException (e); 164 } 165 } 166 }); 167 } catch (JavaModelException e) { 168 String title= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title; 169 String message= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_creationfailed_message; 170 ExceptionHandler.handle(e, shell, title, message); 171 } catch (InvocationTargetException e) { 172 String title= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title; 173 String message= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_applyingfailed_message; 174 ExceptionHandler.handle(e, shell, title, message); 175 } catch (InterruptedException e) { 176 } 178 } 179 180 private int indexOfClasspath(IClasspathEntry[] entries, IPath path) { 181 for (int i= 0; i < entries.length; i++) { 182 IClasspathEntry curr= entries[i]; 183 if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER && curr.getPath().equals(path)) { 184 return i; 185 } 186 } 187 return -1; 188 } 189 190 protected void createUserLibrary(final Shell shell, IPath unboundPath, IJavaProject project) { 191 String name= unboundPath.segment(1); 192 String id= UserLibraryPreferencePage.ID; 193 HashMap data= new HashMap (3); 194 data.put(UserLibraryPreferencePage.DATA_LIBRARY_TO_SELECT, name); 195 data.put(UserLibraryPreferencePage.DATA_DO_CREATE, Boolean.TRUE); 196 PreferencesUtil.createPreferenceDialogOn(shell, id, new String [] { id }, data).open(); 197 } 198 199 private IJavaProject getJavaProject(IMarker marker) { 200 return JavaCore.create(marker.getResource().getProject()); 201 } 202 203 206 private static abstract class UserLibraryMarkerResolution implements IMarkerResolution, IMarkerResolution2 { 207 208 private String fLabel; 209 private Image fImage; 210 211 public UserLibraryMarkerResolution(String label, Image image) { 212 fLabel= label; 213 fImage= image; 214 } 215 216 219 public String getLabel() { 220 return fLabel; 221 } 222 223 226 public String getDescription() { 227 return null; 228 } 229 230 233 public Image getImage() { 234 return fImage; 235 } 236 } 237 238 private static class OpenBuildPathMarkerResolution implements IMarkerResolution2 { 239 private IJavaProject fProject; 240 241 public OpenBuildPathMarkerResolution(IJavaProject project) { 242 fProject= project; 243 } 244 245 public String getDescription() { 246 return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_description, fProject.getElementName()); 247 } 248 249 public Image getImage() { 250 return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ACCESSRULES_ATTRIB); 251 } 252 253 public String getLabel() { 254 return CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_label; 255 } 256 257 public void run(IMarker marker) { 258 PreferencesUtil.createPropertyDialogOn(JavaPlugin.getActiveWorkbenchShell(), fProject, BuildPathsPropertyPage.PROP_ID, null, null).open(); 259 } 260 } 261 262 } 263 | Popular Tags |