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

Last change on this file since 14027 was 14027, checked in by michael2402, 6 years ago

See #16388: New mechanism for plugins to register relation editor actions.

  • Property svn:eol-style set to native
File size: 5.7 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.util.Collection;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.RelationMember;
13import org.openstreetmap.josm.gui.ExtendedDialog;
14import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[2847]15import org.openstreetmap.josm.tools.CheckParameterUtil;
[2512]16
[9496]17/**
18 * Abstract relation editor.
19 * @since 1599
20 */
[9659]21public abstract class RelationEditor extends ExtendedDialog implements IRelationEditor {
[14027]22 private static final long serialVersionUID = 1L;
[9496]23
[14027]24 /** the property name for the current relation.
[2563]25 * @see #setRelation(Relation)
26 * @see #getRelation()
27 */
[6889]28 public static final String RELATION_PROP = RelationEditor.class.getName() + ".relation";
[2512]29
[2563]30 /** the property name for the current relation snapshot
31 * @see #getRelationSnapshot()
32 */
[6889]33 public static final String RELATION_SNAPSHOT_PROP = RelationEditor.class.getName() + ".relationSnapshot";
[2563]34
[9496]35 /** The relation that this editor is working on. */
36 private transient Relation relation;
37
38 /** The version of the relation when editing is started. This is null if a new relation is created. */
39 private transient Relation relationSnapshot;
40
41 /** The data layer the relation belongs to */
42 private final transient OsmDataLayer layer;
43
44 private final PropertyChangeSupport support = new PropertyChangeSupport(this);
45
[2512]46 /**
[9496]47 * Creates a new relation editor
48 *
49 * @param layer the {@link OsmDataLayer} in whose context a relation is edited. Must not be null.
50 * @param relation the relation. Can be null if a new relation is to be edited.
51 * @throws IllegalArgumentException if layer is null
52 */
53 protected RelationEditor(OsmDataLayer layer, Relation relation) {
54 super(Main.parent,
55 "",
56 new String[] {tr("Apply Changes"), tr("Cancel")},
57 false,
58 false
59 );
60 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
61 this.layer = layer;
62 setRelation(relation);
[9668]63 layer.removeRecentRelation(relation);
[9496]64 }
65
66 /**
67 * This is a factory method that creates an appropriate RelationEditor instance suitable for editing the relation
68 * that was passed in as an argument.
[2512]69 *
[14027]70 * This method is guaranteed to return a working RelationEditor.
[2512]71 *
[2563]72 * @param layer the data layer the relation is a member of
[2512]73 * @param r the relation to be edited
[9496]74 * @param selectedMembers a collection of relation members which shall be selected when the editor is first launched
[2512]75 * @return an instance of RelationEditor suitable for editing that kind of relation
76 */
77 public static RelationEditor getEditor(OsmDataLayer layer, Relation r, Collection<RelationMember> selectedMembers) {
78 if (RelationDialogManager.getRelationDialogManager().isOpenInEditor(layer, r))
79 return RelationDialogManager.getRelationDialogManager().getEditorForRelation(layer, r);
80 else {
81 RelationEditor editor = new GenericRelationEditor(layer, r, selectedMembers);
82 RelationDialogManager.getRelationDialogManager().positionOnScreen(editor);
83 RelationDialogManager.getRelationDialogManager().register(layer, r, editor);
84 return editor;
85 }
86 }
87
88 /**
89 * updates the title of the relation editor
90 */
91 protected void updateTitle() {
92 if (getRelation() == null) {
93 setTitle(tr("Create new relation in layer ''{0}''", layer.getName()));
94 } else if (getRelation().isNew()) {
95 setTitle(tr("Edit new relation in layer ''{0}''", layer.getName()));
96 } else {
97 setTitle(tr("Edit relation #{0} in layer ''{1}''", relation.getId(), layer.getName()));
98 }
99 }
[9059]100
[9496]101 @Override
102 public final Relation getRelation() {
[2512]103 return relation;
104 }
105
[9496]106 @Override
107 public final void setRelation(Relation relation) {
[2563]108 setRelationSnapshot((relation == null) ? null : new Relation(relation));
109 Relation oldValue = this.relation;
[2512]110 this.relation = relation;
[2563]111 if (this.relation != oldValue) {
112 support.firePropertyChange(RELATION_PROP, oldValue, this.relation);
113 }
[2512]114 updateTitle();
115 }
116
[10113]117 @Override
118 public final OsmDataLayer getLayer() {
[2512]119 return layer;
120 }
121
[9496]122 @Override
123 public final Relation getRelationSnapshot() {
[2512]124 return relationSnapshot;
125 }
126
[9496]127 protected final void setRelationSnapshot(Relation snapshot) {
[2563]128 Relation oldValue = relationSnapshot;
129 relationSnapshot = snapshot;
130 if (relationSnapshot != oldValue) {
131 support.firePropertyChange(RELATION_SNAPSHOT_PROP, oldValue, relationSnapshot);
132 }
133 }
134
[9496]135 @Override
136 public final boolean isDirtyRelation() {
[8444]137 return !relation.hasEqualSemanticAttributes(relationSnapshot);
[2512]138 }
[2563]139
140 /* ----------------------------------------------------------------------- */
141 /* property change support */
142 /* ----------------------------------------------------------------------- */
143
144 @Override
[9496]145 public final void addPropertyChangeListener(PropertyChangeListener listener) {
[2563]146 this.support.addPropertyChangeListener(listener);
147 }
148
149 @Override
[9496]150 public final void removePropertyChangeListener(PropertyChangeListener listener) {
[2563]151 this.support.removePropertyChangeListener(listener);
152 }
[9668]153
154 @Override
155 public void dispose() {
156 layer.setRecentRelation(relation);
157 super.dispose();
158 }
[2512]159}
Note: See TracBrowser for help on using the repository browser.