001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.bugreport;
003
004import java.awt.Dimension;
005
006import javax.swing.JScrollPane;
007
008import org.openstreetmap.josm.actions.ShowStatusReportAction;
009import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
010import org.openstreetmap.josm.gui.widgets.JosmTextArea;
011import org.openstreetmap.josm.tools.Utils;
012import org.openstreetmap.josm.tools.bugreport.BugReport;
013
014/**
015 * This is a text area that displays the debug text with scroll bars.
016 * @author Michael Zangl
017 * @since 10055
018 */
019public class DebugTextDisplay extends JScrollPane {
020    private static final String CODE_PATTERN = "{{{%n%s%n}}}";
021    private String text;
022    private final JosmTextArea textArea;
023
024    /**
025     * Creates a new text area.
026     * @since 10585
027     */
028    private DebugTextDisplay() {
029        textArea = new JosmTextArea();
030        textArea.setCaretPosition(0);
031        textArea.setEditable(false);
032        setViewportView(textArea);
033        setPreferredSize(new Dimension(600, 270));
034    }
035
036    /**
037     * Creates a new text area with an initial text to display
038     * @param textToDisplay The text to display.
039     */
040    public DebugTextDisplay(String textToDisplay) {
041        this();
042        setCodeText(textToDisplay);
043    }
044
045    /**
046     * Creates a new text area that displays the bug report data
047     * @param report The bug report data to display.
048     * @since 10585
049     */
050    public DebugTextDisplay(BugReport report) {
051        this();
052        setCodeText(report.getReportText(ShowStatusReportAction.getReportHeader()));
053        report.addChangeListener(e -> setCodeText(report.getReportText(ShowStatusReportAction.getReportHeader())));
054    }
055
056    /**
057     * Sets the text that should be displayed in this view.
058     * @param textToDisplay The text
059     */
060    private void setCodeText(String textToDisplay) {
061        text = Utils.strip(textToDisplay).replaceAll("\r", "");
062        textArea.setText(String.format(CODE_PATTERN, text));
063        textArea.setCaretPosition(0);
064    }
065
066    /**
067     * Copies the debug text to the clipboard. This includes the code tags for trac.
068     * @return <code>true</code> if copy was successful
069     * @since 11102 (typo)
070     */
071    public boolean copyToClipboard() {
072        return ClipboardUtils.copyString(String.format(CODE_PATTERN, text));
073    }
074
075    /**
076     * Gets the text this are displays, without the code tag.
077     * @return The stripped text set by {@link #setCodeText(String)}
078     * @since 10585
079     */
080    public String getCodeText() {
081        return text;
082    }
083}