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

Last change on this file since 11534 was 10548, checked in by simon04, 8 years ago

Remove duplicated code

Use updateEnabledStateOnCurrentSelection introduced r10409

  • Property svn:eol-style set to native
File size: 6.3 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;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
19import org.openstreetmap.josm.data.osm.PrimitiveId;
20import org.openstreetmap.josm.gui.ExceptionDialogUtil;
21import org.openstreetmap.josm.gui.io.UpdatePrimitivesTask;
22import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
23import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
24import org.openstreetmap.josm.io.OnlineResource;
25import org.openstreetmap.josm.io.OsmTransferException;
26import org.openstreetmap.josm.tools.Shortcut;
27
28/**
29 * This action synchronizes a set of primitives with their state on the server.
30 * @since 1670
31 */
32public class UpdateSelectionAction extends JosmAction {
33
34 /**
35 * handle an exception thrown because a primitive was deleted on the server
36 *
37 * @param id the primitive id
38 * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE},
39 * {@link OsmPrimitiveType#WAY WAY}, {@link OsmPrimitiveType#RELATION RELATION}
40 */
41 public static void handlePrimitiveGoneException(long id, OsmPrimitiveType type) {
42 MultiFetchServerObjectReader reader = MultiFetchServerObjectReader.create();
43 reader.append(Main.getLayerManager().getEditDataSet(), id, type);
44 try {
45 DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
46 Main.getLayerManager().getEditLayer().mergeFrom(ds);
47 } catch (OsmTransferException e) {
48 ExceptionDialogUtil.explainException(e);
49 }
50 }
51
52 /**
53 * Updates the data for for the {@link OsmPrimitive}s in <code>selection</code>
54 * with the data currently kept on the server.
55 *
56 * @param selection a collection of {@link OsmPrimitive}s to update
57 *
58 */
59 public static void updatePrimitives(final Collection<OsmPrimitive> selection) {
60 Main.worker.submit(new UpdatePrimitivesTask(Main.getLayerManager().getEditLayer(), selection));
61 }
62
63 /**
64 * Updates the data for the {@link OsmPrimitive}s with id <code>id</code>
65 * with the data currently kept on the server.
66 *
67 * @param id the id of a primitive in the {@link DataSet} of the current edit layer. Must not be null.
68 * @throws IllegalArgumentException if id is null
69 * @throws IllegalStateException if there is no primitive with <code>id</code> in the current dataset
70 * @throws IllegalStateException if there is no current dataset
71 */
72 public static void updatePrimitive(PrimitiveId id) {
73 ensureParameterNotNull(id, "id");
74 if (Main.getLayerManager().getEditLayer() == null)
75 throw new IllegalStateException(tr("No current dataset found"));
76 OsmPrimitive primitive = Main.getLayerManager().getEditLayer().data.getPrimitiveById(id);
77 if (primitive == null)
78 throw new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id));
79 updatePrimitives(Collections.singleton(primitive));
80 }
81
82 /**
83 * Constructs a new {@code UpdateSelectionAction}.
84 */
85 public UpdateSelectionAction() {
86 super(tr("Update selection"), "updatedata",
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, "updateselection", true);
92 putValue("help", ht("/Action/UpdateSelection"));
93 }
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);
111 }
112
113 @Override
114 protected void updateEnabledState() {
115 updateEnabledStateOnCurrentSelection();
116 }
117
118 @Override
119 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
120 setEnabled(selection != null && !selection.isEmpty() && !Main.isOffline(OnlineResource.OSM_API));
121 }
122
123 @Override
124 public void actionPerformed(ActionEvent e) {
125 if (!isEnabled())
126 return;
127 Collection<OsmPrimitive> toUpdate = getData();
128 if (toUpdate.isEmpty()) {
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 }
137 updatePrimitives(toUpdate);
138 }
139
140 /**
141 * Returns the data on which this action operates. Override if needed.
142 * @return the data on which this action operates
143 */
144 public Collection<OsmPrimitive> getData() {
145 return getLayerManager().getEditDataSet().getAllSelected();
146 }
147}
Note: See TracBrowser for help on using the repository browser.