001package apps.gui3.dp3;
002
003import java.io.File;
004import javax.swing.AbstractAction;
005import jmri.InstanceManager;
006import jmri.util.FileUtil;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * The JMRI application for developing the DecoderPro 3 GUI.
012 * <p>
013 * Inserts DP3 interface elements stored in xml/config/parts/jmri/
014 * that are also used in the web server interface.
015 *
016 * @author Bob Jacobsen Copyright 2003, 2004, 2007, 2009, 2010
017 */
018public class DecoderPro3 extends apps.gui3.Apps3 {
019
020    private static String menuFile = null;
021    private static String toolbarFile = null;
022    private static final String applicationName = "DecoderPro";
023
024    public DecoderPro3(String[] args) {
025        super(applicationName, "DecoderProConfig3.xml", args);
026    }
027
028    public synchronized static String getMenuFile() {
029        if (menuFile == null) {
030            menuFile = "dp3/Gui3Menus.xml";
031            File file = new File(menuFile);
032            // decide whether name is absolute or relative
033            if (!file.isAbsolute()) {
034                // must be relative, but we want it to
035                // be relative to the preferences directory
036                menuFile = FileUtil.getUserFilesPath() + "dp3/Gui3Menus.xml";
037                file = new File(menuFile);
038            }
039            if (!file.exists()) {
040                menuFile = "xml/config/parts/jmri/jmrit/roster/swing/RosterFrameMenu.xml";
041            } else {
042                log.info("Found user created menu structure this will be used instead of the system default");
043            }
044        }
045        return menuFile;
046    }
047
048    public synchronized static String getToolbarFile() {
049        if (toolbarFile == null) {
050            toolbarFile = "dp3/Gui3MainToolBar.xml";
051            File file = new File(toolbarFile);
052            // decide whether name is absolute or relative
053            if (!file.isAbsolute()) {
054                // must be relative, but we want it to
055                // be relative to the preferences directory
056                toolbarFile = FileUtil.getUserFilesPath() + "dp3/Gui3MainToolBar.xml";
057                file = new File(toolbarFile);
058            }
059            if (!file.exists()) {
060                toolbarFile = "xml/config/parts/jmri/jmrit/roster/swing/RosterFrameToolBar.xml";
061            } else {
062                log.info("Found user created toolbar structure this will be used instead of the system default");
063            }
064        }
065        return toolbarFile;
066    }
067
068    @Override
069    protected void createMainFrame() {
070        // create and populate main window
071        mainFrame = new DecoderPro3Window(getMenuFile(), getToolbarFile());
072    }
073
074    /**
075     * Force our test size. Superclass method set to max size, filling real
076     * window.
077     *
078     * @param d size to use (ignored in this case)
079     */
080    @Override
081    protected void displayMainFrame(java.awt.Dimension d) {
082        jmri.UserPreferencesManager p = InstanceManager.getDefault(jmri.UserPreferencesManager.class);
083        if (!p.hasProperties(mainFrame.getWindowFrameRef())) {
084            mainFrame.setSize(new java.awt.Dimension(1024, 600));
085            mainFrame.setPreferredSize(new java.awt.Dimension(1024, 600));
086        }
087
088        mainFrame.setVisible(true);
089    }
090
091    // Main entry point
092    public static void main(String[] args) {
093        preInit(args);
094        DecoderPro3 app = new DecoderPro3(args);
095        app.start();
096    }
097
098    static public void preInit(String[] args) {
099        apps.gui3.Apps3.preInit(applicationName);
100        apps.gui3.Apps3.setConfigFilename("DecoderProConfig3.xml", args);
101    }
102
103    /**
104     * Final actions before releasing control of app to user
105     */
106    @Override
107    protected void start() {
108        super.start();
109
110        if ((!configOK) || (!configDeferredLoadOK)) {
111            if (preferenceFileExists) {
112                //if the preference file already exists then we will launch the normal preference window
113                AbstractAction prefsAction = new apps.gui3.tabbedpreferences.TabbedPreferencesAction(Bundle.getMessage("MenuItemPreferences"));
114                prefsAction.actionPerformed(null);
115            }
116        }
117
118        // kick off update of decoder index if needed
119        jmri.util.ThreadingUtil.runOnGUI(() -> {
120            try {
121                jmri.jmrit.decoderdefn.DecoderIndexFile.updateIndexIfNeeded();
122            } catch (org.jdom2.JDOMException| java.io.IOException e) {
123                log.error("Exception trying to pre-load decoderIndex", e);
124            }
125        });
126    }
127
128    private final static Logger log = LoggerFactory.getLogger(DecoderPro3.class);
129
130}