Ticket #21825: 21825.4.patch
File 21825.4.patch, 18.0 KB (added by , 3 years ago) |
---|
-
src/org/openstreetmap/josm/data/osm/IRelation.java
diff --git a/src/org/openstreetmap/josm/data/osm/IRelation.java b/src/org/openstreetmap/josm/data/osm/IRelation.java index 6d59bd427a..9a7897d36f 100644
a b 2 2 package org.openstreetmap.josm.data.osm; 3 3 4 4 import java.util.Collection; 5 import java.util.Collections; 5 6 import java.util.List; 6 7 import java.util.stream.Collectors; 7 8 9 import org.openstreetmap.josm.data.validation.TestError; 8 10 import org.openstreetmap.josm.tools.Utils; 9 11 10 12 /** … … public interface IRelation<M extends IRelationMember<?>> extends IPrimitive { 138 140 return getMembers().stream().filter(rmv -> role.equals(rmv.getRole())) 139 141 .map(IRelationMember::getMember).collect(Collectors.toList()); 140 142 } 143 144 /** 145 * Check if this relation is useful 146 * @return {@code true} if this relation is useful 147 */ 148 default boolean isUseful() { 149 return !this.isEmpty() && this.hasKey("type"); 150 } 151 152 /** 153 * Check if this relation is valid 154 * @return A collection of errors, if any 155 */ 156 default Collection<TestError> validate() { 157 return Collections.emptyList(); 158 } 141 159 } -
src/org/openstreetmap/josm/data/osm/Relation.java
diff --git a/src/org/openstreetmap/josm/data/osm/Relation.java b/src/org/openstreetmap/josm/data/osm/Relation.java index 3b12c48c80..1653b30f2e 100644
a b import java.util.stream.Stream; 14 14 15 15 import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor; 16 16 import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor; 17 import org.openstreetmap.josm.data.validation.OsmValidator; 18 import org.openstreetmap.josm.data.validation.TestError; 19 import org.openstreetmap.josm.data.validation.tests.MultipolygonTest; 20 import org.openstreetmap.josm.data.validation.tests.RelationChecker; 21 import org.openstreetmap.josm.data.validation.tests.TurnrestrictionTest; 22 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 17 23 import org.openstreetmap.josm.spi.preferences.Config; 18 24 import org.openstreetmap.josm.tools.CopyList; 19 25 import org.openstreetmap.josm.tools.SubclassFilteredCollection; … … public final class Relation extends OsmPrimitive implements IRelation<RelationMe 567 573 public UniqueIdGenerator getIdGenerator() { 568 574 return idGenerator; 569 575 } 576 577 @Override 578 public Collection<TestError> validate() { 579 return Stream.of(MultipolygonTest.class, TurnrestrictionTest.class, RelationChecker.class) 580 .map(OsmValidator::getTest).filter(test -> test.enabled || test.testBeforeUpload).flatMap(test -> { 581 test.startTest(NullProgressMonitor.INSTANCE); 582 test.visit(this); 583 test.endTest(); 584 return test.getErrors().stream(); 585 }).collect(Collectors.toList()); 586 } 570 587 } -
src/org/openstreetmap/josm/data/validation/tests/RelationChecker.java
diff --git a/src/org/openstreetmap/josm/data/validation/tests/RelationChecker.java b/src/org/openstreetmap/josm/data/validation/tests/RelationChecker.java index 959f73d1d0..1c10a7c575 100644
a b public class RelationChecker extends Test implements TaggingPresetListener { 154 154 .message(tr("Route scheme is unspecified. Add {0} ({1}=public_transport; {2}=legacy)", "public_transport:version", "2", "1")) 155 155 .primitives(n) 156 156 .build()); 157 } else if ( n.hasKey("type") && allroles.isEmpty()) {157 } else if ((n.hasKey("type") && allroles.isEmpty()) || !n.hasKey("type")) { 158 158 errors.add(TestError.builder(this, Severity.OTHER, RELATION_UNKNOWN) 159 159 .message(tr("Relation type is unknown")) 160 160 .primitives(n) -
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..9665db215d 100644
a b package org.openstreetmap.josm.gui.dialogs.relation; 3 3 4 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 import static org.openstreetmap.josm.tools.I18n.trn; 6 7 7 8 import java.awt.BorderLayout; 8 9 import java.awt.Component; … … import java.util.ArrayList; 26 27 import java.util.Arrays; 27 28 import java.util.Collection; 28 29 import java.util.Collections; 30 import java.util.Comparator; 29 31 import java.util.EnumSet; 30 32 import java.util.List; 31 33 import java.util.Set; … … import javax.swing.JTabbedPane; 47 49 import javax.swing.JTable; 48 50 import javax.swing.JToolBar; 49 51 import javax.swing.KeyStroke; 52 import javax.swing.event.TableModelListener; 50 53 51 54 import org.openstreetmap.josm.actions.JosmAction; 52 55 import org.openstreetmap.josm.command.ChangeMembersCommand; … … import org.openstreetmap.josm.command.Command; 54 57 import org.openstreetmap.josm.data.UndoRedoHandler; 55 58 import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener; 56 59 import org.openstreetmap.josm.data.osm.DefaultNameFormatter; 60 import org.openstreetmap.josm.data.osm.IRelation; 57 61 import org.openstreetmap.josm.data.osm.OsmPrimitive; 58 62 import org.openstreetmap.josm.data.osm.Relation; 59 63 import org.openstreetmap.josm.data.osm.RelationMember; 60 64 import org.openstreetmap.josm.data.osm.Tag; 65 import org.openstreetmap.josm.data.validation.TestError; 61 66 import org.openstreetmap.josm.data.validation.tests.RelationChecker; 62 67 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 63 68 import org.openstreetmap.josm.gui.MainApplication; … … import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 108 113 import org.openstreetmap.josm.gui.util.WindowGeometry; 109 114 import org.openstreetmap.josm.spi.preferences.Config; 110 115 import org.openstreetmap.josm.tools.CheckParameterUtil; 116 import org.openstreetmap.josm.tools.GBC; 117 import org.openstreetmap.josm.tools.ImageProvider; 111 118 import org.openstreetmap.josm.tools.InputMapUtils; 112 119 import org.openstreetmap.josm.tools.Logging; 113 120 import org.openstreetmap.josm.tools.Shortcut; … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 132 139 private final SelectionTableModel selectionTableModel; 133 140 134 141 private final AutoCompletingTextField tfRole; 142 private final RelationEditorActionAccess actionAccess; 135 143 136 144 /** 137 145 * the menu item in the windows menu. Required to properly hide on dialog close. … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 262 270 selectedTabPane = sourceTabbedPane.getSelectedComponent(); 263 271 }); 264 272 265 IRelationEditorActionAccessactionAccess = new RelationEditorActionAccess();273 actionAccess = new RelationEditorActionAccess(); 266 274 267 275 refreshAction = new RefreshAction(actionAccess); 268 276 applyAction = new ApplyAction(actionAccess); 269 277 selectAction = new SelectAction(actionAccess); 270 278 duplicateAction = new DuplicateRelationAction(actionAccess); 271 279 deleteAction = new DeleteCurrentRelationAction(actionAccess); 280 281 this.memberTableModel.addTableModelListener(applyAction); 282 this.tagEditorPanel.getModel().addTableModelListener(applyAction); 283 272 284 addPropertyChangeListener(deleteAction); 273 285 274 286 okAction = new OKAction(actionAccess); … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 276 288 277 289 getContentPane().add(buildToolBar(refreshAction, applyAction, selectAction, duplicateAction, deleteAction), BorderLayout.NORTH); 278 290 getContentPane().add(tabbedPane, BorderLayout.CENTER); 279 getContentPane().add(buildOkCancelButtonPanel(okAction, cancelAction), BorderLayout.SOUTH);291 getContentPane().add(buildOkCancelButtonPanel(okAction, deleteAction, cancelAction), BorderLayout.SOUTH); 280 292 281 293 setSize(findMaxDialogSize()); 282 294 … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 407 419 * 408 420 * @return the panel with the OK and the Cancel button 409 421 */ 410 protected static JPanel buildOkCancelButtonPanel(OKAction okAction, CancelAction cancelAction) { 411 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); 412 pnl.add(new JButton(okAction)); 422 protected JPanel buildOkCancelButtonPanel(OKAction okAction, DeleteCurrentRelationAction deleteAction, 423 CancelAction cancelAction) { 424 JPanel mainPanel = new JPanel(new GridBagLayout()); 425 final JLabel errorLabel = new JLabel(" "); 426 final JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); 427 mainPanel.add(errorLabel, GBC.eol().anchor(GridBagConstraints.CENTER)); 428 mainPanel.add(pnl, GBC.eol().anchor(GridBagConstraints.CENTER)); 429 final JButton okButton = new JButton(okAction); 430 final JButton deleteButton = new JButton(deleteAction); 431 okButton.setPreferredSize(deleteButton.getPreferredSize()); 432 pnl.add(okButton); 433 pnl.add(deleteButton); 413 434 pnl.add(new JButton(cancelAction)); 414 435 pnl.add(new JButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor")))); 415 return pnl; 436 // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type" 437 // AND must contain at least one other OSM object. 438 final TableModelListener listener = l -> { 439 final IRelation<?> newRelation = this.actionAccess.getChangedRelation(); 440 updateOkPanel(newRelation, okButton, deleteButton); 441 updateErrorMessage(newRelation, errorLabel); 442 }; 443 listener.tableChanged(null); 444 this.memberTableModel.addTableModelListener(listener); 445 this.tagEditorPanel.getModel().addTableModelListener(listener); 446 return mainPanel; 447 } 448 449 /** 450 * Update the OK panel area 451 * @param newRelation What the new relation would "look" like if it were to be saved now 452 * @param okButton The OK button 453 * @param deleteButton The delete button 454 */ 455 private void updateOkPanel(IRelation<?> newRelation, JButton okButton, JButton deleteButton) { 456 okButton.setVisible(newRelation.isUseful() || this.getRelationSnapshot() == null); 457 deleteButton.setVisible(!newRelation.isUseful() && this.getRelationSnapshot() != null); 458 if (this.getRelationSnapshot() == null && !newRelation.isUseful()) { 459 okButton.setText(tr("Delete")); 460 } else { 461 okButton.setText(tr("OK")); 462 } 463 } 464 465 /** 466 * Update the error message 467 * @param newRelation What the new relation would "look" like if it were to be saved now 468 * @param errorLabel The label to update 469 */ 470 private void updateErrorMessage(IRelation<?> newRelation, JLabel errorLabel) { 471 Collection<TestError> errors = newRelation.validate(); 472 if (errors.isEmpty()) { 473 // Setting " " helps keep the label in place for layout calculations 474 errorLabel.setText(" "); 475 errorLabel.setIcon(null); 476 } else { 477 final TestError error = errors.stream() 478 .min(Comparator.comparingInt(testError -> testError.getSeverity().getLevel())) 479 .orElse(errors.iterator().next()); 480 final StringBuilder sb = new StringBuilder(); 481 if (error.getDescription() != null) { 482 sb.append(error.getDescription()).append(": "); 483 } 484 sb.append(error.getMessage()); 485 if (errors.size() > 1) { 486 sb.append(trn(" with {0} more problem", " with {0} more problems", errors.size() - 1, errors.size() - 1)); 487 } 488 errorLabel.setIcon(ImageProvider.get("data", error.getSeverity().getIcon())); 489 errorLabel.setText(sb.toString()); 490 } 416 491 } 417 492 418 493 /** … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 1001 1076 @Override 1002 1077 public void mouseClicked(MouseEvent e) { 1003 1078 if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) { 1004 new EditAction( new RelationEditorActionAccess()).actionPerformed(null);1079 new EditAction(actionAccess).actionPerformed(null); 1005 1080 } 1006 1081 } 1007 1082 } -
src/org/openstreetmap/josm/gui/dialogs/relation/actions/ApplyAction.java
diff --git a/src/org/openstreetmap/josm/gui/dialogs/relation/actions/ApplyAction.java b/src/org/openstreetmap/josm/gui/dialogs/relation/actions/ApplyAction.java index 7ddfae63ac..2a51047659 100644
a b import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 6 import java.awt.event.ActionEvent; 7 7 8 import org.openstreetmap.josm.data.osm.IRelation; 8 9 import org.openstreetmap.josm.tools.ImageProvider; 9 10 10 11 /** … … public class ApplyAction extends SavingAction { 35 36 36 37 @Override 37 38 public void updateEnabledState() { 38 setEnabled(isEditorDirty()); 39 final IRelation<?> currentRelation = editorAccess.getChangedRelation(); 40 setEnabled(currentRelation.isUseful() && isEditorDirty()); 39 41 } 40 42 } -
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..a51be40834 100644
a b import java.awt.event.ActionEvent; 8 8 import javax.swing.JOptionPane; 9 9 import javax.swing.RootPaneContainer; 10 10 11 import org.openstreetmap.josm.data.osm.IRelation; 11 12 import org.openstreetmap.josm.data.osm.Relation; 12 13 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 13 14 import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; … … public class CancelAction extends SavingAction { 47 48 if ((!getMemberTableModel().hasSameMembersAs(snapshot) || getTagModel().isDirty()) 48 49 && !(snapshot == null && getTagModel().getTags().isEmpty())) { 49 50 //give the user a chance to save the changes 50 int ret = confirmClosingByCancel( );51 int ret = confirmClosingByCancel(this.editorAccess.getChangedRelation()); 51 52 if (ret == 0) { //Yes, save the changes 52 53 //copied from OKAction.run() 53 54 Config.getPref().put("relation.editor.generic.lastrole", Utils.strip(tfRole.getText())); … … public class CancelAction extends SavingAction { 60 61 hideEditor(); 61 62 } 62 63 63 protected int confirmClosingByCancel( ) {64 protected int confirmClosingByCancel(final IRelation<?> newRelation) { 64 65 ButtonSpec[] options = { 65 66 new ButtonSpec( 66 67 tr("Yes, save the changes and close"), … … public class CancelAction extends SavingAction { 82 83 ) 83 84 }; 84 85 86 // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type" 87 // AND must contain at least one other OSM object. 88 options[0].setEnabled(newRelation.isUseful()); 89 85 90 return HelpAwareOptionPane.showOptionDialog( 86 91 MainApplication.getMainFrame(), 87 92 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/IRelationEditorActionAccess.java
diff --git a/src/org/openstreetmap/josm/gui/dialogs/relation/actions/IRelationEditorActionAccess.java b/src/org/openstreetmap/josm/gui/dialogs/relation/actions/IRelationEditorActionAccess.java index cddedaf165..b627262197 100644
a b package org.openstreetmap.josm.gui.dialogs.relation.actions; 3 3 4 4 import javax.swing.Action; 5 5 6 import org.openstreetmap.josm.data.osm.IRelation; 7 import org.openstreetmap.josm.data.osm.Relation; 6 8 import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor; 7 9 import org.openstreetmap.josm.gui.dialogs.relation.MemberTable; 8 10 import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel; … … public interface IRelationEditorActionAccess { 65 67 */ 66 68 TagEditorModel getTagModel(); 67 69 70 /** 71 * Get the changed relation 72 * @return The changed relation (note: may not be part of a dataset). This should never be {@code null}. 73 * @since xxx 74 */ 75 default IRelation<?> getChangedRelation() { 76 final Relation newRelation; 77 if (getEditor().getRelation() != null) { 78 newRelation = getEditor().getRelation(); 79 } else { 80 newRelation = new Relation(); 81 getTagModel().applyToPrimitive(newRelation); 82 getMemberTableModel().applyToRelation(newRelation); 83 } 84 return newRelation; 85 } 86 68 87 /** 69 88 * Get the text field that is used to edit the role. 70 89 * @return The role text field. -
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..35ca1dcfb7 100644
a b abstract class SavingAction extends AbstractRelationEditorAction { 54 54 tagEditorModel.applyToPrimitive(newRelation); 55 55 getMemberTableModel().applyToRelation(newRelation); 56 56 // If the user wanted to create a new relation, but hasn't added any members or 57 // tags , don't add an emptyrelation58 if ( newRelation.isEmpty() && !newRelation.hasKeys())57 // tags (specifically the "type" tag), don't add the relation 58 if (!newRelation.isUseful()) 59 59 return; 60 60 UndoRedoHandler.getInstance().add(new AddCommand(getLayer().getDataSet(), newRelation)); 61 61