source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/RelationEditor.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 7.3 KB
RevLine 
[2512]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
[2563]6import java.beans.PropertyChangeListener;
7import java.beans.PropertyChangeSupport;
[2512]8import java.lang.reflect.Method;
[8454]9import java.util.ArrayList;
[2512]10import java.util.Collection;
[8454]11import java.util.List;
[2512]12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.RelationMember;
16import org.openstreetmap.josm.gui.ExtendedDialog;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[2847]18import org.openstreetmap.josm.tools.CheckParameterUtil;
[12620]19import org.openstreetmap.josm.tools.Logging;
[2512]20
[9496]21/**
22 * Abstract relation editor.
23 * @since 1599
24 */
[9659]25public abstract class RelationEditor extends ExtendedDialog implements IRelationEditor {
[9496]26
[2563]27 /** the property name for the current relation.
28 * @see #setRelation(Relation)
29 * @see #getRelation()
30 */
[6889]31 public static final String RELATION_PROP = RelationEditor.class.getName() + ".relation";
[2512]32
[2563]33 /** the property name for the current relation snapshot
34 * @see #getRelationSnapshot()
35 */
[6889]36 public static final String RELATION_SNAPSHOT_PROP = RelationEditor.class.getName() + ".relationSnapshot";
[2563]37
[2512]38 /** the list of registered relation editor classes */
[8454]39 private static List<Class<RelationEditor>> editors = new ArrayList<>();
[2512]40
[9496]41 /** The relation that this editor is working on. */
42 private transient Relation relation;
43
44 /** The version of the relation when editing is started. This is null if a new relation is created. */
45 private transient Relation relationSnapshot;
46
47 /** The data layer the relation belongs to */
48 private final transient OsmDataLayer layer;
49
50 private final PropertyChangeSupport support = new PropertyChangeSupport(this);
51
[2512]52 /**
[9496]53 * Creates a new relation editor
54 *
55 * @param layer the {@link OsmDataLayer} in whose context a relation is edited. Must not be null.
56 * @param relation the relation. Can be null if a new relation is to be edited.
57 * @throws IllegalArgumentException if layer is null
58 */
59 protected RelationEditor(OsmDataLayer layer, Relation relation) {
60 super(Main.parent,
61 "",
62 new String[] {tr("Apply Changes"), tr("Cancel")},
63 false,
64 false
65 );
66 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
67 this.layer = layer;
68 setRelation(relation);
[9668]69 layer.removeRecentRelation(relation);
[9496]70 }
71
72 /**
[2512]73 * Registers a relation editor class. Depending on the type of relation to be edited
[5266]74 * {@link #getEditor(OsmDataLayer, Relation, Collection)} will create an instance of
[2512]75 * this class.
76 *
77 * @param clazz the class
78 */
79 public void registerRelationEditor(Class<RelationEditor> clazz) {
[9496]80 if (clazz != null && !editors.contains(clazz)) {
[2512]81 editors.add(clazz);
82 }
83 }
84
85 /**
[9496]86 * This is a factory method that creates an appropriate RelationEditor instance suitable for editing the relation
87 * that was passed in as an argument.
[2512]88 *
[9496]89 * This method is guaranteed to return a working RelationEditor. If no specific editor has been registered for the
90 * type of relation, then a generic editor will be returned.
[2512]91 *
[9496]92 * Editors can be registered by adding their class to the static list "editors" in the RelationEditor class.
93 * When it comes to editing a relation, all registered editors are queried via their static "canEdit" method whether
94 * they feel responsible for that kind of relation, and if they return true then an instance of that class will be used.
[2512]95 *
[2563]96 * @param layer the data layer the relation is a member of
[2512]97 * @param r the relation to be edited
[9496]98 * @param selectedMembers a collection of relation members which shall be selected when the editor is first launched
[2512]99 * @return an instance of RelationEditor suitable for editing that kind of relation
100 */
101 public static RelationEditor getEditor(OsmDataLayer layer, Relation r, Collection<RelationMember> selectedMembers) {
102 for (Class<RelationEditor> e : editors) {
103 try {
104 Method m = e.getMethod("canEdit", Relation.class);
105 Boolean canEdit = (Boolean) m.invoke(null, r);
106 if (canEdit) {
[9997]107 return e.getConstructor(Relation.class, Collection.class).newInstance(layer, r, selectedMembers);
[2512]108 }
[10212]109 } catch (ReflectiveOperationException ex) {
[12620]110 Logging.warn(ex);
[2512]111 }
112 }
113 if (RelationDialogManager.getRelationDialogManager().isOpenInEditor(layer, r))
114 return RelationDialogManager.getRelationDialogManager().getEditorForRelation(layer, r);
115 else {
116 RelationEditor editor = new GenericRelationEditor(layer, r, selectedMembers);
117 RelationDialogManager.getRelationDialogManager().positionOnScreen(editor);
118 RelationDialogManager.getRelationDialogManager().register(layer, r, editor);
119 return editor;
120 }
121 }
122
123 /**
124 * updates the title of the relation editor
125 */
126 protected void updateTitle() {
127 if (getRelation() == null) {
128 setTitle(tr("Create new relation in layer ''{0}''", layer.getName()));
129 } else if (getRelation().isNew()) {
130 setTitle(tr("Edit new relation in layer ''{0}''", layer.getName()));
131 } else {
132 setTitle(tr("Edit relation #{0} in layer ''{1}''", relation.getId(), layer.getName()));
133 }
134 }
[9059]135
[9496]136 @Override
137 public final Relation getRelation() {
[2512]138 return relation;
139 }
140
[9496]141 @Override
142 public final void setRelation(Relation relation) {
[2563]143 setRelationSnapshot((relation == null) ? null : new Relation(relation));
144 Relation oldValue = this.relation;
[2512]145 this.relation = relation;
[2563]146 if (this.relation != oldValue) {
147 support.firePropertyChange(RELATION_PROP, oldValue, this.relation);
148 }
[2512]149 updateTitle();
150 }
151
[10113]152 @Override
153 public final OsmDataLayer getLayer() {
[2512]154 return layer;
155 }
156
[9496]157 @Override
158 public final Relation getRelationSnapshot() {
[2512]159 return relationSnapshot;
160 }
161
[9496]162 protected final void setRelationSnapshot(Relation snapshot) {
[2563]163 Relation oldValue = relationSnapshot;
164 relationSnapshot = snapshot;
165 if (relationSnapshot != oldValue) {
166 support.firePropertyChange(RELATION_SNAPSHOT_PROP, oldValue, relationSnapshot);
167 }
168 }
169
[9496]170 @Override
171 public final boolean isDirtyRelation() {
[8444]172 return !relation.hasEqualSemanticAttributes(relationSnapshot);
[2512]173 }
[2563]174
175 /* ----------------------------------------------------------------------- */
176 /* property change support */
177 /* ----------------------------------------------------------------------- */
178
179 @Override
[9496]180 public final void addPropertyChangeListener(PropertyChangeListener listener) {
[2563]181 this.support.addPropertyChangeListener(listener);
182 }
183
184 @Override
[9496]185 public final void removePropertyChangeListener(PropertyChangeListener listener) {
[2563]186 this.support.removePropertyChangeListener(listener);
187 }
[9668]188
189 @Override
190 public void dispose() {
191 layer.setRecentRelation(relation);
192 super.dispose();
193 }
[2512]194}
Note: See TracBrowser for help on using the repository browser.