[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.io;
|
---|
| 3 |
|
---|
| 4 | import java.io.File;
|
---|
| 5 |
|
---|
[7402] | 6 | import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
|
---|
[2848] | 7 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 8 |
|
---|
[2512] | 9 | /**
|
---|
| 10 | * SaveLayerInfo represents the information, user preferences and save/upload states of
|
---|
| 11 | * a layer which might be uploaded/saved.
|
---|
[7358] | 12 | * @since 2025
|
---|
[2512] | 13 | */
|
---|
| 14 | class SaveLayerInfo implements Comparable<SaveLayerInfo> {
|
---|
| 15 |
|
---|
[7358] | 16 | /** the modifiable layer */
|
---|
[9078] | 17 | private final AbstractModifiableLayer layer;
|
---|
[7204] | 18 | private boolean doCheckSaveConditions;
|
---|
[2512] | 19 | private boolean doSaveToFile;
|
---|
| 20 | private boolean doUploadToServer;
|
---|
| 21 | private File file;
|
---|
| 22 | private UploadOrSaveState uploadState;
|
---|
| 23 | private UploadOrSaveState saveState;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
[7204] | 26 | * Constructs a new {@code SaveLayerInfo}.
|
---|
[2512] | 27 | * @param layer the layer. Must not be null.
|
---|
[8291] | 28 | * @throws IllegalArgumentException if layer is null
|
---|
[2512] | 29 | */
|
---|
[8836] | 30 | SaveLayerInfo(AbstractModifiableLayer layer) {
|
---|
[2848] | 31 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
[2512] | 32 | this.layer = layer;
|
---|
[7204] | 33 | this.doCheckSaveConditions = true;
|
---|
[2512] | 34 | this.doSaveToFile = layer.requiresSaveToFile();
|
---|
[5233] | 35 | this.doUploadToServer = layer.requiresUploadToServer() && !layer.isUploadDiscouraged();
|
---|
[2512] | 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 | */
|
---|
[7402] | 44 | public AbstractModifiableLayer getLayer() {
|
---|
[2512] | 45 | return layer;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
[9751] | 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 | /**
|
---|
[7204] | 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 | /**
|
---|
[2512] | 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) {
|
---|
[15121] | 101 | this.doSaveToFile = isSavable() && doSaveToFile;
|
---|
[2512] | 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * Replies true if this layer should be uploaded to the server; false, otherwise
|
---|
| 106 | *
|
---|
[5836] | 107 | * @return {@code true} if this layer should be uploaded to the server; {@code false}, otherwise
|
---|
[2512] | 108 | */
|
---|
| 109 | public boolean isDoUploadToServer() {
|
---|
| 110 | return doUploadToServer;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
[9751] | 114 | * Sets whether this layer should be uploaded to a server
|
---|
[2512] | 115 | *
|
---|
[5836] | 116 | * @param doUploadToServer {@code true} to upload; {@code false}, to skip uploading
|
---|
[2512] | 117 | */
|
---|
| 118 | public void setDoUploadToServer(boolean doUploadToServer) {
|
---|
[15121] | 119 | this.doUploadToServer = isUploadable() && doUploadToServer;
|
---|
[2512] | 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 | /**
|
---|
[5266] | 141 | * Replies the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
|
---|
[2512] | 142 | *
|
---|
[5266] | 143 | * @return the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
|
---|
[2512] | 144 | */
|
---|
| 145 | public File getFile() {
|
---|
| 146 | return file;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
[5266] | 150 | * Sets the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
|
---|
[2512] | 151 | *
|
---|
| 152 | * @param file the file
|
---|
| 153 | */
|
---|
| 154 | public void setFile(File file) {
|
---|
| 155 | this.file = file;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[6084] | 158 | @Override
|
---|
[2512] | 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 | /**
|
---|
[5266] | 182 | * Replies the upload state of {@link #getLayer()}.
|
---|
[2512] | 183 | * <ul>
|
---|
[5836] | 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>
|
---|
[2512] | 187 | * </ul>
|
---|
| 188 | *
|
---|
| 189 | * @return the upload state
|
---|
| 190 | */
|
---|
| 191 | public UploadOrSaveState getUploadState() {
|
---|
| 192 | return uploadState;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
[5266] | 196 | * Sets the upload state for {@link #getLayer()}
|
---|
[2512] | 197 | *
|
---|
| 198 | * @param uploadState the upload state
|
---|
| 199 | */
|
---|
| 200 | public void setUploadState(UploadOrSaveState uploadState) {
|
---|
| 201 | this.uploadState = uploadState;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /**
|
---|
[5266] | 205 | * Replies the save state of {@link #getLayer()}.
|
---|
[2512] | 206 | * <ul>
|
---|
[5836] | 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>
|
---|
[2512] | 210 | * </ul>
|
---|
| 211 | *
|
---|
| 212 | * @return the save state
|
---|
| 213 | */
|
---|
| 214 | public UploadOrSaveState getSaveState() {
|
---|
| 215 | return saveState;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
[5266] | 219 | * Sets the save state for {@link #getLayer()}
|
---|
[2512] | 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 | }
|
---|