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.OsmDataLayer;
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 *
013 */
014class 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 {@code true} if this layer should be uploaded to the server; {@code 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 doUploadToServer {@code true} to upload; {@code 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    @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}