1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.command.PurgePrimitivesCommand;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This action synchronizes a set of primitives with their state on the server.
|
---|
23 | *
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class UpdateSelectionAction extends JosmAction {
|
---|
27 |
|
---|
28 | static public int DEFAULT_MAX_SIZE_UPDATE_SELECTION = 50;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * handle an exception thrown because a primitive was deleted on the server
|
---|
32 | *
|
---|
33 | * @param id the primitive id
|
---|
34 | */
|
---|
35 | protected void handlePrimitiveGoneException(long id) {
|
---|
36 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
37 | reader.append(Main.main.createOrGetEditLayer().data,id);
|
---|
38 | DataSet ds = null;
|
---|
39 | try {
|
---|
40 | ds = reader.parseOsm();
|
---|
41 | } catch(Exception e) {
|
---|
42 | handleUpdateException(e);
|
---|
43 | return;
|
---|
44 | }
|
---|
45 | Main.main.createOrGetEditLayer().mergeFrom(ds);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * handle an exception thrown during updating a primitive
|
---|
50 | *
|
---|
51 | * @param id the id of the primitive
|
---|
52 | * @param e the exception
|
---|
53 | */
|
---|
54 | protected void handleUpdateException(Exception e) {
|
---|
55 | e.printStackTrace();
|
---|
56 | JOptionPane.showMessageDialog(
|
---|
57 | Main.parent,
|
---|
58 | tr("Failed to update the selected primitives."),
|
---|
59 | tr("Update failed"),
|
---|
60 | JOptionPane.ERROR_MESSAGE
|
---|
61 | );
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | *
|
---|
66 | * @param id
|
---|
67 | */
|
---|
68 | protected void handleMissingPrimitive(long id) {
|
---|
69 | JOptionPane.showMessageDialog(
|
---|
70 | Main.parent,
|
---|
71 | tr("Could not find primitive with id {0} in the current dataset", new Long(id).toString()),
|
---|
72 | tr("Missing primitive"),
|
---|
73 | JOptionPane.ERROR_MESSAGE
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | *
|
---|
79 | *
|
---|
80 | *
|
---|
81 | */
|
---|
82 | public void updatePrimitives(Collection<OsmPrimitive> selection) {
|
---|
83 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
84 | reader.append(selection);
|
---|
85 | DataSet ds = null;
|
---|
86 | try {
|
---|
87 | ds = reader.parseOsm();
|
---|
88 | } catch(Exception e) {
|
---|
89 | handleUpdateException(e);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | Main.main.createOrGetEditLayer().mergeFrom(ds);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void updatePrimitive(long id) {
|
---|
96 | OsmPrimitive primitive = Main.main.createOrGetEditLayer().data.getPrimitiveById(id);
|
---|
97 | Set<OsmPrimitive> s = new HashSet<OsmPrimitive>();
|
---|
98 | s.add(primitive);
|
---|
99 | updatePrimitives(s);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public UpdateSelectionAction() {
|
---|
103 | super(tr("Update Selection"),
|
---|
104 | "updateselection",
|
---|
105 | tr("Updates the currently selected primitives from the server"),
|
---|
106 | Shortcut.registerShortcut("file:updateselection",
|
---|
107 | tr("Update Selection"),
|
---|
108 | KeyEvent.VK_U,
|
---|
109 | Shortcut.GROUP_HOTKEY + Shortcut.GROUPS_ALT2),
|
---|
110 | true);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | public void actionPerformed(ActionEvent e) {
|
---|
115 | Collection<OsmPrimitive> selection = Main.ds.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 | }
|
---|