PageRenderTime 36ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/plug-ins/helios/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/hover/ProblemHover.java

https://github.com/vazexqi/CodingSpectator
Java | 304 lines | 216 code | 55 blank | 33 comment | 50 complexity | 074c5d9fec49bada61711a49e2aaa167 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 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.text.java.hover;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import org.eclipse.swt.SWT;
  17. import org.eclipse.swt.widgets.Shell;
  18. import org.eclipse.core.resources.IFile;
  19. import org.eclipse.core.resources.IMarker;
  20. import org.eclipse.core.resources.IResource;
  21. import org.eclipse.jface.action.Action;
  22. import org.eclipse.jface.action.ToolBarManager;
  23. import org.eclipse.jface.dialogs.MessageDialog;
  24. import org.eclipse.jface.text.IInformationControl;
  25. import org.eclipse.jface.text.ITextViewer;
  26. import org.eclipse.jface.text.Position;
  27. import org.eclipse.jface.text.contentassist.ICompletionProposal;
  28. import org.eclipse.jface.text.source.Annotation;
  29. import org.eclipse.jface.text.source.IAnnotationModel;
  30. import org.eclipse.jface.text.source.ISourceViewer;
  31. import org.eclipse.ui.IEditorInput;
  32. import org.eclipse.ui.PlatformUI;
  33. import org.eclipse.ui.dialogs.PreferencesUtil;
  34. import org.eclipse.ui.texteditor.MarkerAnnotation;
  35. import org.eclipse.ui.texteditor.spelling.SpellingAnnotation;
  36. import org.eclipse.jdt.core.ICompilationUnit;
  37. import org.eclipse.jdt.core.IJavaElement;
  38. import org.eclipse.jdt.core.IJavaProject;
  39. import org.eclipse.jdt.core.JavaCore;
  40. import org.eclipse.jdt.core.compiler.IProblem;
  41. import org.eclipse.jdt.internal.corext.util.Messages;
  42. import org.eclipse.jdt.ui.JavaElementLabels;
  43. import org.eclipse.jdt.ui.JavaUI;
  44. import org.eclipse.jdt.ui.SharedASTProvider;
  45. import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
  46. import org.eclipse.jdt.ui.text.java.IInvocationContext;
  47. import org.eclipse.jdt.ui.text.java.IProblemLocation;
  48. import org.eclipse.jdt.internal.ui.JavaPluginImages;
  49. import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
  50. import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
  51. import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation;
  52. import org.eclipse.jdt.internal.ui.preferences.JavadocProblemsConfigurationBlock;
  53. import org.eclipse.jdt.internal.ui.preferences.JavadocProblemsPreferencePage;
  54. import org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock;
  55. import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesConfigurationBlock;
  56. import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesPreferencePage;
  57. import org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock.Key;
  58. import org.eclipse.jdt.internal.ui.text.correction.AssistContext;
  59. import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
  60. import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
  61. /**
  62. * This annotation hover shows the description of the
  63. * selected java annotation.
  64. *
  65. * XXX: Currently this problem hover only works for Java and spelling problems,
  66. * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=62081
  67. *
  68. * @since 3.0
  69. */
  70. public class ProblemHover extends AbstractAnnotationHover {
  71. /**
  72. * Action to configure the problem severity of a compiler option.
  73. *
  74. * @since 3.4
  75. */
  76. private static final class ConfigureProblemSeverityAction extends Action {
  77. private static final String CONFIGURE_PROBLEM_SEVERITY_DIALOG_ID= "configure_problem_severity_dialog_id"; //$NON-NLS-1$
  78. private final IJavaProject fProject;
  79. private final String fOptionId;
  80. private final boolean fIsJavadocOption;
  81. private final IInformationControl fInfoControl;
  82. public ConfigureProblemSeverityAction(IJavaProject project, String optionId, boolean isJavadocOption, IInformationControl infoControl) {
  83. super();
  84. fProject= project;
  85. fOptionId= optionId;
  86. fIsJavadocOption= isJavadocOption;
  87. fInfoControl= infoControl;
  88. setImageDescriptor(JavaPluginImages.DESC_ELCL_CONFIGURE_PROBLEM_SEVERITIES);
  89. setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CONFIGURE_PROBLEM_SEVERITIES);
  90. setToolTipText(JavaHoverMessages.ProblemHover_action_configureProblemSeverity);
  91. }
  92. /* (non-Javadoc)
  93. * @see org.eclipse.jface.action.Action#run()
  94. */
  95. public void run() {
  96. boolean showPropertyPage;
  97. Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
  98. if (! hasProjectSpecificOptions()) {
  99. String message= Messages.format(
  100. JavaHoverMessages.ProblemHover_chooseSettingsTypeDialog_message,
  101. new Object[] { JavaElementLabels.getElementLabel(fProject, JavaElementLabels.ALL_DEFAULT) });
  102. String[] buttons= new String[] {
  103. JavaHoverMessages.ProblemHover_chooseSettingsTypeDialog_button_project,
  104. JavaHoverMessages.ProblemHover_chooseSettingsTypeDialog_button_workspace,
  105. JavaHoverMessages.ProblemHover_chooseSettingsTypeDialog_button_cancel };
  106. int result= OptionalMessageDialog.open(
  107. CONFIGURE_PROBLEM_SEVERITY_DIALOG_ID, shell, JavaHoverMessages.ProblemHover_chooseSettingsTypeDialog_title, null, message, MessageDialog.QUESTION, buttons, 0,
  108. JavaHoverMessages.ProblemHover_chooseSettingsTypeDialog_checkBox_dontShowAgain);
  109. if (result == OptionalMessageDialog.NOT_SHOWN) {
  110. showPropertyPage= false;
  111. } else if (result == 2 || result == SWT.DEFAULT) {
  112. return;
  113. } else if (result == 0) {
  114. showPropertyPage= true;
  115. } else {
  116. showPropertyPage= false;
  117. }
  118. } else {
  119. showPropertyPage= true;
  120. }
  121. Map data= new HashMap();
  122. String pageId;
  123. if (fIsJavadocOption) {
  124. if (showPropertyPage) {
  125. pageId= JavadocProblemsPreferencePage.PROP_ID;
  126. data.put(JavadocProblemsPreferencePage.DATA_USE_PROJECT_SPECIFIC_OPTIONS, Boolean.TRUE);
  127. } else {
  128. pageId= JavadocProblemsPreferencePage.PREF_ID;
  129. }
  130. data.put(JavadocProblemsPreferencePage.DATA_SELECT_OPTION_KEY, fOptionId);
  131. data.put(JavadocProblemsPreferencePage.DATA_SELECT_OPTION_QUALIFIER, JavaCore.PLUGIN_ID);
  132. } else {
  133. if (showPropertyPage) {
  134. pageId= ProblemSeveritiesPreferencePage.PROP_ID;
  135. data.put(ProblemSeveritiesPreferencePage.USE_PROJECT_SPECIFIC_OPTIONS, Boolean.TRUE);
  136. } else {
  137. pageId= ProblemSeveritiesPreferencePage.PREF_ID;
  138. }
  139. data.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_KEY, fOptionId);
  140. data.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_QUALIFIER, JavaCore.PLUGIN_ID);
  141. }
  142. fInfoControl.dispose(); //FIXME: should have protocol to hide, rather than dispose
  143. if (showPropertyPage) {
  144. PreferencesUtil.createPropertyDialogOn(shell, fProject, pageId, null, data).open();
  145. } else {
  146. PreferencesUtil.createPreferenceDialogOn(shell, pageId, null, data).open();
  147. }
  148. }
  149. private boolean hasProjectSpecificOptions() {
  150. Key[] keys= fIsJavadocOption ? JavadocProblemsConfigurationBlock.getKeys() : ProblemSeveritiesConfigurationBlock.getKeys();
  151. return OptionsConfigurationBlock.hasProjectSpecificOptions(fProject.getProject(), keys, null);
  152. }
  153. }
  154. protected static class ProblemInfo extends AnnotationInfo {
  155. private static final ICompletionProposal[] NO_PROPOSALS= new ICompletionProposal[0];
  156. public ProblemInfo(Annotation annotation, Position position, ITextViewer textViewer) {
  157. super(annotation, position, textViewer);
  158. }
  159. /*
  160. * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover.AnnotationInfo#getCompletionProposals()
  161. */
  162. public ICompletionProposal[] getCompletionProposals() {
  163. if (annotation instanceof IJavaAnnotation) {
  164. ICompletionProposal[] result= getJavaAnnotationFixes((IJavaAnnotation) annotation);
  165. if (result.length > 0)
  166. return result;
  167. }
  168. if (annotation instanceof MarkerAnnotation)
  169. return getMarkerAnnotationFixes((MarkerAnnotation) annotation);
  170. return NO_PROPOSALS;
  171. }
  172. private ICompletionProposal[] getJavaAnnotationFixes(IJavaAnnotation javaAnnotation) {
  173. ProblemLocation location= new ProblemLocation(position.getOffset(), position.getLength(), javaAnnotation);
  174. ICompilationUnit cu= javaAnnotation.getCompilationUnit();
  175. if (cu == null)
  176. return NO_PROPOSALS;
  177. ISourceViewer sourceViewer= null;
  178. if (viewer instanceof ISourceViewer)
  179. sourceViewer= (ISourceViewer) viewer;
  180. IInvocationContext context= new AssistContext(cu, sourceViewer, location.getOffset(), location.getLength(), SharedASTProvider.WAIT_ACTIVE_ONLY);
  181. if (!SpellingAnnotation.TYPE.equals(javaAnnotation.getType()) && !hasProblem(context.getASTRoot().getProblems(), location))
  182. return NO_PROPOSALS;
  183. ArrayList proposals= new ArrayList();
  184. JavaCorrectionProcessor.collectCorrections(context, new IProblemLocation[] { location }, proposals);
  185. Collections.sort(proposals, new CompletionProposalComparator());
  186. return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
  187. }
  188. private static boolean hasProblem(IProblem[] problems, IProblemLocation location) {
  189. for (int i= 0; i < problems.length; i++) {
  190. IProblem problem= problems[i];
  191. if (problem.getID() == location.getProblemId() && problem.getSourceStart() == location.getOffset())
  192. return true;
  193. }
  194. return false;
  195. }
  196. private ICompletionProposal[] getMarkerAnnotationFixes(MarkerAnnotation markerAnnotation) {
  197. if (markerAnnotation.isQuickFixableStateSet() && !markerAnnotation.isQuickFixable())
  198. return NO_PROPOSALS;
  199. IMarker marker= markerAnnotation.getMarker();
  200. ICompilationUnit cu= getCompilationUnit(marker);
  201. if (cu == null)
  202. return NO_PROPOSALS;
  203. IEditorInput input= EditorUtility.getEditorInput(cu);
  204. if (input == null)
  205. return NO_PROPOSALS;
  206. IAnnotationModel model= JavaUI.getDocumentProvider().getAnnotationModel(input);
  207. if (model == null)
  208. return NO_PROPOSALS;
  209. ISourceViewer sourceViewer= null;
  210. if (viewer instanceof ISourceViewer)
  211. sourceViewer= (ISourceViewer) viewer;
  212. AssistContext context= new AssistContext(cu, sourceViewer, position.getOffset(), position.getLength());
  213. ArrayList proposals= new ArrayList();
  214. JavaCorrectionProcessor.collectProposals(context, model, new Annotation[] { markerAnnotation }, true, false, proposals);
  215. return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
  216. }
  217. private static ICompilationUnit getCompilationUnit(IMarker marker) {
  218. IResource res= marker.getResource();
  219. if (res instanceof IFile && res.isAccessible()) {
  220. IJavaElement element= JavaCore.create((IFile) res);
  221. if (element instanceof ICompilationUnit)
  222. return (ICompilationUnit) element;
  223. }
  224. return null;
  225. }
  226. /*
  227. * @see org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover.AnnotationInfo#fillToolBar(org.eclipse.jface.action.ToolBarManager)
  228. */
  229. public void fillToolBar(ToolBarManager manager, IInformationControl infoControl) {
  230. super.fillToolBar(manager, infoControl);
  231. if (!(annotation instanceof IJavaAnnotation))
  232. return;
  233. IJavaAnnotation javaAnnotation= (IJavaAnnotation) annotation;
  234. String optionId= JavaCore.getOptionForConfigurableSeverity(javaAnnotation.getId());
  235. if (optionId != null) {
  236. IJavaProject javaProject= javaAnnotation.getCompilationUnit().getJavaProject();
  237. boolean isJavadocProblem= (javaAnnotation.getId() & IProblem.Javadoc) != 0;
  238. ConfigureProblemSeverityAction problemSeverityAction= new ConfigureProblemSeverityAction(javaProject, optionId, isJavadocProblem, infoControl);
  239. manager.add(problemSeverityAction);
  240. }
  241. }
  242. }
  243. public ProblemHover() {
  244. super(false);
  245. }
  246. protected AnnotationInfo createAnnotationInfo(Annotation annotation, Position position, ITextViewer textViewer) {
  247. return new ProblemInfo(annotation, position, textViewer);
  248. }
  249. }