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