001package jmri.jmrit.throttle;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import javax.swing.Icon;
006import jmri.InstanceManager;
007import jmri.jmrit.throttle.implementation.ThrottleUICore;
008import jmri.jmrit.throttle.interfaces.ThrottleControllerUI;
009import jmri.util.swing.JmriAbstractAction;
010import jmri.util.swing.JmriPanel;
011import jmri.util.swing.WindowInterface;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Load Default Throttles Layout Action
017 * 
018 * <hr>
019 * This file is part of JMRI.
020 * <p>
021 * JMRI is free software; you can redistribute it and/or modify it under the
022 * terms of version 2 of the GNU General Public License as published by the Free
023 * Software Foundation. See the "COPYING" file for a copy of this license.
024 * <p>
025 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
026 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
027 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
028 *
029 * @author Lionel Jeanson Copyright 2009
030 */
031public class LoadDefaultXmlThrottlesLayoutAction extends JmriAbstractAction {
032
033    public LoadDefaultXmlThrottlesLayoutAction(String s, WindowInterface wi) {
034        super(s, wi);
035        // disable the ourselves if there is no throttle Manager
036        if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) {
037            setEnabled(false);
038        }
039    }
040
041    public LoadDefaultXmlThrottlesLayoutAction(String s, Icon i, WindowInterface wi) {
042        super(s, i, wi);
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    /**
050     * Constructor
051     *
052     * @param s Name for the action.
053     */
054    public LoadDefaultXmlThrottlesLayoutAction(String s) {
055        super(s);
056        // disable the ourselves if there is no throttle Manager
057        if (jmri.InstanceManager.getNullableDefault(jmri.ThrottleManager.class) == null) {
058            setEnabled(false);
059        }
060    }
061
062    public LoadDefaultXmlThrottlesLayoutAction() {
063        this("Load default throttle layout...");
064    }
065
066    /**
067     * The action is performed. Create a new ThrottleFrame.
068     *
069     * @param e The event causing the action.
070     */
071    @Override
072    public void actionPerformed(ActionEvent e) {
073        // load throttle preference
074        LoadXmlThrottlesLayoutAction lxta = new LoadXmlThrottlesLayoutAction();
075        try {
076            if (lxta.loadThrottlesLayout(new File(ThrottleUICore.getDefaultThrottleFilename()))) {
077                return;
078            }
079        } catch (java.io.IOException ex) {
080            log.error("No default throttle layout, creating an empty throttle window");
081        }
082        // need to create a new one
083        ThrottleControllerUI tf = InstanceManager.getDefault(ThrottleFrameManager.class).createThrottleFrame();
084        tf.toFront();
085    }
086
087    // initialize logging
088    private static final Logger log = LoggerFactory.getLogger(LoadDefaultXmlThrottlesLayoutAction.class);
089
090    @Override
091    public JmriPanel makePanel() {
092        throw new IllegalArgumentException("Should not be invoked");
093    }
094
095}