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;
|
---|
5 | import static org.openstreetmap.josm.tools.CheckParameterUtil.ensureParameterNotNull;
|
---|
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;
|
---|
12 | import java.util.Optional;
|
---|
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;
|
---|
20 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
21 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
|
---|
24 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
|
---|
26 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
27 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
28 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This action synchronizes a set of primitives with their state on the server.
|
---|
32 | * @since 1670
|
---|
33 | */
|
---|
34 | public class UpdateSelectionAction extends JosmAction {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * handle an exception thrown because a primitive was deleted on the server
|
---|
38 | *
|
---|
39 | * @param id the primitive id
|
---|
40 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE},
|
---|
41 | * {@link OsmPrimitiveType#WAY WAY}, {@link OsmPrimitiveType#RELATION RELATION}
|
---|
42 | */
|
---|
43 | public static void handlePrimitiveGoneException(long id, OsmPrimitiveType type) {
|
---|
44 | MultiFetchServerObjectReader reader = MultiFetchServerObjectReader.create();
|
---|
45 | reader.append(MainApplication.getLayerManager().getEditDataSet(), id, type);
|
---|
46 | try {
|
---|
47 | DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
48 | MainApplication.getLayerManager().getEditLayer().mergeFrom(ds);
|
---|
49 | } catch (OsmTransferException e) {
|
---|
50 | ExceptionDialogUtil.explainException(e);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Updates the data for for the {@link OsmPrimitive}s in <code>selection</code>
|
---|
56 | * with the data currently kept on the server.
|
---|
57 | *
|
---|
58 | * @param selection a collection of {@link OsmPrimitive}s to update
|
---|
59 | *
|
---|
60 | */
|
---|
61 | public static void updatePrimitives(final Collection<OsmPrimitive> selection) {
|
---|
62 | MainApplication.worker.submit(new UpdatePrimitivesTask(MainApplication.getLayerManager().getEditLayer(), selection));
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Updates the data for the {@link OsmPrimitive}s with id <code>id</code>
|
---|
67 | * with the data currently kept on the server.
|
---|
68 | *
|
---|
69 | * @param id the id of a primitive in the {@link DataSet} of the current edit layer. Must not be null.
|
---|
70 | * @throws IllegalArgumentException if id is null
|
---|
71 | * @throws IllegalStateException if there is no primitive with <code>id</code> in the current dataset
|
---|
72 | * @throws IllegalStateException if there is no current dataset
|
---|
73 | */
|
---|
74 | public static void updatePrimitive(PrimitiveId id) {
|
---|
75 | ensureParameterNotNull(id, "id");
|
---|
76 | updatePrimitives(Collections.<OsmPrimitive>singleton(Optional.ofNullable(Optional.ofNullable(
|
---|
77 | MainApplication.getLayerManager().getEditLayer()).orElseThrow(
|
---|
78 | () -> new IllegalStateException(tr("No current dataset found")))
|
---|
79 | .data.getPrimitiveById(id)).orElseThrow(
|
---|
80 | () -> new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id)))));
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Constructs a new {@code UpdateSelectionAction}.
|
---|
85 | */
|
---|
86 | public UpdateSelectionAction() {
|
---|
87 | super(tr("Update selection"), "updatedata",
|
---|
88 | tr("Updates the currently selected objects from the server (re-downloads data)"),
|
---|
89 | Shortcut.registerShortcut("file:updateselection",
|
---|
90 | tr("File: {0}", tr("Update selection")), KeyEvent.VK_U,
|
---|
91 | Shortcut.ALT_CTRL),
|
---|
92 | true, "updateselection", true);
|
---|
93 | putValue("help", ht("/Action/UpdateSelection"));
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Constructs a new {@code UpdateSelectionAction}.
|
---|
98 | *
|
---|
99 | * @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 | updateEnabledStateOnCurrentSelection();
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
121 | setEnabled(selection != null && !selection.isEmpty() && !Main.isOffline(OnlineResource.OSM_API));
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public void actionPerformed(ActionEvent e) {
|
---|
126 | if (!isEnabled())
|
---|
127 | return;
|
---|
128 | Collection<OsmPrimitive> toUpdate = getData();
|
---|
129 | if (toUpdate.isEmpty()) {
|
---|
130 | JOptionPane.showMessageDialog(
|
---|
131 | Main.parent,
|
---|
132 | tr("There are no selected objects to update."),
|
---|
133 | tr("Selection empty"),
|
---|
134 | JOptionPane.INFORMATION_MESSAGE
|
---|
135 | );
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | updatePrimitives(toUpdate);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Returns the data on which this action operates. Override if needed.
|
---|
143 | * @return the data on which this action operates
|
---|
144 | */
|
---|
145 | public Collection<OsmPrimitive> getData() {
|
---|
146 | return getLayerManager().getEditDataSet().getAllSelected();
|
---|
147 | }
|
---|
148 | }
|
---|