1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.lang.reflect.Constructor;
|
---|
7 | import java.lang.reflect.Method;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
17 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 |
|
---|
20 | public abstract class RelationEditor extends ExtendedDialog {
|
---|
21 |
|
---|
22 | /** keeps track of open relation editors */
|
---|
23 | static private RelationDialogManager relationDialogManager;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Replies the singleton {@see RelationDialogManager}
|
---|
27 | *
|
---|
28 | * @return the singleton {@see RelationDialogManager}
|
---|
29 | */
|
---|
30 | static public RelationDialogManager getRelationDialogManager() {
|
---|
31 | if (relationDialogManager == null) {
|
---|
32 | relationDialogManager = new RelationDialogManager();
|
---|
33 | Layer.listeners.add(relationDialogManager);
|
---|
34 | }
|
---|
35 | return relationDialogManager;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static ArrayList<Class<RelationEditor>> editors = new ArrayList<Class<RelationEditor>>();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * The relation that this editor is working on, and the clone made for
|
---|
42 | * editing.
|
---|
43 | */
|
---|
44 | private Relation relation;
|
---|
45 | private Relation clone;
|
---|
46 |
|
---|
47 | /** the data layer the relation belongs to */
|
---|
48 | private OsmDataLayer layer;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * This is a factory method that creates an appropriate RelationEditor
|
---|
52 | * instance suitable for editing the relation that was passed in as an
|
---|
53 | * argument.
|
---|
54 | *
|
---|
55 | * This method is guaranteed to return a working RelationEditor. If no
|
---|
56 | * specific editor has been registered for the type of relation, then
|
---|
57 | * a generic editor will be returned.
|
---|
58 | *
|
---|
59 | * Editors can be registered by adding their class to the static list "editors"
|
---|
60 | * in the RelationEditor class. When it comes to editing a relation, all
|
---|
61 | * registered editors are queried via their static "canEdit" method whether they
|
---|
62 | * feel responsible for that kind of relation, and if they return true
|
---|
63 | * then an instance of that class will be used.
|
---|
64 | *
|
---|
65 | * @param r the relation to be edited
|
---|
66 | * @return an instance of RelationEditor suitable for editing that kind of relation
|
---|
67 | */
|
---|
68 | public static RelationEditor getEditor(OsmDataLayer layer, Relation r, Collection<RelationMember> selectedMembers) {
|
---|
69 | for (Class<RelationEditor> e : editors) {
|
---|
70 | try {
|
---|
71 | Method m = e.getMethod("canEdit", Relation.class);
|
---|
72 | Boolean canEdit = (Boolean) m.invoke(null, r);
|
---|
73 | if (canEdit) {
|
---|
74 | Constructor<RelationEditor> con = e.getConstructor(Relation.class, Collection.class);
|
---|
75 | RelationEditor editor = con.newInstance(layer, r, selectedMembers);
|
---|
76 | return editor;
|
---|
77 | }
|
---|
78 | } catch (Exception ex) {
|
---|
79 | // plod on
|
---|
80 | }
|
---|
81 | }
|
---|
82 | if (getRelationDialogManager().isOpenInEditor(layer, r))
|
---|
83 | return getRelationDialogManager().getEditorForRelation(layer, r);
|
---|
84 | else {
|
---|
85 | RelationEditor editor = new GenericRelationEditor(layer, r, selectedMembers);
|
---|
86 | getRelationDialogManager().register(layer, r, editor);
|
---|
87 | return editor;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected RelationEditor(OsmDataLayer layer, Relation relation, Collection<RelationMember> selectedMembers)
|
---|
92 | {
|
---|
93 | // Initalizes ExtendedDialog
|
---|
94 | super(Main.parent,
|
---|
95 | relation == null
|
---|
96 | ? tr("Create new relation in layer ''{0}''", layer.getName())
|
---|
97 | : (relation.id == 0
|
---|
98 | ? tr ("Edit new relation in layer ''{0}''", layer.getName())
|
---|
99 | : tr("Edit relation #{0} in layer ''{1}''", relation.id, layer.getName())
|
---|
100 | ),
|
---|
101 | new String[] { tr("Apply Changes"), tr("Cancel")},
|
---|
102 | false
|
---|
103 | );
|
---|
104 | System.out.println("-- in super constructor");
|
---|
105 | for (OsmPrimitive primitive : layer.data.allNonDeletedPrimitives()) {
|
---|
106 | System.out.println(OsmPrimitiveType.from(primitive) + " " + primitive.id + " incomplete=" + primitive.incomplete);
|
---|
107 | }
|
---|
108 | System.out.println("-------------");
|
---|
109 |
|
---|
110 |
|
---|
111 | this.relation = relation;
|
---|
112 | this.layer = layer;
|
---|
113 |
|
---|
114 | if (relation == null) {
|
---|
115 | // create a new relation
|
---|
116 | this.clone = new Relation();
|
---|
117 | } else {
|
---|
118 | // edit an existing relation
|
---|
119 | this.clone = new Relation(relation);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected Relation getRelation() {
|
---|
124 | return relation;
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected Relation getClone() {
|
---|
128 | return clone;
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected OsmDataLayer getLayer() {
|
---|
132 | return layer;
|
---|
133 | }
|
---|
134 | }
|
---|