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

Last change on this file since 2025 was 2025, checked in by Gubaer, 15 years ago

new: improved dialog for uploading/saving modified layers on exit
new: improved dialog for uploading/saving modified layers if layers are deleted
new: new progress monitor which can delegate rendering to any Swing component
more setters/getters for properties in OSM data classes (fields are @deprecated); started to update references in the code base

File size: 7.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.Collections;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
21import org.openstreetmap.josm.gui.ExceptionDialogUtil;
22import org.openstreetmap.josm.gui.PleaseWaitRunnable;
23import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.openstreetmap.josm.tools.Shortcut;
28import org.xml.sax.SAXException;
29
30/**
31 * This action synchronizes a set of primitives with their state on the server.
32 *
33 */
34public 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 */
41 protected void handlePrimitiveGoneException(long id) {
42 MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
43 reader.append(getCurrentDataSet(),id);
44 DataSet ds = null;
45 try {
46 ds = reader.parseOsm(NullProgressMonitor.INSTANCE);
47 } catch(Exception e) {
48 ExceptionDialogUtil.explainException(e);
49 }
50 Main.map.mapView.getEditLayer().mergeFrom(ds);
51 }
52
53 /**
54 * Updates the data for for the {@see OsmPrimitive}s in <code>selection</code>
55 * with the data currently kept on the server.
56 *
57 * @param selection a collection of {@see OsmPrimitive}s to update
58 *
59 */
60 public void updatePrimitives(final Collection<OsmPrimitive> selection) {
61 UpdatePrimitivesTask task = new UpdatePrimitivesTask(selection);
62 Main.worker.submit(task);
63 }
64
65 /**
66 * Updates the data for for the {@see 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 {@see DataSet} of the current edit layser
70 * @exception IllegalStateException thrown if there is no primitive with <code>id</code> in
71 * the current dataset
72 * @exception IllegalStateException thrown if there is no current dataset
73 *
74 */
75 public void updatePrimitive(long id) throws IllegalStateException{
76 if (getEditLayer() == null)
77 throw new IllegalStateException(tr("No current dataset found"));
78 OsmPrimitive primitive = getEditLayer().data.getPrimitiveById(id);
79 if (primitive == null)
80 throw new IllegalStateException(tr("Didn't find a primitive with id {0} in the current dataset", id));
81 updatePrimitives(Collections.singleton(primitive));
82 }
83
84 /**
85 * constructor
86 */
87 public UpdateSelectionAction() {
88 super(tr("Update Selection"),
89 "updateselection",
90 tr("Updates the currently selected primitives from the server"),
91 Shortcut.registerShortcut("file:updateselection",
92 tr("Update Selection"),
93 KeyEvent.VK_U,
94 Shortcut.GROUP_HOTKEY + Shortcut.GROUPS_ALT2),
95 true);
96 }
97
98 /**
99 * Refreshes the enabled state
100 *
101 */
102 @Override
103 protected void updateEnabledState() {
104 setEnabled(getCurrentDataSet() != null
105 && ! getCurrentDataSet().getSelected().isEmpty()
106 );
107 }
108
109 /**
110 * action handler
111 */
112 public void actionPerformed(ActionEvent e) {
113 if (! isEnabled())
114 return;
115 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
116 if (selection.size() == 0) {
117 JOptionPane.showMessageDialog(
118 Main.parent,
119 tr("There are no selected primitives to update."),
120 tr("Selection empty"),
121 JOptionPane.INFORMATION_MESSAGE
122 );
123 return;
124 }
125 updatePrimitives(selection);
126 }
127
128 /**
129 * The asynchronous task for updating the data using multi fetch.
130 *
131 */
132 class UpdatePrimitivesTask extends PleaseWaitRunnable {
133 private DataSet ds;
134 private boolean canceled;
135 private Exception lastException;
136 private Collection<? extends OsmPrimitive> toUpdate;
137 private MultiFetchServerObjectReader reader;
138
139 public UpdatePrimitivesTask(Collection<? extends OsmPrimitive> toUpdate) {
140 super("Update primitives", false /* don't ignore exception*/);
141 canceled = false;
142 this.toUpdate = toUpdate;
143 }
144
145 @Override
146 protected void cancel() {
147 canceled = true;
148 if (reader != null) {
149 reader.cancel();
150 }
151 }
152
153 @Override
154 protected void finish() {
155 if (canceled)
156 return;
157 if (lastException != null) {
158 ExceptionDialogUtil.explainException(lastException);
159 return;
160 }
161 if (ds != null) {
162 Main.map.mapView.getEditLayer().mergeFrom(ds);
163 }
164 }
165
166 protected void initMultiFetchReaderWithNodes(MultiFetchServerObjectReader reader) {
167 for (OsmPrimitive primitive : toUpdate) {
168 if (primitive instanceof Node && primitive.getId() > 0) {
169 reader.append((Node)primitive);
170 } else if (primitive instanceof Way) {
171 Way way = (Way)primitive;
172 for (Node node: way.getNodes()) {
173 if (node.getId() > 0) {
174 reader.append(node);
175 }
176 }
177 }
178 }
179 }
180
181 protected void initMultiFetchReaderWithWays(MultiFetchServerObjectReader reader) {
182 for (OsmPrimitive primitive : toUpdate) {
183 if (primitive instanceof Way && primitive.getId() > 0) {
184 reader.append((Way)primitive);
185 }
186 }
187 }
188
189 protected void initMultiFetchReaderWithRelations(MultiFetchServerObjectReader reader) {
190 for (OsmPrimitive primitive : toUpdate) {
191 if (primitive instanceof Relation && primitive.getId() > 0) {
192 reader.append((Relation)primitive);
193 }
194 }
195 }
196
197 @Override
198 protected void realRun() throws SAXException, IOException, OsmTransferException {
199 progressMonitor.indeterminateSubTask("");
200 this.ds = new DataSet();
201 DataSet theirDataSet;
202 try {
203 reader = new MultiFetchServerObjectReader();
204 initMultiFetchReaderWithNodes(reader);
205 initMultiFetchReaderWithWays(reader);
206 initMultiFetchReaderWithRelations(reader);
207 theirDataSet = reader.parseOsm(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
208 MergeVisitor merger = new MergeVisitor(ds, theirDataSet);
209 merger.merge();
210 } catch(Exception e) {
211 if (canceled)
212 return;
213 lastException = e;
214 }
215 }
216 }
217}
Note: See TracBrowser for help on using the repository browser.