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 preconditions should be checked before saving; false, otherwise 050 * 051 * @return true if preconditions should be checked before saving; false, otherwise 052 * @since 7204 053 */ 054 public boolean isDoCheckSaveConditions() { 055 return doCheckSaveConditions; 056 } 057 058 /** 059 * Sets whether preconditions should be checked before saving 060 * 061 * @param doCheckSaveConditions true to check save preconditions; false, to skip checking 062 * @since 7204 063 */ 064 public void setDoCheckSaveConditions(boolean doCheckSaveConditions) { 065 this.doCheckSaveConditions = doCheckSaveConditions; 066 } 067 068 /** 069 * Replies true if this layer should be saved to a file; false, otherwise 070 * 071 * @return true if this layers should be saved to a file; false, otherwise 072 */ 073 public boolean isDoSaveToFile() { 074 return doSaveToFile; 075 } 076 077 /** 078 * Sets whether this layer should be saved to a file 079 * 080 * @param doSaveToFile true to save; false, to skip saving 081 */ 082 public void setDoSaveToFile(boolean doSaveToFile) { 083 this.doSaveToFile = doSaveToFile; 084 } 085 086 /** 087 * Replies true if this layer should be uploaded to the server; false, otherwise 088 * 089 * @return {@code true} if this layer should be uploaded to the server; {@code false}, otherwise 090 */ 091 public boolean isDoUploadToServer() { 092 return doUploadToServer; 093 } 094 095 /** 096 * Sets whether this layer should be uploaded to a file 097 * 098 * @param doUploadToServer {@code true} to upload; {@code false}, to skip uploading 099 */ 100 101 public void setDoUploadToServer(boolean doUploadToServer) { 102 this.doUploadToServer = doUploadToServer; 103 } 104 105 /** 106 * Replies true if this layer should be uploaded to the server and saved to file. 107 * 108 * @return true if this layer should be uploaded to the server and saved to file 109 */ 110 public boolean isDoSaveAndUpload() { 111 return isDoSaveToFile() && isDoUploadToServer(); 112 } 113 114 /** 115 * Replies the name of the layer 116 * 117 * @return the name of the layer 118 */ 119 public String getName() { 120 return layer.getName() == null ? "" : layer.getName(); 121 } 122 123 /** 124 * Replies the file this layer should be saved to, if {@link #isDoSaveToFile()} is true 125 * 126 * @return the file this layer should be saved to, if {@link #isDoSaveToFile()} is true 127 */ 128 public File getFile() { 129 return file; 130 } 131 132 /** 133 * Sets the file this layer should be saved to, if {@link #isDoSaveToFile()} is true 134 * 135 * @param file the file 136 */ 137 public void setFile(File file) { 138 this.file = file; 139 } 140 141 @Override 142 public int compareTo(SaveLayerInfo o) { 143 if (isDoSaveAndUpload()) { 144 if (o.isDoSaveAndUpload()) 145 return getName().compareTo(o.getName()); 146 return -1; 147 } else if (o.isDoSaveAndUpload()) 148 return 1; 149 if (isDoUploadToServer()) { 150 if (o.isDoUploadToServer()) 151 return getName().compareTo(o.getName()); 152 return -1; 153 } else if (o.isDoUploadToServer()) 154 return 1; 155 if (isDoSaveToFile()) { 156 if (o.isDoSaveToFile()) 157 return getName().compareTo(o.getName()); 158 return -1; 159 } else if (o.isDoSaveToFile()) 160 return 1; 161 return getName().compareTo(o.getName()); 162 } 163 164 /** 165 * Replies the upload state of {@link #getLayer()}. 166 * <ul> 167 * <li>{@link UploadOrSaveState#OK} if {@link #getLayer()} was successfully uploaded</li> 168 * <li>{@link UploadOrSaveState#CANCELED} if uploading {@link #getLayer()} was canceled</li> 169 * <li>{@link UploadOrSaveState#FAILED} if uploading {@link #getLayer()} has failed</li> 170 * </ul> 171 * 172 * @return the upload state 173 */ 174 public UploadOrSaveState getUploadState() { 175 return uploadState; 176 } 177 178 /** 179 * Sets the upload state for {@link #getLayer()} 180 * 181 * @param uploadState the upload state 182 */ 183 public void setUploadState(UploadOrSaveState uploadState) { 184 this.uploadState = uploadState; 185 } 186 187 /** 188 * Replies the save state of {@link #getLayer()}. 189 * <ul> 190 * <li>{@link UploadOrSaveState#OK} if {@link #getLayer()} was successfully saved to file</li> 191 * <li>{@link UploadOrSaveState#CANCELED} if saving {@link #getLayer()} was canceled</li> 192 * <li>{@link UploadOrSaveState#FAILED} if saving {@link #getLayer()} has failed</li> 193 * </ul> 194 * 195 * @return the save state 196 */ 197 public UploadOrSaveState getSaveState() { 198 return saveState; 199 } 200 201 /** 202 * Sets the save state for {@link #getLayer()} 203 * 204 * @param saveState save the upload state 205 */ 206 public void setSaveState(UploadOrSaveState saveState) { 207 this.saveState = saveState; 208 } 209 210 /** 211 * Resets the upload and save state 212 * 213 * @see #setUploadState(UploadOrSaveState) 214 * @see #setSaveState(UploadOrSaveState) 215 * @see #getUploadState() 216 * @see #getSaveState() 217 */ 218 public void resetUploadAndSaveState() { 219 this.uploadState = null; 220 this.saveState = null; 221 } 222}