KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > ReorgCorrectionsSubProcessor


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  * Matt Chapman, mpchapman@gmail.com - 89977 Make JDT .java agnostic
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.ui.text.correction;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.SubProgressMonitor;
30 import org.eclipse.core.runtime.jobs.Job;
31
32 import org.eclipse.core.resources.IFile;
33 import org.eclipse.core.resources.IProject;
34 import org.eclipse.core.resources.IWorkspaceRunnable;
35
36 import org.eclipse.swt.graphics.Image;
37
38 import org.eclipse.jface.dialogs.MessageDialog;
39
40 import org.eclipse.jface.text.IDocument;
41
42 import org.eclipse.ui.IEditorInput;
43 import org.eclipse.ui.IEditorPart;
44 import org.eclipse.ui.IWorkbenchPage;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.dialogs.PreferencesUtil;
47 import org.eclipse.ui.part.FileEditorInput;
48 import org.eclipse.ui.progress.IProgressService;
49
50 import org.eclipse.ltk.core.refactoring.CompositeChange;
51
52 import org.eclipse.jdt.core.IClasspathEntry;
53 import org.eclipse.jdt.core.ICompilationUnit;
54 import org.eclipse.jdt.core.IJavaElement;
55 import org.eclipse.jdt.core.IJavaProject;
56 import org.eclipse.jdt.core.IPackageDeclaration;
57 import org.eclipse.jdt.core.IPackageFragment;
58 import org.eclipse.jdt.core.IPackageFragmentRoot;
59 import org.eclipse.jdt.core.IType;
60 import org.eclipse.jdt.core.JavaConventions;
61 import org.eclipse.jdt.core.JavaCore;
62 import org.eclipse.jdt.core.JavaModelException;
63 import org.eclipse.jdt.core.Signature;
64 import org.eclipse.jdt.core.dom.ASTNode;
65 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
66 import org.eclipse.jdt.core.dom.CompilationUnit;
67 import org.eclipse.jdt.core.dom.IBinding;
68 import org.eclipse.jdt.core.dom.ImportDeclaration;
69 import org.eclipse.jdt.core.dom.Modifier;
70 import org.eclipse.jdt.core.dom.Name;
71 import org.eclipse.jdt.core.dom.SimpleName;
72 import org.eclipse.jdt.core.dom.Type;
73 import org.eclipse.jdt.core.search.IJavaSearchConstants;
74 import org.eclipse.jdt.core.search.IJavaSearchScope;
75 import org.eclipse.jdt.core.search.SearchEngine;
76 import org.eclipse.jdt.core.search.SearchPattern;
77 import org.eclipse.jdt.core.search.TypeNameMatch;
78
79 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
80 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
81 import org.eclipse.jdt.internal.corext.fix.IFix;
82 import org.eclipse.jdt.internal.corext.fix.UnusedCodeFix;
83 import org.eclipse.jdt.internal.corext.refactoring.changes.AddToClasspathChange;
84 import org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange;
85 import org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange;
86 import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange;
87 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
88 import org.eclipse.jdt.internal.corext.util.Messages;
89 import org.eclipse.jdt.internal.corext.util.TypeNameMatchCollector;
90
91 import org.eclipse.jdt.launching.IVMInstall;
92 import org.eclipse.jdt.launching.IVMInstall2;
93 import org.eclipse.jdt.launching.IVMInstallType;
94 import org.eclipse.jdt.launching.JavaRuntime;
95
96 import org.eclipse.jdt.ui.JavaElementLabels;
97 import org.eclipse.jdt.ui.actions.OrganizeImportsAction;
98 import org.eclipse.jdt.ui.text.java.IInvocationContext;
99 import org.eclipse.jdt.ui.text.java.IProblemLocation;
100
101 import org.eclipse.jdt.internal.ui.JavaPlugin;
102 import org.eclipse.jdt.internal.ui.JavaPluginImages;
103 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
104 import org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUp;
105 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
106 import org.eclipse.jdt.internal.ui.preferences.BuildPathsPropertyPage;
107 import org.eclipse.jdt.internal.ui.util.CoreUtility;
108 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
109
110 public class ReorgCorrectionsSubProcessor {
111
112     public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) throws CoreException {
113         ICompilationUnit cu= context.getCompilationUnit();
114         boolean isLinked= cu.getResource().isLinked();
115
116         IJavaProject javaProject= cu.getJavaProject();
117         String JavaDoc sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
118         String JavaDoc compliance= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
119         
120         CompilationUnit root= context.getASTRoot();
121         
122         ASTNode coveredNode= problem.getCoveredNode(root);
123         if (!(coveredNode instanceof SimpleName))
124             return;
125         
126         ASTNode parentType= coveredNode.getParent();
127         if (!(parentType instanceof AbstractTypeDeclaration))
128             return;
129         
130         String JavaDoc currTypeName= ((SimpleName) coveredNode).getIdentifier();
131         String JavaDoc newTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());
132                 
133         boolean hasOtherPublicTypeBefore= false;
134         
135         boolean found= false;
136         List JavaDoc types= root.types();
137         for (int i= 0; i < types.size(); i++) {
138              AbstractTypeDeclaration curr= (AbstractTypeDeclaration) types.get(i);
139              if (parentType != curr) {
140                  if (newTypeName.equals(curr.getName().getIdentifier())) {
141                      return;
142                  }
143                  if (!found && Modifier.isPublic(curr.getModifiers())) {
144                      hasOtherPublicTypeBefore= true;
145                  }
146              } else {
147                  found= true;
148              }
149          }
150         if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
151             proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, 5));
152         }
153         
154         if (!hasOtherPublicTypeBefore) {
155             String JavaDoc newCUName= JavaModelUtil.getRenamedCUName(cu, currTypeName);
156             ICompilationUnit newCU= ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
157             if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
158                 RenameCompilationUnitChange change= new RenameCompilationUnitChange(cu, newCUName);
159     
160                 // rename CU
161
String JavaDoc label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, newCUName);
162                 proposals.add(new ChangeCorrectionProposal(label, change, 6, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME)));
163             }
164         }
165     }
166
167     public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) throws CoreException {
168         ICompilationUnit cu= context.getCompilationUnit();
169         boolean isLinked= cu.getResource().isLinked();
170
171         // correct package declaration
172
int relevance= cu.getPackageDeclarations().length == 0 ? 7 : 5; // bug 38357
173
proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));
174
175         // move to package
176
IPackageDeclaration[] packDecls= cu.getPackageDeclarations();
177         String JavaDoc newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$
178

179         IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu);
180         IPackageFragment newPack= root.getPackageFragment(newPackName);
181
182         ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName());
183         if (!newCU.exists() && !isLinked) {
184             String JavaDoc label;
185             if (newPack.isDefaultPackage()) {
186                 label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, cu.getElementName());
187             } else {
188                 label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object JavaDoc[] { cu.getElementName(), newPack.getElementName() });
189             }
190             CompositeChange composite= new CompositeChange(label);
191             composite.add(new CreatePackageChange(newPack));
192             composite.add(new MoveCompilationUnitChange(cu, newPack));
193
194             proposals.add(new ChangeCorrectionProposal(label, composite, 6, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
195         }
196     }
197
198     public static void removeImportStatementProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) throws CoreException {
199         IFix fix= UnusedCodeFix.createRemoveUnusedImportFix(context.getASTRoot(), problem);
200         if (fix != null) {
201             Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_DELETE_IMPORT);
202             Map JavaDoc options= new Hashtable JavaDoc();
203             options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpConstants.TRUE);
204             FixCorrectionProposal proposal= new FixCorrectionProposal(fix, new UnusedCodeCleanUp(options), 6, image, context);
205             proposals.add(proposal);
206         }
207         
208         final ICompilationUnit cu= context.getCompilationUnit();
209         String JavaDoc name= CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description;
210         ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(name, null, 5, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)) {
211             public void apply(IDocument document) {
212                 IEditorInput input= new FileEditorInput((IFile) cu.getResource());
213                 IWorkbenchPage p= JavaPlugin.getActivePage();
214                 if (p == null) {
215                     return;
216                 }
217                 IEditorPart part= p.findEditor(input);
218                 if (part instanceof JavaEditor) {
219                     OrganizeImportsAction action= new OrganizeImportsAction((JavaEditor) part);
220                     action.run(cu);
221                 }
222             }
223         };
224         proposals.add(proposal);
225     }
226
227     public static void importNotFoundProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) throws CoreException {
228         ICompilationUnit cu= context.getCompilationUnit();
229         IJavaProject project= cu.getJavaProject();
230
231         ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
232         if (selectedNode != null) {
233             ImportDeclaration importDeclaration= (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
234             if (importDeclaration == null) {
235                 return;
236             }
237             if (!importDeclaration.isOnDemand()) {
238                 int kind= JavaModelUtil.is50OrHigher(cu.getJavaProject()) ? SimilarElementsRequestor.REF_TYPES : SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
239                 UnresolvedElementsSubProcessor.addNewTypeProposals(cu, importDeclaration.getName(), kind, 5, proposals);
240             }
241             
242             
243             String JavaDoc name= ASTNodes.asString(importDeclaration.getName());
244             char[] packageName;
245             char[] typeName= null;
246             if (importDeclaration.isOnDemand()) {
247                 packageName= name.toCharArray();
248             } else {
249                 packageName= Signature.getQualifier(name).toCharArray();
250                 typeName= Signature.getSimpleName(name).toCharArray();
251             }
252             IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
253             ArrayList JavaDoc res= new ArrayList JavaDoc();
254             TypeNameMatchCollector requestor= new TypeNameMatchCollector(res);
255             int matchMode= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
256             new SearchEngine().searchAllTypeNames(packageName, matchMode, typeName,
257                     matchMode, IJavaSearchConstants.TYPE, scope, requestor,
258                     IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
259
260             if (res.isEmpty()) {
261                 return;
262             }
263             HashSet JavaDoc addedClaspaths= new HashSet JavaDoc();
264             for (int i= 0; i < res.size(); i++) {
265                 TypeNameMatch curr= (TypeNameMatch) res.get(i);
266                 IType type= curr.getType();
267                 if (type != null) {
268                     IPackageFragmentRoot root= (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
269                     IClasspathEntry entry= root.getRawClasspathEntry();
270                     if (entry == null) {
271                         continue;
272                     }
273                     IJavaProject other= root.getJavaProject();
274                     int entryKind= entry.getEntryKind();
275                     if ((entry.isExported() || entryKind == IClasspathEntry.CPE_SOURCE) && addedClaspaths.add(other)) {
276                         String JavaDoc[] args= { other.getElementName(), project.getElementName() };
277                         String JavaDoc label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_project_description, args);
278                         IClasspathEntry newEntry= JavaCore.newProjectEntry(other.getPath());
279                         AddToClasspathChange change= new AddToClasspathChange(project, newEntry);
280                         if (change.validateClasspath()) {
281                             ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(label, change, 8, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
282                             proposals.add(proposal);
283                         }
284                     }
285                     if ((entryKind == IClasspathEntry.CPE_LIBRARY || entryKind == IClasspathEntry.CPE_VARIABLE || entryKind == IClasspathEntry.CPE_CONTAINER) && addedClaspaths.add(entry)) {
286                         String JavaDoc label= getAddClasspathLabel(entry, root, project);
287                         if (label != null) {
288                             AddToClasspathChange change= new AddToClasspathChange(project, entry);
289                             if (change.validateClasspath()) {
290                                 ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(label, change, 7, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
291                                 proposals.add(proposal);
292                             }
293                         }
294                     }
295                 }
296             }
297         }
298     }
299
300     private static String JavaDoc getAddClasspathLabel(IClasspathEntry entry, IPackageFragmentRoot root, IJavaProject project) {
301         switch (entry.getEntryKind()) {
302             case IClasspathEntry.CPE_LIBRARY:
303                 if (root.isArchive()) {
304                     String JavaDoc[] args= { JavaElementLabels.getElementLabel(root, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED), project.getElementName() };
305                     return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_archive_description, args);
306                 } else {
307                     String JavaDoc[] args= { JavaElementLabels.getElementLabel(root, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED), project.getElementName() };
308                     return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_classfolder_description, args);
309                 }
310             case IClasspathEntry.CPE_VARIABLE: {
311                     String JavaDoc[] args= { JavaElementLabels.getElementLabel(root, 0), project.getElementName() };
312                     return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_variable_description, args);
313                 }
314             case IClasspathEntry.CPE_CONTAINER:
315                 try {
316                     String JavaDoc[] args= { JavaElementLabels.getContainerEntryLabel(entry.getPath(), root.getJavaProject()), project.getElementName() };
317                     return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_library_description, args);
318                 } catch (JavaModelException e) {
319                     // ignore
320
}
321                 break;
322             }
323         return null;
324     }
325
326     private static final class OpenBuildPathCorrectionProposal extends ChangeCorrectionProposal {
327         private final IProject fProject;
328         private final IBinding fReferencedType;
329         private OpenBuildPathCorrectionProposal(IProject project, String JavaDoc label, int relevance, IBinding referencedType) {
330             super(label, null, relevance, null);
331             fProject= project;
332             fReferencedType= referencedType;
333             setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ACCESSRULES_ATTRIB));
334         }
335         public void apply(IDocument document) {
336             Map JavaDoc data= null;
337             if (fReferencedType != null) {
338                 IJavaElement elem= fReferencedType.getJavaElement();
339                 if (elem != null) {
340                     IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
341                     if (root != null) {
342                         try {
343                             IClasspathEntry entry= root.getRawClasspathEntry();
344                             if (entry != null) {
345                                 data= new HashMap JavaDoc(1);
346                                 data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, entry);
347                                 if (entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
348                                     data.put(BuildPathsPropertyPage.DATA_REVEAL_ATTRIBUTE_KEY, CPListElement.ACCESSRULES);
349                                 }
350                             }
351                         } catch (JavaModelException e) {
352                             // ignore
353
}
354                     }
355                 }
356             }
357             PreferencesUtil.createPropertyDialogOn(JavaPlugin.getActiveWorkbenchShell(), fProject, BuildPathsPropertyPage.PROP_ID, null, data).open();
358         }
359         public String JavaDoc getAdditionalProposalInfo() {
360             return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_description, fProject.getName());
361         }
362     }
363
364     private static final class ChangeTo50Compliance extends ChangeCorrectionProposal implements IWorkspaceRunnable {
365         
366         private final IJavaProject fProject;
367         private final boolean fChangeOnWorkspace;
368         
369         private Job fUpdateJob;
370         private boolean f50JREFound;
371         
372         public ChangeTo50Compliance(String JavaDoc name, IJavaProject project, boolean changeOnWorkspace, int relevance) {
373             super(name, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
374             fProject= project;
375             fChangeOnWorkspace= changeOnWorkspace;
376             fUpdateJob= null;
377             f50JREFound= false;
378         }
379         
380         private boolean is50orGreaterVMInstall(IVMInstall install) {
381             if (install instanceof IVMInstall2) {
382                 String JavaDoc compliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) install, JavaCore.VERSION_1_3);
383                 return JavaModelUtil.is50OrHigher(compliance);
384             }
385             return false;
386         }
387         
388         private IVMInstall find50OrGreaterVMInstall() {
389             IVMInstallType[] installTypes= JavaRuntime.getVMInstallTypes();
390             for (int i= 0; i < installTypes.length; i++) {
391                 IVMInstall[] installs= installTypes[i].getVMInstalls();
392                 for (int k= 0; k < installs.length; k++) {
393                     if (is50orGreaterVMInstall(installs[k])) {
394                         return installs[k];
395                     }
396                 }
397             }
398             return null;
399         }
400         
401         public void run(IProgressMonitor monitor) throws CoreException {
402             boolean needsBuild= updateJRE(monitor);
403             if (needsBuild) {
404                 fUpdateJob= CoreUtility.getBuildJob(fChangeOnWorkspace ? null : fProject.getProject());
405             }
406         }
407         
408         private boolean updateJRE( IProgressMonitor monitor) throws CoreException, JavaModelException {
409             try {
410                 IVMInstall vm50Install= find50OrGreaterVMInstall();
411                 f50JREFound= vm50Install != null;
412                 if (vm50Install != null) {
413                     IVMInstall install= JavaRuntime.getVMInstall(fProject); // can be null
414
if (fChangeOnWorkspace) {
415                         monitor.beginTask(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_operation, 4);
416                         IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall(); // can be null
417
if (defaultVM != null && !defaultVM.equals(install)) {
418                             IPath newPath= new Path(JavaRuntime.JRE_CONTAINER);
419                             updateClasspath(newPath, new SubProgressMonitor(monitor, 1));
420                         } else {
421                             monitor.worked(1);
422                         }
423                         if (defaultVM == null || !is50orGreaterVMInstall(defaultVM)) {
424                             JavaRuntime.setDefaultVMInstall(vm50Install, new SubProgressMonitor(monitor, 3), true);
425                             return false;
426                         }
427                         return true;
428                     } else {
429                         if (install == null || !is50orGreaterVMInstall(install)) {
430                             IPath newPath= new Path(JavaRuntime.JRE_CONTAINER).append(vm50Install.getVMInstallType().getId()).append(vm50Install.getName());
431                             updateClasspath(newPath, monitor);
432                             return false;
433                         }
434                     }
435                 }
436             } finally {
437                 monitor.done();
438             }
439             return true;
440         }
441
442         private void updateClasspath(IPath newPath, IProgressMonitor monitor) throws JavaModelException {
443             IClasspathEntry[] classpath= fProject.getRawClasspath();
444             IPath jreContainerPath= new Path(JavaRuntime.JRE_CONTAINER);
445             for (int i= 0; i < classpath.length; i++) {
446                 IClasspathEntry curr= classpath[i];
447                 if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER && curr.getPath().matchingFirstSegments(jreContainerPath) > 0) {
448                     classpath[i]= JavaCore.newContainerEntry(newPath, curr.getAccessRules(), curr.getExtraAttributes(), curr.isExported());
449                 }
450             }
451             fProject.setRawClasspath(classpath, monitor);
452         }
453         
454         /* (non-Javadoc)
455          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
456          */

457         public String JavaDoc getAdditionalProposalInfo() {
458             StringBuffer JavaDoc message= new StringBuffer JavaDoc();
459             if (fChangeOnWorkspace) {
460                 message.append(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeworkspace_description);
461             } else {
462                 message.append(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeproject_description);
463             }
464             
465             IVMInstall vm50Install= find50OrGreaterVMInstall();
466             if (vm50Install != null) {
467                 try {
468                     IVMInstall install= JavaRuntime.getVMInstall(fProject); // can be null
469
if (fChangeOnWorkspace) {
470                         IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall(); // can be null
471
if (defaultVM != null && !defaultVM.equals(install)) {
472                             message.append(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeProjectJREToDefault_description);
473                         }
474                         if (defaultVM == null || !is50orGreaterVMInstall(defaultVM)) {
475                             message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeWorkspaceJRE_description, vm50Install.getName()));
476                         }
477                     } else {
478                         if (install == null || !is50orGreaterVMInstall(install)) {
479                             message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeProjectJRE_description, vm50Install.getName()));
480                         }
481                     }
482                 } catch (CoreException e) {
483                     // ignore
484
}
485             }
486             return message.toString();
487         }
488         
489         /* (non-Javadoc)
490          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(IDocument)
491          */

492         public void apply(IDocument document) {
493             if (fChangeOnWorkspace) {
494                 Hashtable JavaDoc map= JavaCore.getOptions();
495                 JavaModelUtil.set50CompilanceOptions(map);
496                 JavaCore.setOptions(map);
497             } else {
498                 Map JavaDoc map= fProject.getOptions(false);
499                 int optionsCount= map.size();
500                 JavaModelUtil.set50CompilanceOptions(map);
501                 if (map.size() > optionsCount) {
502                     // options have been added -> ensure that all compliance options from preference page set
503
JavaModelUtil.setDefaultClassfileOptions(map, JavaCore.VERSION_1_5);
504                 }
505                 fProject.setOptions(map);
506             }
507             try {
508                 IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
509                 progressService.run(true, true, new WorkbenchRunnableAdapter(this));
510             } catch (InvocationTargetException JavaDoc e) {
511                 JavaPlugin.log(e);
512             } catch (InterruptedException JavaDoc e) {
513                 return;
514             }
515             
516             if (fUpdateJob != null) {
517                 fUpdateJob.schedule();
518             }
519             
520             if (!f50JREFound) {
521                 MessageDialog.openInformation(JavaPlugin.getActiveWorkbenchShell(), CorrectionMessages.ReorgCorrectionsSubProcessor_no_50jre_title, CorrectionMessages.ReorgCorrectionsSubProcessor_no_50jre_message);
522             }
523         }
524     }
525
526     public static void getNeed50ComplianceProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) {
527         IJavaProject project= context.getCompilationUnit().getJavaProject();
528         
529         String JavaDoc label1= CorrectionMessages.ReorgCorrectionsSubProcessor_50_project_compliance_description;
530         proposals.add(new ChangeTo50Compliance(label1, project, false, 5));
531
532         if (project.getOption(JavaCore.COMPILER_COMPLIANCE, false) == null) {
533             String JavaDoc label2= CorrectionMessages.ReorgCorrectionsSubProcessor_50_workspace_compliance_description;
534             proposals.add(new ChangeTo50Compliance(label2, project, true, 6));
535         }
536     }
537
538     public static void getIncorrectBuildPathProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) {
539         IProject project= context.getCompilationUnit().getJavaProject().getProject();
540         String JavaDoc label= CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_label;
541         OpenBuildPathCorrectionProposal proposal= new OpenBuildPathCorrectionProposal(project, label, 5, null);
542         proposals.add(proposal);
543     }
544
545     public static void getAccessRulesProposals(IInvocationContext context, IProblemLocation problem, Collection JavaDoc proposals) {
546         IBinding referencedElement= null;
547         ASTNode node= problem.getCoveredNode(context.getASTRoot());
548         if (node instanceof Type) {
549             referencedElement= ((Type) node).resolveBinding();
550         } else if (node instanceof Name) {
551             referencedElement= ((Name) node).resolveBinding();
552         }
553         IProject project= context.getCompilationUnit().getJavaProject().getProject();
554         String JavaDoc label= CorrectionMessages.ReorgCorrectionsSubProcessor_accessrules_description;
555         OpenBuildPathCorrectionProposal proposal= new OpenBuildPathCorrectionProposal(project, label, 5, referencedElement);
556         proposals.add(proposal);
557     }
558 }
559
Popular Tags