| 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.OsmDataLayer;
|
|---|
| 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 | *
|
|---|
| 13 | */
|
|---|
| 14 | class SaveLayerInfo implements Comparable<SaveLayerInfo> {
|
|---|
| 15 |
|
|---|
| 16 | /** the osm data layer */
|
|---|
| 17 | private OsmDataLayer layer;
|
|---|
| 18 | private boolean doSaveToFile;
|
|---|
| 19 | private boolean doUploadToServer;
|
|---|
| 20 | private File file;
|
|---|
| 21 | private UploadOrSaveState uploadState;
|
|---|
| 22 | private UploadOrSaveState saveState;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | *
|
|---|
| 26 | * @param layer the layer. Must not be null.
|
|---|
| 27 | * @throws IllegalArgumentException thrown if layer is null
|
|---|
| 28 | */
|
|---|
| 29 | public SaveLayerInfo(OsmDataLayer layer) {
|
|---|
| 30 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
|---|
| 31 | this.layer = layer;
|
|---|
| 32 | this.doSaveToFile = layer.requiresSaveToFile();
|
|---|
| 33 | this.doUploadToServer = layer.requiresUploadToServer() && !layer.isUploadDiscouraged();
|
|---|
| 34 | this.file = layer.getAssociatedFile();
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Replies the layer this info objects holds information for
|
|---|
| 39 | *
|
|---|
| 40 | * @return the layer this info objects holds information for
|
|---|
| 41 | */
|
|---|
| 42 | public OsmDataLayer getLayer() {
|
|---|
| 43 | return layer;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Replies true if this layer should be saved to a file; false, otherwise
|
|---|
| 48 | *
|
|---|
| 49 | * @return true if this layers should be saved to a file; false, otherwise
|
|---|
| 50 | */
|
|---|
| 51 | public boolean isDoSaveToFile() {
|
|---|
| 52 | return doSaveToFile;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Sets whether this layer should be saved to a file
|
|---|
| 57 | *
|
|---|
| 58 | * @param doSaveToFile true to save; false, to skip saving
|
|---|
| 59 | */
|
|---|
| 60 | public void setDoSaveToFile(boolean doSaveToFile) {
|
|---|
| 61 | this.doSaveToFile = doSaveToFile;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Replies true if this layer should be uploaded to the server; false, otherwise
|
|---|
| 66 | *
|
|---|
| 67 | * @return {@code true} if this layer should be uploaded to the server; {@code false}, otherwise
|
|---|
| 68 | */
|
|---|
| 69 | public boolean isDoUploadToServer() {
|
|---|
| 70 | return doUploadToServer;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Sets whether this layer should be uploaded to a file
|
|---|
| 75 | *
|
|---|
| 76 | * @param doUploadToServer {@code true} to upload; {@code false}, to skip uploading
|
|---|
| 77 | */
|
|---|
| 78 |
|
|---|
| 79 | public void setDoUploadToServer(boolean doUploadToServer) {
|
|---|
| 80 | this.doUploadToServer = doUploadToServer;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Replies true if this layer should be uploaded to the server and saved to file.
|
|---|
| 85 | *
|
|---|
| 86 | * @return true if this layer should be uploaded to the server and saved to file
|
|---|
| 87 | */
|
|---|
| 88 | public boolean isDoSaveAndUpload() {
|
|---|
| 89 | return isDoSaveToFile() && isDoUploadToServer();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Replies the name of the layer
|
|---|
| 94 | *
|
|---|
| 95 | * @return the name of the layer
|
|---|
| 96 | */
|
|---|
| 97 | public String getName() {
|
|---|
| 98 | return layer.getName() == null ? "" : layer.getName();
|
|---|
| 99 | }
|
|---|
| 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 | @Override
|
|---|
| 120 | public int compareTo(SaveLayerInfo o) {
|
|---|
| 121 | if (isDoSaveAndUpload()) {
|
|---|
| 122 | if (o.isDoSaveAndUpload())
|
|---|
| 123 | return getName().compareTo(o.getName());
|
|---|
| 124 | return -1;
|
|---|
| 125 | } else if (o.isDoSaveAndUpload())
|
|---|
| 126 | return 1;
|
|---|
| 127 | if (isDoUploadToServer()) {
|
|---|
| 128 | if (o.isDoUploadToServer())
|
|---|
| 129 | return getName().compareTo(o.getName());
|
|---|
| 130 | return -1;
|
|---|
| 131 | } else if (o.isDoUploadToServer())
|
|---|
| 132 | return 1;
|
|---|
| 133 | if (isDoSaveToFile()) {
|
|---|
| 134 | if (o.isDoSaveToFile())
|
|---|
| 135 | return getName().compareTo(o.getName());
|
|---|
| 136 | return -1;
|
|---|
| 137 | } else if (o.isDoSaveToFile())
|
|---|
| 138 | return 1;
|
|---|
| 139 | return getName().compareTo(o.getName());
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Replies the upload state of {@link #getLayer()}.
|
|---|
| 144 | * <ul>
|
|---|
| 145 | * <li>{@link UploadOrSaveState#OK} if {@link #getLayer()} was successfully uploaded</li>
|
|---|
| 146 | * <li>{@link UploadOrSaveState#CANCELED} if uploading {@link #getLayer()} was canceled</li>
|
|---|
| 147 | * <li>{@link UploadOrSaveState#FAILED} if uploading {@link #getLayer()} has failed</li>
|
|---|
| 148 | * </ul>
|
|---|
| 149 | *
|
|---|
| 150 | * @return the upload state
|
|---|
| 151 | */
|
|---|
| 152 | public UploadOrSaveState getUploadState() {
|
|---|
| 153 | return uploadState;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Sets the upload state for {@link #getLayer()}
|
|---|
| 158 | *
|
|---|
| 159 | * @param uploadState the upload state
|
|---|
| 160 | */
|
|---|
| 161 | public void setUploadState(UploadOrSaveState uploadState) {
|
|---|
| 162 | this.uploadState = uploadState;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * Replies the save state of {@link #getLayer()}.
|
|---|
| 167 | * <ul>
|
|---|
| 168 | * <li>{@link UploadOrSaveState#OK} if {@link #getLayer()} was successfully saved to file</li>
|
|---|
| 169 | * <li>{@link UploadOrSaveState#CANCELED} if saving {@link #getLayer()} was canceled</li>
|
|---|
| 170 | * <li>{@link UploadOrSaveState#FAILED} if saving {@link #getLayer()} has failed</li>
|
|---|
| 171 | * </ul>
|
|---|
| 172 | *
|
|---|
| 173 | * @return the save state
|
|---|
| 174 | */
|
|---|
| 175 | public UploadOrSaveState getSaveState() {
|
|---|
| 176 | return saveState;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /**
|
|---|
| 180 | * Sets the save state for {@link #getLayer()}
|
|---|
| 181 | *
|
|---|
| 182 | * @param saveState save the upload state
|
|---|
| 183 | */
|
|---|
| 184 | public void setSaveState(UploadOrSaveState saveState) {
|
|---|
| 185 | this.saveState = saveState;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /**
|
|---|
| 189 | * Resets the upload and save state
|
|---|
| 190 | *
|
|---|
| 191 | * @see #setUploadState(UploadOrSaveState)
|
|---|
| 192 | * @see #setSaveState(UploadOrSaveState)
|
|---|
| 193 | * @see #getUploadState()
|
|---|
| 194 | * @see #getSaveState()
|
|---|
| 195 | */
|
|---|
| 196 | public void resetUploadAndSaveState() {
|
|---|
| 197 | this.uploadState = null;
|
|---|
| 198 | this.saveState = null;
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|