[14590] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data;
|
---|
| 3 |
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Collections;
|
---|
| 6 | import java.util.List;
|
---|
| 7 |
|
---|
| 8 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 9 | import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry;
|
---|
| 10 | import org.openstreetmap.josm.tools.ListenerList;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Class to hold {@link ImageEntry} and the current selection
|
---|
| 14 | * @since 14590
|
---|
| 15 | */
|
---|
| 16 | public class ImageData {
|
---|
| 17 | /**
|
---|
| 18 | * A listener that is informed when the current selection change
|
---|
| 19 | */
|
---|
| 20 | public interface ImageDataUpdateListener {
|
---|
| 21 | /**
|
---|
| 22 | * Called when the data change
|
---|
| 23 | * @param data the image data
|
---|
| 24 | */
|
---|
| 25 | void imageDataUpdated(ImageData data);
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Called when the selection change
|
---|
| 29 | * @param data the image data
|
---|
| 30 | */
|
---|
| 31 | void selectedImageChanged(ImageData data);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | private final List<ImageEntry> data;
|
---|
| 35 |
|
---|
| 36 | private int selectedImageIndex = -1;
|
---|
| 37 |
|
---|
| 38 | private final ListenerList<ImageDataUpdateListener> listeners = ListenerList.create();
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Construct a new image container without images
|
---|
| 42 | */
|
---|
| 43 | public ImageData() {
|
---|
| 44 | this(null);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Construct a new image container with a list of images
|
---|
| 49 | * @param data the list of {@link ImageEntry}
|
---|
| 50 | */
|
---|
| 51 | public ImageData(List<ImageEntry> data) {
|
---|
| 52 | if (data != null) {
|
---|
| 53 | Collections.sort(data);
|
---|
| 54 | this.data = data;
|
---|
| 55 | } else {
|
---|
| 56 | this.data = new ArrayList<>();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Returns the images
|
---|
| 62 | * @return the images
|
---|
| 63 | */
|
---|
| 64 | public List<ImageEntry> getImages() {
|
---|
| 65 | return data;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Determines if one image has modified GPS data.
|
---|
| 70 | * @return {@code true} if data has been modified; {@code false}, otherwise
|
---|
| 71 | */
|
---|
| 72 | public boolean isModified() {
|
---|
| 73 | for (ImageEntry e : data) {
|
---|
| 74 | if (e.hasNewGpsData()) {
|
---|
| 75 | return true;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | return false;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Merge 2 ImageData
|
---|
| 83 | * @param otherData {@link ImageData} to merge
|
---|
| 84 | */
|
---|
| 85 | public void mergeFrom(ImageData otherData) {
|
---|
| 86 | data.addAll(otherData.getImages());
|
---|
| 87 | Collections.sort(data);
|
---|
| 88 |
|
---|
| 89 | final ImageEntry selected = otherData.getSelectedImage();
|
---|
| 90 |
|
---|
| 91 | // Suppress the double photos.
|
---|
| 92 | if (data.size() > 1) {
|
---|
| 93 | ImageEntry prev = data.get(data.size() - 1);
|
---|
| 94 | for (int i = data.size() - 2; i >= 0; i--) {
|
---|
| 95 | ImageEntry cur = data.get(i);
|
---|
| 96 | if (cur.getFile().equals(prev.getFile())) {
|
---|
| 97 | data.remove(i);
|
---|
| 98 | } else {
|
---|
| 99 | prev = cur;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | if (selected != null) {
|
---|
| 104 | setSelectedImageIndex(data.indexOf(selected));
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Return the current selected image
|
---|
| 110 | * @return the selected image as {@link ImageEntry} or null
|
---|
| 111 | */
|
---|
| 112 | public ImageEntry getSelectedImage() {
|
---|
| 113 | if (selectedImageIndex > -1) {
|
---|
| 114 | return data.get(selectedImageIndex);
|
---|
| 115 | }
|
---|
| 116 | return null;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Select the first image of the sequence
|
---|
| 121 | */
|
---|
| 122 | public void selectFirstImage() {
|
---|
| 123 | if (!data.isEmpty()) {
|
---|
| 124 | setSelectedImageIndex(0);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Select the last image of the sequence
|
---|
| 130 | */
|
---|
| 131 | public void selectLastImage() {
|
---|
| 132 | setSelectedImageIndex(data.size() - 1);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Check if there is a next image in the sequence
|
---|
| 137 | * @return {@code true} is there is a next image, {@code false} otherwise
|
---|
| 138 | */
|
---|
| 139 | public boolean hasNextImage() {
|
---|
| 140 | return selectedImageIndex != data.size() - 1;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * Select the next image of the sequence
|
---|
| 145 | */
|
---|
| 146 | public void selectNextImage() {
|
---|
| 147 | if (hasNextImage()) {
|
---|
| 148 | setSelectedImageIndex(selectedImageIndex + 1);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Check if there is a previous image in the sequence
|
---|
| 154 | * @return {@code true} is there is a previous image, {@code false} otherwise
|
---|
| 155 | */
|
---|
| 156 | public boolean hasPreviousImage() {
|
---|
| 157 | return selectedImageIndex - 1 > -1;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Select the previous image of the sequence
|
---|
| 162 | */
|
---|
| 163 | public void selectPreviousImage() {
|
---|
| 164 | if (data.isEmpty()) {
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
| 167 | setSelectedImageIndex(Integer.max(0, selectedImageIndex - 1));
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * Select as the selected the given image
|
---|
| 172 | * @param image the selected image
|
---|
| 173 | */
|
---|
| 174 | public void setSelectedImage(ImageEntry image) {
|
---|
| 175 | setSelectedImageIndex(data.indexOf(image));
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Clear the selected image
|
---|
| 180 | */
|
---|
| 181 | public void clearSelectedImage() {
|
---|
| 182 | setSelectedImageIndex(-1);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | private void setSelectedImageIndex(int index) {
|
---|
| 186 | setSelectedImageIndex(index, false);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | private void setSelectedImageIndex(int index, boolean forceTrigger) {
|
---|
| 190 | if (index == selectedImageIndex && !forceTrigger) {
|
---|
| 191 | return;
|
---|
| 192 | }
|
---|
| 193 | selectedImageIndex = index;
|
---|
| 194 | listeners.fireEvent(l -> l.selectedImageChanged(this));
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Remove the current selected image from the list
|
---|
| 199 | */
|
---|
| 200 | public void removeSelectedImage() {
|
---|
| 201 | data.remove(getSelectedImage());
|
---|
| 202 | if (selectedImageIndex == data.size()) {
|
---|
| 203 | setSelectedImageIndex(data.size() - 1);
|
---|
| 204 | } else {
|
---|
| 205 | setSelectedImageIndex(selectedImageIndex, true);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * Remove the image from the list and trigger update listener
|
---|
| 211 | * @param img the {@link ImageEntry} to remove
|
---|
| 212 | */
|
---|
| 213 | public void removeImage(ImageEntry img) {
|
---|
| 214 | data.remove(img);
|
---|
| 215 | notifyImageUpdate();
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * Update the position of the image and trigger update
|
---|
| 220 | * @param img the image to update
|
---|
| 221 | * @param newPos the new position
|
---|
| 222 | */
|
---|
| 223 | public void updateImagePosition(ImageEntry img, LatLon newPos) {
|
---|
| 224 | img.setPos(newPos);
|
---|
| 225 | afterImageUpdated(img);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Update the image direction of the image and trigger update
|
---|
| 230 | * @param img the image to update
|
---|
| 231 | * @param direction the new direction
|
---|
| 232 | */
|
---|
| 233 | public void updateImageDirection(ImageEntry img, double direction) {
|
---|
| 234 | img.setExifImgDir(direction);
|
---|
| 235 | afterImageUpdated(img);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * Manually trigger the {@link ImageDataUpdateListener#imageDataUpdated(ImageData)}
|
---|
| 240 | */
|
---|
| 241 | public void notifyImageUpdate() {
|
---|
| 242 | listeners.fireEvent(l -> l.imageDataUpdated(this));
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | private void afterImageUpdated(ImageEntry img) {
|
---|
| 246 | img.flagNewGpsData();
|
---|
| 247 | notifyImageUpdate();
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Add a listener that listens to image data changes
|
---|
| 252 | * @param listener the {@link ImageDataUpdateListener}
|
---|
| 253 | */
|
---|
| 254 | public void addImageDataUpdateListener(ImageDataUpdateListener listener) {
|
---|
| 255 | listeners.addListener(listener);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
| 259 | * Removes a listener that listens to image data changes
|
---|
| 260 | * @param listener The listener
|
---|
| 261 | */
|
---|
| 262 | public void removeImageDataUpdateListener(ImageDataUpdateListener listener) {
|
---|
| 263 | listeners.removeListener(listener);
|
---|
| 264 | }
|
---|
| 265 | }
|
---|