Ticket #10739: note_input.patch

File note_input.patch, 8.8 KB (added by ToeBee, 9 years ago)
  • src/org/openstreetmap/josm/actions/mapmode/AddNoteAction.java

     
    55
    66import java.awt.event.MouseEvent;
    77
    8 import javax.swing.JLabel;
    98import javax.swing.JOptionPane;
    10 import javax.swing.JScrollPane;
    11 import javax.swing.JTextArea;
    129
    1310import org.openstreetmap.josm.Main;
    1411import org.openstreetmap.josm.data.coor.LatLon;
    1512import org.openstreetmap.josm.data.osm.NoteData;
    1613import org.openstreetmap.josm.gui.MapFrame;
     14import org.openstreetmap.josm.gui.NoteInputDialog;
    1715import org.openstreetmap.josm.gui.Notification;
    1816import org.openstreetmap.josm.gui.dialogs.NoteDialog;
    1917import org.openstreetmap.josm.tools.ImageProvider;
     
    6260    public void mouseClicked(MouseEvent e) {
    6361        Main.map.selectMapMode(Main.map.mapModeSelect);
    6462        LatLon latlon = Main.map.mapView.getLatLon(e.getPoint().x, e.getPoint().y);
    65         JLabel label = new JLabel(tr("Enter a comment for a new note"));
    66         JTextArea textArea = new JTextArea();
    67         textArea.setRows(6);
    68         textArea.setColumns(30);
    69         textArea.setLineWrap(true);
    70         JScrollPane scrollPane = new JScrollPane(textArea);
    7163
    72         Object[] components = new Object[]{label, scrollPane};
    73         int option = JOptionPane.showConfirmDialog(Main.map,
    74                 components,
    75                 tr("Create new note"),
    76                 JOptionPane.OK_CANCEL_OPTION,
    77                 JOptionPane.PLAIN_MESSAGE,
    78                 NoteDialog.ICON_NEW);
    79         if (option == JOptionPane.OK_OPTION) {
    80             String input = textArea.getText();
    81             if (input != null && !input.isEmpty()) {
    82                 noteData.createNote(latlon, input);
    83             } else {
    84                 Notification notification = new Notification("You must enter a comment to create a new note");
    85                 notification.setIcon(JOptionPane.WARNING_MESSAGE);
    86                 notification.show();
    87             }
     64        NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Create new note"), tr("Create note"));
     65        dialog.showNoteDialog(tr("Enter a detailed comment to create a note"), NoteDialog.ICON_NEW);
     66
     67        if (dialog.getValue() != 1) {
     68            Main.debug("User aborted note creation");
     69            return;
    8870        }
     71        String input = dialog.getInputText();
     72        if (input != null && !input.isEmpty()) {
     73            noteData.createNote(latlon, input);
     74        } else {
     75            Notification notification = new Notification(tr("You must enter a comment to create a new note"));
     76            notification.setIcon(JOptionPane.WARNING_MESSAGE);
     77            notification.show();
     78        }
    8979    }
    9080}
  • src/org/openstreetmap/josm/gui/NoteInputDialog.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.Component;
     7
     8import javax.swing.BoxLayout;
     9import javax.swing.Icon;
     10import javax.swing.JLabel;
     11import javax.swing.JPanel;
     12import javax.swing.JScrollPane;
     13import javax.swing.JTextArea;
     14
     15import org.openstreetmap.josm.tools.ImageProvider;
     16
     17/**
     18 * Class to show user input dialog for notes. It sets up a
     19 * simple label and text area to prompt for user input
     20 */
     21public class NoteInputDialog extends ExtendedDialog {
     22
     23    private JTextArea textArea = new JTextArea();
     24
     25    /**
     26     * Construct the dialog with a title and button text. A cancel button is
     27     * automatically added
     28     * @param parent The parent GUI element
     29     * @param title Translated string to display in the dialog's title bar
     30     * @param buttonText Translated string to display on the action button
     31     */
     32    public NoteInputDialog(Component parent, String title, String buttonText) {
     33        super(parent, title, new String[] {buttonText, tr("Cancel")});
     34    }
     35
     36    /**
     37     * Displays the dialog to the user
     38     * @param message Translated message to display to the user as input prompt
     39     * @param icon Icon to display in the action button
     40     */
     41    public void showNoteDialog(String message, Icon icon) {
     42        JLabel label = new JLabel(message);
     43        textArea.setRows(6);
     44        textArea.setColumns(30);
     45        textArea.setLineWrap(true);
     46        JScrollPane scrollPane = new JScrollPane(textArea);
     47        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT); //without this the label gets pushed to the right
     48
     49        JPanel contentPanel = new JPanel();
     50        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
     51        contentPanel.add(label);
     52        contentPanel.add(scrollPane);
     53        setContent(contentPanel, false);
     54        setButtonIcons(new Icon[] {icon, ImageProvider.get("cancel.png")});
     55
     56        showDialog();
     57    }
     58
     59    /** Get the content of the text area
     60     * @return Text input by user
     61     */
     62    public String getInputText() {
     63        return textArea.getText();
     64    }
     65
     66}
  • src/org/openstreetmap/josm/gui/dialogs/NoteDialog.java

     
    3434import org.openstreetmap.josm.data.osm.NoteData;
    3535import org.openstreetmap.josm.gui.MapView;
    3636import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
     37import org.openstreetmap.josm.gui.NoteInputDialog;
    3738import org.openstreetmap.josm.gui.SideButton;
    3839import org.openstreetmap.josm.gui.layer.Layer;
    3940import org.openstreetmap.josm.gui.layer.NoteLayer;
     
    293294                        JOptionPane.ERROR_MESSAGE);
    294295                return;
    295296            }
    296             Object userInput = JOptionPane.showInputDialog(Main.map,
    297                     tr("Add comment to note:"),
    298                     tr("Add comment"),
    299                     JOptionPane.QUESTION_MESSAGE,
    300                     ICON_COMMENT,
    301                     null,null);
    302             if (userInput == null) { //user pressed cancel
     297            NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Comment on note"), tr("Add comment"));
     298            dialog.showNoteDialog(tr("Add comment to note:"), NoteDialog.ICON_COMMENT);
     299            if (dialog.getValue() != 1) {
     300                Main.debug("User aborted note reopening");
    303301                return;
    304302            }
    305             noteData.addCommentToNote(note, userInput.toString());
     303            noteData.addCommentToNote(note, dialog.getInputText());
    306304        }
    307305    }
    308306
     
    316314
    317315        @Override
    318316        public void actionPerformed(ActionEvent e) {
    319             Object userInput = JOptionPane.showInputDialog(Main.map,
    320                     tr("Close note with message:"),
    321                     tr("Close Note"),
    322                     JOptionPane.QUESTION_MESSAGE,
    323                     ICON_CLOSED,
    324                     null,null);
    325             if (userInput == null) { //user pressed cancel
     317            NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Close note"), tr("Close note"));
     318            dialog.showNoteDialog(tr("Close note with message:"), NoteDialog.ICON_CLOSED);
     319            if (dialog.getValue() != 1) {
     320                Main.debug("User aborted note closing");
    326321                return;
    327322            }
    328323            Note note = displayList.getSelectedValue();
    329             noteData.closeNote(note, userInput.toString());
     324            noteData.closeNote(note, dialog.getInputText());
    330325        }
    331326    }
    332327
     
    357352
    358353        @Override
    359354        public void actionPerformed(ActionEvent e) {
    360             Object userInput = JOptionPane.showInputDialog(Main.map,
    361                     tr("Reopen note with message:"),
    362                     tr("Reopen note"),
    363                     JOptionPane.QUESTION_MESSAGE,
    364                     ICON_OPEN,
    365                     null,null);
    366             if (userInput == null) { //user pressed cancel
     355            NoteInputDialog dialog = new NoteInputDialog(Main.parent, tr("Reopen note"), tr("Reopen note"));
     356            dialog.showNoteDialog(tr("Reopen note with message:"), NoteDialog.ICON_OPEN);
     357            if (dialog.getValue() != 1) {
     358                Main.debug("User aborted note reopening");
    367359                return;
    368360            }
     361
    369362            Note note = displayList.getSelectedValue();
    370             noteData.reOpenNote(note, userInput.toString());
     363            noteData.reOpenNote(note, dialog.getInputText());
    371364        }
    372365    }
    373366}