001package jmri.jmrit.throttle;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import javax.swing.Icon;
006import jmri.InstanceManager;
007import jmri.util.swing.JmriAbstractAction;
008import jmri.util.swing.JmriPanel;
009import jmri.util.swing.WindowInterface;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Create a new throttle.
015 *
016 * @author Lionel Jeanson Copyright 2009
017 */
018public class LoadDefaultXmlThrottlesLayoutAction extends JmriAbstractAction {
019
020    public LoadDefaultXmlThrottlesLayoutAction(String s, WindowInterface wi) {
021        super(s, wi);
022        // disable the ourselves if there is no throttle Manager
023        if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) {
024            setEnabled(false);
025        }
026    }
027
028    public LoadDefaultXmlThrottlesLayoutAction(String s, Icon i, WindowInterface wi) {
029        super(s, i, wi);
030        // disable the ourselves if there is no throttle Manager
031        if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) {
032            setEnabled(false);
033        }
034    }
035
036    /**
037     * Constructor
038     *
039     * @param s Name for the action.
040     */
041    public LoadDefaultXmlThrottlesLayoutAction(String s) {
042        super(s);
043        // disable the ourselves if there is no throttle Manager
044        if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) {
045            setEnabled(false);
046        }
047    }
048
049    public LoadDefaultXmlThrottlesLayoutAction() {
050        this("Load default throttle layout...");
051    }
052
053    /**
054     * The action is performed. Create a new ThrottleFrame.
055     *
056     * @param e The event causing the action.
057     */
058    @Override
059    public void actionPerformed(ActionEvent e) {
060        // load throttle preference
061        LoadXmlThrottlesLayoutAction lxta = new LoadXmlThrottlesLayoutAction();
062        try {
063            if (lxta.loadThrottlesLayout(new File(ThrottleFrame.getDefaultThrottleFilename()))) {
064                return;
065            }
066        } catch (java.io.IOException ex) {
067            log.error("No default throttle layout, creating an empty throttle window");
068        }
069        // need to create a new one
070        ThrottleFrame tf = InstanceManager.getDefault(ThrottleFrameManager.class).createThrottleFrame();
071        tf.toFront();
072    }
073
074    // initialize logging
075    private final static Logger log = LoggerFactory.getLogger(LoadDefaultXmlThrottlesLayoutAction.class);
076
077    @Override
078    public JmriPanel makePanel() {
079        throw new IllegalArgumentException("Should not be invoked");
080    }
081
082}