1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import java.awt.event.WindowAdapter;
|
---|
5 | import java.awt.event.WindowEvent;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Iterator;
|
---|
8 | import java.util.Map.Entry;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
11 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
12 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
13 | import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * RelationDialogManager keeps track of the open relation editors.
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class RelationDialogManager extends WindowAdapter implements LayerChangeListener{
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Helper class for keeping the context of a relation editor. A relation editor
|
---|
23 | * is open for a specific relation managed by a specific {@see OsmDataLayer}
|
---|
24 | *
|
---|
25 | */
|
---|
26 | static private class DialogContext {
|
---|
27 | public Relation relation;
|
---|
28 | public OsmDataLayer layer;
|
---|
29 |
|
---|
30 | public DialogContext(OsmDataLayer layer, Relation relation) {
|
---|
31 | this.layer = layer;
|
---|
32 | this.relation = relation;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public int hashCode() {
|
---|
37 | final int prime = 31;
|
---|
38 | int result = 1;
|
---|
39 | result = prime * result + ((layer == null) ? 0 : layer.hashCode());
|
---|
40 | result = prime * result + ((relation == null) ? 0 : relation.hashCode());
|
---|
41 | return result;
|
---|
42 | }
|
---|
43 | @Override
|
---|
44 | public boolean equals(Object obj) {
|
---|
45 | if (this == obj)
|
---|
46 | return true;
|
---|
47 | if (obj == null)
|
---|
48 | return false;
|
---|
49 | if (getClass() != obj.getClass())
|
---|
50 | return false;
|
---|
51 | DialogContext other = (DialogContext) obj;
|
---|
52 | if (layer == null) {
|
---|
53 | if (other.layer != null)
|
---|
54 | return false;
|
---|
55 | } else if (!layer.equals(other.layer))
|
---|
56 | return false;
|
---|
57 | if (relation == null) {
|
---|
58 | if (other.relation != null)
|
---|
59 | return false;
|
---|
60 | } else if (!relation.equals(other.relation))
|
---|
61 | return false;
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean matchesLayer(OsmDataLayer layer) {
|
---|
66 | if (layer == null) return false;
|
---|
67 | return this.layer.equals(layer);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public String toString() {
|
---|
72 | return "[Context: layer=" + layer.getName() + ",relation=" + relation.id + "]";
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** the map of open dialogs */
|
---|
77 | private HashMap<DialogContext, RelationEditor> openDialogs;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * constructor
|
---|
81 | */
|
---|
82 | public RelationDialogManager(){
|
---|
83 | openDialogs = new HashMap<DialogContext, RelationEditor>();
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Register the relation editor for a relation managed by a
|
---|
88 | * {@see OsmDataLayer}.
|
---|
89 | *
|
---|
90 | * @param layer the layer
|
---|
91 | * @param relation the relation
|
---|
92 | * @param editor the editor
|
---|
93 | */
|
---|
94 | public void register(OsmDataLayer layer, Relation relation, RelationEditor editor) {
|
---|
95 | DialogContext context = new DialogContext(layer, relation);
|
---|
96 | openDialogs.put(context, editor);
|
---|
97 | editor.addWindowListener(this);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Replies true if there is an open relation editor for the relation managed
|
---|
102 | * by the given layer
|
---|
103 | *
|
---|
104 | * @param layer the layer
|
---|
105 | * @param relation the relation
|
---|
106 | * @return true if there is an open relation editor for the relation managed
|
---|
107 | * by the given layer; false otherwise
|
---|
108 | */
|
---|
109 | public boolean isOpenInEditor(OsmDataLayer layer, Relation relation) {
|
---|
110 | DialogContext context = new DialogContext(layer, relation);
|
---|
111 | return openDialogs.keySet().contains(context);
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Replies the editor for the relation managed by layer. Null, if no such editor
|
---|
117 | * is currently open.
|
---|
118 | *
|
---|
119 | * @param layer the layer
|
---|
120 | * @param relation the relation
|
---|
121 | * @return the editor for the relation managed by layer. Null, if no such editor
|
---|
122 | * is currently open.
|
---|
123 | *
|
---|
124 | * @see #isOpenInEditor(OsmDataLayer, Relation)
|
---|
125 | */
|
---|
126 | public RelationEditor getEditorForRelation(OsmDataLayer layer, Relation relation) {
|
---|
127 | DialogContext context = new DialogContext(layer, relation);
|
---|
128 | return openDialogs.get(context);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * called when a layer is removed
|
---|
133 | *
|
---|
134 | */
|
---|
135 | public void layerRemoved(Layer oldLayer) {
|
---|
136 | if (oldLayer == null || ! (oldLayer instanceof OsmDataLayer))
|
---|
137 | return;
|
---|
138 | OsmDataLayer dataLayer = (OsmDataLayer)oldLayer;
|
---|
139 |
|
---|
140 | Iterator<Entry<DialogContext,RelationEditor>> it = openDialogs.entrySet().iterator();
|
---|
141 | while(it.hasNext()) {
|
---|
142 | Entry<DialogContext,RelationEditor> entry = it.next();
|
---|
143 | if (entry.getKey().matchesLayer(dataLayer)) {
|
---|
144 | RelationEditor editor = entry.getValue();
|
---|
145 | it.remove();
|
---|
146 | editor.setVisible(false);
|
---|
147 | editor.dispose();
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
153 | // do nothing
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void layerAdded(Layer newLayer) {
|
---|
157 | // do nothing
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public void windowClosed(WindowEvent e) {
|
---|
162 | RelationEditor editor = (RelationEditor)e.getWindow();
|
---|
163 | DialogContext context = null;
|
---|
164 | for (DialogContext c : openDialogs.keySet()) {
|
---|
165 | if (openDialogs.get(c).equals(editor)) {
|
---|
166 | context = c;
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | if (context != null) {
|
---|
171 | openDialogs.remove(context);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|