KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > UserLibraryMarkerResolutionGenerator


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.ui.wizards.buildpaths;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
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     /* (non-Javadoc)
63      * @see org.eclipse.ui.IMarkerResolutionGenerator2#hasResolutions(org.eclipse.core.resources.IMarker)
64      */

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     /* (non-Javadoc)
78      * @see org.eclipse.ui.IMarkerResolutionGenerator#getResolutions(org.eclipse.core.resources.IMarker)
79      */

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 JavaDoc resolutions= new ArrayList JavaDoc();
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 JavaDoc[] 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc, InterruptedException JavaDoc {
160                     try {
161                         project.setRawClasspath(newEntries, project.getOutputLocation(), monitor);
162                     } catch (CoreException e) {
163                         throw new InvocationTargetException JavaDoc(e);
164                     }
165                 }
166             });
167         } catch (JavaModelException e) {
168             String JavaDoc title= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title;
169             String JavaDoc message= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_creationfailed_message;
170             ExceptionHandler.handle(e, shell, title, message);
171         } catch (InvocationTargetException JavaDoc e) {
172             String JavaDoc title= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title;
173             String JavaDoc message= NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_applyingfailed_message;
174             ExceptionHandler.handle(e, shell, title, message);
175         } catch (InterruptedException JavaDoc e) {
176             // user cancelled
177
}
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 JavaDoc name= unboundPath.segment(1);
192         String JavaDoc id= UserLibraryPreferencePage.ID;
193         HashMap JavaDoc data= new HashMap JavaDoc(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 JavaDoc[] { id }, data).open();
197     }
198
199     private IJavaProject getJavaProject(IMarker marker) {
200         return JavaCore.create(marker.getResource().getProject());
201     }
202
203     /**
204      * Library quick fix base class
205      */

206     private static abstract class UserLibraryMarkerResolution implements IMarkerResolution, IMarkerResolution2 {
207         
208         private String JavaDoc fLabel;
209         private Image fImage;
210         
211         public UserLibraryMarkerResolution(String JavaDoc label, Image image) {
212             fLabel= label;
213             fImage= image;
214         }
215         
216         /* (non-Javadoc)
217          * @see org.eclipse.ui.IMarkerResolution#getLabel()
218          */

219         public String JavaDoc getLabel() {
220             return fLabel;
221         }
222         
223         /* (non-Javadoc)
224          * @see org.eclipse.ui.IMarkerResolution2#getDescription()
225          */

226         public String JavaDoc getDescription() {
227             return null;
228         }
229         
230         /* (non-Javadoc)
231          * @see org.eclipse.ui.IMarkerResolution2#getImage()
232          */

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 JavaDoc 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 JavaDoc 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