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

Last change on this file since 8413 was 8413, checked in by Don-vip, 9 years ago

code style - A close curly brace should be located at the beginning of a line

  • Property svn:eol-style set to native
File size: 15.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
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.List;
14
15import javax.swing.AbstractAction;
16import javax.swing.Action;
17import javax.swing.Icon;
18import javax.swing.JOptionPane;
19import javax.swing.JSeparator;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.actions.GpxExportAction;
23import org.openstreetmap.josm.actions.SaveAction;
24import org.openstreetmap.josm.actions.SaveActionBase;
25import org.openstreetmap.josm.actions.SaveAsAction;
26import org.openstreetmap.josm.data.Bounds;
27import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
28import org.openstreetmap.josm.data.projection.Projection;
29import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
30import org.openstreetmap.josm.gui.MapView;
31import org.openstreetmap.josm.tools.Destroyable;
32import org.openstreetmap.josm.tools.ImageProvider;
33import org.openstreetmap.josm.tools.Utils;
34
35/**
36 * A layer encapsulates the gui component of one dataset and its representation.
37 *
38 * Some layers may display data directly imported from OSM server. Other only
39 * display background images. Some can be edited, some not. Some are static and
40 * other changes dynamically (auto-updated).
41 *
42 * Layers can be visible or not. Most actions the user can do applies only on
43 * selected layers. The available actions depend on the selected layers too.
44 *
45 * All layers are managed by the MapView. They are displayed in a list to the
46 * right of the screen.
47 *
48 * @author imi
49 */
50public abstract class Layer implements Destroyable, MapViewPaintable, ProjectionChangeListener {
51
52 public interface LayerAction {
53 boolean supportLayers(List<Layer> layers);
54 Component createMenuComponent();
55 }
56
57 public interface MultiLayerAction {
58 Action getMultiLayerAction(List<Layer> layers);
59 }
60
61 /**
62 * Special class that can be returned by getMenuEntries when JSeparator needs to be created
63 *
64 */
65 public static class SeparatorLayerAction extends AbstractAction implements LayerAction {
66 public static final SeparatorLayerAction INSTANCE = new SeparatorLayerAction();
67 @Override
68 public void actionPerformed(ActionEvent e) {
69 throw new UnsupportedOperationException();
70 }
71 @Override
72 public Component createMenuComponent() {
73 return new JSeparator();
74 }
75 @Override
76 public boolean supportLayers(List<Layer> layers) {
77 return false;
78 }
79 }
80
81 public static final String VISIBLE_PROP = Layer.class.getName() + ".visible";
82 public static final String OPACITY_PROP = Layer.class.getName() + ".opacity";
83 public static final String NAME_PROP = Layer.class.getName() + ".name";
84
85 public static final int ICON_SIZE = 16;
86
87 /** keeps track of property change listeners */
88 protected PropertyChangeSupport propertyChangeSupport;
89
90 /**
91 * The visibility state of the layer.
92 *
93 */
94 private boolean visible = true;
95
96 /**
97 * The opacity of the layer.
98 *
99 */
100 private double opacity = 1;
101
102 /**
103 * The layer should be handled as a background layer in automatic handling
104 *
105 */
106 private boolean background = false;
107
108 /**
109 * The name of this layer.
110 *
111 */
112 private String name;
113
114 /**
115 * If a file is associated with this layer, this variable should be set to it.
116 */
117 private File associatedFile;
118
119 /**
120 * Create the layer and fill in the necessary components.
121 * @param name Layer name
122 */
123 public Layer(String name) {
124 this.propertyChangeSupport = new PropertyChangeSupport(this);
125 setName(name);
126 }
127
128 /**
129 * Initialization code, that depends on Main.map.mapView.
130 *
131 * It is always called in the event dispatching thread.
132 * Note that Main.map is null as long as no layer has been added, so do
133 * not execute code in the constructor, that assumes Main.map.mapView is
134 * not null. Instead override this method.
135 */
136 public void hookUpMapView() {
137 }
138
139 /**
140 * Paint the dataset using the engine set.
141 * @param mv The object that can translate GeoPoints to screen coordinates.
142 */
143 @Override
144 public abstract void paint(Graphics2D g, MapView mv, Bounds box);
145
146 /**
147 * Return a representative small image for this layer. The image must not
148 * be larger than 64 pixel in any dimension.
149 */
150 public abstract Icon getIcon();
151
152 /**
153 * Return a Color for this layer. Return null when no color specified.
154 * @param ignoreCustom Custom color should return null, as no default color
155 * is used. When this is true, then even for custom coloring the base
156 * color is returned - mainly for layer internal use.
157 */
158 public Color getColor(boolean ignoreCustom) {
159 return null;
160 }
161
162 /**
163 * @return A small tooltip hint about some statistics for this layer.
164 */
165 public abstract String getToolTipText();
166
167 /**
168 * Merges the given layer into this layer. Throws if the layer types are
169 * incompatible.
170 * @param from The layer that get merged into this one. After the merge,
171 * the other layer is not usable anymore and passing to one others
172 * mergeFrom should be one of the last things to do with a layer.
173 */
174 public abstract void mergeFrom(Layer from);
175
176 /**
177 * @param other The other layer that is tested to be mergable with this.
178 * @return Whether the other layer can be merged into this layer.
179 */
180 public abstract boolean isMergable(Layer other);
181
182 public abstract void visitBoundingBox(BoundingXYVisitor v);
183
184 public abstract Object getInfoComponent();
185
186 /**
187 * Determines if info dialog can be resized (false by default).
188 * @return {@code true} if the info dialog can be resized, {@code false} otherwise
189 * @since 6708
190 */
191 public boolean isInfoResizable() {
192 return false;
193 }
194
195 /**
196 * Returns list of actions. Action can implement LayerAction interface when it needs to be represented by other
197 * menu component than JMenuItem or when it supports multiple layers. Actions that support multiple layers should also
198 * have correct equals implementation.
199 *
200 * Use SeparatorLayerAction.INSTANCE instead of new JSeparator
201 *
202 */
203 public abstract Action[] getMenuEntries();
204
205 /**
206 * Called, when the layer is removed from the mapview and is going to be destroyed.
207 *
208 * This is because the Layer constructor can not add itself safely as listener
209 * to the layerlist dialog, because there may be no such dialog yet (loaded
210 * via command line parameter).
211 */
212 @Override
213 public void destroy() {
214 // Override in subclasses if needed
215 }
216
217 public File getAssociatedFile() {
218 return associatedFile;
219 }
220
221 public void setAssociatedFile(File file) {
222 associatedFile = file;
223 }
224
225 /**
226 * Replies the name of the layer
227 *
228 * @return the name of the layer
229 */
230 public String getName() {
231 return name;
232 }
233
234 /**
235 * Sets the name of the layer
236 *
237 *@param name the name. If null, the name is set to the empty string.
238 *
239 */
240 public final void setName(String name) {
241 if (name == null) {
242 name = "";
243 }
244 String oldValue = this.name;
245 this.name = name;
246 if (!this.name.equals(oldValue)) {
247 propertyChangeSupport.firePropertyChange(NAME_PROP, oldValue, this.name);
248 }
249 }
250
251 /**
252 * Replies true if this layer is a background layer
253 *
254 * @return true if this layer is a background layer
255 */
256 public boolean isBackgroundLayer() {
257 return background;
258 }
259
260 /**
261 * Sets whether this layer is a background layer
262 *
263 * @param background true, if this layer is a background layer
264 */
265 public void setBackgroundLayer(boolean background) {
266 this.background = background;
267 }
268
269 /**
270 * Sets the visibility of this layer. Emits property change event for
271 * property {@link #VISIBLE_PROP}.
272 *
273 * @param visible true, if the layer is visible; false, otherwise.
274 */
275 public void setVisible(boolean visible) {
276 boolean oldValue = isVisible();
277 this.visible = visible;
278 if (visible && opacity == 0) {
279 setOpacity(1);
280 } else if (oldValue != isVisible()) {
281 fireVisibleChanged(oldValue, isVisible());
282 }
283 }
284
285 /**
286 * Replies true if this layer is visible. False, otherwise.
287 * @return true if this layer is visible. False, otherwise.
288 */
289 public boolean isVisible() {
290 return visible && opacity != 0;
291 }
292
293 public double getOpacity() {
294 return opacity;
295 }
296
297 public void setOpacity(double opacity) {
298 if (!(opacity >= 0 && opacity <= 1))
299 throw new IllegalArgumentException("Opacity value must be between 0 and 1");
300 double oldOpacity = getOpacity();
301 boolean oldVisible = isVisible();
302 this.opacity = opacity;
303 if (!Utils.equalsEpsilon(oldOpacity, getOpacity())) {
304 fireOpacityChanged(oldOpacity, getOpacity());
305 }
306 if (oldVisible != isVisible()) {
307 fireVisibleChanged(oldVisible, isVisible());
308 }
309 }
310
311 /**
312 * Toggles the visibility state of this layer.
313 */
314 public void toggleVisible() {
315 setVisible(!isVisible());
316 }
317
318 /**
319 * Adds a {@link PropertyChangeListener}
320 *
321 * @param listener the listener
322 */
323 public void addPropertyChangeListener(PropertyChangeListener listener) {
324 propertyChangeSupport.addPropertyChangeListener(listener);
325 }
326
327 /**
328 * Removes a {@link PropertyChangeListener}
329 *
330 * @param listener the listener
331 */
332 public void removePropertyChangeListener(PropertyChangeListener listener) {
333 propertyChangeSupport.removePropertyChangeListener(listener);
334 }
335
336 /**
337 * fires a property change for the property {@link #VISIBLE_PROP}
338 *
339 * @param oldValue the old value
340 * @param newValue the new value
341 */
342 protected void fireVisibleChanged(boolean oldValue, boolean newValue) {
343 propertyChangeSupport.firePropertyChange(VISIBLE_PROP, oldValue, newValue);
344 }
345
346 /**
347 * fires a property change for the property {@link #OPACITY_PROP}
348 *
349 * @param oldValue the old value
350 * @param newValue the new value
351 */
352 protected void fireOpacityChanged(double oldValue, double newValue) {
353 propertyChangeSupport.firePropertyChange(OPACITY_PROP, oldValue, newValue);
354 }
355
356 /**
357 * Check changed status of layer
358 *
359 * @return True if layer was changed since last paint
360 */
361 public boolean isChanged() {
362 return true;
363 }
364
365 /**
366 * allows to check whether a projection is supported or not
367 *
368 * @return True if projection is supported for this layer
369 */
370 public boolean isProjectionSupported(Projection proj) {
371 return true;
372 }
373
374 /**
375 * Specify user information about projections
376 *
377 * @return User readable text telling about supported projections
378 */
379 public String nameSupportedProjections() {
380 return tr("All projections are supported");
381 }
382
383 /**
384 * The action to save a layer
385 *
386 */
387 public static class LayerSaveAction extends AbstractAction {
388 private final transient Layer layer;
389 public LayerSaveAction(Layer layer) {
390 putValue(SMALL_ICON, ImageProvider.get("save"));
391 putValue(SHORT_DESCRIPTION, tr("Save the current data."));
392 putValue(NAME, tr("Save"));
393 setEnabled(true);
394 this.layer = layer;
395 }
396
397 @Override
398 public void actionPerformed(ActionEvent e) {
399 SaveAction.getInstance().doSave(layer);
400 }
401 }
402
403 public static class LayerSaveAsAction extends AbstractAction {
404 private final transient Layer layer;
405 public LayerSaveAsAction(Layer layer) {
406 putValue(SMALL_ICON, ImageProvider.get("save_as"));
407 putValue(SHORT_DESCRIPTION, tr("Save the current data to a new file."));
408 putValue(NAME, tr("Save As..."));
409 setEnabled(true);
410 this.layer = layer;
411 }
412
413 @Override
414 public void actionPerformed(ActionEvent e) {
415 SaveAsAction.getInstance().doSave(layer);
416 }
417 }
418
419 public static class LayerGpxExportAction extends AbstractAction {
420 private final transient Layer layer;
421 public LayerGpxExportAction(Layer layer) {
422 putValue(SMALL_ICON, ImageProvider.get("exportgpx"));
423 putValue(SHORT_DESCRIPTION, tr("Export the data to GPX file."));
424 putValue(NAME, tr("Export to GPX..."));
425 setEnabled(true);
426 this.layer = layer;
427 }
428
429 @Override
430 public void actionPerformed(ActionEvent e) {
431 new GpxExportAction().export(layer);
432 }
433 }
434
435 /* --------------------------------------------------------------------------------- */
436 /* interface ProjectionChangeListener */
437 /* --------------------------------------------------------------------------------- */
438 @Override
439 public void projectionChanged(Projection oldValue, Projection newValue) {
440 if(!isProjectionSupported(newValue)) {
441 JOptionPane.showMessageDialog(Main.parent,
442 tr("The layer {0} does not support the new projection {1}.\n{2}\n"
443 + "Change the projection again or remove the layer.",
444 getName(), newValue.toCode(), nameSupportedProjections()),
445 tr("Warning"),
446 JOptionPane.WARNING_MESSAGE);
447 }
448 }
449
450 /**
451 * Initializes the layer after a successful load of data from a file
452 * @since 5459
453 */
454 public void onPostLoadFromFile() {
455 // To be overriden if needed
456 }
457
458 /**
459 * Replies the savable state of this layer (i.e if it can be saved through a "File-&gt;Save" dialog).
460 * @return true if this layer can be saved to a file
461 * @since 5459
462 */
463 public boolean isSavable() {
464 return false;
465 }
466
467 /**
468 * Checks whether it is ok to launch a save (whether we have data, there is no conflict etc.)
469 * @return <code>true</code>, if it is safe to save.
470 * @since 5459
471 */
472 public boolean checkSaveConditions() {
473 return true;
474 }
475
476 /**
477 * Creates a new "Save" dialog for this layer and makes it visible.<br>
478 * When the user has chosen a file, checks the file extension, and confirms overwrite if needed.
479 * @return The output {@code File}
480 * @since 5459
481 * @see SaveActionBase#createAndOpenSaveFileChooser
482 */
483 public File createAndOpenSaveFileChooser() {
484 return SaveActionBase.createAndOpenSaveFileChooser(tr("Save Layer"), "lay");
485 }
486}
Note: See TracBrowser for help on using the repository browser.