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