001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.io;
003
004 import java.io.File;
005
006 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
007 import 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 *
013 */
014 class SaveLayerInfo implements Comparable<SaveLayerInfo> {
015
016 /** the osm data layer */
017 private OsmDataLayer layer;
018 private boolean doSaveToFile;
019 private boolean doUploadToServer;
020 private File file;
021 private UploadOrSaveState uploadState;
022 private UploadOrSaveState saveState;
023
024 /**
025 *
026 * @param layer the layer. Must not be null.
027 * @throws IllegalArgumentException thrown if layer is null
028 */
029 public SaveLayerInfo(OsmDataLayer layer) {
030 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
031 this.layer = layer;
032 this.doSaveToFile = layer.requiresSaveToFile();
033 this.doUploadToServer = layer.requiresUploadToServer() && !layer.isUploadDiscouraged();
034 this.file = layer.getAssociatedFile();
035 }
036
037 /**
038 * Replies the layer this info objects holds information for
039 *
040 * @return the layer this info objects holds information for
041 */
042 public OsmDataLayer getLayer() {
043 return layer;
044 }
045
046 /**
047 * Replies true if this layer should be saved to a file; false, otherwise
048 *
049 * @return true if this layers should be saved to a file; false, otherwise
050 */
051 public boolean isDoSaveToFile() {
052 return doSaveToFile;
053 }
054
055 /**
056 * Sets whether this layer should be saved to a file
057 *
058 * @param doSaveToFile true to save; false, to skip saving
059 */
060 public void setDoSaveToFile(boolean doSaveToFile) {
061 this.doSaveToFile = doSaveToFile;
062 }
063
064 /**
065 * Replies true if this layer should be uploaded to the server; false, otherwise
066 *
067 * @return true if this layer should be uploaded to the server; false, otherwise
068 */
069 public boolean isDoUploadToServer() {
070 return doUploadToServer;
071 }
072
073 /**
074 * Sets whether this layer should be uploaded to a file
075 *
076 * @param doSaveToFile true to upload; false, to skip uploading
077 */
078
079 public void setDoUploadToServer(boolean doUploadToServer) {
080 this.doUploadToServer = doUploadToServer;
081 }
082
083 /**
084 * Replies true if this layer should be uploaded to the server and saved to file.
085 *
086 * @return true if this layer should be uploaded to the server and saved to file
087 */
088 public boolean isDoSaveAndUpload() {
089 return isDoSaveToFile() && isDoUploadToServer();
090 }
091
092 /**
093 * Replies the name of the layer
094 *
095 * @return the name of the layer
096 */
097 public String getName() {
098 return layer.getName() == null ? "" : layer.getName();
099 }
100
101 /**
102 * Replies the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
103 *
104 * @return the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
105 */
106 public File getFile() {
107 return file;
108 }
109
110 /**
111 * Sets the file this layer should be saved to, if {@link #isDoSaveToFile()} is true
112 *
113 * @param file the file
114 */
115 public void setFile(File file) {
116 this.file = file;
117 }
118
119 public int compareTo(SaveLayerInfo o) {
120 if (isDoSaveAndUpload()) {
121 if (o.isDoSaveAndUpload())
122 return getName().compareTo(o.getName());
123 return -1;
124 } else if (o.isDoSaveAndUpload())
125 return 1;
126 if (isDoUploadToServer()) {
127 if (o.isDoUploadToServer())
128 return getName().compareTo(o.getName());
129 return -1;
130 } else if (o.isDoUploadToServer())
131 return 1;
132 if (isDoSaveToFile()) {
133 if (o.isDoSaveToFile())
134 return getName().compareTo(o.getName());
135 return -1;
136 } else if (o.isDoSaveToFile())
137 return 1;
138 return getName().compareTo(o.getName());
139 }
140
141 /**
142 * Replies the upload state of {@link #getLayer()}.
143 * <ul>
144 * <li>{@link UploadOrSaveState#OK} if {@link #getLayer() was successfully uploaded</li>
145 * <li>{@link UploadOrSaveState#canceled} if uploading {@link #getLayer() was canceled</li>
146 * <li>{@link UploadOrSaveState#FAILED} if uploading {@link #getLayer() has failed</li>
147 * </ul>
148 *
149 * @return the upload state
150 */
151 public UploadOrSaveState getUploadState() {
152 return uploadState;
153 }
154
155 /**
156 * Sets the upload state for {@link #getLayer()}
157 *
158 * @param uploadState the upload state
159 */
160 public void setUploadState(UploadOrSaveState uploadState) {
161 this.uploadState = uploadState;
162 }
163
164 /**
165 * Replies the save state of {@link #getLayer()}.
166 * <ul>
167 * <li>{@link UploadOrSaveState#OK} if {@link #getLayer() was successfully saved to file</li>
168 * <li>{@link UploadOrSaveState#canceled} if saving {@link #getLayer() was canceled</li>
169 * <li>{@link UploadOrSaveState#FAILED} if saving {@link #getLayer() has failed</li>
170 * </ul>
171 *
172 * @return the save state
173 */
174 public UploadOrSaveState getSaveState() {
175 return saveState;
176 }
177
178 /**
179 * Sets the save state for {@link #getLayer()}
180 *
181 * @param saveState save the upload state
182 */
183 public void setSaveState(UploadOrSaveState saveState) {
184 this.saveState = saveState;
185 }
186
187 /**
188 * Resets the upload and save state
189 *
190 * @see #setUploadState(UploadOrSaveState)
191 * @see #setSaveState(UploadOrSaveState)
192 * @see #getUploadState()
193 * @see #getSaveState()
194 */
195 public void resetUploadAndSaveState() {
196 this.uploadState = null;
197 this.saveState = null;
198 }
199 }