| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext.relationfix;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.util.ArrayList;
|
|---|
| 7 | import java.util.Arrays;
|
|---|
| 8 | import java.util.List;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.Action;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.command.Command;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 14 |
|
|---|
| 15 | import relcontext.actions.SortAndFixAction;
|
|---|
| 16 |
|
|---|
| 17 | public abstract class RelationFixer {
|
|---|
| 18 |
|
|---|
| 19 | private List<String> applicableTypes;
|
|---|
| 20 | private SortAndFixAction sortAndFixAction;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Construct new RelationFixer by a list of applicable types
|
|---|
| 24 | * @param types types
|
|---|
| 25 | */
|
|---|
| 26 | public RelationFixer(String... types) {
|
|---|
| 27 | applicableTypes = new ArrayList<>();
|
|---|
| 28 | applicableTypes.addAll(Arrays.asList(types));
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Check if given relation is of needed type. You may override this method to check first type
|
|---|
| 33 | * and then check desired relation properties.
|
|---|
| 34 | * Note that this only verifies if current RelationFixer can be used to check and fix given relation
|
|---|
| 35 | * Deeper relation checking is at {@link #isRelationGood}
|
|---|
| 36 | *
|
|---|
| 37 | * @param rel Relation to check
|
|---|
| 38 | * @return true if relation can be verified by current RelationFixer
|
|---|
| 39 | */
|
|---|
| 40 | public boolean isFixerApplicable(Relation rel) {
|
|---|
| 41 | if (rel == null)
|
|---|
| 42 | return false;
|
|---|
| 43 | if (!rel.hasKey("type"))
|
|---|
| 44 | return false;
|
|---|
| 45 |
|
|---|
| 46 | String type = rel.get("type");
|
|---|
| 47 | for (String oktype: applicableTypes) {
|
|---|
| 48 | if (oktype.equals(type))
|
|---|
| 49 | return true;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | return false;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Check if given relation is OK. That means if all roles are given properly, all tags exist as expected etc.
|
|---|
| 57 | * Should be written in children classes.
|
|---|
| 58 | *
|
|---|
| 59 | * @param rel Relation to verify
|
|---|
| 60 | * @return true if given relation is OK
|
|---|
| 61 | */
|
|---|
| 62 | public abstract boolean isRelationGood(Relation rel);
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Fix relation and return new relation with fixed tags, roles etc.
|
|---|
| 66 | * Note that is not obligatory to return true for isRelationGood for new relation
|
|---|
| 67 | *
|
|---|
| 68 | * @param rel Relation to fix
|
|---|
| 69 | * @return command that fixes the relation {@code null} if it cannot be fixed or is already OK
|
|---|
| 70 | */
|
|---|
| 71 | public abstract Command fixRelation(Relation rel);
|
|---|
| 72 |
|
|---|
| 73 | public void setFixAction(SortAndFixAction sortAndFixAction) {
|
|---|
| 74 | this.sortAndFixAction = sortAndFixAction;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | protected void setWarningMessage(String text) {
|
|---|
| 78 | if (text == null) {
|
|---|
| 79 | clearWarningMessage();
|
|---|
| 80 | } else {
|
|---|
| 81 | sortAndFixAction.putValue(Action.SHORT_DESCRIPTION, text);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | protected void clearWarningMessage() {
|
|---|
| 86 | sortAndFixAction.putValue(Action.SHORT_DESCRIPTION, tr("Fix roles of the chosen relation members"));
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|