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