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