001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.KeyEvent;
008import java.util.Collection;
009import java.util.Collections;
010
011import org.openstreetmap.josm.Main;
012import org.openstreetmap.josm.data.osm.DataSet;
013import org.openstreetmap.josm.data.osm.OsmPrimitive;
014import org.openstreetmap.josm.io.OnlineResource;
015import org.openstreetmap.josm.tools.Shortcut;
016
017/**
018 * This action synchronizes a set of primitives with their state on the server.
019 * @since 2682
020 */
021public class UpdateModifiedAction extends UpdateSelectionAction {
022
023    /**
024     * Constructs a new {@code UpdateModifiedAction}.
025     */
026    public UpdateModifiedAction() {
027        super(tr("Update modified"), "updatedata",
028                tr("Updates the currently modified objects from the server (re-downloads data)"),
029                Shortcut.registerShortcut("file:updatemodified",
030                        tr("File: {0}", tr("Update modified")), KeyEvent.VK_M,
031                        Shortcut.ALT_CTRL),
032                        true, "updatemodified");
033        putValue("help", ht("/Action/UpdateModified"));
034    }
035
036    // FIXME: overrides the behaviour of UpdateSelectionAction. Doesn't update
037    // the enabled state based on the current selection because it doesn't depend on it.
038    // The action should be enabled/disabled based on whether there is a least
039    // one modified object in the current dataset. Unfortunately, there is no
040    // efficient way to find out here. getDataSet().allModifiedPrimitives() is
041    // too heavy weight because it loops over the whole dataset.
042    // Perhaps this action should  be a DataSetListener? Or it could listen to the
043    // REQUIRES_SAVE_TO_DISK_PROP and REQUIRES_UPLOAD_TO_SERVER_PROP properties
044    // in the OsmLayer?
045    //
046    @Override
047    protected void updateEnabledState() {
048        setEnabled(getLayerManager().getEditDataSet() != null && !Main.isOffline(OnlineResource.OSM_API));
049    }
050
051    @Override
052    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
053        // Do nothing
054    }
055
056    @Override
057    public Collection<OsmPrimitive> getData() {
058        DataSet ds = getLayerManager().getEditDataSet();
059        return ds == null ? Collections.<OsmPrimitive>emptyList() : ds.allModifiedPrimitives();
060    }
061}