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 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 thrown if layer is null
|
---|
29 | */
|
---|
30 | public 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 preconditions should be checked before saving; false, otherwise
|
---|
50 | *
|
---|
51 | * @return true if preconditions should be checked before saving; false, otherwise
|
---|
52 | * @since 7204
|
---|
53 | */
|
---|
54 | public boolean isDoCheckSaveConditions() {
|
---|
55 | return doCheckSaveConditions;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Sets whether preconditions should be checked before saving
|
---|
60 | *
|
---|
61 | * @param doCheckSaveConditions true to check save preconditions; false, to skip checking
|
---|
62 | * @since 7204
|
---|
63 | */
|
---|
64 | public void setDoCheckSaveConditions(boolean doCheckSaveConditions) {
|
---|
65 | this.doCheckSaveConditions = doCheckSaveConditions;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Replies true if this layer should be saved to a file; false, otherwise
|
---|
70 | *
|
---|
71 | * @return true if this layers should be saved to a file; false, otherwise
|
---|
72 | */
|
---|
73 | public boolean isDoSaveToFile() {
|
---|
74 | return doSaveToFile;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Sets whether this layer should be saved to a file
|
---|
79 | *
|
---|
80 | * @param doSaveToFile true to save; false, to skip saving
|
---|
81 | */
|
---|
82 | public void setDoSaveToFile(boolean doSaveToFile) {
|
---|
83 | this.doSaveToFile = doSaveToFile;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Replies true if this layer should be uploaded to the server; false, otherwise
|
---|
88 | *
|
---|
89 | * @return {@code true} if this layer should be uploaded to the server; {@code false}, otherwise
|
---|
90 | */
|
---|
91 | public boolean isDoUploadToServer() {
|
---|
92 | return doUploadToServer;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Sets whether this layer should be uploaded to a file
|
---|
97 | *
|
---|
98 | * @param doUploadToServer {@code true} to upload; {@code false}, to skip uploading
|
---|
99 | */
|
---|
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 | }
|
---|