Skip to content

Commit 433596d

Browse files
committed
Synchronize PriorityFilter changes between violation outline
and violation overview
1 parent e51cc96 commit 433596d

File tree

5 files changed

+94
-24
lines changed

5 files changed

+94
-24
lines changed

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

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
package net.sourceforge.pmd.eclipse.ui.views;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.EnumSet;
10+
import java.util.HashSet;
811
import java.util.List;
12+
import java.util.Set;
913

1014
import org.eclipse.core.resources.IMarker;
1115
import org.eclipse.core.runtime.CoreException;
@@ -14,7 +18,6 @@
1418

1519
import net.sourceforge.pmd.RulePriority;
1620
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
17-
import net.sourceforge.pmd.eclipse.plugin.UISettings;
1821
import net.sourceforge.pmd.eclipse.ui.PMDUiConstants;
1922
import net.sourceforge.pmd.eclipse.ui.model.AbstractPMDRecord;
2023
import net.sourceforge.pmd.eclipse.ui.model.FileRecord;
@@ -30,10 +33,11 @@
3033
* @author SebastianRaffel ( 17.05.2005 )
3134
*/
3235
public class PriorityFilter extends ViewerFilter {
36+
private static final PriorityFilter INSTANCE = new PriorityFilter();
3337

34-
private List<Integer> priorityList;
38+
private final Set<RulePriority> enabledPriorities;
3539

36-
private static final PriorityFilter INSTANCE = new PriorityFilter();
40+
private Set<PriorityFilterChangeListener> listeners = Collections.synchronizedSet(new HashSet<PriorityFilterChangeListener>());
3741

3842
/**
3943
* Constructor
@@ -43,7 +47,7 @@ public class PriorityFilter extends ViewerFilter {
4347
*/
4448
@Deprecated
4549
public PriorityFilter() {
46-
priorityList = UISettings.getPriorityIntValues();
50+
enabledPriorities = Collections.synchronizedSet(EnumSet.allOf(RulePriority.class));
4751
}
4852

4953
public static PriorityFilter getInstance() {
@@ -88,12 +92,7 @@ private boolean isPriorityEnabled(Integer markerPrio) {
8892
boolean isEnabled = false;
8993
// for some unknown reasons markerPrio may be null.
9094
if (markerPrio != null) {
91-
for (Integer priority : priorityList) {
92-
if (markerPrio.equals(priority)) {
93-
isEnabled = true;
94-
break;
95-
}
96-
}
95+
isEnabled = enabledPriorities.contains(RulePriority.valueOf(markerPrio));
9796
}
9897
return isEnabled;
9998
}
@@ -104,8 +103,8 @@ public boolean isPriorityEnabled(RulePriority priority) {
104103

105104
private boolean hasMarkersToShow(AbstractPMDRecord record) {
106105
boolean hasMarkers = false;
107-
for (Integer priority : priorityList) {
108-
final IMarker[] markers = record.findMarkersByAttribute(PMDUiConstants.KEY_MARKERATT_PRIORITY, priority);
106+
for (RulePriority priority : enabledPriorities) {
107+
final IMarker[] markers = record.findMarkersByAttribute(PMDUiConstants.KEY_MARKERATT_PRIORITY, priority.getPriority());
109108
if (markers.length > 0) {
110109
hasMarkers = true;
111110
break;
@@ -121,7 +120,10 @@ private boolean hasMarkersToShow(AbstractPMDRecord record) {
121120
* an ArrayLust of Integers
122121
*/
123122
public void setPriorityFilterList(List<Integer> newList) {
124-
priorityList = newList;
123+
enabledPriorities.clear();
124+
for (Integer priority : newList) {
125+
enabledPriorities.add(RulePriority.valueOf(priority));
126+
}
125127
}
126128

127129
/**
@@ -130,6 +132,10 @@ public void setPriorityFilterList(List<Integer> newList) {
130132
* @return an List of Integers
131133
*/
132134
public List<Integer> getPriorityFilterList() {
135+
List<Integer> priorityList = new ArrayList<>();
136+
for (RulePriority priority : enabledPriorities) {
137+
priorityList.add(priority.getPriority());
138+
}
133139
return priorityList;
134140
}
135141

@@ -139,7 +145,12 @@ public List<Integer> getPriorityFilterList() {
139145
* @param priority
140146
*/
141147
public void addPriorityToList(Integer priority) {
142-
priorityList.add(priority);
148+
if (priority != null) {
149+
RulePriority rulePriority = RulePriority.valueOf(priority);
150+
if (enabledPriorities.add(rulePriority)) {
151+
notifyPriorityEnabled(rulePriority);
152+
}
153+
}
143154
}
144155

145156
/**
@@ -148,7 +159,12 @@ public void addPriorityToList(Integer priority) {
148159
* @param priority
149160
*/
150161
public void removePriorityFromList(Integer priority) {
151-
priorityList.remove(priority);
162+
if (priority != null) {
163+
RulePriority rulePriority = RulePriority.valueOf(priority);
164+
if (enabledPriorities.remove(rulePriority)) {
165+
notifyPriorityDisabled(rulePriority);
166+
}
167+
}
152168
}
153169

154170
/**
@@ -159,7 +175,9 @@ public void removePriorityFromList(Integer priority) {
159175
* the List-String
160176
* @param splitter,
161177
* the List splitter (in general ",")
178+
* @deprecated will be removed
162179
*/
180+
@Deprecated
163181
public void setPriorityFilterListFromString(String newList, String splitter) {
164182
if (newList != null) {
165183
final String[] newArray = newList.split(splitter);
@@ -169,7 +187,7 @@ public void setPriorityFilterListFromString(String newList, String splitter) {
169187
priorities.add(Integer.valueOf(element));
170188
}
171189

172-
priorityList = priorities;
190+
setPriorityFilterList(priorities);
173191
}
174192
}
175193

@@ -180,16 +198,52 @@ public void setPriorityFilterListFromString(String newList, String splitter) {
180198
* @param splitter,
181199
* The String splitter (in general ",")
182200
* @return the List-String
201+
* @deprecated will be removed
183202
*/
203+
@Deprecated
184204
public String getPriorityFilterListAsString(String splitter) {
185-
if (priorityList.isEmpty()) {
205+
if (enabledPriorities.isEmpty()) {
186206
return "";
187207
}
188208

189-
final StringBuilder listString = new StringBuilder(priorityList.get(0));
190-
for (int i = 1; i < priorityList.size(); i++) {
191-
listString.append(splitter).append(priorityList.get(i));
209+
StringBuilder listString = new StringBuilder();
210+
int i = 0;
211+
for (RulePriority priority : enabledPriorities) {
212+
if (i > 0) {
213+
listString.append(splitter);
214+
}
215+
listString.append(priority.getPriority());
216+
i++;
192217
}
193218
return listString.toString();
194219
}
220+
221+
public void addPriorityFilterChangeListener(PriorityFilterChangeListener listener) {
222+
listeners.add(listener);
223+
}
224+
225+
public void removePriorityFilterChangeListener(PriorityFilterChangeListener listener) {
226+
listeners.remove(listener);
227+
}
228+
229+
private void notifyPriorityEnabled(RulePriority priority) {
230+
synchronized (listeners) {
231+
for (PriorityFilterChangeListener listener : listeners) {
232+
listener.priorityEnabled(priority);
233+
}
234+
}
235+
}
236+
237+
private void notifyPriorityDisabled(RulePriority priority) {
238+
synchronized (listeners) {
239+
for (PriorityFilterChangeListener listener : listeners) {
240+
listener.priorityDisabled(priority);
241+
}
242+
}
243+
}
244+
245+
public interface PriorityFilterChangeListener {
246+
void priorityEnabled(RulePriority priority);
247+
void priorityDisabled(RulePriority priority);
248+
}
195249
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,12 @@ protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
135135
*/
136136
private void addFilterControls() {
137137
IMenuManager manager = getViewSite().getActionBars().getMenuManager();
138-
List<Integer> filterList = priorityFilter.getPriorityFilterList();
139138

140139
// we add the PriorityFilter-Actions to this Menu
141140
RulePriority[] priorities = UISettings.currentPriorities(true);
142141
for (RulePriority priority : priorities) {
143142
Action filterAction = new PriorityFilterAction(priority, this);
144-
if (filterList.contains(priority.getPriority())) {
143+
if (priorityFilter.isPriorityEnabled(priority)) {
145144
filterAction.setChecked(true);
146145
}
147146

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ public List<AbstractPMDRecord> getProjectFilterList() {
342342
* Delegate method for {@link ProjectFilter#getProjectFilterList()}.
343343
*
344344
* @return project filter list
345+
* @deprecated not needed - will be removed
345346
*/
347+
@Deprecated
346348
public List<Integer> getPriorityFilterList() {
347349
return priorityFilter.getPriorityFilterList();
348350
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
import net.sourceforge.pmd.eclipse.ui.views.PriorityFilter;
1919
import net.sourceforge.pmd.eclipse.ui.views.ViolationOutline;
2020
import net.sourceforge.pmd.eclipse.ui.views.ViolationOverview;
21+
import net.sourceforge.pmd.eclipse.ui.views.PriorityFilter.PriorityFilterChangeListener;
2122

2223
/**
2324
* Filters elements by the Marker priorities
2425
*
2526
* @author SebastianRaffel ( 22.05.2005 )
2627
* @author bremedios (15.9.2010)
2728
*/
28-
public class PriorityFilterAction extends Action {
29+
public class PriorityFilterAction extends Action implements PriorityFilterChangeListener {
2930

3031
private ViolationOutline outlineView;
3132
private ViolationOverview overviewView;
@@ -71,6 +72,7 @@ private void setFilterFrom(ViewerFilter[] filters) {
7172
for (Object filter : filters) {
7273
if (filter instanceof PriorityFilter) {
7374
priorityFilter = (PriorityFilter) filter;
75+
priorityFilter.addPriorityFilterChangeListener(this);
7476
}
7577
}
7678
}
@@ -119,4 +121,17 @@ public void run() {
119121
}
120122
}
121123

124+
@Override
125+
public void priorityEnabled(RulePriority priority) {
126+
if (this.priority == priority) {
127+
this.setChecked(true);
128+
}
129+
}
130+
131+
@Override
132+
public void priorityDisabled(RulePriority priority) {
133+
if (this.priority == priority) {
134+
this.setChecked(false);
135+
}
136+
}
122137
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/util/PriorityUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static boolean isPriorityActive(RulePriority priority) {
4141
* Get all the priorities that are turned on right now.
4242
*
4343
* @return
44-
* @deprecated not needed, will be removed.
44+
* @deprecated not needed, will be removed. Use {@link PriorityFilter#isPriorityEnabled(RulePriority)}.
4545
*/
4646
@Deprecated
4747
public static List<RulePriority> getActivePriorites() {

0 commit comments

Comments
 (0)