001package jmri.jmrit.turnoutoperations;
002
003import java.lang.reflect.Constructor;
004import java.lang.reflect.InvocationTargetException;
005import javax.swing.JPanel;
006import jmri.TurnoutOperation;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Configuration panel for TurnoutOperation class. Must be overridden to define
012 * specific panel details for class. Must have exactly one constructor like the
013 * one shown below.
014 *
015 * @author John Harper Copyright 2005
016 */
017public class TurnoutOperationConfig extends JPanel {
018
019    TurnoutOperation myOperation;
020    //boolean valid = true;
021
022    TurnoutOperationConfig(TurnoutOperation op) {
023        myOperation = op;
024    }
025
026    TurnoutOperation getOperation() {
027        return myOperation;
028    }
029
030    public void endConfigure() {
031        log.error("Should have been overridden!");
032    }
033
034    /**
035     * Given an instance of a concrete subclass of the TurnoutOperation class,
036     * looks for a corresponding ...Config class and creates an instance of it.
037     * If anything goes wrong (no such class, wrong constructors, instantiation
038     * error, ....) just return null
039     *
040     * @param op operation for which configurator is required
041     * @return the configurator or null in case of an error
042     */
043    static public TurnoutOperationConfig getConfigPanel(TurnoutOperation op) {
044        TurnoutOperationConfig config = null;
045        String[] path = op.getClass().getName().split("\\.");
046        String configName = "jmri.jmrit.turnoutoperations." + path[path.length - 1] + "Config";
047        try {
048            Class<?> configClass = Class.forName(configName);
049            Constructor<?>[] constrs = configClass.getConstructors();
050            if (constrs.length == 1) {
051                try {
052                    config = (TurnoutOperationConfig) constrs[0].newInstance(new Object[]{op});
053                } catch (IllegalAccessException | IllegalArgumentException | InstantiationException | InvocationTargetException ex) {
054                    log.error("Error configuring TurnoutOperation", ex);
055                }
056            }
057        } catch (ClassNotFoundException e) {
058        }
059        if (config == null) {
060            //config = null;
061            log.debug("could not create configurator for {} \"{}\"", op.getClass().getName(), op.getName());
062        }
063        return config;
064    }
065
066    private final static Logger log = LoggerFactory.getLogger(TurnoutOperationConfig.class);
067
068}