Changeset 17500 in josm


Ignore:
Timestamp:
2021-02-21T01:02:50+01:00 (11 days ago)
Author:
Don-vip
Message:

see #20493 - add initial support to comment changesets directly from JOSM

Doesn't seem to work yet, server always answer HTTP 401 even if OAuth authorization headers are set?

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetDiscussionPanel.java

    r17371 r17500  
    1515import javax.swing.AbstractAction;
    1616import javax.swing.BorderFactory;
     17import javax.swing.JOptionPane;
    1718import javax.swing.JPanel;
    1819import javax.swing.JScrollPane;
    1920import javax.swing.JTable;
    2021import javax.swing.JToolBar;
     22import javax.swing.SwingConstants;
    2123
    2224import org.openstreetmap.josm.actions.downloadtasks.ChangesetHeaderDownloadTask;
     
    2426import org.openstreetmap.josm.data.osm.Changeset;
    2527import org.openstreetmap.josm.gui.MainApplication;
     28import org.openstreetmap.josm.gui.NoteInputDialog;
    2629import org.openstreetmap.josm.io.NetworkManager;
    2730import org.openstreetmap.josm.io.OnlineResource;
     31import org.openstreetmap.josm.io.OsmApi;
     32import org.openstreetmap.josm.io.OsmTransferException;
     33import org.openstreetmap.josm.tools.ExceptionUtil;
    2834import org.openstreetmap.josm.tools.ImageProvider;
     35import org.openstreetmap.josm.tools.Logging;
    2936
    3037/**
     
    3946
    4047    private final UpdateChangesetDiscussionAction actUpdateChangesets = new UpdateChangesetDiscussionAction();
     48    private final AddChangesetCommentAction actAddChangesetComment = new AddChangesetCommentAction();
    4149
    4250    private final ChangesetDiscussionTableModel model = new ChangesetDiscussionTableModel();
     
    4957        JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
    5058
    51         JToolBar tb = new JToolBar(JToolBar.VERTICAL);
     59        JToolBar tb = new JToolBar(SwingConstants.VERTICAL);
    5260        tb.setFloatable(false);
    5361
    5462        // -- changeset discussion update
    5563        tb.add(actUpdateChangesets);
    56         actUpdateChangesets.initProperties(current);
     64        // -- add a comment to changeset discussion
     65        tb.add(actAddChangesetComment);
     66
     67        initProperties();
    5768
    5869        pnl.add(tb);
    5970        return pnl;
     71    }
     72
     73    void initProperties() {
     74        actUpdateChangesets.initProperties(current);
     75        actAddChangesetComment.initProperties(current);
    6076    }
    6177
     
    8298        }
    8399
    84         public void initProperties(Changeset cs) {
     100        void initProperties(Changeset cs) {
    85101            setEnabled(cs != null && !NetworkManager.isOffline(OnlineResource.OSM_API));
     102        }
     103    }
     104
     105    /**
     106     * Adds a discussion comment to the current changeset
     107     */
     108    class AddChangesetCommentAction extends AbstractAction {
     109        AddChangesetCommentAction() {
     110            putValue(NAME, tr("Comment"));
     111            new ImageProvider("dialogs/notes", "note_comment").getResource().attachImageIcon(this);
     112            putValue(SHORT_DESCRIPTION, tr("Add comment"));
     113        }
     114
     115        @Override
     116        public void actionPerformed(ActionEvent evt) {
     117            if (current == null)
     118                return;
     119            NoteInputDialog dialog = new NoteInputDialog(MainApplication.getMainFrame(), tr("Comment on changeset"), tr("Add comment"));
     120            dialog.showNoteDialog(tr("Add comment to changeset:"), ImageProvider.get("dialogs/notes", "note_comment"));
     121            if (dialog.getValue() != 1) {
     122                return;
     123            }
     124            try {
     125                OsmApi.getOsmApi().addCommentToChangeset(current, dialog.getInputText(), null);
     126            } catch (OsmTransferException e) {
     127                Logging.error(e);
     128                JOptionPane.showMessageDialog(
     129                        MainApplication.getMainFrame(),
     130                        ExceptionUtil.explainOsmTransferException(e),
     131                        tr("Error"),
     132                        JOptionPane.ERROR_MESSAGE);
     133            }
     134        }
     135
     136        void initProperties(Changeset cs) {
     137            setEnabled(cs != null && !cs.isOpen() && !NetworkManager.isOffline(OnlineResource.OSM_API));
    86138        }
    87139    }
     
    101153            updateView(cs);
    102154        }
    103         actUpdateChangesets.initProperties(current);
     155        initProperties();
    104156        if (cs != null && cs.getDiscussion().size() < cs.getCommentsCount()) {
    105157            actUpdateChangesets.actionPerformed(null);
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r17498 r17500  
    536536
    537537    /**
     538     * Adds a comment to the discussion of a closed changeset.
     539     *
     540     * @param changeset the changeset where to add a comment. Must be closed. changeset.getId() &gt; 0 required.
     541     * @param comment Text of the comment
     542     * @param monitor the progress monitor. If null, uses {@link NullProgressMonitor#INSTANCE}
     543     *
     544     * @throws OsmTransferException if something goes wrong.
     545     * @since 17500
     546     */
     547    public void addCommentToChangeset(Changeset changeset, String comment, ProgressMonitor monitor) throws OsmTransferException {
     548        if (changeset.isOpen())
     549            throw new IllegalArgumentException(tr("Changeset must be closed in order to add a comment"));
     550        else if (changeset.getId() <= 0)
     551            throw new IllegalArgumentException(tr("Changeset ID > 0 expected. Got {0}.", changeset.getId()));
     552        sendRequest("POST", "changeset/" + changeset.getId() + "/comment",
     553                "text=" + Utils.encodeUrl(comment), monitor, "application/x-www-form-urlencoded", true, false);
     554    }
     555
     556    /**
    538557     * Uploads a list of changes in "diff" form to the server.
    539558     *
     
    644663    }
    645664
     665    protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor,
     666            boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
     667        return sendRequest(requestMethod, urlSuffix, requestBody, monitor, null, doAuthenticate, fastFail);
     668    }
     669
    646670    /**
    647671     * Generic method for sending requests to the OSM API.
     
    655679     * @param requestBody the body of the HTTP request, if any.
    656680     * @param monitor the progress monitor
    657      * @param doAuthenticate  set to true, if the request sent to the server shall include authentication
    658      * credentials;
     681     * @param contentType Content-Type to set for PUT/POST/DELETE requests.
     682     *    Can be set to {@code null}, in that case it means {@code text/xml}
     683     * @param doAuthenticate  set to true, if the request sent to the server shall include authentication credentials;
    659684     * @param fastFail true to request a short timeout
    660685     *
     
    664689     */
    665690    protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor,
    666             boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
     691            String contentType, boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
    667692        int retries = fastFail ? 0 : getMaxRetries();
    668693
     
    686711
    687712                if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) {
    688                     client.setHeader("Content-Type", "text/xml");
     713                    client.setHeader("Content-Type", contentType == null ? "text/xml" : contentType);
    689714                    // It seems that certain bits of the Ruby API are very unhappy upon
    690715                    // receipt of a PUT/POST message without a Content-length header,
Note: See TracChangeset for help on using the changeset viewer.