Ticket #21825: 21825.2.patch

File 21825.2.patch, 5.0 KB (added by taylor.smock, 3 years ago)

Prevent users from saving invalid relations

  • src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java b/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java
    index 743e05f4e0..7ab8f6638f 100644
    a b import javax.swing.JTabbedPane;  
    4747import javax.swing.JTable;
    4848import javax.swing.JToolBar;
    4949import javax.swing.KeyStroke;
     50import javax.swing.event.TableModelListener;
    5051
    5152import org.openstreetmap.josm.actions.JosmAction;
    5253import org.openstreetmap.josm.command.ChangeMembersCommand;
    public class GenericRelationEditor extends RelationEditor implements CommandQueu  
    276277
    277278        getContentPane().add(buildToolBar(refreshAction, applyAction, selectAction, duplicateAction, deleteAction), BorderLayout.NORTH);
    278279        getContentPane().add(tabbedPane, BorderLayout.CENTER);
    279         getContentPane().add(buildOkCancelButtonPanel(okAction, cancelAction), BorderLayout.SOUTH);
     280        getContentPane().add(buildOkCancelButtonPanel(okAction, deleteAction, cancelAction), BorderLayout.SOUTH);
    280281
    281282        setSize(findMaxDialogSize());
    282283
    public class GenericRelationEditor extends RelationEditor implements CommandQueu  
    407408     *
    408409     * @return the panel with the OK and the Cancel button
    409410     */
    410     protected static JPanel buildOkCancelButtonPanel(OKAction okAction, CancelAction cancelAction) {
     411    protected JPanel buildOkCancelButtonPanel(OKAction okAction, DeleteCurrentRelationAction deleteAction,
     412            CancelAction cancelAction) {
    411413        JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
    412         pnl.add(new JButton(okAction));
     414        final JButton okButton = new JButton(okAction);
     415        final JButton deleteButton = new JButton(deleteAction);
     416        okButton.setPreferredSize(deleteButton.getPreferredSize());
     417        pnl.add(okButton);
     418        pnl.add(deleteButton);
    413419        pnl.add(new JButton(cancelAction));
    414420        pnl.add(new JButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor"))));
     421        // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type"
     422        // AND must contain at least one other OSM object.
     423        final TableModelListener listener = l -> {
     424            final boolean delete = this.memberTableModel.getRowCount() == 0
     425                    || !this.tagEditorPanel.getModel().includesTag("type");
     426            okButton.setVisible(!delete || this.getRelationSnapshot() == null);
     427            deleteButton.setVisible(delete && this.getRelationSnapshot() != null);
     428            if (this.getRelationSnapshot() == null && delete) {
     429                okButton.setText(tr("Delete"));
     430            } else {
     431                okButton.setText(tr("OK"));
     432            }
     433        };
     434        listener.tableChanged(null);
     435        this.memberTableModel.addTableModelListener(listener);
     436        this.tagEditorPanel.getModel().addTableModelListener(listener);
    415437        return pnl;
    416438    }
    417439
  • src/org/openstreetmap/josm/gui/dialogs/relation/actions/CancelAction.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/relation/actions/CancelAction.java b/src/org/openstreetmap/josm/gui/dialogs/relation/actions/CancelAction.java
    index 6efdaeabdf..c41649b396 100644
    a b public class CancelAction extends SavingAction {  
    8282                )
    8383        };
    8484
     85        // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type"
     86        // AND must contain at least one other OSM object.
     87        options[0].setEnabled(getMemberTableModel().getRowCount() > 0 && getTagModel().includesTag("type"));
     88
    8589        return HelpAwareOptionPane.showOptionDialog(
    8690                MainApplication.getMainFrame(),
    8791                tr("<html>The relation has been changed.<br><br>Do you want to save your changes?</html>"),
  • src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java b/src/org/openstreetmap/josm/gui/dialogs/relation/actions/SavingAction.java
    index 26813f23c1..d83ba8f6cd 100644
    a b abstract class SavingAction extends AbstractRelationEditorAction {  
    5454        tagEditorModel.applyToPrimitive(newRelation);
    5555        getMemberTableModel().applyToRelation(newRelation);
    5656        // If the user wanted to create a new relation, but hasn't added any members or
    57         // tags, don't add an empty relation
    58         if (newRelation.isEmpty() && !newRelation.hasKeys())
     57        // tags (specifically the "type" tag), don't add the relation
     58        if (newRelation.isEmpty() || !newRelation.hasKey("type"))
    5959            return;
    6060        UndoRedoHandler.getInstance().add(new AddCommand(getLayer().getDataSet(), newRelation));
    6161