imagej / ImageJ

Public domain software for processing and analyzing scientific images
http://imagej.org
Other
513 stars 218 forks source link

GenericDialog: new method 'addEnumChoice()' for reducing choices #188

Closed imagingbook closed 1 year ago

imagingbook commented 1 year ago

This PR adds a new method to GenericDialog for adding enum-based choices to dialogs. The existing method void addEnumChoice(String label, E defaultItem) includes all items defined by an enum class in the list of choices. While this creates very simple and elegant code, I found that this is not always desirable. Therefore, the new method

void addEnumChoice(String label, E[] enumArray, E defaultItem)

includes only a subset of the available enum items, as specified in enumArray. Note that its signature is completely analogous to the original string-based method addChoice(String, String[], String). This can also be used to present the items in a different order than used in the enum definition. In this case, defaultItem may be null, in which case the first item in enumArray is used instead. The same happens if defaultItem is not contained in enumArray.

The existing "get" method getNextEnumChoice(Class<E> enumClass) works without modification.

I have also included usage examples for both methods, e.g.,

import ij.process.AutoThresholder.Method;
...
Method[] selectMethods = {Method.Triangle, Method.Otsu, Method.Huang};
Method method = Method.Otsu;

GenericDialog gd = new GenericDialog("Select AutoThresholder Method");
gd.addEnumChoice("Select threshold methods", selectMethods, method);
...
gd.showDialog();
...
method = gd.getNextEnumChoice(Method.class);

There are other modifications in file GenericDialog.java, since my IDE expands imports and inserts @Override annotations automatically. Sorry if this is any inconvenience.

Regards, Wilhelm

rasband commented 1 year ago

This new method is in the ImageJ 1.54a3 daily build. The commit is at https://github.com/imagej/ImageJ/commit/e6c6c809d3963b5bab852490f1bb8462f5bf2ecf.