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.Graphics;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.io.File;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
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.osm.visitor.BoundingXYVisitor;
|
---|
21 | import org.openstreetmap.josm.gui.MapView;
|
---|
22 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * A layer encapsulates the gui component of one dataset and its representation.
|
---|
27 | *
|
---|
28 | * Some layers may display data directly imported from OSM server. Other only
|
---|
29 | * display background images. Some can be edited, some not. Some are static and
|
---|
30 | * other changes dynamically (auto-updated).
|
---|
31 | *
|
---|
32 | * Layers can be visible or not. Most actions the user can do applies only on
|
---|
33 | * selected layers. The available actions depend on the selected layers too.
|
---|
34 | *
|
---|
35 | * All layers are managed by the MapView. They are displayed in a list to the
|
---|
36 | * right of the screen.
|
---|
37 | *
|
---|
38 | * @author imi
|
---|
39 | */
|
---|
40 | abstract public class Layer implements Destroyable, MapViewPaintable {
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Interface to notify listeners of the change of the active layer.
|
---|
44 | * @author imi
|
---|
45 | */
|
---|
46 | public interface LayerChangeListener {
|
---|
47 | void activeLayerChange(Layer oldLayer, Layer newLayer);
|
---|
48 | void layerAdded(Layer newLayer);
|
---|
49 | void layerRemoved(Layer oldLayer);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The listener of the active layer changes. You may register/deregister yourself
|
---|
54 | * while an LayerChangeListener - action is executed.
|
---|
55 | */
|
---|
56 | public static final Collection<LayerChangeListener> listeners = new CopyOnWriteArrayList<LayerChangeListener>();
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The visibility state of the layer.
|
---|
60 | */
|
---|
61 | public boolean visible = true;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * The layer should be handled as a background layer in automatic handling
|
---|
65 | */
|
---|
66 | public boolean background = false;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * The name of this layer.
|
---|
70 | */
|
---|
71 | public String name;
|
---|
72 | /**
|
---|
73 | * If a file is associated with this layer, this variable should be set to it.
|
---|
74 | */
|
---|
75 | private File associatedFile;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Create the layer and fill in the necessary components.
|
---|
79 | */
|
---|
80 | public Layer(String name) {
|
---|
81 | this.name = name;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Paint the dataset using the engine set.
|
---|
86 | * @param mv The object that can translate GeoPoints to screen coordinates.
|
---|
87 | */
|
---|
88 | abstract public void paint(Graphics g, MapView mv);
|
---|
89 | /**
|
---|
90 | * Return a representative small image for this layer. The image must not
|
---|
91 | * be larger than 64 pixel in any dimension.
|
---|
92 | */
|
---|
93 | abstract public Icon getIcon();
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @return A small tooltip hint about some statistics for this layer.
|
---|
97 | */
|
---|
98 | abstract public String getToolTipText();
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Merges the given layer into this layer. Throws if the layer types are
|
---|
102 | * incompatible.
|
---|
103 | * @param from The layer that get merged into this one. After the merge,
|
---|
104 | * the other layer is not usable anymore and passing to one others
|
---|
105 | * mergeFrom should be one of the last things to do with a layer.
|
---|
106 | */
|
---|
107 | abstract public void mergeFrom(Layer from);
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * @param other The other layer that is tested to be mergable with this.
|
---|
111 | * @return Whether the other layer can be merged into this layer.
|
---|
112 | */
|
---|
113 | abstract public boolean isMergable(Layer other);
|
---|
114 |
|
---|
115 | abstract public void visitBoundingBox(BoundingXYVisitor v);
|
---|
116 |
|
---|
117 | abstract public Object getInfoComponent();
|
---|
118 |
|
---|
119 | abstract public Component[] getMenuEntries();
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Called, when the layer is removed from the mapview and is going to be
|
---|
123 | * destroyed.
|
---|
124 | *
|
---|
125 | * This is because the Layer constructor can not add itself safely as listener
|
---|
126 | * to the layerlist dialog, because there may be no such dialog yet (loaded
|
---|
127 | * via command line parameter).
|
---|
128 | */
|
---|
129 | public void destroy() {}
|
---|
130 |
|
---|
131 | public File getAssociatedFile() { return associatedFile; }
|
---|
132 | public void setAssociatedFile(File file) { associatedFile = file; }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Replies the name of the layer
|
---|
137 | *
|
---|
138 | * @return the name of the layer
|
---|
139 | */
|
---|
140 | public String getName() {
|
---|
141 | return name;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | public static class LayerSaveAction extends AbstractAction {
|
---|
146 | private Layer layer;
|
---|
147 | public LayerSaveAction(Layer layer) {
|
---|
148 | putValue(SMALL_ICON, ImageProvider.get("save"));
|
---|
149 | putValue(SHORT_DESCRIPTION, tr("Save the current data."));
|
---|
150 | putValue(NAME, tr("Save"));
|
---|
151 | setEnabled(true);
|
---|
152 | this.layer = layer;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void actionPerformed(ActionEvent e) {
|
---|
156 | new SaveAction().doSave(layer);
|
---|
157 |
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public static class LayerSaveAsAction extends AbstractAction {
|
---|
162 | private Layer layer;
|
---|
163 | public LayerSaveAsAction(Layer layer) {
|
---|
164 | putValue(SMALL_ICON, ImageProvider.get("save_as"));
|
---|
165 | putValue(SHORT_DESCRIPTION, tr("Save the current data to a new file."));
|
---|
166 | putValue(NAME, tr("Save As..."));
|
---|
167 | setEnabled(true);
|
---|
168 | this.layer = layer;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void actionPerformed(ActionEvent e) {
|
---|
172 | new SaveAsAction().doSave(layer);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static class LayerGpxExportAction extends AbstractAction {
|
---|
177 | private Layer layer;
|
---|
178 | public LayerGpxExportAction(Layer layer) {
|
---|
179 | putValue(SMALL_ICON, ImageProvider.get("exportgpx"));
|
---|
180 | putValue(SHORT_DESCRIPTION, tr("Export the data to GPX file."));
|
---|
181 | putValue(NAME, tr("Export to GPX..."));
|
---|
182 | setEnabled(true);
|
---|
183 | this.layer = layer;
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void actionPerformed(ActionEvent e) {
|
---|
187 | new GpxExportAction().export(layer);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|