Skip to content

Commit 7464859

Browse files
committed
Save the enabled priorities of PriorityFilter in preferences
And load it from there. ViolationOutline and ViolationOverview don't do this anymore.
1 parent 433596d commit 7464859

File tree

4 files changed

+64
-37
lines changed

4 files changed

+64
-37
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
8383
import net.sourceforge.pmd.eclipse.ui.nls.StringTable;
8484
import net.sourceforge.pmd.eclipse.ui.priority.PriorityDescriptorCache;
85+
import net.sourceforge.pmd.eclipse.ui.views.PriorityFilter;
8586
import net.sourceforge.pmd.eclipse.util.ResourceManager;
8687
import net.sourceforge.pmd.lang.LanguageRegistry;
8788
import net.sourceforge.pmd.lang.LanguageVersion;
@@ -127,6 +128,7 @@ public class PMDPlugin extends AbstractUIPlugin {
127128
* The constructor
128129
*/
129130
public PMDPlugin() {
131+
plugin = this;
130132
}
131133

132134
public Color colorFor(RGB rgb) {
@@ -232,7 +234,6 @@ public static File getPluginFolder() {
232234
*/
233235
public void start(BundleContext context) throws Exception {
234236
super.start(context);
235-
plugin = this;
236237

237238
// this needs to be executed before the preferences are loaded, because
238239
// the standard
@@ -253,6 +254,10 @@ public void resourceChanged(IResourceChangeEvent arg0) {
253254
}
254255
});
255256

257+
// the initialization can only take place, after the plugin has been started.
258+
// otherwise the preferences are not available yet.
259+
PriorityFilter.getInstance().initialize();
260+
256261
version = context.getBundle().getHeaders().get("Bundle-Version");
257262
}
258263

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/PriorityFilter.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import java.util.ArrayList;
88
import java.util.Collections;
99
import java.util.EnumSet;
10-
import java.util.HashSet;
1110
import java.util.List;
1211
import java.util.Set;
12+
import java.util.concurrent.CopyOnWriteArraySet;
1313

1414
import org.eclipse.core.resources.IMarker;
1515
import org.eclipse.core.runtime.CoreException;
@@ -30,14 +30,19 @@
3030
* The ViewerFilter for Priorities.
3131
* This is used for both Violation Outline and Violation Overview.
3232
*
33+
* <p>The current enabled filters are saved automatically in the preferences.
34+
* See {@link #PREFERENCE_KEY}.
35+
*
3336
* @author SebastianRaffel ( 17.05.2005 )
3437
*/
3538
public class PriorityFilter extends ViewerFilter {
3639
private static final PriorityFilter INSTANCE = new PriorityFilter();
3740

41+
private static final String PREFERENCE_KEY = PriorityFilter.class.getName() + ".enabledPriorities";
42+
3843
private final Set<RulePriority> enabledPriorities;
3944

40-
private Set<PriorityFilterChangeListener> listeners = Collections.synchronizedSet(new HashSet<PriorityFilterChangeListener>());
45+
private Set<PriorityFilterChangeListener> listeners = new CopyOnWriteArraySet<>();
4146

4247
/**
4348
* Constructor
@@ -54,6 +59,42 @@ public static PriorityFilter getInstance() {
5459
return INSTANCE;
5560
}
5661

62+
/**
63+
* Initialize the priority filter by loading the preferences and making sure, that
64+
* any change to the filter is stored to the preferences.
65+
*/
66+
public final void initialize() {
67+
addPriorityFilterChangeListener(new PriorityFilterChangeListener() {
68+
@Override
69+
public void priorityEnabled(RulePriority priority) {
70+
saveToPreferenceStore();
71+
}
72+
@Override
73+
public void priorityDisabled(RulePriority priority) {
74+
saveToPreferenceStore();
75+
}
76+
});
77+
loadFromPreferenceStore();
78+
}
79+
80+
private void saveToPreferenceStore() {
81+
StringBuilder asString = new StringBuilder(50);
82+
for (RulePriority rulePriority : enabledPriorities) {
83+
asString.append(rulePriority.name()).append(',');
84+
}
85+
PMDPlugin.getDefault().getPreferenceStore().setValue(PREFERENCE_KEY, asString.toString());
86+
}
87+
88+
private void loadFromPreferenceStore() {
89+
String priorities = PMDPlugin.getDefault().getPreferenceStore().getString(PREFERENCE_KEY);
90+
if (priorities != null && !priorities.isEmpty()) {
91+
enabledPriorities.clear();
92+
for (String priority : priorities.split(",")) {
93+
enabledPriorities.add(RulePriority.valueOf(priority));
94+
}
95+
}
96+
}
97+
5798
/*
5899
* @see
59100
* org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.
@@ -92,13 +133,13 @@ private boolean isPriorityEnabled(Integer markerPrio) {
92133
boolean isEnabled = false;
93134
// for some unknown reasons markerPrio may be null.
94135
if (markerPrio != null) {
95-
isEnabled = enabledPriorities.contains(RulePriority.valueOf(markerPrio));
136+
isEnabled = isPriorityEnabled(RulePriority.valueOf(markerPrio));
96137
}
97138
return isEnabled;
98139
}
99140

100141
public boolean isPriorityEnabled(RulePriority priority) {
101-
return isPriorityEnabled(priority.getPriority());
142+
return enabledPriorities.contains(priority);
102143
}
103144

104145
private boolean hasMarkersToShow(AbstractPMDRecord record) {
@@ -118,7 +159,9 @@ private boolean hasMarkersToShow(AbstractPMDRecord record) {
118159
*
119160
* @param newList,
120161
* an ArrayLust of Integers
162+
* @deprecated will be removed. The enabled priorities are stored in the preferences automatically now.
121163
*/
164+
@Deprecated
122165
public void setPriorityFilterList(List<Integer> newList) {
123166
enabledPriorities.clear();
124167
for (Integer priority : newList) {
@@ -130,7 +173,9 @@ public void setPriorityFilterList(List<Integer> newList) {
130173
* Gets the FilterList with the Priorities
131174
*
132175
* @return an List of Integers
176+
* @deprecated will be removed. Use {@link #isPriorityEnabled(RulePriority)} instead.
133177
*/
178+
@Deprecated
134179
public List<Integer> getPriorityFilterList() {
135180
List<Integer> priorityList = new ArrayList<>();
136181
for (RulePriority priority : enabledPriorities) {
@@ -227,18 +272,14 @@ public void removePriorityFilterChangeListener(PriorityFilterChangeListener list
227272
}
228273

229274
private void notifyPriorityEnabled(RulePriority priority) {
230-
synchronized (listeners) {
231-
for (PriorityFilterChangeListener listener : listeners) {
232-
listener.priorityEnabled(priority);
233-
}
275+
for (PriorityFilterChangeListener listener : listeners) {
276+
listener.priorityEnabled(priority);
234277
}
235278
}
236279

237280
private void notifyPriorityDisabled(RulePriority priority) {
238-
synchronized (listeners) {
239-
for (PriorityFilterChangeListener listener : listeners) {
240-
listener.priorityDisabled(priority);
241-
}
281+
for (PriorityFilterChangeListener listener : listeners) {
282+
listener.priorityDisabled(priority);
242283
}
243284
}
244285

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/ViolationOutline.java

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

55
package net.sourceforge.pmd.eclipse.ui.views;
66

7-
import java.util.List;
8-
97
import org.eclipse.jface.action.Action;
108
import org.eclipse.jface.action.IMenuListener;
119
import org.eclipse.jface.action.IMenuManager;
@@ -49,7 +47,6 @@ public class ViolationOutline extends AbstractPMDPagebookView implements ISelect
4947
private FileRecord resourceRecord;
5048
private PriorityFilter priorityFilter;
5149

52-
protected static final String PRIORITY_LIST = "priorityFilterList";
5350
protected static final String COLUMN_WIDTHS = "tableColumnWidths";
5451
protected static final String COLUMN_SORTER = "tableColumnSorter";
5552

@@ -77,19 +74,11 @@ protected String mementoFileId() {
7774
@Override
7875
public void init(IViewSite site) throws PartInitException {
7976
super.init(site);
80-
8177
priorityFilter = PriorityFilter.getInstance();
82-
83-
List<Integer> priorityList = getIntegerList(PRIORITY_LIST);
84-
if (!priorityList.isEmpty()) {
85-
// set the loaded List for the Priority Filter
86-
priorityFilter.setPriorityFilterList(priorityList);
87-
}
8878
}
8979

9080
@Override
9181
public void dispose() {
92-
save(PRIORITY_LIST, priorityFilter.getPriorityFilterList());
9382
super.dispose();
9483
}
9584

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/views/ViolationOverview.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.eclipse.ui.PartInitException;
3333
import org.eclipse.ui.part.ViewPart;
3434

35+
import net.sourceforge.pmd.RulePriority;
3536
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
3637
import net.sourceforge.pmd.eclipse.runtime.builder.MarkerUtil;
3738
import net.sourceforge.pmd.eclipse.runtime.cmd.DeleteMarkersCommand;
@@ -71,7 +72,6 @@ public class ViolationOverview extends ViewPart implements ISelectionProvider, I
7172
private int showType;
7273

7374
protected static final String PACKAGE_SWITCH = "packageSwitch";
74-
protected static final String PRIORITY_LIST = "priorityFilterList";
7575
protected static final String PROJECT_LIST = "projectFilterList";
7676
protected static final String COLUMN_WIDTHS = "tableColumnWidths";
7777
protected static final String COLUMN_SORTER = "tableColumnSorter";
@@ -149,8 +149,6 @@ public void createPartControl(Composite parent) {
149149
*/
150150
@Override
151151
public void dispose() {
152-
memento.putList(PRIORITY_LIST, priorityFilter.getPriorityFilterList());
153-
154152
// on Dispose of the View we save its State into a Memento
155153

156154
// we save the filtered Projects
@@ -302,10 +300,10 @@ private ViewerSorter getViewerSorter(int columnNr) {
302300
public int getNumberOfFilteredViolations(AbstractPMDRecord record) {
303301
int number = 0;
304302

305-
List<Integer> filterList = priorityFilter.getPriorityFilterList();
306-
for (int i = 0; i < filterList.size(); i++) {
307-
Integer priority = filterList.get(i);
308-
number += record.getNumberOfViolationsToPriority(priority.intValue(), getShowType() == SHOW_MARKERS_FILES);
303+
for (RulePriority rulePriority : RulePriority.values()) {
304+
if (priorityFilter.isPriorityEnabled(rulePriority)) {
305+
number += record.getNumberOfViolationsToPriority(rulePriority.getPriority(), getShowType() == SHOW_MARKERS_FILES);
306+
}
309307
}
310308
return number;
311309
}
@@ -460,13 +458,7 @@ private String getString(String key) {
460458
*
461459
*/
462460
private void restoreFilterSettings() {
463-
464461
// Provide the Filters with their last State
465-
List<Integer> priorityList = memento.getIntegerList(PRIORITY_LIST);
466-
if (!priorityList.isEmpty()) {
467-
priorityFilter.setPriorityFilterList(priorityList);
468-
}
469-
470462
List<String> projectNames = memento.getStringList(PROJECT_LIST);
471463
if (!projectNames.isEmpty()) {
472464
List<AbstractPMDRecord> projectList = new ArrayList<AbstractPMDRecord>();

0 commit comments

Comments
 (0)