Ticket #4043: patch1.diff

File patch1.diff, 23.0 KB (added by Don-vip, 14 years ago)
  • core/src/org/openstreetmap/josm/Main.java

     
    587587        SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
    588588        List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
    589589        for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
    590             if (l.requiresSaveToFile() || l.requiresUploadToServer()) {
     590            if (l.requiresSaveToFile() || (l.isUploadAllowed() && l.requiresUploadToServer())) {
    591591                layersWithUnmodifiedChanges.add(l);
    592592            }
    593593        }
  • core/src/org/openstreetmap/josm/actions/UploadAction.java

     
    2121import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    2222import org.openstreetmap.josm.gui.help.HelpUtil;
    2323import org.openstreetmap.josm.gui.io.UploadDialog;
     24import org.openstreetmap.josm.gui.io.UploadForbiddenException;
    2425import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
    2526import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2627import org.openstreetmap.josm.tools.Shortcut;
     
    103104        return checkPreUploadConditions(layer, new APIDataSet(layer.data));
    104105    }
    105106
    106     protected void alertUnresolvedConflicts(OsmDataLayer layer) {
     107    protected static void alertForbiddenUpload(OsmDataLayer layer) {
     108        JOptionPane.showMessageDialog(
     109                Main.parent,
     110                tr("Upload is forbidden for layer ''{0}''", layer.getName()),
     111                tr("Warning"),
     112                JOptionPane.INFORMATION_MESSAGE
     113        );
     114    }
     115
     116    protected static void alertUnresolvedConflicts(OsmDataLayer layer) {
    107117        HelpAwareOptionPane.showOptionDialog(
    108118                Main.parent,
    109119                tr("<html>The data to be uploaded participates in unresolved conflicts of layer ''{0}''.<br>"
     
    117127
    118128    /**
    119129     * Check whether the preconditions are met to upload data in <code>apiData</code>.
    120      * Makes sure primitives in <code>apiData</code> don't participate in conflicts and
     130     * Makes sure upload is allowed, primitives in <code>apiData</code> don't participate in conflicts and
    121131     * runs the installed {@see UploadHook}s.
    122132     *
    123133     * @param layer the source layer of the data to be uploaded
     
    125135     * @return true, if the preconditions are met; false, otherwise
    126136     */
    127137    public boolean checkPreUploadConditions(OsmDataLayer layer, APIDataSet apiData) {
     138        if (!layer.isUploadAllowed()) {
     139            alertForbiddenUpload(layer);
     140            return false;
     141        }
    128142        ConflictCollection conflicts = layer.getConflicts();
    129143        if (apiData.participatesInConflict(conflicts)) {
    130144            alertUnresolvedConflicts(layer);
     
    177191            return;
    178192        dialog.rememberUserInput();
    179193
    180         Main.worker.execute(
    181                 new UploadPrimitivesTask(
    182                         UploadDialog.getUploadDialog().getUploadStrategySpecification(),
    183                         layer,
    184                         apiData,
    185                         UploadDialog.getUploadDialog().getChangeset()
    186                 )
    187         );
     194        try {
     195            Main.worker.execute(
     196                    new UploadPrimitivesTask(
     197                            UploadDialog.getUploadDialog().getUploadStrategySpecification(),
     198                            layer,
     199                            apiData,
     200                            UploadDialog.getUploadDialog().getChangeset()
     201                    )
     202            );
     203        } catch (UploadForbiddenException ex) {
     204            // This should not happen
     205            System.err.println(ex.getMessage());
     206        }
    188207    }
    189208
    190209    public void actionPerformed(ActionEvent e) {
  • core/src/org/openstreetmap/josm/actions/UploadSelectionAction.java

     
    2525import org.openstreetmap.josm.data.osm.visitor.Visitor;
    2626import org.openstreetmap.josm.gui.DefaultNameFormatter;
    2727import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     28import org.openstreetmap.josm.gui.io.UploadForbiddenException;
    2829import org.openstreetmap.josm.gui.io.UploadSelectionDialog;
    2930import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3031import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
     
    8788    public void actionPerformed(ActionEvent e) {
    8889        if (!isEnabled())
    8990            return;
     91        if (!getEditLayer().isUploadAllowed()) {
     92            JOptionPane.showMessageDialog(
     93                    Main.parent,
     94                    tr("Upload is forbidden for layer ''{0}''", getEditLayer().getName()),
     95                    tr("Warning"),
     96                    JOptionPane.INFORMATION_MESSAGE
     97            );
     98            return;
     99        }
    90100        UploadHullBuilder builder = new UploadHullBuilder();
    91101        UploadSelectionDialog dialog = new UploadSelectionDialog();
    92102        Collection<OsmPrimitive> modifiedCandidates = getModifiedPrimitives(getEditLayer().data.getSelected());
     
    117127            );
    118128            return;
    119129        }
    120         uploadPrimitives(getEditLayer(), toUpload);
     130        try {
     131            uploadPrimitives(getEditLayer(), toUpload);
     132        } catch (UploadForbiddenException ex) {
     133            // This should not happen as it has been previously checked
     134            System.err.println(ex.getMessage());
     135        }
    121136    }
    122137
    123138    /**
     
    144159     *
    145160     * @param layer the data layer from which we upload a subset of primitives
    146161     * @param toUpload the primitives to upload. If null or empty returns immediatelly
     162     * @throws UploadForbiddenException if upload is not allowed for layer
    147163     */
    148     public void uploadPrimitives(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) {
     164    public void uploadPrimitives(OsmDataLayer layer, Collection<OsmPrimitive> toUpload) throws UploadForbiddenException {
    149165        if (toUpload == null || toUpload.isEmpty()) return;
    150166        UploadHullBuilder builder = new UploadHullBuilder();
    151167        toUpload = builder.build(toUpload);
  • core/src/org/openstreetmap/josm/data/osm/DataSet.java

     
    124124    private final List<AbstractDatasetChangedEvent> cachedEvents = new ArrayList<AbstractDatasetChangedEvent>();
    125125
    126126    private int highlightUpdateCount;
     127   
     128    private boolean uploadAllowed = true;
    127129
    128130    private final ReadWriteLock lock = new ReentrantReadWriteLock();
    129131    private final Object selectionLock = new Object();
     
    206208        this.version = version;
    207209    }
    208210
     211    public final boolean isUploadAllowed() {
     212        return uploadAllowed;
     213    }
     214
     215    public final void setUploadAllowed(boolean uploadAllowed) {
     216        this.uploadAllowed = uploadAllowed;
     217    }
     218
    209219    /*
    210220     * Holding bin for changeset tag information, to be applied when or if this is ever uploaded.
    211221     */
  • core/src/org/openstreetmap/josm/gui/MapView.java

     
    824824    }
    825825
    826826    protected void refreshTitle() {
    827         boolean dirty = editLayer != null && (editLayer.requiresSaveToFile() || editLayer.requiresUploadToServer());
     827        boolean dirty = editLayer != null && (editLayer.requiresSaveToFile() || (editLayer.isUploadAllowed() && editLayer.requiresUploadToServer()));
    828828        if (dirty) {
    829829            JOptionPane.getFrameForComponent(Main.parent).setTitle("* " + tr("Java OpenStreetMap Editor"));
    830830        } else {
  • core/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

     
    376376                    continue;
    377377                }
    378378                OsmDataLayer odl = (OsmDataLayer)l;
    379                 if ((odl.requiresSaveToFile() || odl.requiresUploadToServer()) && odl.data.isModified()) {
     379                if ((odl.requiresSaveToFile() || (odl.isUploadAllowed() && odl.requiresUploadToServer())) && odl.data.isModified()) {
    380380                    layersWithUnmodifiedChanges.add(odl);
    381381                }
    382382            }
  • core/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

     
    427427                }
    428428                dialog.rememberUserInput();
    429429
    430                 currentTask = new UploadLayerTask(
    431                         UploadDialog.getUploadDialog().getUploadStrategySpecification(),
    432                         layerInfo.getLayer(),
    433                         monitor,
    434                         UploadDialog.getUploadDialog().getChangeset()
    435                 );
    436                 currentFuture = worker.submit(currentTask);
    437430                try {
    438                     // wait for the asynchronous task to complete
    439                     //
    440                     currentFuture.get();
    441                 } catch(CancellationException e) {
    442                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
    443                 } catch(Exception e) {
    444                     e.printStackTrace();
    445                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
    446                     ExceptionDialogUtil.explainException(e);
    447                 }
    448                 if (currentTask.isCanceled()) {
    449                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
    450                 } else if (currentTask.isFailed()) {
    451                     currentTask.getLastException().printStackTrace();
    452                     ExceptionDialogUtil.explainException(currentTask.getLastException());
    453                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
    454                 } else {
    455                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.OK);
     431                    currentTask = new UploadLayerTask(
     432                            UploadDialog.getUploadDialog().getUploadStrategySpecification(),
     433                            layerInfo.getLayer(),
     434                            monitor,
     435                            UploadDialog.getUploadDialog().getChangeset()
     436                    );
     437                    currentFuture = worker.submit(currentTask);
     438                    try {
     439                        // wait for the asynchronous task to complete
     440                        //
     441                        currentFuture.get();
     442                    } catch(CancellationException e) {
     443                        model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
     444                    } catch(Exception e) {
     445                        e.printStackTrace();
     446                        model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
     447                        ExceptionDialogUtil.explainException(e);
     448                    }
     449                    if (currentTask.isCanceled()) {
     450                        model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
     451                    } else if (currentTask.isFailed()) {
     452                        currentTask.getLastException().printStackTrace();
     453                        ExceptionDialogUtil.explainException(currentTask.getLastException());
     454                        model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
     455                    } else {
     456                        model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.OK);
     457                    }
     458                } catch (UploadForbiddenException ex) {
     459                    // This should not happen has it has been previously checked in checkPreUploadConditions()
     460                    System.err.println(ex.getMessage());
    456461                }
    457462                currentTask = null;
    458463                currentFuture = null;
  • core/src/org/openstreetmap/josm/gui/io/UploadForbiddenException.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui.io;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     7
     8public class UploadForbiddenException extends Exception {
     9
     10    public UploadForbiddenException(OsmDataLayer layer) {
     11        super(tr("Upload is forbidden for layer ''{0}''", layer.getName()));
     12    }
     13}
  • core/src/org/openstreetmap/josm/gui/io/UploadLayerTask.java

     
    5353     * @param layer the layer. Must not be null.
    5454     * @param monitor  a progress monitor. If monitor is null, uses {@see NullProgressMonitor#INSTANCE}
    5555     * @param changeset the changeset to be used
    56      * @throws IllegalArgumentException thrown, if layer is null
     56     * @throws UploadForbiddenException thrown if upload is not allowed for layer
     57     * @throws IllegalArgumentException thrown if layer is null
    5758     * @throws IllegalArgumentException thrown if strategy is null
    5859     */
    59     public UploadLayerTask(UploadStrategySpecification strategy, OsmDataLayer layer, ProgressMonitor monitor, Changeset changeset) {
     60    public UploadLayerTask(UploadStrategySpecification strategy, OsmDataLayer layer, ProgressMonitor monitor, Changeset changeset) throws UploadForbiddenException {
    6061        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    6162        CheckParameterUtil.ensureParameterNotNull(strategy, "strategy");
     63        if (!layer.isUploadAllowed()) {
     64            throw new UploadForbiddenException(layer);
     65        }
    6266        if (monitor == null) {
    6367            monitor = NullProgressMonitor.INSTANCE;
    6468        }
  • core/src/org/openstreetmap/josm/gui/io/UploadPrimitivesTask.java

     
    5555     * @param toUpload the collection of primitives to upload. Set to the empty collection if null.
    5656     * @param changeset the changeset to use for uploading. Must not be null. changeset.getId()
    5757     * can be 0 in which case the upload task creates a new changeset
     58     * @throws UploadForbiddenException thrown if upload is not allowed for layer
    5859     * @throws IllegalArgumentException thrown if layer is null
    5960     * @throws IllegalArgumentException thrown if toUpload is null
    6061     * @throws IllegalArgumentException thrown if strategy is null
    6162     * @throws IllegalArgumentException thrown if changeset is null
    6263     */
    63     public UploadPrimitivesTask(UploadStrategySpecification strategy, OsmDataLayer layer, APIDataSet toUpload, Changeset changeset) {
     64    public UploadPrimitivesTask(UploadStrategySpecification strategy, OsmDataLayer layer, APIDataSet toUpload, Changeset changeset) throws UploadForbiddenException {
    6465        super(tr("Uploading data for layer ''{0}''", layer.getName()),false /* don't ignore exceptions */);
    6566        ensureParameterNotNull(layer,"layer");
    6667        ensureParameterNotNull(strategy, "strategy");
    6768        ensureParameterNotNull(changeset, "changeset");
     69        if (!layer.isUploadAllowed()) {
     70            throw new UploadForbiddenException(layer);
     71        }
    6872        this.toUpload = toUpload;
    6973        this.layer = layer;
    7074        this.changeset = changeset;
  • core/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

     
    394394
    395395
    396396    @Override public boolean isMergable(final Layer other) {
    397         return other instanceof OsmDataLayer;
     397        return other instanceof OsmDataLayer && (isUploadAllowed() == ((OsmDataLayer)other).isUploadAllowed());
    398398    }
    399399
    400400    @Override public void visitBoundingBox(final BoundingXYVisitor v) {
     
    456456        p.add(new JLabel(nodeText, ImageProvider.get("data", "node"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
    457457        p.add(new JLabel(wayText, ImageProvider.get("data", "way"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
    458458        p.add(new JLabel(relationText, ImageProvider.get("data", "relation"), JLabel.HORIZONTAL), GBC.eop().insets(15,0,0,0));
    459         p.add(new JLabel(tr("API version: {0}", (data.getVersion() != null) ? data.getVersion() : tr("unset"))));
     459        p.add(new JLabel(tr("API version: {0}", (data.getVersion() != null) ? data.getVersion() : tr("unset"))), GBC.eop().insets(15,0,0,0));
     460        p.add(new JLabel(tr("Upload allowed: {0}", isUploadAllowed() ? tr("yes") : tr("no"))), GBC.eop().insets(15,0,0,0));
    460461
    461462        return p;
    462463    }
     
    701702         * change listener and already got notified.
    702703         */
    703704    }
     705
     706    public final boolean isUploadAllowed() {
     707        return data.isUploadAllowed();
     708    }
     709
     710    public final void setUploadAllowed(boolean uploadAllowed) {
     711        data.setUploadAllowed(uploadAllowed);
     712    }
    704713}
  • core/src/org/openstreetmap/josm/io/OsmApi.java

     
    260260    }
    261261
    262262    /**
    263      * Makes an XML string from an OSM primitive. Uses the OsmWriter class.
    264      * @param o the OSM primitive
    265      * @param addBody true to generate the full XML, false to only generate the encapsulating tag
     263     * Makes an XML string from an OSM changeset. Uses the OsmWriter class.
     264     * @param s the OSM changeset
    266265     * @return XML string
    267266     */
    268267    private String toXml(Changeset s) {
  • core/src/org/openstreetmap/josm/io/OsmExporter.java

     
    7373            OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
    7474            layer.data.getReadLock().lock();
    7575            try {
    76                 w.header();
    77                 w.writeDataSources(layer.data);
    78                 w.writeContent(layer.data);
    79                 w.footer();
     76                w.writeLayer(layer);
    8077                w.close();
    8178            } finally {
    8279                layer.data.getReadLock().unlock();
  • core/src/org/openstreetmap/josm/io/OsmReader.java

     
    116116            throwException(tr("Unsupported version: {0}", v));
    117117        }
    118118        ds.setVersion(v);
     119        String upload = parser.getAttributeValue(null, "upload");
     120        if (upload != null) {
     121            ds.setUploadAllowed(Boolean.parseBoolean(upload));
     122        }
    119123        String generator = parser.getAttributeValue(null, "generator");
    120124        Long uploadChangesetId = null;
    121125        if (parser.getAttributeValue(null, "upload-changeset") != null) {
  • core/src/org/openstreetmap/josm/io/OsmWriter.java

     
    2525import org.openstreetmap.josm.data.osm.Tagged;
    2626import org.openstreetmap.josm.data.osm.Way;
    2727import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
     28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2829import org.openstreetmap.josm.tools.DateUtils;
    2930
    3031/**
     
    5354    public void setWithBody(boolean wb) {
    5455        this.withBody = wb;
    5556    }
     57   
    5658    public void setChangeset(Changeset cs) {
    5759        this.changeset = cs;
    5860    }
     61   
    5962    public void setVersion(String v) {
    6063        this.version = v;
    6164    }
    6265
    6366    public void header() {
     67        header(null);
     68    }
     69   
     70    public void header(Boolean upload) {
    6471        out.println("<?xml version='1.0' encoding='UTF-8'?>");
    6572        out.print("<osm version='");
    6673        out.print(version);
     74        if (upload != null) {
     75            out.print("' upload='");
     76            out.print(upload);
     77        }
    6778        out.println("' generator='JOSM'>");
    6879    }
     80   
    6981    public void footer() {
    7082        out.println("</osm>");
    7183    }
     
    8294        Collections.sort(result, byIdComparator);
    8395        return result;
    8496    }
     97   
     98    public void writeLayer(OsmDataLayer layer) {
     99        header(layer.isUploadAllowed());
     100        writeDataSources(layer.data);
     101        writeContent(layer.data);
     102        footer();
     103    }
    85104
    86105    public void writeContent(DataSet ds) {
    87106        for (OsmPrimitive n : sortById(ds.getNodes())) {
  • core/src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java

     
    221221        OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
    222222        layer.data.getReadLock().lock();
    223223        try {
    224             w.header();
    225             w.writeDataSources(layer.data);
    226             w.writeContent(layer.data);
    227             w.footer();
     224            w.writeLayer(layer);
    228225            w.flush();
    229226        } finally {
    230227            layer.data.getReadLock().unlock();