001package jmri.jmrit;
002
003import java.awt.Desktop;
004import java.awt.FlowLayout;
005import java.awt.event.ActionEvent;
006import java.io.File;
007import java.io.IOException;
008import javax.swing.AbstractAction;
009import javax.swing.BoxLayout;
010import javax.swing.JButton;
011import javax.swing.JFrame;
012import javax.swing.JPanel;
013import javax.swing.JScrollPane;
014import javax.swing.JTextArea;
015import jmri.jmrit.roster.Roster;
016import jmri.util.FileUtil;
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020/**
021 * Swing action to display the JMRI directory locations.
022 * <p>
023 * Although this has "XML" in its name, it's actually much more general. It
024 * displays:
025 * <ul>
026 * <li>The user files and profiles directories
027 * <li>The roster directory
028 * <li>The preferences directory
029 * <li>The program directory
030 * <li>and any log files seen in the program directory
031 * </ul>
032 *
033 * @author Bob Jacobsen Copyright (C) 2004, 2007
034 */
035public class XmlFileLocationAction extends AbstractAction {
036
037    public XmlFileLocationAction() {
038        super();
039    }
040
041    @Override
042    public void actionPerformed(ActionEvent ev) {
043
044        JFrame frame = new jmri.util.JmriJFrame(); // to ensure fits
045        frame.setTitle(Bundle.getMessage("TitleFileLocations"));
046
047        JPanel pane = new JPanel();
048        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
049
050        JPanel buttons = new JPanel();
051        buttons.setLayout(new FlowLayout());
052        pane.add(buttons);
053
054        JButton b = new JButton(Bundle.getMessage("ButtonOpenLocX",
055                Bundle.getMessage("ButtonUserFilesLoc")));
056        buttons.add(b);
057        b.addActionListener((ActionEvent event) -> {
058            try {
059                Desktop.getDesktop().open(new File(FileUtil.getUserFilesPath()));
060            } catch (IOException | UnsupportedOperationException e) {
061                log.error("Error when opening user files location: ", e);
062            }
063        });
064        b = new JButton(Bundle.getMessage("ButtonOpenLocX",
065                Bundle.getMessage("ButtonRosterLoc")));
066        buttons.add(b);
067        b.addActionListener((ActionEvent event) -> {
068            try {
069                Desktop.getDesktop().open(new java.io.File(Roster.getDefault().getRosterLocation()));
070            } catch (java.io.IOException | UnsupportedOperationException e) {
071                log.error("Error when opening roster location: ", e);
072            }
073        });
074        b = new JButton(Bundle.getMessage("ButtonOpenLocX",
075                Bundle.getMessage("ButtonProfileLoc")));
076        buttons.add(b);
077        b.addActionListener((ActionEvent event) -> {
078            try {
079                Desktop.getDesktop().open(new java.io.File(FileUtil.getProfilePath()));
080            } catch (java.io.IOException | UnsupportedOperationException e) {
081                XmlFileLocationAction.log.error("Error when opening profile location: ", e);
082            }
083        });
084        b = new JButton(Bundle.getMessage("ButtonOpenLocX",
085                Bundle.getMessage("ButtonSettingsLoc")));
086        buttons.add(b);
087        b.addActionListener((ActionEvent event) -> {
088            try {
089                Desktop.getDesktop().open(new java.io.File(FileUtil.getPreferencesPath()));
090            } catch (java.io.IOException | UnsupportedOperationException e) {
091                log.error("Error when opening settings location: ", e);
092            }
093        });
094        b = new JButton(Bundle.getMessage("ButtonOpenLocX",
095                Bundle.getMessage("ButtonScriptsLoc")));
096        buttons.add(b);
097        b.addActionListener((ActionEvent event) -> {
098            try {
099                Desktop.getDesktop().open(new java.io.File(FileUtil.getScriptsPath()));
100            } catch (java.io.IOException | UnsupportedOperationException e) {
101                log.error("Error when opening scripts location: ", e);
102            }
103        });
104        b = new JButton(Bundle.getMessage("ButtonOpenLocX",
105                Bundle.getMessage("ButtonProgramLoc")));
106        buttons.add(b);
107        b.addActionListener((ActionEvent event) -> {
108            try {
109                Desktop.getDesktop().open(new java.io.File(System.getProperty("user.dir")));
110            } catch (java.io.IOException | UnsupportedOperationException e) {
111                log.error("Error when opening program location: ", e);
112            }
113        });
114
115        b = new JButton(Bundle.getMessage("ButtonOpenLocX",
116                Bundle.getMessage("ButtonLogFilesLoc")));
117        buttons.add(b);
118        b.addActionListener((ActionEvent event) -> {
119            try {
120                Desktop.getDesktop().open(new java.io.File(System.getProperty("jmri.log.path")));
121            } catch (java.io.IOException | UnsupportedOperationException e) {
122                log.error("Error when opening log files location: ", e);
123            }
124        });
125
126        JScrollPane scroll = new JScrollPane(pane);
127        frame.getContentPane().add(scroll);
128
129        JTextArea textPane = new javax.swing.JTextArea();
130        textPane.setEditable(false);
131        pane.add(textPane);
132
133        textPane.append(getLocationsReport());
134
135        frame.pack();
136        frame.setVisible(true);
137    }
138
139    //return a text string listing the various locations and filenames of interest
140    public static String getLocationsReport() {
141        String logDir = System.getProperty("jmri.log.path");
142
143        String configName = System.getProperty("org.jmri.Apps.configFilename");
144        if (!new File(configName).isAbsolute()) {
145            // must be relative, but we want it to
146            // be relative to the preferences directory
147            configName = FileUtil.getProfilePath() + configName;
148        }
149
150        StringBuilder s = new StringBuilder();
151        s.append(Bundle.getMessage("ButtonUserFilesLoc") + ": ").append(FileUtil.getUserFilesPath()).append("\n");
152        s.append(Bundle.getMessage("ButtonRosterLoc") + ": ").append(Roster.getDefault().getRosterLocation()).append("\n");
153        s.append(Bundle.getMessage("ButtonProfileLoc") + ": ").append(FileUtil.getProfilePath()).append("\n");
154        s.append(Bundle.getMessage("ButtonSettingsLoc") + ": ").append(FileUtil.getPreferencesPath()).append("\n");
155        s.append(Bundle.getMessage("CurrentConfig") + ": ").append(configName).append("\n");
156        s.append(Bundle.getMessage("ButtonScriptsLoc") + ": ").append(FileUtil.getScriptsPath()).append("\n");
157        s.append(Bundle.getMessage("ButtonProgramLoc") + ": ").append(System.getProperty("user.dir")).append("\n");
158        s.append(Bundle.getMessage("TempFilesLoc") + ": ").append(System.getProperty("java.io.tmpdir")).append("\n");
159        s.append(Bundle.getMessage("ButtonLogFilesLoc") + ": ").append(logDir).append("\n");
160
161        //include names of any *.log files in log folder
162        File dir = new File(logDir);
163        String[] files = dir.list();
164        if (files != null) {
165            for (String file : files) {
166                if (file.contains(".log")) {
167                    s.append("  ").append(logDir).append(file).append("\n");
168                }
169            }
170        }
171        return s.toString();
172    }
173
174    private static final Logger log = LoggerFactory.getLogger(XmlFileLocationAction.class);
175
176}