1616import java .util .ArrayList ;
1717import java .util .EnumMap ;
1818import java .util .List ;
19- import java .util .Map ;
19+ import java .util .Set ;
2020import java .util .StringTokenizer ;
2121import java .util .stream .Collectors ;
2222
3535 * Service to help categorize the stack frames into {@link IJavaStackFrame.Category}, based on the internally stored preferences.
3636 */
3737public class StackFrameCategorizer implements IPreferenceChangeListener {
38- private final static Map <String , Category > PREFERENCE_MAPPING = Map .of (JDIDebugPlugin .PREF_COLORIZE_CUSTOM_METHODS , Category .CUSTOM_FILTERED , //
39- JDIDebugPlugin .PREF_COLORIZE_SYNTHETIC_METHODS , Category .SYNTHETIC , //
40- JDIDebugPlugin .PREF_COLORIZE_PLATFORM_METHODS , Category .PLATFORM , //
41- JDIDebugPlugin .PREF_COLORIZE_TEST_METHODS , Category .TEST , //
42- JDIDebugPlugin .PREF_COLORIZE_PRODUCTION_METHODS , Category .PRODUCTION , //
43- JDIDebugPlugin .PREF_COLORIZE_LIBRARY_METHODS , Category .LIBRARY );
44-
45- private final static EnumMap <Category , String > REVERSE_MAPPING = new EnumMap <>(PREFERENCE_MAPPING .entrySet ().stream ().collect (Collectors .toUnmodifiableMap (Map .Entry ::getValue , Map .Entry ::getKey )));
38+ private final static String PREFIX = JDIDebugPlugin .getUniqueIdentifier () + ".enable_category_" ; //$NON-NLS-1$
4639
4740 /**
4841 * Class to decide if a particular class name is part of a list of classes and list of packages.
@@ -68,13 +61,15 @@ boolean match(String fqcName) {
6861 private Filters custom ;
6962 private final EnumMap <Category , Boolean > enabledCategories ;
7063 private final IPreferencesService preferenceService ;
64+ private final IEclipsePreferences instancePreferences ;
7165
72- public StackFrameCategorizer (IPreferencesService preferenceService ) {
66+ public StackFrameCategorizer (IPreferencesService preferenceService , IEclipsePreferences instancePreferences ) {
7367 this .preferenceService = preferenceService ;
68+ this .instancePreferences = instancePreferences ;
7469 enabledCategories = new EnumMap <>(Category .class );
75- for (var kv : PREFERENCE_MAPPING . entrySet ()) {
76- boolean enabled = preferenceService .getBoolean (JDIDebugPlugin .getUniqueIdentifier (), kv . getKey ( ), true , null );
77- enabledCategories .put (kv . getValue () , enabled );
70+ for (var category : Category . values ()) {
71+ boolean enabled = preferenceService .getBoolean (JDIDebugPlugin .getUniqueIdentifier (), getNameOfTheFlagToEnable ( category ), true , null );
72+ enabledCategories .put (category , enabled );
7873 }
7974
8075 platform = createActivePlatformFilters ();
@@ -187,9 +182,20 @@ public boolean isEnabled(Category category) {
187182 return Boolean .TRUE .equals (enabledCategories .get (category ));
188183 }
189184
185+ private String getNameOfTheFlagToEnable (Category category ) {
186+ return PREFIX + category .name ();
187+ }
188+
189+ private Category getCategoryFromNameOfTheFlag (String flagName ) {
190+ if (flagName .startsWith (PREFIX )) {
191+ return Category .valueOf (flagName .substring (PREFIX .length ()));
192+ }
193+ return null ;
194+ }
195+
190196 public void setEnabled (Category category , boolean flag ) {
191197 enabledCategories .put (category , flag );
192- JDIDebugPlugin . getInstancePreferences (). putBoolean (REVERSE_MAPPING . get (category ), flag );
198+ instancePreferences . putBoolean (getNameOfTheFlagToEnable (category ), flag );
193199 }
194200
195201 /**
@@ -217,11 +223,33 @@ public void preferenceChange(PreferenceChangeEvent event) {
217223 } else if (JDIDebugPlugin .PREF_ACTIVE_CUSTOM_FRAME_FILTER_LIST .equals (prop )) {
218224 custom = createActiveCustomFilters ();
219225 } else {
220- var category = PREFERENCE_MAPPING . get (prop );
226+ var category = getCategoryFromNameOfTheFlag (prop );
221227 if (category != null ) {
222228 enabledCategories .put (category , (Boolean ) event .getNewValue ());
223229 }
224230 }
225231 }
226232
233+ /**
234+ * Adds the given class names to the definition of the active list of custom, highlighted classes.
235+ *
236+ * @param classNames
237+ * name of the classes.
238+ */
239+ public void addTypesToActiveCustomFilters (Set <String > classNames ) {
240+ List <String > actives = new ArrayList <>(List .of (getActiveCustomStackFilter ()));
241+ List <String > inactives = new ArrayList <>(List .of (getInactiveCustomStackFilter ()));
242+ for (String className : classNames ) {
243+ inactives .remove (className );
244+ if (!actives .contains (className )) {
245+ actives .add (className );
246+ }
247+ }
248+ instancePreferences .put (JDIDebugPlugin .PREF_ACTIVE_CUSTOM_FRAME_FILTER_LIST , convert (actives ));
249+ instancePreferences .put (JDIDebugPlugin .PREF_INACTIVE_CUSTOM_FRAME_FILTER_LIST , convert (inactives ));
250+ }
251+
252+ private String convert (List <String > classNames ) {
253+ return classNames .stream ().collect (Collectors .joining ("," )); //$NON-NLS-1$
254+ }
227255}
0 commit comments