Skip to content

Commit b2520ad

Browse files
committed
Use PropertyDescriptorCache as the central instance for
providing annotation images for PMD's marker
1 parent b7385db commit b2520ad

File tree

13 files changed

+138
-193
lines changed

13 files changed

+138
-193
lines changed

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/UISettings.java

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
package net.sourceforge.pmd.eclipse.plugin;
66

7-
import java.net.MalformedURLException;
8-
import java.net.URL;
97
import java.util.ArrayList;
108
import java.util.Arrays;
119
import java.util.Comparator;
@@ -23,9 +21,7 @@
2321
import org.eclipse.swt.widgets.Display;
2422

2523
import net.sourceforge.pmd.RulePriority;
26-
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences;
2724
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferencesManager;
28-
import net.sourceforge.pmd.eclipse.ui.PMDMarkerImageProvider;
2925
import net.sourceforge.pmd.eclipse.ui.Shape;
3026
import net.sourceforge.pmd.eclipse.ui.ShapeDescriptor;
3127
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
@@ -46,32 +42,16 @@ private UISettings() {
4642

4743
private static String[] priorityLabels;
4844

49-
private static Map<Object, ShapeDescriptor> shapesByPriority;
50-
private static Map<Integer, RulePriority> prioritiesByIntValue;
5145
private static Map<RulePriority, String> labelsByPriority = new HashMap<RulePriority, String>();
5246

5347
private static final int MAX_MARKER_DIMENSION = 9;
5448
private static IPreferencesManager preferencesManager = PMDPlugin.getDefault().getPreferencesManager();
55-
private static final Map<RulePriority, PriorityDescriptor> UI_DESCRIPTORS_BY_PRIORITY = new HashMap<RulePriority, PriorityDescriptor>(
56-
5);
5749

5850
public static final FontBuilder CODE_FONT_BUILDER = new FontBuilder("Courier", 11, SWT.NORMAL);
5951

52+
@Deprecated
6053
public static void reloadPriorities() {
61-
UI_DESCRIPTORS_BY_PRIORITY.clear();
62-
uiDescriptorsByPriority(); // cause a reload
63-
}
64-
65-
private static Map<RulePriority, PriorityDescriptor> uiDescriptorsByPriority() {
66-
67-
if (UI_DESCRIPTORS_BY_PRIORITY.isEmpty()) {
68-
IPreferences preferences = preferencesManager.loadPreferences();
69-
for (RulePriority rp : currentPriorities(true)) {
70-
UI_DESCRIPTORS_BY_PRIORITY.put(rp, preferences.getPriorityDescriptor(rp));
71-
}
72-
}
73-
74-
return UI_DESCRIPTORS_BY_PRIORITY;
54+
// no-op - nothing to be done anymore. The priority descriptor are cached by PriorityDescriptorCache and not here.
7555
}
7656

7757
public static Shape[] allShapes() {
@@ -104,34 +84,37 @@ public static Map<Shape, ShapeDescriptor> shapeSet(RGB color, int size) {
10484
return shapes;
10585
}
10686

87+
/**
88+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor}.
89+
*/
90+
@Deprecated
10791
public static String markerFilenameFor(RulePriority priority) {
10892
String fileDir = PMDPlugin.getPluginFolder().getAbsolutePath();
10993
return fileDir + "/" + relativeMarkerFilenameFor(priority);
11094
}
11195

96+
/**
97+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor}.
98+
*/
99+
@Deprecated
112100
public static String relativeMarkerFilenameFor(RulePriority priority) {
113101
return "icons/markerP" + priority.getPriority() + ".png";
114102
}
115103

116-
private static ImageDescriptor getImageDescriptor(final String fileName) {
117-
118-
URL installURL = PMDPlugin.getDefault().getBundle().getEntry("/");
119-
try {
120-
URL url = new URL(installURL, fileName);
121-
return ImageDescriptor.createFromURL(url);
122-
} catch (MalformedURLException mue) {
123-
mue.printStackTrace();
124-
return null;
125-
}
126-
}
127-
104+
/**
105+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor}.
106+
*/
107+
@Deprecated
128108
public static ImageDescriptor markerDescriptorFor(RulePriority priority) {
129-
String path = relativeMarkerFilenameFor(priority);
130-
return getImageDescriptor(path);
109+
PriorityDescriptor pd = PriorityDescriptorCache.INSTANCE.descriptorFor(priority);
110+
return pd.getAnnotationImageDescriptor();
131111
}
132112

113+
/**
114+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor}.
115+
*/
116+
@Deprecated
133117
public static Map<Integer, ImageDescriptor> markerImgDescriptorsByPriority() {
134-
135118
RulePriority[] priorities = currentPriorities(true);
136119
Map<Integer, ImageDescriptor> overlaysByPriority = new HashMap<Integer, ImageDescriptor>(priorities.length);
137120
for (RulePriority priority : priorities) {
@@ -140,21 +123,24 @@ public static Map<Integer, ImageDescriptor> markerImgDescriptorsByPriority() {
140123
return overlaysByPriority;
141124
}
142125

126+
/**
127+
* Marker Icons are not stored to files anymore. They are generated on the fly and cached by {@link PriorityDescriptorCache}.
128+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor}.
129+
*/
130+
@Deprecated
143131
public static void createRuleMarkerIcons(Display display) {
144-
145132
ImageLoader loader = new ImageLoader();
146133

147134
PriorityDescriptorCache pdc = PriorityDescriptorCache.INSTANCE;
148135

149136
for (RulePriority priority : currentPriorities(true)) {
150-
Image image = pdc.descriptorFor(priority).getImage(display, MAX_MARKER_DIMENSION);
137+
Image image = pdc.descriptorFor(priority).getAnnotationImage();
151138
loader.data = new ImageData[] { image.getImageData() };
152139
String fullPath = markerFilenameFor(priority);
153140
PMDPlugin.getDefault().logInformation("Writing marker icon to: " + fullPath);
154141
loader.save(fullPath, SWT.IMAGE_PNG);
155142
image.dispose();
156143
}
157-
PMDMarkerImageProvider.removeCachedImages();
158144
}
159145

160146
private static String pLabelFor(RulePriority priority, boolean useCustom) {
@@ -176,12 +162,20 @@ public static void useCustomPriorityLabels(boolean flag) {
176162
}
177163
}
178164

165+
/**
166+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor#description}.
167+
*/
168+
@Deprecated
179169
public static String descriptionFor(RulePriority priority) {
180170
return descriptorFor(priority).description;
181171
}
182172

173+
/**
174+
* @deprecated Use {@link PriorityDescriptorCache#descriptorFor(RulePriority)} to retrieve the {@link PriorityDescriptor}.
175+
*/
176+
@Deprecated
183177
public static PriorityDescriptor descriptorFor(RulePriority priority) {
184-
return uiDescriptorsByPriority().get(priority);
178+
return PriorityDescriptorCache.INSTANCE.descriptorFor(priority);
185179
}
186180

187181
public static String labelFor(RulePriority priority) {
@@ -192,36 +186,27 @@ public static String labelFor(RulePriority priority) {
192186
}
193187

194188
public static Map<Object, ShapeDescriptor> shapesByPriority() {
195-
196-
if (shapesByPriority != null) {
197-
return shapesByPriority;
198-
}
199-
200-
Map<Object, ShapeDescriptor> shapesByPriority = new HashMap<Object, ShapeDescriptor>(
201-
uiDescriptorsByPriority().size());
202-
for (Map.Entry<RulePriority, PriorityDescriptor> entry : uiDescriptorsByPriority().entrySet()) {
203-
shapesByPriority.put(entry.getKey(), entry.getValue().shape);
189+
Map<Object, ShapeDescriptor> shapesByPriority = new HashMap<Object, ShapeDescriptor>(RulePriority.values().length);
190+
for (RulePriority priority : RulePriority.values()) {
191+
shapesByPriority.put(priority, PriorityDescriptorCache.INSTANCE.descriptorFor(priority).shape);
204192
}
205-
206193
return shapesByPriority;
207194
}
208195

196+
/**
197+
* @deprecated Use {@link RulePriority#valueOf(int)} directly.
198+
*/
199+
@Deprecated
209200
public static RulePriority priorityFor(int value) {
210-
211-
if (prioritiesByIntValue == null) {
212-
prioritiesByIntValue = new HashMap<Integer, RulePriority>(uiDescriptorsByPriority().size());
213-
for (Map.Entry<RulePriority, PriorityDescriptor> entry : uiDescriptorsByPriority().entrySet()) {
214-
prioritiesByIntValue.put(entry.getKey().getPriority(), entry.getKey());
215-
}
216-
}
217-
return prioritiesByIntValue.get(value);
201+
return RulePriority.valueOf(value);
218202
}
219203

220204
/**
221205
* Return the priority labels
222206
*
223207
* @deprecated - not referenced in the modern UI
224208
*/
209+
@Deprecated
225210
public static String[] getPriorityLabels() {
226211
if (priorityLabels == null) {
227212
final StringTable stringTable = PMDPlugin.getDefault().getStringTable();

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/PMDMarkerImageProvider.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
package net.sourceforge.pmd.eclipse.ui;
66

77
import org.eclipse.jface.resource.ImageDescriptor;
8-
import org.eclipse.jface.resource.ImageRegistry;
98
import org.eclipse.jface.text.source.Annotation;
109
import org.eclipse.swt.graphics.Image;
1110
import org.eclipse.ui.texteditor.IAnnotationImageProvider;
1211

1312
import net.sourceforge.pmd.RulePriority;
14-
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
13+
import net.sourceforge.pmd.eclipse.ui.priority.PriorityDescriptorCache;
1514

1615
public class PMDMarkerImageProvider implements IAnnotationImageProvider {
1716

@@ -27,26 +26,20 @@ public String getImageDescriptorId(Annotation annotation) {
2726

2827
@Override
2928
public Image getManagedImage(Annotation annotation) {
30-
PMDPlugin plugin = PMDPlugin.getDefault();
3129
String type = annotation.getType();
30+
RulePriority priority = RulePriority.HIGH;
31+
3232
if ("net.sourceforge.pmd.eclipse.plugin.annotation.prio1".equals(type)) {
33-
return plugin.getImage(type, "icons/markerP1.png");
33+
priority = RulePriority.HIGH;
3434
} else if ("net.sourceforge.pmd.eclipse.plugin.annotation.prio2".equals(type)) {
35-
return plugin.getImage(type, "icons/markerP2.png");
35+
priority = RulePriority.MEDIUM_HIGH;
3636
} else if ("net.sourceforge.pmd.eclipse.plugin.annotation.prio3".equals(type)) {
37-
return plugin.getImage(type, "icons/markerP3.png");
37+
priority = RulePriority.MEDIUM;
3838
} else if ("net.sourceforge.pmd.eclipse.plugin.annotation.prio4".equals(type)) {
39-
return plugin.getImage(type, "icons/markerP4.png");
39+
priority = RulePriority.MEDIUM_LOW;
4040
} else if ("net.sourceforge.pmd.eclipse.plugin.annotation.prio5".equals(type)) {
41-
return plugin.getImage(type, "icons/markerP5.png");
42-
}
43-
return null;
44-
}
45-
46-
public static void removeCachedImages() {
47-
ImageRegistry imageRegistry = PMDPlugin.getDefault().getImageRegistry();
48-
for (RulePriority priority : RulePriority.values()) {
49-
imageRegistry.remove("net.sourceforge.pmd.eclipse.plugin.annotation.prio" + priority.getPriority());
41+
priority = RulePriority.LOW;
5042
}
43+
return PriorityDescriptorCache.INSTANCE.descriptorFor(priority).getAnnotationImage();
5144
}
5245
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/RuleLabelDecorator.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.Collection;
88
import java.util.HashSet;
9-
import java.util.Map;
109
import java.util.Set;
1110

1211
import org.eclipse.core.resources.IResource;
@@ -17,24 +16,19 @@
1716
import org.eclipse.jface.viewers.ILightweightLabelDecorator;
1817
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
1918

20-
import net.sourceforge.pmd.eclipse.plugin.UISettings;
19+
import net.sourceforge.pmd.RulePriority;
20+
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
2121
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
2222
import net.sourceforge.pmd.eclipse.runtime.builder.MarkerUtil;
23+
import net.sourceforge.pmd.eclipse.ui.priority.PriorityDescriptorCache;
2324

2425
/**
2526
*
2627
* @author Brian Remedios
2728
*/
2829
public class RuleLabelDecorator implements ILightweightLabelDecorator {
29-
3030
private Collection<ILabelProviderListener> listeners;
3131

32-
private Map<Integer, ImageDescriptor> overlaysByPriority;
33-
34-
public RuleLabelDecorator() {
35-
reloadDecorators();
36-
}
37-
3832
public void addListener(ILabelProviderListener listener) {
3933
if (listeners == null) {
4034
listeners = new HashSet<ILabelProviderListener>();
@@ -59,8 +53,11 @@ public void changed(Collection<IResource> resources) {
5953

6054
}
6155

56+
/**
57+
* reloading is not necessary anymore
58+
*/
59+
@Deprecated
6260
public void reloadDecorators() {
63-
overlaysByPriority = UISettings.markerImgDescriptorsByPriority();
6461
}
6562

6663
public boolean isLabelProperty(Object element, String property) {
@@ -93,18 +90,15 @@ public void decorate(Object element, IDecoration decoration) {
9390
}
9491

9592
Integer first = range.iterator().next();
96-
ImageDescriptor overlay = overlaysByPriority.get(first);
93+
ImageDescriptor overlay = PriorityDescriptorCache.INSTANCE.descriptorFor(RulePriority.valueOf(first)).getAnnotationImageDescriptor();
9794

9895
try {
9996
boolean hasMarkers = MarkerUtil.hasAnyRuleMarkers(resource);
10097
if (hasMarkers) {
10198
decoration.addOverlay(overlay);
10299
}
103100
} catch (CoreException e) {
104-
// TODO Auto-generated catch block
105-
e.printStackTrace();
101+
PMDPlugin.getDefault().logError("Error while adding overlay icon", e);
106102
}
107-
108103
}
109-
110104
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/ShapeDescriptor.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public ShapeDescriptor(Shape theShape, RGB theColor, int theSize) {
2222
size = theSize;
2323
}
2424

25-
private ShapeDescriptor() {
26-
27-
}
28-
2925
public boolean equals(Object other) {
3026
if (other == null) {
3127
return false;
@@ -47,11 +43,7 @@ public int hashCode() {
4743
}
4844

4945
public ShapeDescriptor clone() {
50-
51-
ShapeDescriptor copy = new ShapeDescriptor();
52-
copy.shape = shape;
53-
copy.rgbColor = new RGB(rgbColor.red, rgbColor.green, rgbColor.blue);
54-
copy.size = size;
46+
ShapeDescriptor copy = new ShapeDescriptor(shape, new RGB(rgbColor.red, rgbColor.green, rgbColor.blue), size);
5547
return copy;
5648
}
5749

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/GeneralPreferencesPage.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -817,18 +817,11 @@ protected void browseLogFile() {
817817
}
818818

819819
private void updateMarkerIcons() {
820-
821820
if (!PriorityDescriptorCache.INSTANCE.hasChanges()) {
822821
return;
823822
}
824823

825-
// TODO show in UI...could take a while to update
826-
827-
System.out.println("updating icons");
828-
829824
PriorityDescriptorCache.INSTANCE.storeInPreferences();
830-
UISettings.createRuleMarkerIcons(getShell().getDisplay());
831-
UISettings.reloadPriorities();
832825

833826
// ensure that the decorator gets these new images...
834827
RuleLabelDecorator decorator = PMDPlugin.getDefault().ruleLabelDecorator();
@@ -839,13 +832,12 @@ private void updateMarkerIcons() {
839832
RootRecord root = new RootRecord(ResourcesPlugin.getWorkspace().getRoot());
840833
Set<IFile> files = MarkerUtil.allMarkedFiles(root);
841834
PMDPlugin.getDefault().changedFiles(files);
842-
835+
843836
/* Refresh the views to pick up the marker change */
844837
PMDPlugin.getDefault().refreshView(PMDPlugin.VIOLATIONS_OVERVIEW_ID);
845838
PMDPlugin.getDefault().refreshView(PMDPlugin.VIOLATIONS_OUTLINE_ID);
846839
PMDPlugin.getDefault().refreshView(IPageLayout.ID_PROJECT_EXPLORER);
847840
PMDPlugin.getDefault().refreshView(IPageLayout.ID_OUTLINE);
848-
849841
}
850842

851843
public boolean performCancel() {

0 commit comments

Comments
 (0)