001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.io; 003 004import java.io.File; 005 006import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer; 007import org.openstreetmap.josm.tools.CheckParameterUtil; 008 009/** 010 * SaveLayerInfo represents the information, user preferences and save/upload states of 011 * a layer which might be uploaded/saved. 012 * @since 2025 013 */ 014class SaveLayerInfo implements Comparable<SaveLayerInfo> { 015 016 /** the modifiable layer */ 017 private final AbstractModifiableLayer layer; 018 private boolean doCheckSaveConditions; 019 private boolean doSaveToFile; 020 private boolean doUploadToServer; 021 private File file; 022 private UploadOrSaveState uploadState; 023 private UploadOrSaveState saveState; 024 025 /** 026 * Constructs a new {@code SaveLayerInfo}. 027 * @param layer the layer. Must not be null. 028 * @throws IllegalArgumentException if layer is null 029 */ 030 SaveLayerInfo(AbstractModifiableLayer layer) { 031 CheckParameterUtil.ensureParameterNotNull(layer, "layer"); 032 this.layer = layer; 033 this.doCheckSaveConditions = true; 034 this.doSaveToFile = layer.requiresSaveToFile(); 035 this.doUploadToServer = layer.requiresUploadToServer() && !layer.isUploadDiscouraged(); 036 this.file = layer.getAssociatedFile(); 037 } 038 039 /** 040 * Replies the layer this info objects holds information for 041 * 042 * @return the layer this info objects holds information for 043 */ 044 public AbstractModifiableLayer getLayer() { 045 return layer; 046 } 047 048 /** 049 * Replies true if the layer can be saved to a file 050 * 051 * @return {@code true} if the layer can be saved to a file; {@code false} otherwise 052 */ 053 public boolean isSavable() { 054 return layer.isSavable(); 055 } 056 057 /** 058 * Replies true if the layer can be uploaded to a server 059 * 060 * @return {@code true} if the layer can be uploaded to a server; {@code false} otherwise 061 */ 062 public boolean isUploadable() { 063 return layer.isUploadable(); 064 } 065 066 /** 067 * Replies true if preconditions should be checked before saving; false, otherwise 068 * 069 * @return true if preconditions should be checked before saving; false, otherwise 070 * @since 7204 071 */ 072 public boolean isDoCheckSaveConditions() { 073 return doCheckSaveConditions; 074 } 075 076 /** 077 * Sets whether preconditions should be checked before saving 078 * 079 * @param doCheckSaveConditions true to check save preconditions; false, to skip checking 080 * @since 7204 081 */ 082 public void setDoCheckSaveConditions(boolean doCheckSaveConditions) { 083 this.doCheckSaveConditions = doCheckSaveConditions; 084 } 085 086 /** 087 * Replies true if this layer should be saved to a file; false, otherwise 088 * 089 * @return true if this layers should be saved to a file; false, otherwise 090 */ 091 public boolean isDoSaveToFile() { 092 return doSaveToFile; 093 } 094 095 /** 096 * Sets whether this layer should be saved to a file 097 * 098 * @param doSaveToFile true to save; false, to skip saving 099 */ 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}