| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.io;
|
|---|
| 3 |
|
|---|
| 4 | import java.io.File;
|
|---|
| 5 |
|
|---|
| 6 | import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
|
|---|
| 7 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * SaveLayerInfo represents the information, user preferences and save/upload states of
|
|---|
| 11 | * a layer which might be uploaded/saved.
|
|---|
| 12 | * @since 2025
|
|---|
| 13 | */
|
|---|
| 14 | class SaveLayerInfo implements Comparable<SaveLayerInfo> {
|
|---|
| 15 |
|
|---|
| 16 | /** the modifiable layer */
|
|---|
| 17 | private final AbstractModifiableLayer layer;
|
|---|
| 18 | private boolean doCheckSaveConditions;
|
|---|
| 19 | private boolean doSaveToFile;
|
|---|
| 20 | private boolean doUploadToServer;
|
|---|
| 21 | private File file;
|
|---|
| 22 | private UploadOrSaveState uploadState;
|
|---|
| 23 | private UploadOrSaveState saveState;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Constructs a new {@code SaveLayerInfo}.
|
|---|
| 27 | * @param layer the layer. Must not be null.
|
|---|
| 28 | * @throws IllegalArgumentException if layer is null
|
|---|
| 29 | */
|
|---|
| 30 | SaveLayerInfo(AbstractModifiableLayer layer) {
|
|---|
| 31 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
|---|
| 32 | this.layer = layer;
|
|---|
| 33 | this.doCheckSaveConditions = true;
|
|---|
| 34 | this.doSaveToFile = layer.requiresSaveToFile();
|
|---|
| 35 | this.doUploadToServer = layer.requiresUploadToServer() && !layer.isUploadDiscouraged();
|
|---|
| 36 | this.file = layer.getAssociatedFile();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Replies the layer this info objects holds information for
|
|---|
| 41 | *
|
|---|
| 42 | * @return the layer this info objects holds information for
|
|---|
| 43 | */
|
|---|
| 44 | public AbstractModifiableLayer getLayer() {
|
|---|
| 45 | return layer;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Replies true if the layer can be saved to a file
|
|---|
| 50 | *
|
|---|
| 51 | * @return {@code true} if the layer can be saved to a file; {@code false} otherwise
|
|---|
| 52 | */
|
|---|
| 53 | public boolean isSavable() {
|
|---|
| 54 | return layer.isSavable();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Replies true if the layer can be uploaded to a server
|
|---|
| 59 | *
|
|---|
| 60 | * @return {@code true} if the layer can be uploaded to a server; {@code false} otherwise
|
|---|
| 61 | */
|
|---|
| 62 | public boolean isUploadable() {
|
|---|
| 63 | return layer.isUploadable();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Replies true if preconditions should be checked before saving; false, otherwise
|
|---|
| 68 | *
|
|---|
| 69 | * @return true if preconditions should be checked before saving; false, otherwise
|
|---|
| 70 | * @since 7204
|
|---|
| 71 | */
|
|---|
| 72 | public boolean isDoCheckSaveConditions() {
|
|---|
| 73 | return doCheckSaveConditions;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Sets whether preconditions should be checked before saving
|
|---|
| 78 | *
|
|---|
| 79 | * @param doCheckSaveConditions true to check save preconditions; false, to skip checking
|
|---|
| 80 | * @since 7204
|
|---|
| 81 | */
|
|---|
| 82 | public void setDoCheckSaveConditions(boolean doCheckSaveConditions) {
|
|---|
| 83 | this.doCheckSaveConditions = doCheckSaveConditions;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * Replies true if this layer should be saved to a file; false, otherwise
|
|---|
| 88 | *
|
|---|
| 89 | * @return true if this layers should be saved to a file; false, otherwise
|
|---|
| 90 | */
|
|---|
| 91 | public boolean isDoSaveToFile() {
|
|---|
| 92 | return doSaveToFile;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Sets whether this layer should be saved to a file
|
|---|
| 97 | *
|
|---|
| 98 | * @param doSaveToFile true to save; false, to skip saving
|
|---|
| 99 | */
|
|---|
| 100 | public void setDoSaveToFile(boolean doSaveToFile) {
|
|---|
| 101 | this.doSaveToFile = isSavable() && doSaveToFile;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Replies true if this layer should be uploaded to the server; false, otherwise
|
|---|
| 106 | *
|
|---|
| 107 | * @return {@code true} if this layer should be uploaded to the server; {@code false}, otherwise
|
|---|
| 108 | */
|
|---|
| 109 | public boolean isDoUploadToServer() {
|
|---|
| 110 | return doUploadToServer;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Sets whether this layer should be uploaded to a server
|
|---|
| 115 | *
|
|---|
| 116 | * @param doUploadToServer {@code true} to upload; {@code false}, to skip uploading
|
|---|
| 117 | */
|
|---|
| 118 | public void setDoUploadToServer(boolean doUploadToServer) {
|
|---|
| 119 | this.doUploadToServer = isUploadable() && doUploadToServer;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Replies true if this layer should be uploaded to the server and saved to file.
|
|---|
| 124 | *
|
|---|
| 125 | * @return true if this layer should be uploaded to the server and saved to file
|
|---|
| 126 | */
|
|---|
| 127 | public boolean isDoSaveAndUpload() {
|
|---|
| 128 | return isDoSaveToFile() && isDoUploadToServer();
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Replies the name of the layer
|
|---|
| 133 | *
|
|---|
| 134 | * @return the name of the layer
|
|---|
| 135 | */
|
|---|
| 136 | public String getName() {
|
|---|
| 137 | return layer.getName() == null ? "" : layer.getName();
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /**
|
|---|
| 141 | * Replies the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
|
|---|
| 142 | *
|
|---|
| 143 | * @return the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
|
|---|
| 144 | */
|
|---|
| 145 | public File getFile() {
|
|---|
| 146 | return file;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Sets the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
|
|---|
| 151 | *
|
|---|
| 152 | * @param file the file
|
|---|
| 153 | */
|
|---|
| 154 | public void setFile(File file) {
|
|---|
| 155 | this.file = file;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | @Override
|
|---|
| 159 | public int compareTo(SaveLayerInfo o) {
|
|---|
| 160 | if (isDoSaveAndUpload()) {
|
|---|
| 161 | if (o.isDoSaveAndUpload())
|
|---|
| 162 | return getName().compareTo(o.getName());
|
|---|
| 163 | return -1;
|
|---|
| 164 | } else if (o.isDoSaveAndUpload())
|
|---|
| 165 | return 1;
|
|---|
| 166 | if (isDoUploadToServer()) {
|
|---|
| 167 | if (o.isDoUploadToServer())
|
|---|
| 168 | return getName().compareTo(o.getName());
|
|---|
| 169 | return -1;
|
|---|
| 170 | } else if (o.isDoUploadToServer())
|
|---|
| 171 | return 1;
|
|---|
| 172 | if (isDoSaveToFile()) {
|
|---|
| 173 | if (o.isDoSaveToFile())
|
|---|
| 174 | return getName().compareTo(o.getName());
|
|---|
| 175 | return -1;
|
|---|
| 176 | } else if (o.isDoSaveToFile())
|
|---|
| 177 | return 1;
|
|---|
| 178 | return getName().compareTo(o.getName());
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Replies the upload state of {@link #getLayer()}.
|
|---|
| 183 | * <ul>
|
|---|
| 184 | * <li>{@link UploadOrSaveState#OK} if {@link #getLayer()} was successfully uploaded</li>
|
|---|
| 185 | * <li>{@link UploadOrSaveState#CANCELED} if uploading {@link #getLayer()} was canceled</li>
|
|---|
| 186 | * <li>{@link UploadOrSaveState#FAILED} if uploading {@link #getLayer()} has failed</li>
|
|---|
| 187 | * </ul>
|
|---|
| 188 | *
|
|---|
| 189 | * @return the upload state
|
|---|
| 190 | */
|
|---|
| 191 | public UploadOrSaveState getUploadState() {
|
|---|
| 192 | return uploadState;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Sets the upload state for {@link #getLayer()}
|
|---|
| 197 | *
|
|---|
| 198 | * @param uploadState the upload state
|
|---|
| 199 | */
|
|---|
| 200 | public void setUploadState(UploadOrSaveState uploadState) {
|
|---|
| 201 | this.uploadState = uploadState;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * Replies the save state of {@link #getLayer()}.
|
|---|
| 206 | * <ul>
|
|---|
| 207 | * <li>{@link UploadOrSaveState#OK} if {@link #getLayer()} was successfully saved to file</li>
|
|---|
| 208 | * <li>{@link UploadOrSaveState#CANCELED} if saving {@link #getLayer()} was canceled</li>
|
|---|
| 209 | * <li>{@link UploadOrSaveState#FAILED} if saving {@link #getLayer()} has failed</li>
|
|---|
| 210 | * </ul>
|
|---|
| 211 | *
|
|---|
| 212 | * @return the save state
|
|---|
| 213 | */
|
|---|
| 214 | public UploadOrSaveState getSaveState() {
|
|---|
| 215 | return saveState;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /**
|
|---|
| 219 | * Sets the save state for {@link #getLayer()}
|
|---|
| 220 | *
|
|---|
| 221 | * @param saveState save the upload state
|
|---|
| 222 | */
|
|---|
| 223 | public void setSaveState(UploadOrSaveState saveState) {
|
|---|
| 224 | this.saveState = saveState;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Resets the upload and save state
|
|---|
| 229 | *
|
|---|
| 230 | * @see #setUploadState(UploadOrSaveState)
|
|---|
| 231 | * @see #setSaveState(UploadOrSaveState)
|
|---|
| 232 | * @see #getUploadState()
|
|---|
| 233 | * @see #getSaveState()
|
|---|
| 234 | */
|
|---|
| 235 | public void resetUploadAndSaveState() {
|
|---|
| 236 | this.uploadState = null;
|
|---|
| 237 | this.saveState = null;
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|