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        textArea.setRows(6);
048        textArea.setColumns(30);
049        textArea.setLineWrap(true);
050        textArea.setWrapStyleWord(true);
051        JScrollPane scrollPane = new JScrollPane(textArea);
052        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT); //without this the label gets pushed to the right
053
054        JLabel label = new JLabel(message);
055        label.setLabelFor(textArea);
056
057        JPanel contentPanel = new JPanel();
058        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
059        contentPanel.add(label);
060        contentPanel.add(scrollPane);
061        setContent(contentPanel, false);
062        setButtonIcons(icon, ImageProvider.get("cancel"));
063
064        showDialog();
065    }
066
067    /** Get the content of the text area
068     * @return Text input by user
069     */
070    public String getInputText() {
071        return textArea.getText();
072    }
073}