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