]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.javaeditor; | |
12 | ||
13 | import org.eclipse.swt.graphics.Image; | |
14 | ||
15 | import org.eclipse.jface.resource.ImageDescriptor; | |
16 | ||
17 | import org.eclipse.jface.text.source.Annotation; | |
18 | ||
19 | import org.eclipse.ui.texteditor.IAnnotationImageProvider; | |
20 | ||
21 | import org.eclipse.jdt.ui.PreferenceConstants; | |
22 | ||
23 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
24 | import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor; | |
25 | ||
26 | /** | |
27 | * Image provider for annotations based on Java problem markers. | |
28 | * | |
29 | * @since 3.0 | |
30 | */ | |
31 | public class JavaAnnotationImageProvider implements IAnnotationImageProvider { | |
32 | ||
33 | private final static int NO_IMAGE= 0; | |
34 | private final static int GRAY_IMAGE= 1; | |
35 | private final static int OVERLAY_IMAGE= 2; | |
36 | private final static int QUICKFIX_IMAGE= 3; | |
37 | private final static int QUICKFIX_ERROR_IMAGE= 4; | |
38 | ||
39 | ||
40 | private static Image fgQuickFixImage; | |
41 | private static Image fgQuickFixErrorImage; | |
42 | private boolean fShowQuickFixIcon; | |
43 | private int fCachedImageType; | |
44 | private Image fCachedImage; | |
45 | ||
46 | ||
47 | public JavaAnnotationImageProvider() { | |
48 | fShowQuickFixIcon= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION); | |
49 | } | |
50 | ||
51 | /* | |
52 | * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getManagedImage(org.eclipse.jface.text.source.Annotation) | |
53 | */ | |
54 | public Image getManagedImage(Annotation annotation) { | |
55 | if (annotation instanceof IJavaAnnotation) { | |
56 | IJavaAnnotation javaAnnotation= (IJavaAnnotation) annotation; | |
57 | int imageType= getImageType(javaAnnotation); | |
58 | return getImage(javaAnnotation, imageType); | |
59 | } | |
60 | return null; | |
61 | } | |
62 | ||
63 | /* | |
64 | * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptorId(org.eclipse.jface.text.source.Annotation) | |
65 | */ | |
66 | public String getImageDescriptorId(Annotation annotation) { | |
67 | // unmanaged images are not supported | |
68 | return null; | |
69 | } | |
70 | ||
71 | /* | |
72 | * @see org.eclipse.jface.text.source.IAnnotationImageProvider#getImageDescriptor(java.lang.String) | |
73 | */ | |
74 | public ImageDescriptor getImageDescriptor(String symbolicName) { | |
75 | // unmanaged images are not supported | |
76 | return null; | |
77 | } | |
78 | ||
79 | ||
80 | private boolean showQuickFix(IJavaAnnotation annotation) { | |
81 | return fShowQuickFixIcon && annotation.isProblem() && JavaCorrectionProcessor.hasCorrections((Annotation) annotation); | |
82 | } | |
83 | ||
84 | private Image getQuickFixImage() { | |
85 | if (fgQuickFixImage == null) | |
86 | fgQuickFixImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_PROBLEM); | |
87 | return fgQuickFixImage; | |
88 | } | |
89 | ||
90 | private Image getQuickFixErrorImage() { | |
91 | if (fgQuickFixErrorImage == null) | |
92 | fgQuickFixErrorImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR); | |
93 | return fgQuickFixErrorImage; | |
94 | } | |
95 | ||
96 | private int getImageType(IJavaAnnotation annotation) { | |
97 | int imageType= NO_IMAGE; | |
98 | if (annotation.hasOverlay()) | |
99 | imageType= OVERLAY_IMAGE; | |
100 | else if (!annotation.isMarkedDeleted()) { | |
101 | if (showQuickFix(annotation)) | |
102 | imageType= JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotation.getType()) ? QUICKFIX_ERROR_IMAGE : QUICKFIX_IMAGE; | |
103 | } else { | |
104 | imageType= GRAY_IMAGE; | |
105 | } | |
106 | return imageType; | |
107 | } | |
108 | ||
109 | private Image getImage(IJavaAnnotation annotation, int imageType) { | |
110 | if ((imageType == QUICKFIX_IMAGE || imageType == QUICKFIX_ERROR_IMAGE) && fCachedImageType == imageType) | |
111 | return fCachedImage; | |
112 | ||
113 | Image image= null; | |
114 | switch (imageType) { | |
115 | case OVERLAY_IMAGE: | |
116 | IJavaAnnotation overlay= annotation.getOverlay(); | |
117 | image= getManagedImage((Annotation) overlay); | |
118 | fCachedImageType= -1; | |
119 | break; | |
120 | case QUICKFIX_IMAGE: | |
121 | image= getQuickFixImage(); | |
122 | fCachedImageType= imageType; | |
123 | fCachedImage= image; | |
124 | break; | |
125 | case QUICKFIX_ERROR_IMAGE: | |
126 | image= getQuickFixErrorImage(); | |
127 | fCachedImageType= imageType; | |
128 | fCachedImage= image; | |
129 | break; | |
130 | case GRAY_IMAGE: { | |
131 | String annotationType= annotation.getType(); | |
132 | if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotationType)) { | |
133 | image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ERROR_ALT); | |
134 | } else if (JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(annotationType)) { | |
135 | image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_WARNING_ALT); | |
136 | } else if (JavaMarkerAnnotation.INFO_ANNOTATION_TYPE.equals(annotationType)) { | |
137 | image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_INFO_ALT); | |
138 | } | |
139 | fCachedImageType= -1; | |
140 | break; | |
141 | } | |
142 | } | |
143 | ||
144 | return image; | |
145 | } | |
146 | } |