| 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 | |
|---|
| 13 | import javax.swing.JOptionPane; |
|---|
| 14 | |
|---|
| 15 | import org.openstreetmap.josm.Main; |
|---|
| 16 | import org.openstreetmap.josm.data.osm.DataSet; |
|---|
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 18 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
|---|
| 19 | import org.openstreetmap.josm.data.osm.PrimitiveId; |
|---|
| 20 | import org.openstreetmap.josm.gui.ExceptionDialogUtil; |
|---|
| 21 | import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask; |
|---|
| 22 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor; |
|---|
| 23 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader; |
|---|
| 24 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * This action synchronizes a set of primitives with their state on the server. |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | public class UpdateSelectionAction extends JosmAction { |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * handle an exception thrown because a primitive was deleted on the server |
|---|
| 34 | * |
|---|
| 35 | * @param id the primitive id |
|---|
| 36 | */ |
|---|
| 37 | public void handlePrimitiveGoneException(long id, OsmPrimitiveType type) { |
|---|
| 38 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader(); |
|---|
| 39 | reader.append(getCurrentDataSet(),id, type); |
|---|
| 40 | try { |
|---|
| 41 | DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE); |
|---|
| 42 | Main.map.mapView.getEditLayer().mergeFrom(ds); |
|---|
| 43 | } catch(Exception e) { |
|---|
| 44 | ExceptionDialogUtil.explainException(e); |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Updates the data for for the {@see OsmPrimitive}s in <code>selection</code> |
|---|
| 50 | * with the data currently kept on the server. |
|---|
| 51 | * |
|---|
| 52 | * @param selection a collection of {@see OsmPrimitive}s to update |
|---|
| 53 | * |
|---|
| 54 | */ |
|---|
| 55 | public void updatePrimitives(final Collection<OsmPrimitive> selection) { |
|---|
| 56 | UpdatePrimitivesTask task = new UpdatePrimitivesTask(Main.main.getEditLayer(),selection); |
|---|
| 57 | Main.worker.submit(task); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Updates the data for the {@see OsmPrimitive}s with id <code>id</code> |
|---|
| 62 | * with the data currently kept on the server. |
|---|
| 63 | * |
|---|
| 64 | * @param id the id of a primitive in the {@see DataSet} of the current edit layer. Must not be null. |
|---|
| 65 | * @throws IllegalArgumentException thrown if id is null |
|---|
| 66 | * @exception IllegalStateException thrown if there is no primitive with <code>id</code> in |
|---|
| 67 | * the current dataset |
|---|
| 68 | * @exception IllegalStateException thrown if there is no current dataset |
|---|
| 69 | * |
|---|
| 70 | */ |
|---|
| 71 | public void updatePrimitive(PrimitiveId id) throws IllegalStateException, IllegalArgumentException{ |
|---|
| 72 | ensureParameterNotNull(id, "id"); |
|---|
| 73 | if (getEditLayer() == null) |
|---|
| 74 | throw new IllegalStateException(tr("No current dataset found")); |
|---|
| 75 | OsmPrimitive primitive = getEditLayer().data.getPrimitiveById(id); |
|---|
| 76 | if (primitive == null) |
|---|
| 77 | throw new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id)); |
|---|
| 78 | updatePrimitives(Collections.singleton(primitive)); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * constructor |
|---|
| 83 | */ |
|---|
| 84 | public UpdateSelectionAction() { |
|---|
| 85 | super(tr("Update selection"), |
|---|
| 86 | "updateselection", |
|---|
| 87 | tr("Updates the currently selected objects from the server (re-downloads data)"), |
|---|
| 88 | Shortcut.registerShortcut("file:updateselection", |
|---|
| 89 | tr("File: {0}", tr("Update selection")), KeyEvent.VK_U, |
|---|
| 90 | Shortcut.ALT_CTRL), |
|---|
| 91 | true); |
|---|
| 92 | putValue("help", ht("/Action/UpdateSelection")); |
|---|
| 93 | } |
|---|
| 94 | public UpdateSelectionAction(String name, String iconName, String tooltip, |
|---|
| 95 | Shortcut shortcut, boolean register) { |
|---|
| 96 | super(name, iconName, tooltip, shortcut, register); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | @Override |
|---|
| 100 | protected void updateEnabledState() { |
|---|
| 101 | if (getCurrentDataSet() == null) { |
|---|
| 102 | setEnabled(false); |
|---|
| 103 | } else { |
|---|
| 104 | updateEnabledState(getCurrentDataSet().getSelected()); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | @Override |
|---|
| 109 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
|---|
| 110 | setEnabled(selection != null && !selection.isEmpty()); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * action handler |
|---|
| 115 | */ |
|---|
| 116 | public void actionPerformed(ActionEvent e) { |
|---|
| 117 | if (! isEnabled()) |
|---|
| 118 | return; |
|---|
| 119 | Collection<OsmPrimitive> toUpdate =getData(); |
|---|
| 120 | if (toUpdate.size() == 0) { |
|---|
| 121 | JOptionPane.showMessageDialog( |
|---|
| 122 | Main.parent, |
|---|
| 123 | tr("There are no selected objects to update."), |
|---|
| 124 | tr("Selection empty"), |
|---|
| 125 | JOptionPane.INFORMATION_MESSAGE |
|---|
| 126 | ); |
|---|
| 127 | return; |
|---|
| 128 | } |
|---|
| 129 | updatePrimitives(toUpdate); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public Collection<OsmPrimitive> getData() { |
|---|
| 133 | return getCurrentDataSet().getSelected(); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|