source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/RelationDialogManager.java@ 3200

Last change on this file since 3200 was 3200, checked in by bastiK, 14 years ago

fixed #4930 - Relation editor "creeps"

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