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

Last change on this file was 18208, checked in by Don-vip, 3 years ago

global use of Utils.isEmpty/isBlank

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