Ticket #21825: 21825.3.patch
| File 21825.3.patch, 14.1 KB (added by , 4 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..d0405bd938 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((Relation) 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..26184c7725 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..52f17e72e7 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.data.osm.OsmPrimitive; 58 61 import org.openstreetmap.josm.data.osm.Relation; 59 62 import org.openstreetmap.josm.data.osm.RelationMember; 60 63 import org.openstreetmap.josm.data.osm.Tag; 64 import org.openstreetmap.josm.data.validation.TestError; 61 65 import org.openstreetmap.josm.data.validation.tests.RelationChecker; 62 66 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 63 67 import org.openstreetmap.josm.gui.MainApplication; … … import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 108 112 import org.openstreetmap.josm.gui.util.WindowGeometry; 109 113 import org.openstreetmap.josm.spi.preferences.Config; 110 114 import org.openstreetmap.josm.tools.CheckParameterUtil; 115 import org.openstreetmap.josm.tools.GBC; 116 import org.openstreetmap.josm.tools.ImageProvider; 111 117 import org.openstreetmap.josm.tools.InputMapUtils; 112 118 import org.openstreetmap.josm.tools.Logging; 113 119 import org.openstreetmap.josm.tools.Shortcut; … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 276 282 277 283 getContentPane().add(buildToolBar(refreshAction, applyAction, selectAction, duplicateAction, deleteAction), BorderLayout.NORTH); 278 284 getContentPane().add(tabbedPane, BorderLayout.CENTER); 279 getContentPane().add(buildOkCancelButtonPanel(okAction, cancelAction), BorderLayout.SOUTH);285 getContentPane().add(buildOkCancelButtonPanel(okAction, deleteAction, cancelAction), BorderLayout.SOUTH); 280 286 281 287 setSize(findMaxDialogSize()); 282 288 … … public class GenericRelationEditor extends RelationEditor implements CommandQueu 407 413 * 408 414 * @return the panel with the OK and the Cancel button 409 415 */ 410 protected static JPanel buildOkCancelButtonPanel(OKAction okAction, CancelAction cancelAction) { 411 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); 412 pnl.add(new JButton(okAction)); 416 protected JPanel buildOkCancelButtonPanel(OKAction okAction, DeleteCurrentRelationAction deleteAction, 417 CancelAction cancelAction) { 418 JPanel mainPanel = new JPanel(new GridBagLayout()); 419 final JLabel errorLabel = new JLabel(" "); 420 final JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); 421 mainPanel.add(errorLabel, GBC.eol().anchor(GridBagConstraints.CENTER)); 422 mainPanel.add(pnl, GBC.eol().anchor(GridBagConstraints.CENTER)); 423 final JButton okButton = new JButton(okAction); 424 final JButton deleteButton = new JButton(deleteAction); 425 okButton.setPreferredSize(deleteButton.getPreferredSize()); 426 pnl.add(okButton); 427 pnl.add(deleteButton); 413 428 pnl.add(new JButton(cancelAction)); 414 429 pnl.add(new JButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor")))); 415 return pnl; 430 // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type" 431 // AND must contain at least one other OSM object. 432 final TableModelListener listener = l -> { 433 final Relation newRelation = new Relation(); 434 this.tagEditorPanel.getModel().applyToPrimitive(newRelation); 435 this.memberTableModel.applyToRelation(newRelation); 436 updateOkPanel(newRelation, okButton, deleteButton); 437 updateErrorMessage(newRelation, errorLabel); 438 }; 439 listener.tableChanged(null); 440 this.memberTableModel.addTableModelListener(listener); 441 this.tagEditorPanel.getModel().addTableModelListener(listener); 442 return mainPanel; 443 } 444 445 /** 446 * Update the OK panel area 447 * @param newRelation What the new relation would "look" like if it were to be saved now 448 * @param okButton The OK button 449 * @param deleteButton The delete button 450 */ 451 private void updateOkPanel(Relation newRelation, JButton okButton, JButton deleteButton) { 452 okButton.setVisible(newRelation.isUseful() || this.getRelationSnapshot() == null); 453 deleteButton.setVisible(!newRelation.isUseful() && this.getRelationSnapshot() != null); 454 if (this.getRelationSnapshot() == null && !newRelation.isUseful()) { 455 okButton.setText(tr("Delete")); 456 } else { 457 okButton.setText(tr("OK")); 458 } 459 } 460 461 /** 462 * Update the error message 463 * @param newRelation What the new relation would "look" like if it were to be saved now 464 * @param errorPane The pane (used for background colour) 465 * @param errorLabel The label to update 466 */ 467 private void updateErrorMessage(Relation newRelation, JLabel errorLabel) { 468 Collection<TestError> errors = newRelation.validate(); 469 if (errors.isEmpty()) { 470 // Setting " " helps keep the label in place for layout calculations 471 errorLabel.setText(" "); 472 errorLabel.setIcon(null); 473 } else { 474 final TestError error = errors.stream() 475 .min(Comparator.comparingInt(testError -> testError.getSeverity().getLevel())) 476 .orElse(errors.iterator().next()); 477 final StringBuilder sb = new StringBuilder(); 478 if (error.getDescription() != null) { 479 sb.append(error.getDescription()).append(": "); 480 } 481 sb.append(error.getMessage()); 482 if (errors.size() > 1) { 483 sb.append(trn(" with {0} more problem", " with {0} more problems", errors.size() - 1, errors.size() - 1)); 484 } 485 errorLabel.setIcon(ImageProvider.get("data", error.getSeverity().getIcon())); 486 errorLabel.setText(sb.toString()); 487 } 416 488 } 417 489 418 490 /** -
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..e36ae38182 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 final Relation newRelation; 52 if (getEditor().getRelation() != null) { 53 newRelation = getEditor().getRelation(); 54 } else { 55 newRelation = new Relation(); 56 getTagModel().applyToPrimitive(newRelation); 57 getMemberTableModel().applyToRelation(newRelation); 58 } 59 int ret = confirmClosingByCancel(newRelation); 51 60 if (ret == 0) { //Yes, save the changes 52 61 //copied from OKAction.run() 53 62 Config.getPref().put("relation.editor.generic.lastrole", Utils.strip(tfRole.getText())); … … public class CancelAction extends SavingAction { 60 69 hideEditor(); 61 70 } 62 71 63 protected int confirmClosingByCancel( ) {72 protected int confirmClosingByCancel(final IRelation<?> newRelation) { 64 73 ButtonSpec[] options = { 65 74 new ButtonSpec( 66 75 tr("Yes, save the changes and close"), … … public class CancelAction extends SavingAction { 82 91 ) 83 92 }; 84 93 94 // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type" 95 // AND must contain at least one other OSM object. 96 options[0].setEnabled(newRelation.isUseful()); 97 85 98 return HelpAwareOptionPane.showOptionDialog( 86 99 MainApplication.getMainFrame(), 87 100 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..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
