| 1 | package relcontext.relationfix;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.util.ArrayList;
|
|---|
| 6 | import java.util.List;
|
|---|
| 7 |
|
|---|
| 8 | import javax.swing.Action;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.command.Command;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 12 |
|
|---|
| 13 | import relcontext.actions.SortAndFixAction;
|
|---|
| 14 |
|
|---|
| 15 | public abstract class RelationFixer {
|
|---|
| 16 |
|
|---|
| 17 | private List<String> applicableTypes;
|
|---|
| 18 | private SortAndFixAction sortAndFixAction;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Construct new RelationFixer by a list of applicable types
|
|---|
| 22 | * @param types
|
|---|
| 23 | */
|
|---|
| 24 | public RelationFixer(String... types) {
|
|---|
| 25 | applicableTypes = new ArrayList<>();
|
|---|
| 26 | for(String type: types) {
|
|---|
| 27 | applicableTypes.add(type);
|
|---|
| 28 | }
|
|---|
| 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 | return false;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Check if given relation is OK. That means if all roles are given properly, all tags exist as expected etc.
|
|---|
| 56 | * Should be written in children classes.
|
|---|
| 57 | *
|
|---|
| 58 | * @param rel Relation to verify
|
|---|
| 59 | * @return true if given relation is OK
|
|---|
| 60 | */
|
|---|
| 61 | public abstract boolean isRelationGood(Relation rel);
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Fix relation and return new relation with fixed tags, roles etc.
|
|---|
| 65 | * Note that is not obligatory to return true for isRelationGood for new relation
|
|---|
| 66 | *
|
|---|
| 67 | * @param rel Relation to fix
|
|---|
| 68 | * @return command that fixes the relation {@code null} if it cannot be fixed or is already OK
|
|---|
| 69 | */
|
|---|
| 70 | public abstract Command fixRelation(Relation rel);
|
|---|
| 71 |
|
|---|
| 72 | public void setFixAction(SortAndFixAction sortAndFixAction) {
|
|---|
| 73 | this.sortAndFixAction = sortAndFixAction;
|
|---|
| 74 | }
|
|---|
| 75 | protected void setWarningMessage(String text) {
|
|---|
| 76 | if (text == null) {
|
|---|
| 77 | clearWarningMessage();
|
|---|
| 78 | } else {
|
|---|
| 79 | sortAndFixAction.putValue(Action.SHORT_DESCRIPTION, text);
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | protected void clearWarningMessage() {
|
|---|
| 83 | sortAndFixAction.putValue(Action.SHORT_DESCRIPTION, tr("Fix roles of the chosen relation members"));
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | }
|
|---|