source: josm/trunk/src/org/openstreetmap/josm/actions/UpdateSelectionAction.java @ 13486

Last change on this file since 13486 was 13486, checked in by Don-vip, 7 months ago

fix #8039, see #10456 - fix bugs with non-downloadable layers

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.CheckParameterUtil.ensureParameterNotNull;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.Optional;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.DataSet.DownloadPolicy;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
21import org.openstreetmap.josm.data.osm.PrimitiveId;
22import org.openstreetmap.josm.gui.ExceptionDialogUtil;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
25import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
26import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
27import org.openstreetmap.josm.io.OnlineResource;
28import org.openstreetmap.josm.io.OsmTransferException;
29import org.openstreetmap.josm.tools.Shortcut;
30
31/**
32 * This action synchronizes a set of primitives with their state on the server.
33 * @since 1670
34 */
35public class UpdateSelectionAction extends JosmAction {
36
37    /**
38     * handle an exception thrown because a primitive was deleted on the server
39     *
40     * @param id the primitive id
41     * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE},
42     * {@link OsmPrimitiveType#WAY WAY}, {@link OsmPrimitiveType#RELATION RELATION}
43     */
44    public static void handlePrimitiveGoneException(long id, OsmPrimitiveType type) {
45        MultiFetchServerObjectReader reader = MultiFetchServerObjectReader.create();
46        reader.append(MainApplication.getLayerManager().getEditDataSet(), id, type);
47        try {
48            DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
49            MainApplication.getLayerManager().getEditLayer().mergeFrom(ds);
50        } catch (OsmTransferException e) {
51            ExceptionDialogUtil.explainException(e);
52        }
53    }
54
55    /**
56     * Updates the data for for the {@link OsmPrimitive}s in <code>selection</code>
57     * with the data currently kept on the server.
58     *
59     * @param selection a collection of {@link OsmPrimitive}s to update
60     *
61     */
62    public static void updatePrimitives(final Collection<OsmPrimitive> selection) {
63        MainApplication.worker.submit(new UpdatePrimitivesTask(MainApplication.getLayerManager().getEditLayer(), selection));
64    }
65
66    /**
67     * Updates the data for  the {@link OsmPrimitive}s with id <code>id</code>
68     * with the data currently kept on the server.
69     *
70     * @param id  the id of a primitive in the {@link DataSet} of the current edit layer. Must not be null.
71     * @throws IllegalArgumentException if id is null
72     * @throws IllegalStateException if there is no primitive with <code>id</code> in the current dataset
73     * @throws IllegalStateException if there is no current dataset
74     */
75    public static void updatePrimitive(PrimitiveId id) {
76        ensureParameterNotNull(id, "id");
77        updatePrimitives(Collections.<OsmPrimitive>singleton(Optional.ofNullable(Optional.ofNullable(
78                MainApplication.getLayerManager().getEditLayer()).orElseThrow(
79                        () -> new IllegalStateException(tr("No current dataset found")))
80                .data.getPrimitiveById(id)).orElseThrow(
81                        () -> new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id)))));
82    }
83
84    /**
85     * Constructs a new {@code UpdateSelectionAction}.
86     */
87    public UpdateSelectionAction() {
88        super(tr("Update selection"), "updatedata",
89                tr("Updates the currently selected objects from the server (re-downloads data)"),
90                Shortcut.registerShortcut("file:updateselection",
91                        tr("File: {0}", tr("Update selection")), KeyEvent.VK_U,
92                        Shortcut.ALT_CTRL),
93                true, "updateselection", true);
94        putValue("help", ht("/Action/UpdateSelection"));
95    }
96
97    /**
98     * Constructs a new {@code UpdateSelectionAction}.
99     *
100     * @param name the action's text as displayed on the menu (if it is added to a menu)
101     * @param iconName the filename of the icon to use
102     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
103     *           that html is not supported for menu actions on some platforms.
104     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
105     *            do want a shortcut, remember you can always register it with group=none, so you
106     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
107     *            the user CANNOT configure a shortcut for your action.
108     * @param register register this action for the toolbar preferences?
109     * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
110     */
111    public UpdateSelectionAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId) {
112        super(name, iconName, tooltip, shortcut, register, toolbarId, true);
113    }
114
115    @Override
116    protected void updateEnabledState() {
117        updateEnabledStateOnCurrentSelection();
118    }
119
120    @Override
121    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
122        if (selection == null || selection.isEmpty()) {
123            setEnabled(false);
124        } else {
125            DataSet ds = selection.iterator().next().getDataSet();
126            setEnabled(!ds.isLocked() && !DownloadPolicy.BLOCKED.equals(ds.getDownloadPolicy())
127                    && !Main.isOffline(OnlineResource.OSM_API));
128        }
129    }
130
131    @Override
132    public void actionPerformed(ActionEvent e) {
133        if (!isEnabled())
134            return;
135        Collection<OsmPrimitive> toUpdate = getData();
136        if (toUpdate.isEmpty()) {
137            JOptionPane.showMessageDialog(
138                    Main.parent,
139                    tr("There are no selected objects to update."),
140                    tr("Selection empty"),
141                    JOptionPane.INFORMATION_MESSAGE
142            );
143            return;
144        }
145        updatePrimitives(toUpdate);
146    }
147
148    /**
149     * Returns the data on which this action operates. Override if needed.
150     * @return the data on which this action operates
151     */
152    public Collection<OsmPrimitive> getData() {
153        return getLayerManager().getEditDataSet().getAllSelected();
154    }
155}
Note: See TracBrowser for help on using the repository browser.