1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.beans.PropertyChangeListener;
|
---|
10 | import java.beans.PropertyChangeSupport;
|
---|
11 | import java.io.File;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.Action;
|
---|
16 | import javax.swing.Icon;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JSeparator;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.actions.GpxExportAction;
|
---|
23 | import org.openstreetmap.josm.actions.SaveAction;
|
---|
24 | import org.openstreetmap.josm.actions.SaveActionBase;
|
---|
25 | import org.openstreetmap.josm.actions.SaveAsAction;
|
---|
26 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
27 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
28 | import org.openstreetmap.josm.data.preferences.AbstractProperty;
|
---|
29 | import org.openstreetmap.josm.data.preferences.AbstractProperty.ValueChangeListener;
|
---|
30 | import org.openstreetmap.josm.data.preferences.ColorProperty;
|
---|
31 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
32 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
33 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 | import org.openstreetmap.josm.tools.Utils;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * A layer encapsulates the gui component of one dataset and its representation.
|
---|
39 | *
|
---|
40 | * Some layers may display data directly imported from OSM server. Other only
|
---|
41 | * display background images. Some can be edited, some not. Some are static and
|
---|
42 | * other changes dynamically (auto-updated).
|
---|
43 | *
|
---|
44 | * Layers can be visible or not. Most actions the user can do applies only on
|
---|
45 | * selected layers. The available actions depend on the selected layers too.
|
---|
46 | *
|
---|
47 | * All layers are managed by the MapView. They are displayed in a list to the
|
---|
48 | * right of the screen.
|
---|
49 | *
|
---|
50 | * @author imi
|
---|
51 | */
|
---|
52 | public abstract class Layer extends AbstractMapViewPaintable implements Destroyable, ProjectionChangeListener {
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Action related to a single layer.
|
---|
56 | */
|
---|
57 | public interface LayerAction {
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Determines if this action supports a given list of layers.
|
---|
61 | * @param layers list of layers
|
---|
62 | * @return {@code true} if this action supports the given list of layers, {@code false} otherwise
|
---|
63 | */
|
---|
64 | boolean supportLayers(List<Layer> layers);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Creates and return the menu component.
|
---|
68 | * @return the menu component
|
---|
69 | */
|
---|
70 | Component createMenuComponent();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Action related to several layers.
|
---|
75 | * @since 10600 (functional interface)
|
---|
76 | */
|
---|
77 | @FunctionalInterface
|
---|
78 | public interface MultiLayerAction {
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Returns the action for a given list of layers.
|
---|
82 | * @param layers list of layers
|
---|
83 | * @return the action for the given list of layers
|
---|
84 | */
|
---|
85 | Action getMultiLayerAction(List<Layer> layers);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Special class that can be returned by getMenuEntries when JSeparator needs to be created
|
---|
90 | */
|
---|
91 | public static class SeparatorLayerAction extends AbstractAction implements LayerAction {
|
---|
92 | /** Unique instance */
|
---|
93 | public static final SeparatorLayerAction INSTANCE = new SeparatorLayerAction();
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void actionPerformed(ActionEvent e) {
|
---|
97 | throw new UnsupportedOperationException();
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public Component createMenuComponent() {
|
---|
102 | return new JSeparator();
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public boolean supportLayers(List<Layer> layers) {
|
---|
107 | return false;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public static final String VISIBLE_PROP = Layer.class.getName() + ".visible";
|
---|
112 | public static final String OPACITY_PROP = Layer.class.getName() + ".opacity";
|
---|
113 | public static final String NAME_PROP = Layer.class.getName() + ".name";
|
---|
114 | public static final String FILTER_STATE_PROP = Layer.class.getName() + ".filterstate";
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * keeps track of property change listeners
|
---|
118 | */
|
---|
119 | protected PropertyChangeSupport propertyChangeSupport;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * The visibility state of the layer.
|
---|
123 | */
|
---|
124 | private boolean visible = true;
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * The opacity of the layer.
|
---|
128 | */
|
---|
129 | private double opacity = 1;
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * The layer should be handled as a background layer in automatic handling
|
---|
133 | */
|
---|
134 | private boolean background;
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * The name of this layer.
|
---|
138 | */
|
---|
139 | private String name;
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * This is set if user renamed this layer.
|
---|
143 | */
|
---|
144 | private boolean renamed;
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * If a file is associated with this layer, this variable should be set to it.
|
---|
148 | */
|
---|
149 | private File associatedFile;
|
---|
150 |
|
---|
151 | private final ValueChangeListener<Object> invalidateListener = change -> invalidate();
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Create the layer and fill in the necessary components.
|
---|
155 | * @param name Layer name
|
---|
156 | */
|
---|
157 | public Layer(String name) {
|
---|
158 | this.propertyChangeSupport = new PropertyChangeSupport(this);
|
---|
159 | setName(name);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Initialization code, that depends on Main.map.mapView.
|
---|
164 | *
|
---|
165 | * It is always called in the event dispatching thread.
|
---|
166 | * Note that Main.map is null as long as no layer has been added, so do
|
---|
167 | * not execute code in the constructor, that assumes Main.map.mapView is
|
---|
168 | * not null.
|
---|
169 | *
|
---|
170 | * If you need to execute code when this layer is added to the map view, use
|
---|
171 | * {@link #attachToMapView(org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent)}
|
---|
172 | */
|
---|
173 | public void hookUpMapView() {
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Return a representative small image for this layer. The image must not
|
---|
178 | * be larger than 64 pixel in any dimension.
|
---|
179 | * @return layer icon
|
---|
180 | */
|
---|
181 | public abstract Icon getIcon();
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Return a Color for this layer. Return null when no color specified.
|
---|
185 | * @param ignoreCustom Custom color should return null, as no default color
|
---|
186 | * is used. When this is true, then even for custom coloring the base
|
---|
187 | * color is returned - mainly for layer internal use.
|
---|
188 | * @return layer color
|
---|
189 | * @deprecated Use the new {@link #getColorProperty()}. To be removed end of 2016.
|
---|
190 | */
|
---|
191 | @Deprecated
|
---|
192 | public Color getColor(boolean ignoreCustom) {
|
---|
193 | return null;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Gets the color property to use for this layer.
|
---|
198 | * @return The color property.
|
---|
199 | * @since 10824
|
---|
200 | */
|
---|
201 | public AbstractProperty<Color> getColorProperty() {
|
---|
202 | ColorProperty base = getBaseColorProperty();
|
---|
203 | if (base != null) {
|
---|
204 | // cannot cache this - name may change.
|
---|
205 | return base.getChildColor("layer " + getName());
|
---|
206 | } else {
|
---|
207 | return null;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Gets the color property that stores the default color for this layer.
|
---|
213 | * @return The property or <code>null</code> if this layer is not colored.
|
---|
214 | * @since 10824
|
---|
215 | */
|
---|
216 | protected ColorProperty getBaseColorProperty() {
|
---|
217 | return null;
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void addColorPropertyListener() {
|
---|
221 | AbstractProperty<Color> colorProperty = getColorProperty();
|
---|
222 | if (colorProperty != null) {
|
---|
223 | colorProperty.addListener(invalidateListener);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | private void removeColorPropertyListener() {
|
---|
228 | AbstractProperty<Color> colorProperty = getColorProperty();
|
---|
229 | if (colorProperty != null) {
|
---|
230 | colorProperty.removeListener(invalidateListener);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * @return A small tooltip hint about some statistics for this layer.
|
---|
236 | */
|
---|
237 | public abstract String getToolTipText();
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Merges the given layer into this layer. Throws if the layer types are
|
---|
241 | * incompatible.
|
---|
242 | * @param from The layer that get merged into this one. After the merge,
|
---|
243 | * the other layer is not usable anymore and passing to one others
|
---|
244 | * mergeFrom should be one of the last things to do with a layer.
|
---|
245 | */
|
---|
246 | public abstract void mergeFrom(Layer from);
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * @param other The other layer that is tested to be mergable with this.
|
---|
250 | * @return Whether the other layer can be merged into this layer.
|
---|
251 | */
|
---|
252 | public abstract boolean isMergable(Layer other);
|
---|
253 |
|
---|
254 | public abstract void visitBoundingBox(BoundingXYVisitor v);
|
---|
255 |
|
---|
256 | public abstract Object getInfoComponent();
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Determines if info dialog can be resized (false by default).
|
---|
260 | * @return {@code true} if the info dialog can be resized, {@code false} otherwise
|
---|
261 | * @since 6708
|
---|
262 | */
|
---|
263 | public boolean isInfoResizable() {
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Returns list of actions. Action can implement LayerAction interface when it needs to be represented by other
|
---|
269 | * menu component than JMenuItem or when it supports multiple layers. Actions that support multiple layers should also
|
---|
270 | * have correct equals implementation.
|
---|
271 | *
|
---|
272 | * Use {@link SeparatorLayerAction#INSTANCE} instead of new JSeparator
|
---|
273 | * @return menu actions for this layer
|
---|
274 | */
|
---|
275 | public abstract Action[] getMenuEntries();
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Called, when the layer is removed from the mapview and is going to be destroyed.
|
---|
279 | *
|
---|
280 | * This is because the Layer constructor can not add itself safely as listener
|
---|
281 | * to the layerlist dialog, because there may be no such dialog yet (loaded
|
---|
282 | * via command line parameter).
|
---|
283 | */
|
---|
284 | @Override
|
---|
285 | public void destroy() {
|
---|
286 | // Override in subclasses if needed
|
---|
287 | removeColorPropertyListener();
|
---|
288 | }
|
---|
289 |
|
---|
290 | public File getAssociatedFile() {
|
---|
291 | return associatedFile;
|
---|
292 | }
|
---|
293 |
|
---|
294 | public void setAssociatedFile(File file) {
|
---|
295 | associatedFile = file;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Replies the name of the layer
|
---|
300 | *
|
---|
301 | * @return the name of the layer
|
---|
302 | */
|
---|
303 | public String getName() {
|
---|
304 | return name;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Sets the name of the layer
|
---|
309 | *
|
---|
310 | * @param name the name. If null, the name is set to the empty string.
|
---|
311 | */
|
---|
312 | public final void setName(String name) {
|
---|
313 | if (this.name != null) {
|
---|
314 | removeColorPropertyListener();
|
---|
315 | }
|
---|
316 | if (name == null) {
|
---|
317 | name = "";
|
---|
318 | }
|
---|
319 |
|
---|
320 | String oldValue = this.name;
|
---|
321 | this.name = name;
|
---|
322 | if (!this.name.equals(oldValue)) {
|
---|
323 | propertyChangeSupport.firePropertyChange(NAME_PROP, oldValue, this.name);
|
---|
324 | }
|
---|
325 |
|
---|
326 | // re-add listener
|
---|
327 | addColorPropertyListener();
|
---|
328 | invalidate();
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Rename layer and set renamed flag to mark it as renamed (has user given name).
|
---|
333 | *
|
---|
334 | * @param name the name. If null, the name is set to the empty string.
|
---|
335 | */
|
---|
336 | public final void rename(String name) {
|
---|
337 | renamed = true;
|
---|
338 | setName(name);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Replies true if this layer was renamed by user
|
---|
343 | *
|
---|
344 | * @return true if this layer was renamed by user
|
---|
345 | */
|
---|
346 | public boolean isRenamed() {
|
---|
347 | return renamed;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Replies true if this layer is a background layer
|
---|
352 | *
|
---|
353 | * @return true if this layer is a background layer
|
---|
354 | */
|
---|
355 | public boolean isBackgroundLayer() {
|
---|
356 | return background;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Sets whether this layer is a background layer
|
---|
361 | *
|
---|
362 | * @param background true, if this layer is a background layer
|
---|
363 | */
|
---|
364 | public void setBackgroundLayer(boolean background) {
|
---|
365 | this.background = background;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Sets the visibility of this layer. Emits property change event for
|
---|
370 | * property {@link #VISIBLE_PROP}.
|
---|
371 | *
|
---|
372 | * @param visible true, if the layer is visible; false, otherwise.
|
---|
373 | */
|
---|
374 | public void setVisible(boolean visible) {
|
---|
375 | boolean oldValue = isVisible();
|
---|
376 | this.visible = visible;
|
---|
377 | if (visible && opacity == 0) {
|
---|
378 | setOpacity(1);
|
---|
379 | } else if (oldValue != isVisible()) {
|
---|
380 | fireVisibleChanged(oldValue, isVisible());
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Replies true if this layer is visible. False, otherwise.
|
---|
386 | * @return true if this layer is visible. False, otherwise.
|
---|
387 | */
|
---|
388 | public boolean isVisible() {
|
---|
389 | return visible && opacity != 0;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Gets the opacity of the layer, in range 0...1
|
---|
394 | * @return The opacity
|
---|
395 | */
|
---|
396 | public double getOpacity() {
|
---|
397 | return opacity;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Sets the opacity of the layer, in range 0...1
|
---|
402 | * @param opacity The opacity
|
---|
403 | * @throws IllegalArgumentException if the opacity is out of range
|
---|
404 | */
|
---|
405 | public void setOpacity(double opacity) {
|
---|
406 | if (!(opacity >= 0 && opacity <= 1))
|
---|
407 | throw new IllegalArgumentException("Opacity value must be between 0 and 1");
|
---|
408 | double oldOpacity = getOpacity();
|
---|
409 | boolean oldVisible = isVisible();
|
---|
410 | this.opacity = opacity;
|
---|
411 | if (!Utils.equalsEpsilon(oldOpacity, getOpacity())) {
|
---|
412 | fireOpacityChanged(oldOpacity, getOpacity());
|
---|
413 | }
|
---|
414 | if (oldVisible != isVisible()) {
|
---|
415 | fireVisibleChanged(oldVisible, isVisible());
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Sets new state to the layer after applying {@link ImageProcessor}.
|
---|
421 | */
|
---|
422 | public void setFilterStateChanged() {
|
---|
423 | fireFilterStateChanged();
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Toggles the visibility state of this layer.
|
---|
428 | */
|
---|
429 | public void toggleVisible() {
|
---|
430 | setVisible(!isVisible());
|
---|
431 | }
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Adds a {@link PropertyChangeListener}
|
---|
435 | *
|
---|
436 | * @param listener the listener
|
---|
437 | */
|
---|
438 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
---|
439 | propertyChangeSupport.addPropertyChangeListener(listener);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Removes a {@link PropertyChangeListener}
|
---|
444 | *
|
---|
445 | * @param listener the listener
|
---|
446 | */
|
---|
447 | public void removePropertyChangeListener(PropertyChangeListener listener) {
|
---|
448 | propertyChangeSupport.removePropertyChangeListener(listener);
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * fires a property change for the property {@link #VISIBLE_PROP}
|
---|
453 | *
|
---|
454 | * @param oldValue the old value
|
---|
455 | * @param newValue the new value
|
---|
456 | */
|
---|
457 | protected void fireVisibleChanged(boolean oldValue, boolean newValue) {
|
---|
458 | propertyChangeSupport.firePropertyChange(VISIBLE_PROP, oldValue, newValue);
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * fires a property change for the property {@link #OPACITY_PROP}
|
---|
463 | *
|
---|
464 | * @param oldValue the old value
|
---|
465 | * @param newValue the new value
|
---|
466 | */
|
---|
467 | protected void fireOpacityChanged(double oldValue, double newValue) {
|
---|
468 | propertyChangeSupport.firePropertyChange(OPACITY_PROP, oldValue, newValue);
|
---|
469 | }
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * fires a property change for the property {@link #FILTER_STATE_PROP}.
|
---|
473 | */
|
---|
474 | protected void fireFilterStateChanged() {
|
---|
475 | propertyChangeSupport.firePropertyChange(FILTER_STATE_PROP, null, null);
|
---|
476 | }
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * Check changed status of layer
|
---|
480 | *
|
---|
481 | * @return True if layer was changed since last paint
|
---|
482 | * @deprecated This is not supported by multiple map views.
|
---|
483 | * Fire an {@link #invalidate()} to trigger a repaint.
|
---|
484 | * Let this method return false if you only use invalidation events.
|
---|
485 | */
|
---|
486 | @Deprecated
|
---|
487 | public boolean isChanged() {
|
---|
488 | return true;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * allows to check whether a projection is supported or not
|
---|
493 | * @param proj projection
|
---|
494 | *
|
---|
495 | * @return True if projection is supported for this layer
|
---|
496 | */
|
---|
497 | public boolean isProjectionSupported(Projection proj) {
|
---|
498 | return proj != null;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Specify user information about projections
|
---|
503 | *
|
---|
504 | * @return User readable text telling about supported projections
|
---|
505 | */
|
---|
506 | public String nameSupportedProjections() {
|
---|
507 | return tr("All projections are supported");
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * The action to save a layer
|
---|
512 | */
|
---|
513 | public static class LayerSaveAction extends AbstractAction {
|
---|
514 | private final transient Layer layer;
|
---|
515 |
|
---|
516 | public LayerSaveAction(Layer layer) {
|
---|
517 | putValue(SMALL_ICON, ImageProvider.get("save"));
|
---|
518 | putValue(SHORT_DESCRIPTION, tr("Save the current data."));
|
---|
519 | putValue(NAME, tr("Save"));
|
---|
520 | setEnabled(true);
|
---|
521 | this.layer = layer;
|
---|
522 | }
|
---|
523 |
|
---|
524 | @Override
|
---|
525 | public void actionPerformed(ActionEvent e) {
|
---|
526 | SaveAction.getInstance().doSave(layer);
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | public static class LayerSaveAsAction extends AbstractAction {
|
---|
531 | private final transient Layer layer;
|
---|
532 |
|
---|
533 | public LayerSaveAsAction(Layer layer) {
|
---|
534 | putValue(SMALL_ICON, ImageProvider.get("save_as"));
|
---|
535 | putValue(SHORT_DESCRIPTION, tr("Save the current data to a new file."));
|
---|
536 | putValue(NAME, tr("Save As..."));
|
---|
537 | setEnabled(true);
|
---|
538 | this.layer = layer;
|
---|
539 | }
|
---|
540 |
|
---|
541 | @Override
|
---|
542 | public void actionPerformed(ActionEvent e) {
|
---|
543 | SaveAsAction.getInstance().doSave(layer);
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | public static class LayerGpxExportAction extends AbstractAction {
|
---|
548 | private final transient Layer layer;
|
---|
549 |
|
---|
550 | public LayerGpxExportAction(Layer layer) {
|
---|
551 | putValue(SMALL_ICON, ImageProvider.get("exportgpx"));
|
---|
552 | putValue(SHORT_DESCRIPTION, tr("Export the data to GPX file."));
|
---|
553 | putValue(NAME, tr("Export to GPX..."));
|
---|
554 | setEnabled(true);
|
---|
555 | this.layer = layer;
|
---|
556 | }
|
---|
557 |
|
---|
558 | @Override
|
---|
559 | public void actionPerformed(ActionEvent e) {
|
---|
560 | new GpxExportAction().export(layer);
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 | /* --------------------------------------------------------------------------------- */
|
---|
565 | /* interface ProjectionChangeListener */
|
---|
566 | /* --------------------------------------------------------------------------------- */
|
---|
567 | @Override
|
---|
568 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
569 | if (!isProjectionSupported(newValue)) {
|
---|
570 | final String message = "<html><body><p>" +
|
---|
571 | tr("The layer {0} does not support the new projection {1}.", getName(), newValue.toCode()) + "</p>" +
|
---|
572 | "<p style='width: 450px;'>" + tr("Supported projections are: {0}", nameSupportedProjections()) + "</p>" +
|
---|
573 | tr("Change the projection again or remove the layer.");
|
---|
574 |
|
---|
575 | // run later to not block loading the UI.
|
---|
576 | SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(Main.parent,
|
---|
577 | message,
|
---|
578 | tr("Warning"),
|
---|
579 | JOptionPane.WARNING_MESSAGE));
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * Initializes the layer after a successful load of data from a file
|
---|
585 | * @since 5459
|
---|
586 | */
|
---|
587 | public void onPostLoadFromFile() {
|
---|
588 | // To be overriden if needed
|
---|
589 | }
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * Replies the savable state of this layer (i.e if it can be saved through a "File->Save" dialog).
|
---|
593 | * @return true if this layer can be saved to a file
|
---|
594 | * @since 5459
|
---|
595 | */
|
---|
596 | public boolean isSavable() {
|
---|
597 | return false;
|
---|
598 | }
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Checks whether it is ok to launch a save (whether we have data, there is no conflict etc.)
|
---|
602 | * @return <code>true</code>, if it is safe to save.
|
---|
603 | * @since 5459
|
---|
604 | */
|
---|
605 | public boolean checkSaveConditions() {
|
---|
606 | return true;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /**
|
---|
610 | * Creates a new "Save" dialog for this layer and makes it visible.<br>
|
---|
611 | * When the user has chosen a file, checks the file extension, and confirms overwrite if needed.
|
---|
612 | * @return The output {@code File}
|
---|
613 | * @see SaveActionBase#createAndOpenSaveFileChooser
|
---|
614 | * @since 5459
|
---|
615 | */
|
---|
616 | public File createAndOpenSaveFileChooser() {
|
---|
617 | return SaveActionBase.createAndOpenSaveFileChooser(tr("Save Layer"), "lay");
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * @return bytes that the tile will use. Needed for resource management
|
---|
622 | * @deprecated Not used any more.
|
---|
623 | */
|
---|
624 | @Deprecated
|
---|
625 | protected long estimateMemoryUsage() {
|
---|
626 | return 0;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Gets the strategy that specifies where this layer should be inserted in a layer list.
|
---|
631 | * @return That strategy.
|
---|
632 | * @since 10008
|
---|
633 | */
|
---|
634 | public LayerPositionStrategy getDefaultLayerPosition() {
|
---|
635 | if (isBackgroundLayer()) {
|
---|
636 | return LayerPositionStrategy.BEFORE_FIRST_BACKGROUND_LAYER;
|
---|
637 | } else {
|
---|
638 | return LayerPositionStrategy.AFTER_LAST_VALIDATION_LAYER;
|
---|
639 | }
|
---|
640 | }
|
---|
641 |
|
---|
642 | /**
|
---|
643 | * Gets the {@link ProjectionBounds} for this layer to be visible to the user. This can be the exact bounds, the UI handles padding. Return
|
---|
644 | * <code>null</code> if you cannot provide this information. The default implementation uses the bounds from
|
---|
645 | * {@link #visitBoundingBox(BoundingXYVisitor)}.
|
---|
646 | * @return The bounds for this layer.
|
---|
647 | * @since 10371
|
---|
648 | */
|
---|
649 | public ProjectionBounds getViewProjectionBounds() {
|
---|
650 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
651 | visitBoundingBox(v);
|
---|
652 | return v.getBounds();
|
---|
653 | }
|
---|
654 |
|
---|
655 | @Override
|
---|
656 | public String toString() {
|
---|
657 | return getClass().getSimpleName() + " [name=" + name + ", associatedFile=" + associatedFile + ']';
|
---|
658 | }
|
---|
659 | }
|
---|