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

Last change on this file since 7746 was 7434, checked in by Don-vip, 10 years ago

fix #8885 (see #4614) - add offline mode with new command line argument --offline which can take one of several of these values (comma separated):

  • josm_website: to disable all accesses to JOSM website (when not cached, disables Getting Started page, help, plugin list, styles, imagery, presets, rules)
  • osm_api: to disable all accesses to OSM API (disables download, upload, changeset queries, history, user message notification)
  • all: alias to disable all values. Currently equivalent to "josm_website,osm_api"

Plus improved javadoc, fixed EDT violations, and fixed a bug with HTTP redirection sent without "Location" header

  • 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.tools.Shortcut;
26
27/**
28 * This action synchronizes a set of primitives with their state on the server.
29 * @since 1670
30 */
31public class UpdateSelectionAction extends JosmAction {
32
33 /**
34 * handle an exception thrown because a primitive was deleted on the server
35 *
36 * @param id the primitive id
37 * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY}, {@link OsmPrimitiveType#RELATION RELATION}
38 */
39 public static void handlePrimitiveGoneException(long id, OsmPrimitiveType type) {
40 MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
41 reader.append(getCurrentDataSet(), id, type);
42 try {
43 DataSet ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
44 Main.main.getEditLayer().mergeFrom(ds);
45 } catch(Exception e) {
46 ExceptionDialogUtil.explainException(e);
47 }
48 }
49
50 /**
51 * Updates the data for for the {@link OsmPrimitive}s in <code>selection</code>
52 * with the data currently kept on the server.
53 *
54 * @param selection a collection of {@link OsmPrimitive}s to update
55 *
56 */
57 public static void updatePrimitives(final Collection<OsmPrimitive> selection) {
58 UpdatePrimitivesTask task = new UpdatePrimitivesTask(Main.main.getEditLayer(),selection);
59 Main.worker.submit(task);
60 }
61
62 /**
63 * Updates the data for the {@link OsmPrimitive}s with id <code>id</code>
64 * with the data currently kept on the server.
65 *
66 * @param id the id of a primitive in the {@link DataSet} of the current edit layer. Must not be null.
67 * @throws IllegalArgumentException thrown if id is null
68 * @exception IllegalStateException thrown if there is no primitive with <code>id</code> in
69 * the current dataset
70 * @exception IllegalStateException thrown if there is no current dataset
71 *
72 */
73 public static void updatePrimitive(PrimitiveId id) throws IllegalStateException, IllegalArgumentException{
74 ensureParameterNotNull(id, "id");
75 if (getEditLayer() == null)
76 throw new IllegalStateException(tr("No current dataset found"));
77 OsmPrimitive primitive = getEditLayer().data.getPrimitiveById(id);
78 if (primitive == null)
79 throw new IllegalStateException(tr("Did not find an object with id {0} in the current dataset", id));
80 updatePrimitives(Collections.singleton(primitive));
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 if (getCurrentDataSet() == null) {
117 setEnabled(false);
118 } else {
119 updateEnabledState(getCurrentDataSet().getAllSelected());
120 }
121 }
122
123 @Override
124 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
125 setEnabled(selection != null && !selection.isEmpty() && !Main.isOffline(OnlineResource.OSM_API));
126 }
127
128 @Override
129 public void actionPerformed(ActionEvent e) {
130 if (! isEnabled())
131 return;
132 Collection<OsmPrimitive> toUpdate = getData();
133 if (toUpdate.isEmpty()) {
134 JOptionPane.showMessageDialog(
135 Main.parent,
136 tr("There are no selected objects to update."),
137 tr("Selection empty"),
138 JOptionPane.INFORMATION_MESSAGE
139 );
140 return;
141 }
142 updatePrimitives(toUpdate);
143 }
144
145 /**
146 * Returns the data on which this action operates. Override if needed.
147 * @return the data on which this action operates
148 */
149 public Collection<OsmPrimitive> getData() {
150 return getCurrentDataSet().getAllSelected();
151 }
152}
Note: See TracBrowser for help on using the repository browser.