55package net .sourceforge .pmd .eclipse .ui .views ;
66
77import java .util .ArrayList ;
8+ import java .util .Collections ;
9+ import java .util .EnumSet ;
10+ import java .util .HashSet ;
811import java .util .List ;
12+ import java .util .Set ;
913
1014import org .eclipse .core .resources .IMarker ;
1115import org .eclipse .core .runtime .CoreException ;
1418
1519import net .sourceforge .pmd .RulePriority ;
1620import net .sourceforge .pmd .eclipse .plugin .PMDPlugin ;
17- import net .sourceforge .pmd .eclipse .plugin .UISettings ;
1821import net .sourceforge .pmd .eclipse .ui .PMDUiConstants ;
1922import net .sourceforge .pmd .eclipse .ui .model .AbstractPMDRecord ;
2023import net .sourceforge .pmd .eclipse .ui .model .FileRecord ;
3033 * @author SebastianRaffel ( 17.05.2005 )
3134 */
3235public 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}
0 commit comments