[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[2598] | 5 | import static org.openstreetmap.josm.tools.CheckParameterUtil.ensureParameterNotNull;
|
---|
[2512] | 6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 7 |
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
| 10 | import java.util.Collection;
|
---|
| 11 | import java.util.Collections;
|
---|
[11553] | 12 | import java.util.Optional;
|
---|
[2512] | 13 |
|
---|
| 14 | import javax.swing.JOptionPane;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[13486] | 18 | import org.openstreetmap.josm.data.osm.DataSet.DownloadPolicy;
|
---|
[2512] | 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[2598] | 21 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
[2512] | 22 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
[12634] | 23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[2598] | 24 | import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
|
---|
[2512] | 25 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 26 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
|
---|
[7434] | 27 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
[10212] | 28 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
[2512] | 29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * This action synchronizes a set of primitives with their state on the server.
|
---|
[6814] | 33 | * @since 1670
|
---|
[2512] | 34 | */
|
---|
| 35 | public 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
|
---|
[8509] | 41 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE},
|
---|
| 42 | * {@link OsmPrimitiveType#WAY WAY}, {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[2512] | 43 | */
|
---|
[6046] | 44 | public static void handlePrimitiveGoneException(long id, OsmPrimitiveType type) {
|
---|
[9241] | 45 | MultiFetchServerObjectReader reader = MultiFetchServerObjectReader.create();
|
---|
[12636] | 46 | reader.append(MainApplication.getLayerManager().getEditDataSet(), id, type);
|
---|
[2512] | 47 | try {
|
---|
[2626] | 48 | DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
[12636] | 49 | MainApplication.getLayerManager().getEditLayer().mergeFrom(ds);
|
---|
[10212] | 50 | } catch (OsmTransferException e) {
|
---|
[2512] | 51 | ExceptionDialogUtil.explainException(e);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
[5266] | 56 | * Updates the data for for the {@link OsmPrimitive}s in <code>selection</code>
|
---|
[2512] | 57 | * with the data currently kept on the server.
|
---|
| 58 | *
|
---|
[5266] | 59 | * @param selection a collection of {@link OsmPrimitive}s to update
|
---|
[2512] | 60 | *
|
---|
| 61 | */
|
---|
[6046] | 62 | public static void updatePrimitives(final Collection<OsmPrimitive> selection) {
|
---|
[12636] | 63 | MainApplication.worker.submit(new UpdatePrimitivesTask(MainApplication.getLayerManager().getEditLayer(), selection));
|
---|
[2512] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
[5266] | 67 | * Updates the data for the {@link OsmPrimitive}s with id <code>id</code>
|
---|
[2512] | 68 | * with the data currently kept on the server.
|
---|
| 69 | *
|
---|
[5266] | 70 | * @param id the id of a primitive in the {@link DataSet} of the current edit layer. Must not be null.
|
---|
[8291] | 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
|
---|
[2512] | 74 | */
|
---|
[8291] | 75 | public static void updatePrimitive(PrimitiveId id) {
|
---|
[2598] | 76 | ensureParameterNotNull(id, "id");
|
---|
[11643] | 77 | updatePrimitives(Collections.<OsmPrimitive>singleton(Optional.ofNullable(Optional.ofNullable(
|
---|
[12636] | 78 | MainApplication.getLayerManager().getEditLayer()).orElseThrow(
|
---|
[11553] | 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)))));
|
---|
[2512] | 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
[6814] | 85 | * Constructs a new {@code UpdateSelectionAction}.
|
---|
[2512] | 86 | */
|
---|
| 87 | public UpdateSelectionAction() {
|
---|
[6814] | 88 | super(tr("Update selection"), "updatedata",
|
---|
[2512] | 89 | tr("Updates the currently selected objects from the server (re-downloads data)"),
|
---|
| 90 | Shortcut.registerShortcut("file:updateselection",
|
---|
[4927] | 91 | tr("File: {0}", tr("Update selection")), KeyEvent.VK_U,
|
---|
[4982] | 92 | Shortcut.ALT_CTRL),
|
---|
[6814] | 93 | true, "updateselection", true);
|
---|
[2684] | 94 | putValue("help", ht("/Action/UpdateSelection"));
|
---|
[2512] | 95 | }
|
---|
[6814] | 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);
|
---|
[2682] | 113 | }
|
---|
[2512] | 114 |
|
---|
| 115 | @Override
|
---|
| 116 | protected void updateEnabledState() {
|
---|
[10548] | 117 | updateEnabledStateOnCurrentSelection();
|
---|
[2512] | 118 | }
|
---|
| 119 |
|
---|
| 120 | @Override
|
---|
| 121 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
[13486] | 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 | }
|
---|
[2512] | 129 | }
|
---|
| 130 |
|
---|
[6046] | 131 | @Override
|
---|
[2512] | 132 | public void actionPerformed(ActionEvent e) {
|
---|
[8443] | 133 | if (!isEnabled())
|
---|
[2512] | 134 | return;
|
---|
[6814] | 135 | Collection<OsmPrimitive> toUpdate = getData();
|
---|
[6046] | 136 | if (toUpdate.isEmpty()) {
|
---|
[2512] | 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 | }
|
---|
[3093] | 145 | updatePrimitives(toUpdate);
|
---|
[2512] | 146 | }
|
---|
[3093] | 147 |
|
---|
[6814] | 148 | /**
|
---|
| 149 | * Returns the data on which this action operates. Override if needed.
|
---|
| 150 | * @return the data on which this action operates
|
---|
| 151 | */
|
---|
[3093] | 152 | public Collection<OsmPrimitive> getData() {
|
---|
[10382] | 153 | return getLayerManager().getEditDataSet().getAllSelected();
|
---|
[3093] | 154 | }
|
---|
[2512] | 155 | }
|
---|