001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.actions;
003    
004    import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005    import static org.openstreetmap.josm.tools.I18n.tr;
006    
007    import java.awt.Dimension;
008    import java.awt.event.ActionEvent;
009    import java.awt.event.KeyEvent;
010    import java.util.HashSet;
011    import java.util.Map;
012    import java.util.Map.Entry;
013    import java.util.Set;
014    
015    import javax.swing.JScrollPane;
016    import javax.swing.JTextArea;
017    
018    import org.openstreetmap.josm.Main;
019    import org.openstreetmap.josm.data.Preferences.Setting;
020    import org.openstreetmap.josm.data.Version;
021    import org.openstreetmap.josm.data.osm.DataSet;
022    import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
023    import org.openstreetmap.josm.gui.ExtendedDialog;
024    import org.openstreetmap.josm.plugins.PluginHandler;
025    import org.openstreetmap.josm.tools.Shortcut;
026    import org.openstreetmap.josm.tools.Utils;
027    
028    
029    /**
030     * @author xeen
031     *
032     * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
033     * Also includes preferences with stripped username and password
034     */
035    public final class ShowStatusReportAction extends JosmAction {
036        public ShowStatusReportAction() {
037            super(
038                    tr("Show Status Report"),
039                    "clock",
040                    tr("Show status report with useful information that can be attached to bugs"),
041                    Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
042                            tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
043    
044            putValue("help", ht("/Action/ShowStatusReport"));
045            putValue("toolbar", "help/showstatusreport");
046            Main.toolbar.register(this);
047        }
048    
049        public static String getReportHeader()
050        {
051            StringBuilder text = new StringBuilder();
052            text.append(Version.getInstance().getReleaseAttributes());
053            text.append("\n");
054            text.append("Identification: " + Version.getInstance().getAgentString());
055            text.append("\n");
056            text.append("Memory Usage: ");
057            text.append(Runtime.getRuntime().totalMemory()/1024/1024);
058            text.append(" MB / ");
059            text.append(Runtime.getRuntime().maxMemory()/1024/1024);
060            text.append(" MB (");
061            text.append(Runtime.getRuntime().freeMemory()/1024/1024);
062            text.append(" MB allocated, but free)");
063            text.append("\n");
064            text.append("Java version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + ", " + System.getProperty("java.vm.name"));
065            text.append("\n");
066            text.append("Operating system: "+ System.getProperty("os.name"));
067            text.append("\n");
068            DataSet dataset = Main.main.getCurrentDataSet();
069            if (dataset != null) {
070                String result = DatasetConsistencyTest.runTests(dataset);
071                if (result.length() == 0) {
072                    text.append("Dataset consistency test: No problems found\n");
073                } else {
074                    text.append("\nDataset consistency test:\n"+result+"\n");
075                }
076            }
077            text.append("\n");
078            text.append(PluginHandler.getBugReportText());
079            text.append("\n");
080    
081            return text.toString();
082        }
083    
084        public void actionPerformed(ActionEvent e) {
085            StringBuilder text = new StringBuilder();
086            text.append(getReportHeader());
087            try {
088                Map<String, Setting> settings = Main.pref.getAllSettings();
089                settings.remove("osm-server.username");
090                settings.remove("osm-server.password");
091                settings.remove("oauth.access-token.key");
092                settings.remove("oauth.access-token.secret");
093                Set<String> keys = new HashSet<String>(settings.keySet());
094                for (String key : keys) {
095                    if (key.startsWith("marker.show")) {
096                        settings.remove(key);
097                    }
098                }
099                for (Entry<String, Setting> entry : settings.entrySet()) {
100                    text.append(entry.getKey()).append("=").append(entry.getValue().getValue().toString()).append("\n");
101                }
102            } catch (Exception x) {
103                x.printStackTrace();
104            }
105    
106            JTextArea ta = new JTextArea(text.toString());
107            ta.setWrapStyleWord(true);
108            ta.setLineWrap(true);
109            ta.setEditable(false);
110            JScrollPane sp = new JScrollPane(ta);
111    
112            ExtendedDialog ed = new ExtendedDialog(Main.parent,
113                    tr("Status Report"),
114                    new String[] {tr("Copy to clipboard and close"), tr("Close") });
115            ed.setButtonIcons(new String[] {"copy.png", "cancel.png" });
116            ed.setContent(sp, false);
117            ed.setMinimumSize(new Dimension(500, 0));
118            ed.showDialog();
119    
120            if(ed.getValue() != 1) return;
121            Utils.copyToClipboard(text.toString());
122        }
123    }