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 | return Objects.hash(relation, layer);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public boolean equals(Object obj) {
|
---|
61 | if (this == obj) return true;
|
---|
62 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
63 | DialogContext that = (DialogContext) obj;
|
---|
64 | return Objects.equals(relation, that.relation) &&
|
---|
65 | Objects.equals(layer, that.layer);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public boolean matchesLayer(OsmDataLayer layer) {
|
---|
69 | if (layer == null) return false;
|
---|
70 | return this.layer.equals(layer);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public String toString() {
|
---|
75 | return "[Context: layer=" + layer.getName() + ",relation=" + relation.getId() + ']';
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** the map of open dialogs */
|
---|
80 | private final Map<DialogContext, RelationEditor> openDialogs;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * constructor
|
---|
84 | */
|
---|
85 | public RelationDialogManager() {
|
---|
86 | openDialogs = new HashMap<>();
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Register the relation editor for a relation managed by a
|
---|
91 | * {@link OsmDataLayer}.
|
---|
92 | *
|
---|
93 | * @param layer the layer
|
---|
94 | * @param relation the relation
|
---|
95 | * @param editor the editor
|
---|
96 | */
|
---|
97 | public void register(OsmDataLayer layer, Relation relation, RelationEditor editor) {
|
---|
98 | if (relation == null) {
|
---|
99 | relation = new Relation();
|
---|
100 | }
|
---|
101 | DialogContext context = new DialogContext(layer, relation);
|
---|
102 | openDialogs.put(context, editor);
|
---|
103 | editor.addWindowListener(this);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void updateContext(OsmDataLayer layer, Relation relation, RelationEditor editor) {
|
---|
107 | // lookup the entry for editor and remove it
|
---|
108 | //
|
---|
109 | for (Iterator<Entry<DialogContext, RelationEditor>> it = openDialogs.entrySet().iterator(); it.hasNext();) {
|
---|
110 | Entry<DialogContext, RelationEditor> entry = it.next();
|
---|
111 | if (Objects.equals(entry.getValue(), editor)) {
|
---|
112 | it.remove();
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | // don't add a window listener. Editor is already known to the relation dialog manager
|
---|
117 | //
|
---|
118 | DialogContext context = new DialogContext(layer, relation);
|
---|
119 | openDialogs.put(context, editor);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Closes the editor open for a specific layer and a specific relation.
|
---|
124 | *
|
---|
125 | * @param layer the layer
|
---|
126 | * @param relation the relation
|
---|
127 | */
|
---|
128 | public void close(OsmDataLayer layer, Relation relation) {
|
---|
129 | DialogContext context = new DialogContext(layer, relation);
|
---|
130 | RelationEditor editor = openDialogs.get(context);
|
---|
131 | if (editor != null) {
|
---|
132 | editor.setVisible(false);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Replies true if there is an open relation editor for the relation managed
|
---|
138 | * by the given layer. Replies false if relation is null.
|
---|
139 | *
|
---|
140 | * @param layer the layer
|
---|
141 | * @param relation the relation. May be null.
|
---|
142 | * @return true if there is an open relation editor for the relation managed
|
---|
143 | * by the given layer; false otherwise
|
---|
144 | */
|
---|
145 | public boolean isOpenInEditor(OsmDataLayer layer, Relation relation) {
|
---|
146 | if (relation == null) return false;
|
---|
147 | DialogContext context = new DialogContext(layer, relation);
|
---|
148 | return openDialogs.keySet().contains(context);
|
---|
149 |
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Replies the editor for the relation managed by layer. Null, if no such editor
|
---|
154 | * is currently open. Returns null, if relation is null.
|
---|
155 | *
|
---|
156 | * @param layer the layer
|
---|
157 | * @param relation the relation
|
---|
158 | * @return the editor for the relation managed by layer. Null, if no such editor
|
---|
159 | * is currently open.
|
---|
160 | *
|
---|
161 | * @see #isOpenInEditor(OsmDataLayer, Relation)
|
---|
162 | */
|
---|
163 | public RelationEditor getEditorForRelation(OsmDataLayer layer, Relation relation) {
|
---|
164 | if (relation == null) return null;
|
---|
165 | DialogContext context = new DialogContext(layer, relation);
|
---|
166 | return openDialogs.get(context);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * called when a layer is removed
|
---|
171 | *
|
---|
172 | */
|
---|
173 | @Override
|
---|
174 | public void layerRemoved(Layer oldLayer) {
|
---|
175 | if (!(oldLayer instanceof OsmDataLayer))
|
---|
176 | return;
|
---|
177 | OsmDataLayer dataLayer = (OsmDataLayer) oldLayer;
|
---|
178 |
|
---|
179 | Iterator<Entry<DialogContext, RelationEditor>> it = openDialogs.entrySet().iterator();
|
---|
180 | while (it.hasNext()) {
|
---|
181 | Entry<DialogContext, RelationEditor> entry = it.next();
|
---|
182 | if (entry.getKey().matchesLayer(dataLayer)) {
|
---|
183 | RelationEditor editor = entry.getValue();
|
---|
184 | it.remove();
|
---|
185 | editor.setVisible(false);
|
---|
186 | editor.dispose();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
193 | // do nothing
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public void layerAdded(Layer newLayer) {
|
---|
198 | // do nothing
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Override
|
---|
202 | public void windowClosed(WindowEvent e) {
|
---|
203 | RelationEditor editor = (RelationEditor) e.getWindow();
|
---|
204 | for (Iterator<Entry<DialogContext, RelationEditor>> it = openDialogs.entrySet().iterator(); it.hasNext();) {
|
---|
205 | if (editor.equals(it.next().getValue())) {
|
---|
206 | it.remove();
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Replies true, if there is another open {@link RelationEditor} whose
|
---|
214 | * upper left corner is close to <code>p</code>.
|
---|
215 | *
|
---|
216 | * @param p the reference point to check
|
---|
217 | * @param thisEditor the current editor
|
---|
218 | * @return true, if there is another open {@link RelationEditor} whose
|
---|
219 | * upper left corner is close to <code>p</code>.
|
---|
220 | */
|
---|
221 | protected boolean hasEditorWithCloseUpperLeftCorner(Point p, RelationEditor thisEditor) {
|
---|
222 | for (RelationEditor editor: openDialogs.values()) {
|
---|
223 | if (editor == thisEditor) {
|
---|
224 | continue;
|
---|
225 | }
|
---|
226 | Point corner = editor.getLocation();
|
---|
227 | if (p.x >= corner.x -5 && corner.x + 5 >= p.x
|
---|
228 | && p.y >= corner.y -5 && corner.y + 5 >= p.y)
|
---|
229 | return true;
|
---|
230 | }
|
---|
231 | return false;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Positions a {@link RelationEditor} on the screen. Tries to center it on the
|
---|
236 | * screen. If it hide another instance of an editor at the same position this
|
---|
237 | * method tries to reposition <code>editor</code> by moving it slightly down and
|
---|
238 | * slightly to the right.
|
---|
239 | *
|
---|
240 | * @param editor the editor
|
---|
241 | */
|
---|
242 | public void positionOnScreen(RelationEditor editor) {
|
---|
243 | if (editor == null) return;
|
---|
244 | if (!openDialogs.isEmpty()) {
|
---|
245 | Point corner = editor.getLocation();
|
---|
246 | while (hasEditorWithCloseUpperLeftCorner(corner, editor)) {
|
---|
247 | // shift a little, so that the dialogs are not exactly on top of each other
|
---|
248 | corner.x += 20;
|
---|
249 | corner.y += 20;
|
---|
250 | }
|
---|
251 | editor.setLocation(corner);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | }
|
---|