001package jmri.jmrix; 002 003import java.util.Arrays; 004import java.util.HashSet; 005import java.util.Locale; 006import java.util.ResourceBundle; 007import java.util.Set; 008import jmri.util.startup.AbstractStartupActionFactory; 009 010/** 011 * Create a factory of startup actions from a ResourceBundle. 012 * 013 * @author Randall Wood Copyright 2020 014 */ 015public class ResourceBundleStartupActionFactory extends AbstractStartupActionFactory { 016 017 private final ResourceBundle bundle; 018 private Class<?>[] classes; 019 020 protected ResourceBundleStartupActionFactory(ResourceBundle resource) { 021 bundle = resource; 022 } 023 024 @Override 025 public String getTitle(Class<?> clazz, Locale locale) throws IllegalArgumentException { 026 if (Arrays.asList(getActionClasses()).contains(clazz)) { 027 return ResourceBundle.getBundle(bundle.getBaseBundleName(), locale).getString(clazz.getName()); 028 } 029 throw new IllegalArgumentException(clazz.getName() + " is not supported by " + this.getClass().getName()); 030 } 031 032 @Override 033 public Class<?>[] getActionClasses() { 034 if (classes == null) { 035 if (bundle != null) { 036 Set<Class<?>> set = new HashSet<>(); 037 bundle.keySet().forEach(k -> { 038 try { 039 set.add(Class.forName(k)); 040 } catch (ClassNotFoundException ex) { 041 log.error("Specified class {} not found.", k, ex); 042 } 043 }); 044 classes = set.toArray(new Class<?>[set.size()]); 045 } else { 046 classes = new Class<?>[0]; 047 } 048 } 049 return classes; 050 } 051 052 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ResourceBundleStartupActionFactory.class); 053}