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

Last change on this file since 2566 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

  • 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.Graphics2D;
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.Bounds;
23import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
24import org.openstreetmap.josm.gui.MapView;
25import org.openstreetmap.josm.tools.Destroyable;
26import org.openstreetmap.josm.tools.ImageProvider;
27
28/**
29 * A layer encapsulates the gui component of one dataset and its representation.
30 *
31 * Some layers may display data directly imported from OSM server. Other only
32 * display background images. Some can be edited, some not. Some are static and
33 * other changes dynamically (auto-updated).
34 *
35 * Layers can be visible or not. Most actions the user can do applies only on
36 * selected layers. The available actions depend on the selected layers too.
37 *
38 * All layers are managed by the MapView. They are displayed in a list to the
39 * right of the screen.
40 *
41 * @author imi
42 */
43abstract public class Layer implements Destroyable, MapViewPaintable {
44 static public final String VISIBLE_PROP = Layer.class.getName() + ".visible";
45 static public final String NAME_PROP = Layer.class.getName() + ".name";
46
47 /** keeps track of property change listeners */
48 protected PropertyChangeSupport propertyChangeSupport;
49
50 /**
51 * Interface to notify listeners of the change of the active layer.
52 * @author imi
53 */
54 public interface LayerChangeListener {
55 void activeLayerChange(Layer oldLayer, Layer newLayer);
56 void layerAdded(Layer newLayer);
57 void layerRemoved(Layer oldLayer);
58 }
59
60 /**
61 * The listener of the active layer changes. You may register/deregister yourself
62 * while an LayerChangeListener - action is executed.
63 */
64 public static final Collection<LayerChangeListener> listeners = new CopyOnWriteArrayList<LayerChangeListener>();
65
66 /**
67 * The visibility state of the layer.
68 *
69 */
70 private boolean visible = true;
71
72 /**
73 * The layer should be handled as a background layer in automatic handling
74 *
75 */
76 private boolean background = false;
77
78 /**
79 * The name of this layer.
80 *
81 */
82 private String name;
83
84 /**
85 * If a file is associated with this layer, this variable should be set to it.
86 */
87 private File associatedFile;
88
89 /**
90 * Create the layer and fill in the necessary components.
91 */
92 public Layer(String name) {
93 this.propertyChangeSupport = new PropertyChangeSupport(this);
94 setName(name);
95 }
96
97 /**
98 * Paint the dataset using the engine set.
99 * @param mv The object that can translate GeoPoints to screen coordinates.
100 */
101 abstract public void paint(Graphics2D g, MapView mv, Bounds box);
102 /**
103 * Return a representative small image for this layer. The image must not
104 * be larger than 64 pixel in any dimension.
105 */
106 abstract public Icon getIcon();
107
108 /**
109 * @return A small tooltip hint about some statistics for this layer.
110 */
111 abstract public String getToolTipText();
112
113 /**
114 * Merges the given layer into this layer. Throws if the layer types are
115 * incompatible.
116 * @param from The layer that get merged into this one. After the merge,
117 * the other layer is not usable anymore and passing to one others
118 * mergeFrom should be one of the last things to do with a layer.
119 */
120 abstract public void mergeFrom(Layer from);
121
122 /**
123 * @param other The other layer that is tested to be mergable with this.
124 * @return Whether the other layer can be merged into this layer.
125 */
126 abstract public boolean isMergable(Layer other);
127
128 abstract public void visitBoundingBox(BoundingXYVisitor v);
129
130 abstract public Object getInfoComponent();
131
132 abstract public Component[] getMenuEntries();
133
134 /**
135 * Called, when the layer is removed from the mapview and is going to be
136 * destroyed.
137 *
138 * This is because the Layer constructor can not add itself safely as listener
139 * to the layerlist dialog, because there may be no such dialog yet (loaded
140 * via command line parameter).
141 */
142 public void destroy() {}
143
144 public File getAssociatedFile() { return associatedFile; }
145 public void setAssociatedFile(File file) { associatedFile = file; }
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.