source: josm/trunk/src/org/openstreetmap/josm/gui/layer/Layer.java@ 2192

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

fixed #3529: New OSM layers should be put on top of layer stack.

  • Property svn:eol-style set to native
File size: 8.9 KB
Line 
1// License: GPL. See LICENSE file for details.
2
3package org.openstreetmap.josm.gui.layer;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Component;
8import java.awt.Graphics;
9import java.awt.event.ActionEvent;
10import java.beans.PropertyChangeListener;
11import java.beans.PropertyChangeSupport;
12import java.io.File;
13import java.util.Collection;
14import java.util.concurrent.CopyOnWriteArrayList;
15
16import javax.swing.AbstractAction;
17import javax.swing.Icon;
18
19import org.openstreetmap.josm.actions.GpxExportAction;
20import org.openstreetmap.josm.actions.SaveAction;
21import org.openstreetmap.josm.actions.SaveAsAction;
22import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
23import org.openstreetmap.josm.gui.MapView;
24import org.openstreetmap.josm.tools.Destroyable;
25import org.openstreetmap.josm.tools.ImageProvider;
26
27/**
28 * A layer encapsulates the gui component of one dataset and its representation.
29 *
30 * Some layers may display data directly imported from OSM server. Other only
31 * display background images. Some can be edited, some not. Some are static and
32 * other changes dynamically (auto-updated).
33 *
34 * Layers can be visible or not. Most actions the user can do applies only on
35 * selected layers. The available actions depend on the selected layers too.
36 *
37 * All layers are managed by the MapView. They are displayed in a list to the
38 * right of the screen.
39 *
40 * @author imi
41 */
42abstract public class Layer implements Destroyable, MapViewPaintable {
43 static public final String VISIBLE_PROP = Layer.class.getName() + ".visible";
44 static public final String NAME_PROP = Layer.class.getName() + ".name";
45
46 /** keeps track of property change listeners */
47 protected PropertyChangeSupport propertyChangeSupport;
48
49 /**
50 * Interface to notify listeners of the change of the active layer.
51 * @author imi
52 */
53 public interface LayerChangeListener {
54 void activeLayerChange(Layer oldLayer, Layer newLayer);
55 void layerAdded(Layer newLayer);
56 void layerRemoved(Layer oldLayer);
57 }
58
59 /**
60 * The listener of the active layer changes. You may register/deregister yourself
61 * while an LayerChangeListener - action is executed.
62 */
63 public static final Collection<LayerChangeListener> listeners = new CopyOnWriteArrayList<LayerChangeListener>();
64
65 /**
66 * The visibility state of the layer.
67 *
68 */
69 private boolean visible = true;
70
71 /**
72 * The layer should be handled as a background layer in automatic handling
73 *
74 */
75 private boolean background = false;
76
77 /**
78 * The name of this layer.
79 *
80 */
81 private String name;
82
83 /**
84 * If a file is associated with this layer, this variable should be set to it.
85 */
86 private File associatedFile;
87
88 /**
89 * Create the layer and fill in the necessary components.
90 */
91 public Layer(String name) {
92 this.propertyChangeSupport = new PropertyChangeSupport(this);
93 setName(name);
94 }
95
96 /**
97 * Paint the dataset using the engine set.
98 * @param mv The object that can translate GeoPoints to screen coordinates.
99 */
100 abstract public void paint(Graphics g, MapView mv);
101 /**
102 * Return a representative small image for this layer. The image must not
103 * be larger than 64 pixel in any dimension.
104 */
105 abstract public Icon getIcon();
106
107 /**
108 * @return A small tooltip hint about some statistics for this layer.
109 */
110 abstract public String getToolTipText();
111
112 /**
113 * Merges the given layer into this layer. Throws if the layer types are
114 * incompatible.
115 * @param from The layer that get merged into this one. After the merge,
116 * the other layer is not usable anymore and passing to one others
117 * mergeFrom should be one of the last things to do with a layer.
118 */
119 abstract public void mergeFrom(Layer from);
120
121 /**
122 * @param other The other layer that is tested to be mergable with this.
123 * @return Whether the other layer can be merged into this layer.
124 */
125 abstract public boolean isMergable(Layer other);
126
127 abstract public void visitBoundingBox(BoundingXYVisitor v);
128
129 abstract public Object getInfoComponent();
130
131 abstract public Component[] getMenuEntries();
132
133 /**
134 * Called, when the layer is removed from the mapview and is going to be
135 * destroyed.
136 *
137 * This is because the Layer constructor can not add itself safely as listener
138 * to the layerlist dialog, because there may be no such dialog yet (loaded
139 * via command line parameter).
140 */
141 public void destroy() {}
142
143 public File getAssociatedFile() { return associatedFile; }
144 public void setAssociatedFile(File file) { associatedFile = file; }
145
146
147 /**
148 * Replies the name of the layer
149 *
150 * @return the name of the layer
151 */
152 public String getName() {
153 return name;
154 }
155
156 /**
157 * Sets the name of the layer
158 *
159 *@param name the name. If null, the name is set to the empty string.
160 *
161 */
162 public void setName(String name) {
163 if (name == null) {
164 name = "";
165 }
166 String oldValue = this.name;
167 this.name = name;
168 if (!this.name.equals(oldValue)) {
169 propertyChangeSupport.firePropertyChange(NAME_PROP, oldValue, this.name);
170 }
171 }
172
173 /**
174 * Replies true if this layer is a background layer
175 *
176 * @return true if this layer is a background layer
177 */
178 public boolean isBackgroundLayer() {
179 return background;
180 }
181
182 /**
183 * Sets whether this layer is a background layer
184 *
185 * @param background true, if this layer is a background layer
186 */
187 public void setBackgroundLayer(boolean background) {
188 this.background = background;
189 }
190
191 /**
192 * Sets the visibility of this layer. Emits property change event for
193 * property {@see #VISIBLE_PROP}.
194 *
195 * @param visible true, if the layer is visible; false, otherwise.
196 */
197 public void setVisible(boolean visible) {
198 boolean oldValue = this.visible;
199 this.visible = visible;
200 if (oldValue != this.visible) {
201 fireVisibleChanged(oldValue, this.visible);
202 }
203 }
204
205 /**
206 * Replies true if this layer is visible. False, otherwise.
207 * @return true if this layer is visible. False, otherwise.
208 */
209 public boolean isVisible() {
210 return visible;
211 }
212
213 /**
214 * Toggles the visibility state of this layer.
215 */
216 public void toggleVisible() {
217 setVisible(!isVisible());
218 }
219
220 /**
221 * Adds a {@see PropertyChangeListener}
222 *
223 * @param listener the listener
224 */
225 public void addPropertyChangeListener(PropertyChangeListener listener) {
226 propertyChangeSupport.addPropertyChangeListener(listener);
227 }
228
229 /**
230 * Removes a {@see PropertyChangeListener}
231 *
232 * @param listener the listener
233 */
234 public void removePropertyChangeListener(PropertyChangeListener listener) {
235 propertyChangeSupport.removePropertyChangeListener(listener);
236 }
237
238 /**
239 * fires a property change for the property {@see #VISIBLE_PROP}
240 *
241 * @param oldValue the old value
242 * @param newValue the new value
243 */
244 protected void fireVisibleChanged(boolean oldValue, boolean newValue) {
245 propertyChangeSupport.firePropertyChange(VISIBLE_PROP, oldValue, newValue);
246 }
247
248 /**
249 * The action to save a layer
250 *
251 */
252 public static class LayerSaveAction extends AbstractAction {
253 private Layer layer;
254 public LayerSaveAction(Layer layer) {
255 putValue(SMALL_ICON, ImageProvider.get("save"));
256 putValue(SHORT_DESCRIPTION, tr("Save the current data."));
257 putValue(NAME, tr("Save"));
258 setEnabled(true);
259 this.layer = layer;
260 }
261
262 public void actionPerformed(ActionEvent e) {
263 new SaveAction().doSave(layer);
264 }
265 }
266
267 public static class LayerSaveAsAction extends AbstractAction {
268 private Layer layer;
269 public LayerSaveAsAction(Layer layer) {
270 putValue(SMALL_ICON, ImageProvider.get("save_as"));
271 putValue(SHORT_DESCRIPTION, tr("Save the current data to a new file."));
272 putValue(NAME, tr("Save As..."));
273 setEnabled(true);
274 this.layer = layer;
275 }
276
277 public void actionPerformed(ActionEvent e) {
278 new SaveAsAction().doSave(layer);
279 }
280 }
281
282 public static class LayerGpxExportAction extends AbstractAction {
283 private Layer layer;
284 public LayerGpxExportAction(Layer layer) {
285 putValue(SMALL_ICON, ImageProvider.get("exportgpx"));
286 putValue(SHORT_DESCRIPTION, tr("Export the data to GPX file."));
287 putValue(NAME, tr("Export to GPX..."));
288 setEnabled(true);
289 this.layer = layer;
290 }
291
292 public void actionPerformed(ActionEvent e) {
293 new GpxExportAction().export(layer);
294 }
295 }
296}
Note: See TracBrowser for help on using the repository browser.