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.CheckParameterUtil.ensureParameterNotNull;
006import static org.openstreetmap.josm.tools.I18n.tr;
007
008import java.awt.event.ActionEvent;
009import java.awt.event.KeyEvent;
010import java.util.Collection;
011import java.util.Collections;
012
013import javax.swing.JOptionPane;
014
015import org.openstreetmap.josm.Main;
016import org.openstreetmap.josm.data.osm.DataSet;
017import org.openstreetmap.josm.data.osm.OsmPrimitive;
018import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
019import org.openstreetmap.josm.data.osm.PrimitiveId;
020import org.openstreetmap.josm.gui.ExceptionDialogUtil;
021import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
022import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
023import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
024import org.openstreetmap.josm.io.OnlineResource;
025import org.openstreetmap.josm.tools.Shortcut;
026
027/**
028 * This action synchronizes a set of primitives with their state on the server.
029 * @since 1670
030 */
031public class UpdateSelectionAction extends JosmAction {
032
033    /**
034     * handle an exception thrown because a primitive was deleted on the server
035     *
036     * @param id the primitive id
037     * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY}, {@link OsmPrimitiveType#RELATION RELATION}
038     */
039    public static void handlePrimitiveGoneException(long id, OsmPrimitiveType type) {
040        MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
041        reader.append(getCurrentDataSet(), id, type);
042        try {
043            DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
044            Main.main.getEditLayer().mergeFrom(ds);
045        } catch(Exception e) {
046            ExceptionDialogUtil.explainException(e);
047        }
048    }
049
050    /**
051     * Updates the data for for the {@link OsmPrimitive}s in <code>selection</code>
052     * with the data currently kept on the server.
053     *
054     * @param selection a collection of {@link OsmPrimitive}s to update
055     *
056     */
057    public static void updatePrimitives(final Collection<OsmPrimitive> selection) {
058        UpdatePrimitivesTask task = new UpdatePrimitivesTask(Main.main.getEditLayer(),selection);
059        Main.worker.submit(task);
060    }
061
062    /**
063     * Updates the data for  the {@link OsmPrimitive}s with id <code>id</code>
064     * with the data currently kept on the server.
065     *
066     * @param id  the id of a primitive in the {@link DataSet} of the current edit layer. Must not be null.
067     * @throws IllegalArgumentException thrown if id is null
068     * @exception IllegalStateException thrown if there is no primitive with <code>id</code> in
069     *   the current dataset
070     * @exception IllegalStateException thrown if there is no current dataset
071     *
072     */
073    public static void updatePrimitive(PrimitiveId id) throws IllegalStateException, IllegalArgumentException{
074        ensureParameterNotNull(id, "id");
075        if (getEditLayer() == null)
076            throw new IllegalStateException(tr("No current dataset found"));
077        OsmPrimitive primitive = getEditLayer().data.getPrimitiveById(id);
078        if (primitive == null)
079            throw new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id));
080        updatePrimitives(Collections.singleton(primitive));
081    }
082
083    /**
084     * Constructs a new {@code UpdateSelectionAction}.
085     */
086    public UpdateSelectionAction() {
087        super(tr("Update selection"), "updatedata",
088                tr("Updates the currently selected objects from the server (re-downloads data)"),
089                Shortcut.registerShortcut("file:updateselection",
090                        tr("File: {0}", tr("Update selection")), KeyEvent.VK_U,
091                        Shortcut.ALT_CTRL),
092                true, "updateselection", true);
093        putValue("help", ht("/Action/UpdateSelection"));
094    }
095
096    /**
097     * Constructs a new {@code UpdateSelectionAction}.
098     *
099     * @param name the action's text as displayed on the menu (if it is added to a menu)
100     * @param iconName the filename of the icon to use
101     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
102     *           that html is not supported for menu actions on some platforms.
103     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
104     *            do want a shortcut, remember you can always register it with group=none, so you
105     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
106     *            the user CANNOT configure a shortcut for your action.
107     * @param register register this action for the toolbar preferences?
108     * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
109     */
110    public UpdateSelectionAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId) {
111        super(name, iconName, tooltip, shortcut, register, toolbarId, true);
112    }
113
114    @Override
115    protected void updateEnabledState() {
116        if (getCurrentDataSet() == null) {
117            setEnabled(false);
118        } else {
119            updateEnabledState(getCurrentDataSet().getAllSelected());
120        }
121    }
122
123    @Override
124    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
125        setEnabled(selection != null && !selection.isEmpty() && !Main.isOffline(OnlineResource.OSM_API));
126    }
127
128    @Override
129    public void actionPerformed(ActionEvent e) {
130        if (! isEnabled())
131            return;
132        Collection<OsmPrimitive> toUpdate = getData();
133        if (toUpdate.isEmpty()) {
134            JOptionPane.showMessageDialog(
135                    Main.parent,
136                    tr("There are no selected objects to update."),
137                    tr("Selection empty"),
138                    JOptionPane.INFORMATION_MESSAGE
139            );
140            return;
141        }
142        updatePrimitives(toUpdate);
143    }
144
145    /**
146     * Returns the data on which this action operates. Override if needed.
147     * @return the data on which this action operates
148     */
149    public Collection<OsmPrimitive> getData() {
150        return getCurrentDataSet().getAllSelected();
151    }
152}