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

Last change on this file since 1828 was 1828, checked in by Gubaer, 15 years ago

new: tree-like browser for child relations
new: action/task for recursively downloading all child relations. Use case: download a complete network with cycle routes in one step
applied #2953: patch by cjw - Sorting a relation adds new members or crashes

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.layer.Layer;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
17
18
19/**
20 * RelationDialogManager keeps track of the open relation editors.
21 *
22 */
23public class RelationDialogManager extends WindowAdapter implements LayerChangeListener{
24
25 /** keeps track of open relation editors */
26 static RelationDialogManager relationDialogManager;
27
28 /**
29 * Replies the singleton {@see RelationDialogManager}
30 *
31 * @return the singleton {@see RelationDialogManager}
32 */
33 static public RelationDialogManager getRelationDialogManager() {
34 if (RelationDialogManager.relationDialogManager == null) {
35 RelationDialogManager.relationDialogManager = new RelationDialogManager();
36 Layer.listeners.add(RelationDialogManager.relationDialogManager);
37 }
38 return RelationDialogManager.relationDialogManager;
39 }
40
41 /**
42 * Helper class for keeping the context of a relation editor. A relation editor
43 * is open for a specific relation managed by a specific {@see OsmDataLayer}
44 *
45 */
46 static private class DialogContext {
47 public Relation relation;
48 public OsmDataLayer layer;
49
50 public DialogContext(OsmDataLayer layer, Relation relation) {
51 this.layer = layer;
52 this.relation = relation;
53 }
54
55 @Override
56 public int hashCode() {
57 final int prime = 31;
58 int result = 1;
59 result = prime * result + ((layer == null) ? 0 : layer.hashCode());
60 result = prime * result + ((relation == null) ? 0 : relation.hashCode());
61 return result;
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.id + "]";
93 }
94 }
95
96 /** the map of open dialogs */
97 private HashMap<DialogContext, RelationEditor> openDialogs;
98
99 /**
100 * constructor
101 */
102 public RelationDialogManager(){
103 openDialogs = new HashMap<DialogContext, RelationEditor>();
104 }
105
106 /**
107 * Register the relation editor for a relation managed by a
108 * {@see OsmDataLayer}.
109 *
110 * @param layer the layer
111 * @param relation the relation
112 * @param editor the editor
113 */
114 public void register(OsmDataLayer layer, Relation relation, RelationEditor editor) {
115 if (relation == null) {
116 relation = new Relation();
117 }
118 DialogContext context = new DialogContext(layer, relation);
119 openDialogs.put(context, editor);
120 editor.addWindowListener(this);
121 }
122
123 /**
124 * Replies true if there is an open relation editor for the relation managed
125 * by the given layer. Replies false if relation is null.
126 *
127 * @param layer the layer
128 * @param relation the relation. May be null.
129 * @return true if there is an open relation editor for the relation managed
130 * by the given layer; false otherwise
131 */
132 public boolean isOpenInEditor(OsmDataLayer layer, Relation relation) {
133 if (relation == null) return false;
134 DialogContext context = new DialogContext(layer, relation);
135 return openDialogs.keySet().contains(context);
136
137 }
138
139 /**
140 * Replies the editor for the relation managed by layer. Null, if no such editor
141 * is currently open. Returns null, if relation is null.
142 *
143 * @param layer the layer
144 * @param relation the relation
145 * @return the editor for the relation managed by layer. Null, if no such editor
146 * is currently open.
147 *
148 * @see #isOpenInEditor(OsmDataLayer, Relation)
149 */
150 public RelationEditor getEditorForRelation(OsmDataLayer layer, Relation relation) {
151 if (relation == null) return null;
152 DialogContext context = new DialogContext(layer, relation);
153 return openDialogs.get(context);
154 }
155
156 /**
157 * called when a layer is removed
158 *
159 */
160 public void layerRemoved(Layer oldLayer) {
161 if (oldLayer == null || ! (oldLayer instanceof OsmDataLayer))
162 return;
163 OsmDataLayer dataLayer = (OsmDataLayer)oldLayer;
164
165 Iterator<Entry<DialogContext,RelationEditor>> it = openDialogs.entrySet().iterator();
166 while(it.hasNext()) {
167 Entry<DialogContext,RelationEditor> entry = it.next();
168 if (entry.getKey().matchesLayer(dataLayer)) {
169 RelationEditor editor = entry.getValue();
170 it.remove();
171 editor.setVisible(false);
172 editor.dispose();
173 }
174 }
175 }
176
177 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
178 // do nothing
179 }
180
181 public void layerAdded(Layer newLayer) {
182 // do nothing
183 }
184
185 @Override
186 public void windowClosed(WindowEvent e) {
187 RelationEditor editor = (RelationEditor)e.getWindow();
188 DialogContext context = null;
189 for (DialogContext c : openDialogs.keySet()) {
190 if (openDialogs.get(c).equals(editor)) {
191 context = c;
192 break;
193 }
194 }
195 if (context != null) {
196 openDialogs.remove(context);
197 }
198 }
199
200 /**
201 * Positions an {@see RelationEditor} centered on the screen
202 *
203 * @param editor the editor
204 */
205 protected void centerOnScreen(RelationEditor editor) {
206 Point p = new Point(0,0);
207 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
208 p.x = (d.width - editor.getSize().width)/2;
209 p.y = (d.height - editor.getSize().height)/2;
210 p.x = Math.max(p.x,0);
211 p.y = Math.max(p.y,0);
212 editor.setLocation(p);
213 }
214
215 /**
216 * Replies true, if there is another open {@see RelationEditor} whose
217 * upper left corner is close to <code>p</code>.
218 *
219 * @param p the reference point to check
220 * @return true, if there is another open {@see RelationEditor} whose
221 * upper left corner is close to <code>p</code>.
222 */
223 protected boolean hasEditorWithCloseUpperLeftCorner(Point p) {
224 for (RelationEditor editor: openDialogs.values()) {
225 Point corner = editor.getLocation();
226 if (p.x >= corner.x -5 && corner.x + 5 >= p.x
227 && p.y >= corner.y -5 && corner.y + 5 >= p.y)
228 return true;
229 }
230 return false;
231 }
232
233 /**
234 * Positions a {@see RelationEditor} close to the center of the screen, in such
235 * a way, that it doesn't entirely cover another {@see RelationEditor}
236 *
237 * @param editor
238 */
239 protected void positionCloseToScreenCenter(RelationEditor editor) {
240 Point p = new Point(0,0);
241 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
242 p.x = (d.width - editor.getSize().width)/2;
243 p.y = (d.height - editor.getSize().height)/2;
244 p.x = Math.max(p.x,0);
245 p.y = Math.max(p.y,0);
246 while(hasEditorWithCloseUpperLeftCorner(p)) {
247 p.x += 20;
248 p.y += 20;
249 }
250 editor.setLocation(p);
251 }
252
253 /**
254 * Positions a {@see RelationEditor} on the screen. Tries to center it on the
255 * screen. If it hide another instance of an editor at the same position this
256 * method tries to reposition <code>editor</code> by moving it slightly down and
257 * slightly to the right.
258 *
259 * @param editor the editor
260 */
261 public void positionOnScreen(RelationEditor editor) {
262 if (editor == null) return;
263 if (openDialogs.isEmpty()) {
264 centerOnScreen(editor);
265 } else {
266 positionCloseToScreenCenter(editor);
267 }
268 }
269
270}
Note: See TracBrowser for help on using the repository browser.