1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.awt.event.WindowAdapter;
|
---|
6 | import java.awt.event.WindowEvent;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Iterator;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Map.Entry;
|
---|
11 | import java.util.Objects;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.gui.MapView;
|
---|
15 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * RelationDialogManager keeps track of the open relation editors.
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class RelationDialogManager extends WindowAdapter implements MapView.LayerChangeListener{
|
---|
23 |
|
---|
24 | /** keeps track of open relation editors */
|
---|
25 | private static RelationDialogManager relationDialogManager;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Replies the singleton {@link RelationDialogManager}
|
---|
29 | *
|
---|
30 | * @return the singleton {@link RelationDialogManager}
|
---|
31 | */
|
---|
32 | public static RelationDialogManager getRelationDialogManager() {
|
---|
33 | if (RelationDialogManager.relationDialogManager == null) {
|
---|
34 | RelationDialogManager.relationDialogManager = new RelationDialogManager();
|
---|
35 | MapView.addLayerChangeListener(RelationDialogManager.relationDialogManager);
|
---|
36 | }
|
---|
37 | return RelationDialogManager.relationDialogManager;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Helper class for keeping the context of a relation editor. A relation editor
|
---|
42 | * is open for a specific relation managed by a specific {@link OsmDataLayer}
|
---|
43 | *
|
---|
44 | */
|
---|
45 | private static class DialogContext {
|
---|
46 | public final Relation relation;
|
---|
47 | public final OsmDataLayer layer;
|
---|
48 |
|
---|
49 | DialogContext(OsmDataLayer layer, Relation relation) {
|
---|
50 | this.layer = layer;
|
---|
51 | this.relation = relation;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public int hashCode() {
|
---|
56 | final int prime = 31;
|
---|
57 | int result = 1;
|
---|
58 | result = prime * result + ((layer == null) ? 0 : layer.hashCode());
|
---|
59 | result = prime * result + ((relation == null) ? 0 : relation.hashCode());
|
---|
60 | return result;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public boolean equals(Object obj) {
|
---|
65 | if (this == obj)
|
---|
66 | return true;
|
---|
67 | if (obj == null)
|
---|
68 | return false;
|
---|
69 | if (getClass() != obj.getClass())
|
---|
70 | return false;
|
---|
71 | DialogContext other = (DialogContext) obj;
|
---|
72 | if (layer == null) {
|
---|
73 | if (other.layer != null)
|
---|
74 | return false;
|
---|
75 | } else if (!layer.equals(other.layer))
|
---|
76 | return false;
|
---|
77 | if (relation == null) {
|
---|
78 | if (other.relation != null)
|
---|
79 | return false;
|
---|
80 | } else if (!relation.equals(other.relation))
|
---|
81 | return false;
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public boolean matchesLayer(OsmDataLayer layer) {
|
---|
86 | if (layer == null) return false;
|
---|
87 | return this.layer.equals(layer);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public String toString() {
|
---|
92 | return "[Context: layer=" + layer.getName() + ",relation=" + relation.getId() + ']';
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** the map of open dialogs */
|
---|
97 | private final Map<DialogContext, RelationEditor> openDialogs;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * constructor
|
---|
101 | */
|
---|
102 | public RelationDialogManager() {
|
---|
103 | openDialogs = new HashMap<>();
|
---|
104 | }
|
---|
105 | /**
|
---|
106 | * Register the relation editor for a relation managed by a
|
---|
107 | * {@link OsmDataLayer}.
|
---|
108 | *
|
---|
109 | * @param layer the layer
|
---|
110 | * @param relation the relation
|
---|
111 | * @param editor the editor
|
---|
112 | */
|
---|
113 | public void register(OsmDataLayer layer, Relation relation, RelationEditor editor) {
|
---|
114 | if (relation == null) {
|
---|
115 | relation = new Relation();
|
---|
116 | }
|
---|
117 | DialogContext context = new DialogContext(layer, relation);
|
---|
118 | openDialogs.put(context, editor);
|
---|
119 | editor.addWindowListener(this);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void updateContext(OsmDataLayer layer, Relation relation, RelationEditor editor) {
|
---|
123 | // lookup the entry for editor and remove it
|
---|
124 | //
|
---|
125 | for (Iterator<Entry<DialogContext, RelationEditor>> it = openDialogs.entrySet().iterator(); it.hasNext();) {
|
---|
126 | Entry<DialogContext, RelationEditor> entry = it.next();
|
---|
127 | if (Objects.equals(entry.getValue(), editor)) {
|
---|
128 | it.remove();
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | // don't add a window listener. Editor is already known to the relation dialog manager
|
---|
133 | //
|
---|
134 | DialogContext context = new DialogContext(layer, relation);
|
---|
135 | openDialogs.put(context, editor);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Closes the editor open for a specific layer and a specific relation.
|
---|
140 | *
|
---|
141 | * @param layer the layer
|
---|
142 | * @param relation the relation
|
---|
143 | */
|
---|
144 | public void close(OsmDataLayer layer, Relation relation) {
|
---|
145 | DialogContext context = new DialogContext(layer, relation);
|
---|
146 | RelationEditor editor = openDialogs.get(context);
|
---|
147 | if (editor != null) {
|
---|
148 | editor.setVisible(false);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Replies true if there is an open relation editor for the relation managed
|
---|
154 | * by the given layer. Replies false if relation is null.
|
---|
155 | *
|
---|
156 | * @param layer the layer
|
---|
157 | * @param relation the relation. May be null.
|
---|
158 | * @return true if there is an open relation editor for the relation managed
|
---|
159 | * by the given layer; false otherwise
|
---|
160 | */
|
---|
161 | public boolean isOpenInEditor(OsmDataLayer layer, Relation relation) {
|
---|
162 | if (relation == null) return false;
|
---|
163 | DialogContext context = new DialogContext(layer, relation);
|
---|
164 | return openDialogs.keySet().contains(context);
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Replies the editor for the relation managed by layer. Null, if no such editor
|
---|
170 | * is currently open. Returns null, if relation is null.
|
---|
171 | *
|
---|
172 | * @param layer the layer
|
---|
173 | * @param relation the relation
|
---|
174 | * @return the editor for the relation managed by layer. Null, if no such editor
|
---|
175 | * is currently open.
|
---|
176 | *
|
---|
177 | * @see #isOpenInEditor(OsmDataLayer, Relation)
|
---|
178 | */
|
---|
179 | public RelationEditor getEditorForRelation(OsmDataLayer layer, Relation relation) {
|
---|
180 | if (relation == null) return null;
|
---|
181 | DialogContext context = new DialogContext(layer, relation);
|
---|
182 | return openDialogs.get(context);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * called when a layer is removed
|
---|
187 | *
|
---|
188 | */
|
---|
189 | @Override
|
---|
190 | public void layerRemoved(Layer oldLayer) {
|
---|
191 | if (!(oldLayer instanceof OsmDataLayer))
|
---|
192 | return;
|
---|
193 | OsmDataLayer dataLayer = (OsmDataLayer) oldLayer;
|
---|
194 |
|
---|
195 | Iterator<Entry<DialogContext, RelationEditor>> it = openDialogs.entrySet().iterator();
|
---|
196 | while (it.hasNext()) {
|
---|
197 | Entry<DialogContext, RelationEditor> entry = it.next();
|
---|
198 | if (entry.getKey().matchesLayer(dataLayer)) {
|
---|
199 | RelationEditor editor = entry.getValue();
|
---|
200 | it.remove();
|
---|
201 | editor.setVisible(false);
|
---|
202 | editor.dispose();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
209 | // do nothing
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | public void layerAdded(Layer newLayer) {
|
---|
214 | // do nothing
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public void windowClosed(WindowEvent e) {
|
---|
219 | RelationEditor editor = (RelationEditor) e.getWindow();
|
---|
220 | for (Iterator<Entry<DialogContext, RelationEditor>> it = openDialogs.entrySet().iterator(); it.hasNext();) {
|
---|
221 | if (editor.equals(it.next().getValue())) {
|
---|
222 | it.remove();
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Replies true, if there is another open {@link RelationEditor} whose
|
---|
230 | * upper left corner is close to <code>p</code>.
|
---|
231 | *
|
---|
232 | * @param p the reference point to check
|
---|
233 | * @return true, if there is another open {@link RelationEditor} whose
|
---|
234 | * upper left corner is close to <code>p</code>.
|
---|
235 | */
|
---|
236 | protected boolean hasEditorWithCloseUpperLeftCorner(Point p, RelationEditor thisEditor) {
|
---|
237 | for (RelationEditor editor: openDialogs.values()) {
|
---|
238 | if (editor == thisEditor) {
|
---|
239 | continue;
|
---|
240 | }
|
---|
241 | Point corner = editor.getLocation();
|
---|
242 | if (p.x >= corner.x -5 && corner.x + 5 >= p.x
|
---|
243 | && p.y >= corner.y -5 && corner.y + 5 >= p.y)
|
---|
244 | return true;
|
---|
245 | }
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Positions a {@link RelationEditor} on the screen. Tries to center it on the
|
---|
251 | * screen. If it hide another instance of an editor at the same position this
|
---|
252 | * method tries to reposition <code>editor</code> by moving it slightly down and
|
---|
253 | * slightly to the right.
|
---|
254 | *
|
---|
255 | * @param editor the editor
|
---|
256 | */
|
---|
257 | public void positionOnScreen(RelationEditor editor) {
|
---|
258 | if (editor == null) return;
|
---|
259 | if (!openDialogs.isEmpty()) {
|
---|
260 | Point corner = editor.getLocation();
|
---|
261 | while (hasEditorWithCloseUpperLeftCorner(corner, editor)) {
|
---|
262 | // shift a little, so that the dialogs are not exactly on top of each other
|
---|
263 | corner.x += 20;
|
---|
264 | corner.y += 20;
|
---|
265 | }
|
---|
266 | editor.setLocation(corner);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | }
|
---|