001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.Dimension;
008
009import javax.swing.BoxLayout;
010import javax.swing.Icon;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013import javax.swing.JScrollPane;
014
015import org.openstreetmap.josm.gui.util.WindowGeometry;
016import org.openstreetmap.josm.gui.widgets.JosmTextArea;
017import org.openstreetmap.josm.tools.ImageProvider;
018
019/**
020 * Class to show user input dialog for notes. It sets up a
021 * simple label and text area to prompt for user input.
022 * @since 7720
023 */
024public class NoteInputDialog extends ExtendedDialog {
025
026    private final JosmTextArea textArea = new JosmTextArea();
027
028    /**
029     * Construct the dialog with a title and button text. A cancel button is
030     * automatically added
031     * @param parent The parent GUI element
032     * @param title Translated string to display in the dialog's title bar
033     * @param buttonText Translated string to display on the action button
034     */
035    public NoteInputDialog(Component parent, String title, String buttonText) {
036        super(parent, title, buttonText, tr("Cancel"));
037        setRememberWindowGeometry(getClass().getName() + ".geometry",
038                WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(400, 300)));
039    }
040
041    /**
042     * Displays the dialog to the user
043     * @param message Translated message to display to the user as input prompt
044     * @param icon Icon to display in the action button
045     */
046    public void showNoteDialog(String message, Icon icon) {
047        showNoteDialog(message, icon, "");
048    }
049
050    /**
051     * Displays the dialog to the user
052     * @param message Translated message to display to the user as input prompt
053     * @param icon Icon to display in the action button
054     * @param text Default text of the note's comment
055     * @since 18839
056     */
057    public void showNoteDialog(String message, Icon icon, String text) {
058        textArea.setText(text);
059        textArea.setRows(6);
060        textArea.setColumns(30);
061        textArea.setLineWrap(true);
062        textArea.setWrapStyleWord(true);
063        JScrollPane scrollPane = new JScrollPane(textArea);
064        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT); //without this the label gets pushed to the right
065
066        JLabel label = new JLabel(message);
067        label.setLabelFor(textArea);
068
069        JPanel contentPanel = new JPanel();
070        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
071        contentPanel.add(label);
072        contentPanel.add(scrollPane);
073        setContent(contentPanel, false);
074        setButtonIcons(icon, ImageProvider.get("cancel"));
075
076        showDialog();
077    }
078
079    /** Get the content of the text area
080     * @return Text input by user
081     */
082    public String getInputText() {
083        return textArea.getText();
084    }
085}