Skip to content

Commit 26f2fb5

Browse files
committed
Added readEnum option
1 parent a5ac409 commit 26f2fb5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

jbase/ui/JBaseDialog.java

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Collection;
55
import java.util.Map;
66
import java.util.Set;
7+
import java.util.EnumSet;
78

89
/**
910
* Interface for interacting with any JBase Dialog
@@ -247,4 +248,29 @@ public static Integer readIntRange(String prompt, String failure, int one, int t
247248
}
248249

249250

251+
/**
252+
* Ask the user to choose a value from an enum
253+
*
254+
* @param enumClass The enum to read
255+
* @param prompt String to display before the text
256+
* @param loop If true, keeps looping until user inputs valid input. Otherwise, returns null on failure.
257+
* @return The read enum, or null upon failure
258+
*/
259+
public static <T extends Enum<T>> T readEnum(Class<T> enumClass, String prompt, boolean loop) {
260+
261+
Set<T> values = EnumSet.allOf(enumClass);
262+
263+
//Print the list of values to pick from
264+
System.out.println("Pick "+enumClass.getName()+":");
265+
printCollection(values,false);
266+
System.out.println("");
267+
268+
Integer v = readIntRange(prompt,"*** Invalid Option ***",1,values.size(), loop);
269+
if (v == null) {return null;}
270+
271+
//Remember, we number starting at 1 (not 0)
272+
return enumClass.getEnumConstants()[v-1];
273+
}
274+
275+
250276
}

0 commit comments

Comments
 (0)