001package jmri.swing;
002
003import jmri.Permission;
004
005/**
006 * Permission Swing tools.
007 *
008 * @author Daniel Bergqvist 2024
009 */
010public final class PermissionSwingTools {
011
012    // Private constructor to ensure this class never get instanciated.
013    private PermissionSwingTools() {
014    }
015
016    /**
017     * Find the name of the adapter class for an object.
018     *
019     * @param o object of a configurable type
020     * @return class name of adapter
021     */
022    public static String adapterNameForObject(Object o) {
023        return adapterNameForClass(o.getClass());
024    }
025
026    /**
027     * Find the name of the adapter class for an object.
028     *
029     * @param c class of a configurable type
030     * @return class name of adapter
031     */
032    public static String adapterNameForClass(Class<?> c) {
033        String className = c.getName();
034        log.trace("handle object of class {}", className);
035        int lastDot = className.lastIndexOf(".");
036        if (lastDot > 0) {
037            // found package-class boundary OK
038            int firstDollarSign = className.indexOf("$");
039            String result;
040            if (firstDollarSign > 0) {
041                // found inner-class boundary OK
042                result = className.substring(0, lastDot)
043                        + ".swing."
044                        + className.substring(lastDot + 1, firstDollarSign)
045                        + "Swing$"
046                        + className.substring(firstDollarSign + 1)
047                        + "Swing";
048            } else {
049                result = className.substring(0, lastDot)
050                        + ".swing."
051                        + className.substring(lastDot + 1, className.length())
052                        + "Swing";
053            }
054            log.trace("adapter class name is {}", result);
055            return result;
056        } else {
057            // No last dot found! This should not be possible in Java.
058            log.error("No package name found, which is not yet handled!");
059            throw new RuntimeException("No package name found, which is not yet handled!");
060        }
061    }
062
063    /**
064     * Get a SwingConfiguratorInterface for a class
065     * @param permission The permission to get a PermissionSwing of
066     * @return a PermissionSwing object
067     */
068    static public PermissionSwing getPermissionSwingForClass(Permission permission) {
069        Class<?> clazz = permission.getClass();
070        PermissionSwing permissionSwing;
071        try {
072            permissionSwing = (PermissionSwing) Class.forName(adapterNameForClass(clazz)).getDeclaredConstructor().newInstance();
073        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException
074                    | NoSuchMethodException | java.lang.reflect.InvocationTargetException ex) {
075            log.error("Cannot load PermissionSwing for {}", clazz.getName(), ex);
076            return null;
077        }
078        return permissionSwing;
079    }
080
081
082    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PermissionSwingTools.class);
083
084}