001package apps;
002
003import java.awt.BorderLayout;
004import java.awt.datatransfer.Clipboard;
005import java.awt.datatransfer.StringSelection;
006import java.awt.event.ActionEvent;
007import java.awt.event.ActionListener;
008import javax.swing.Icon;
009import javax.swing.JButton;
010import javax.swing.JFrame;
011import javax.swing.JPanel;
012import javax.swing.JScrollBar;
013import javax.swing.JScrollPane;
014import javax.swing.JTextArea;
015import jmri.jmrit.mailreport.ReportContext;
016import jmri.util.JmriJFrame;
017import jmri.util.swing.JmriPanel;
018import jmri.util.swing.WindowInterface;
019
020/**
021 * Swing action to display the JMRI context for the user
022 *
023 * @author Bob Jacobsen Copyright (C) 2007
024 * @author Matt Harris Copyright (C) 2008
025 *
026 */
027public class ReportContextAction extends jmri.util.swing.JmriAbstractAction {
028
029    public ReportContextAction(String s, WindowInterface wi) {
030        super(s, wi);
031    }
032
033    public ReportContextAction(String s, Icon i, WindowInterface wi) {
034        super(s, i, wi);
035    }
036
037    public ReportContextAction() {
038        super(Bundle.getMessage("TitleContext"));
039    }
040
041    JTextArea pane;
042
043    @Override
044    public void actionPerformed(ActionEvent ev) {
045
046        final JFrame frame = new JmriJFrame(Bundle.getMessage("TitleContext"));  // JmriJFrame to ensure fits on screen
047
048        final Clipboard clipboard = frame.getToolkit().getSystemClipboard();
049
050        pane = new JTextArea();
051        pane.append("\n"); // add a little space at top
052        pane.setEditable(false);
053        pane.setLineWrap(true);
054        pane.setWrapStyleWord(true);
055        pane.setColumns(120);
056
057        JScrollPane scroll = new JScrollPane(pane);
058        frame.add(scroll, BorderLayout.CENTER);
059
060        ReportContext r = new ReportContext();
061        addString(r.getReport(true));
062
063        pane.append("\n"); // add a little space at bottom
064
065        // Add button to allow copy to clipboard
066        JPanel p = new JPanel();
067        JButton copy = new JButton(Bundle.getMessage("ButtonCopyClip"));
068        copy.addActionListener(new ActionListener() {
069            @Override
070            public void actionPerformed(ActionEvent event) {
071                StringSelection text = new StringSelection(pane.getText());
072                clipboard.setContents(text, text);
073            }
074        });
075        p.add(copy);
076        JButton close = new JButton(Bundle.getMessage("ButtonClose"));
077        close.addActionListener(new ActionListener() {
078            @Override
079            public void actionPerformed(ActionEvent event) {
080                frame.setVisible(false);
081                frame.dispose();
082            }
083        });
084        p.add(close);
085        frame.add(p, BorderLayout.SOUTH);
086        frame.pack();
087
088        // start scrolled to top
089        pane.setCaretPosition(0);
090        JScrollBar b = scroll.getVerticalScrollBar();
091        b.setValue(b.getMaximum());
092
093        // show
094        frame.setVisible(true);
095
096    }
097
098    void addString(String val) {
099        pane.append(val + "\n");
100    }
101
102    void addProperty(String prop) {
103        addString(prop + ": " + System.getProperty(prop) + "  ");
104    }
105
106    // never invoked, because we overrode actionPerformed above
107    @Override
108    public JmriPanel makePanel() {
109        throw new IllegalArgumentException("Should not be invoked");
110    }
111
112}