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