Ticket #23527: 23527-beta.patch

File 23527-beta.patch, 8.5 KB (added by GerdP, 11 months ago)

different approach: create new method which returns wether a saved relation would be useful

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

     
    5555import org.openstreetmap.josm.data.UndoRedoHandler;
    5656import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
    5757import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
    58 import org.openstreetmap.josm.data.osm.IRelation;
    5958import org.openstreetmap.josm.data.osm.OsmPrimitive;
    6059import org.openstreetmap.josm.data.osm.Relation;
    6160import org.openstreetmap.josm.data.osm.RelationMember;
     
    405404    /**
    406405     * builds the panel with the OK and the Cancel button
    407406     * @param okAction OK action
     407     * @param deleteAction  Delete action
    408408     * @param cancelAction Cancel action
    409409     *
    410410     * @return the panel with the OK and the Cancel button
     
    421421        pnl.add(new JButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor"))));
    422422        // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type"
    423423        // AND must contain at least one other OSM object.
    424         final TableModelListener listener = l -> updateOkPanel(this.actionAccess.getChangedRelation(), okButton, deleteButton);
     424        final TableModelListener listener = l -> updateOkPanel(okButton, deleteButton);
    425425        listener.tableChanged(null);
    426426        this.memberTableModel.addTableModelListener(listener);
    427427        this.tagEditorPanel.getModel().addTableModelListener(listener);
     
    429429    }
    430430
    431431    /**
    432      * Update the OK panel area
    433      * @param newRelation What the new relation would "look" like if it were to be saved now
     432     * Update the OK panel area with a temporary relation that looks if it were to be saved now.
    434433     * @param okButton The OK button
    435434     * @param deleteButton The delete button
    436435     */
    437     private void updateOkPanel(IRelation<?> newRelation, JButton okButton, JButton deleteButton) {
    438         okButton.setVisible(newRelation.isUseful() || this.getRelationSnapshot() == null);
    439         deleteButton.setVisible(!newRelation.isUseful() && this.getRelationSnapshot() != null);
    440         if (this.getRelationSnapshot() == null && !newRelation.isUseful()) {
     436    private void updateOkPanel(JButton okButton, JButton deleteButton) {
     437        boolean useful = this.actionAccess.wouldRelationBeUseful();
     438        okButton.setVisible(useful || this.getRelationSnapshot() == null);
     439        deleteButton.setVisible(!useful && this.getRelationSnapshot() != null);
     440        if (this.getRelationSnapshot() == null && !useful) {
    441441            okButton.setText(tr("Delete"));
    442442        } else {
    443443            okButton.setText(tr("OK"));
  • src/org/openstreetmap/josm/gui/dialogs/relation/actions/ApplyAction.java

     
    3535
    3636    @Override
    3737    public void updateEnabledState() {
    38         setEnabled(this.editorAccess.getChangedRelation().isUseful() && isEditorDirty());
     38        setEnabled(this.editorAccess.wouldRelationBeUseful() && isEditorDirty());
    3939    }
    4040}
  • src/org/openstreetmap/josm/gui/dialogs/relation/actions/CancelAction.java

     
    88import javax.swing.JOptionPane;
    99import javax.swing.RootPaneContainer;
    1010
    11 import org.openstreetmap.josm.data.osm.IRelation;
    1211import org.openstreetmap.josm.data.osm.Relation;
    1312import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    1413import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
     
    4847        if ((!getMemberTableModel().hasSameMembersAs(snapshot) || getTagModel().isDirty())
    4948         && !(snapshot == null && getTagModel().getTags().isEmpty())) {
    5049            //give the user a chance to save the changes
    51             int ret = confirmClosingByCancel(this.editorAccess.getChangedRelation());
     50            int ret = confirmClosingByCancel(this.editorAccess.wouldRelationBeUseful());
    5251            if (ret == 0) { //Yes, save the changes
    5352                //copied from OKAction.run()
    5453                Config.getPref().put("relation.editor.generic.lastrole", Utils.strip(tfRole.getText()));
     
    6160        hideEditor();
    6261    }
    6362
    64     protected int confirmClosingByCancel(final IRelation<?> newRelation) {
     63    protected int confirmClosingByCancel(boolean isUseful) {
    6564        ButtonSpec[] options = {
    6665                new ButtonSpec(
    6766                        tr("Yes, save the changes and close"),
     
    8584
    8685        // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type"
    8786        // AND must contain at least one other OSM object.
    88         options[0].setEnabled(newRelation.isUseful());
     87        options[0].setEnabled(isUseful);
    8988
    9089        return HelpAwareOptionPane.showOptionDialog(
    9190                MainApplication.getMainFrame(),
  • src/org/openstreetmap/josm/gui/dialogs/relation/actions/IRelationEditorActionAccess.java

     
    7474     * @return The changed relation (note: will not be part of a dataset). This should never be {@code null}.
    7575     * @since 18413
    7676     */
     77    @Deprecated
    7778    default IRelation<?> getChangedRelation() {
    7879        final Relation newRelation;
    7980        final Relation oldRelation = getEditor().getRelation();
     
    9798    }
    9899
    99100    /**
     101     * Check if the changed relation would be useful.
     102     * @return true if the saved relation would be useful
     103     * @since xxx
     104     */
     105    default boolean wouldRelationBeUseful() {
     106        final Relation newRelation;
     107        final Relation oldRelation = getEditor().getRelation();
     108        boolean isUploadInProgress = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class)
     109                .stream().anyMatch(OsmDataLayer::isUploadInProgress);
     110        if (isUploadInProgress || (oldRelation != null && oldRelation.getDataSet() != null && oldRelation.getDataSet().isLocked())) {
     111            // If the dataset is locked, then we cannot change the relation. See JOSM #22024.
     112            // We should also avoid changing the relation if there is an upload in progress. See JOSM #22268/#22398.
     113            // There appears to be a race condition where a dataset might not be locked in the check, then is locked while using the
     114            // copy relation constructor.
     115            // This is due to the `setMembers` -> `addReferrer` call chain requires that the dataset is not read only.
     116            return oldRelation != null && oldRelation.isUseful();
     117        } else if (oldRelation != null) {
     118            newRelation = new Relation(oldRelation);
     119        } else {
     120            newRelation = new Relation();
     121        }
     122        getTagModel().applyToPrimitive(newRelation);
     123        getMemberTableModel().applyToRelation(newRelation);
     124        boolean res = newRelation.isUseful();
     125        newRelation.setMembers(null);
     126        return res;
     127    }
     128
     129    /**
    100130     * Get the text field that is used to edit the role.
    101131     * @return The role text field.
    102132     */
  • test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java

     
    169169        relationEditorAccess.getTagModel().initFromPrimitive(relation);
    170170        relationEditorAccess.getEditor().reloadDataFromRelation();
    171171        assertDoesNotThrow(relationEditorAccess::getChangedRelation);
     172        assertDoesNotThrow(relationEditorAccess::wouldRelationBeUseful);
    172173    }
    173174}