source: josm/trunk/src/org/openstreetmap/josm/actions/UploadAction.java@ 1054

Last change on this file since 1054 was 1054, checked in by stoecker, 16 years ago

fixed a lot of the shortcut related translations

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.LinkedList;
11
12import javax.swing.JLabel;
13import javax.swing.JList;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.io.OsmServerWriter;
23import org.openstreetmap.josm.tools.GBC;
24import org.xml.sax.SAXException;
25import org.openstreetmap.josm.tools.ShortCut;
26
27/**
28 * Action that opens a connection to the osm server and uploads all changes.
29 *
30 * An dialog is displayed asking the user to specify a rectangle to grab.
31 * The url and account settings from the preferences are used.
32 *
33 * @author imi
34 */
35public class UploadAction extends JosmAction {
36
37 /** Upload Hook */
38 public interface UploadHook {
39 /**
40 * Checks the upload.
41 * @param add The added primitives
42 * @param update The updated primitives
43 * @param delete The deleted primitives
44 * @return true, if the upload can continue
45 */
46 public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete);
47 }
48
49 /**
50 * The list of upload hooks. These hooks will be called one after the other
51 * when the user wants to upload data. Plugins can insert their own hooks here
52 * if they want to be able to veto an upload.
53 *
54 * Be default, the standard upload dialog is the only element in the list.
55 * Plugins should normally insert their code before that, so that the upload
56 * dialog is the last thing shown before upload really starts; on occasion
57 * however, a plugin might also want to insert something after that.
58 */
59 public final LinkedList<UploadHook> uploadHooks = new LinkedList<UploadHook>();
60
61 public UploadAction() {
62 super(tr("Upload to OSM ..."), "upload", tr("Upload all changes to the OSM server."),
63 ShortCut.registerShortCut("file:upload", tr("File: {0}", tr("Upload to OSM ...")), KeyEvent.VK_U, ShortCut.GROUPS_ALT1+ShortCut.GROUP_HOTKEY), true);
64
65 /**
66 * Displays a screen where the actions that would be taken are displayed and
67 * give the user the possibility to cancel the upload.
68 */
69 uploadHooks.add(new UploadHook() {
70 public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
71
72 JPanel p = new JPanel(new GridBagLayout());
73
74 OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
75
76 if (!add.isEmpty()) {
77 p.add(new JLabel(tr("Objects to add:")), GBC.eol());
78 JList l = new JList(add.toArray());
79 l.setCellRenderer(renderer);
80 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
81 p.add(new JScrollPane(l), GBC.eol().fill());
82 }
83
84 if (!update.isEmpty()) {
85 p.add(new JLabel(tr("Objects to modify:")), GBC.eol());
86 JList l = new JList(update.toArray());
87 l.setCellRenderer(renderer);
88 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
89 p.add(new JScrollPane(l), GBC.eol().fill());
90 }
91
92 if (!delete.isEmpty()) {
93 p.add(new JLabel(tr("Objects to delete:")), GBC.eol());
94 JList l = new JList(delete.toArray());
95 l.setCellRenderer(renderer);
96 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
97 p.add(new JScrollPane(l), GBC.eol().fill());
98 }
99
100 return JOptionPane.showConfirmDialog(Main.parent, p, tr("Upload these changes?"),
101 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
102 }
103 });
104 }
105
106 public void actionPerformed(ActionEvent e) {
107 if (Main.map == null) {
108 JOptionPane.showMessageDialog(Main.parent,tr("Nothing to upload. Get some data first."));
109 return;
110 }
111
112 if (!Main.map.conflictDialog.conflicts.isEmpty()) {
113 JOptionPane.showMessageDialog(Main.parent,tr("There are unresolved conflicts. You have to resolve these first."));
114 Main.map.conflictDialog.action.button.setSelected(true);
115 Main.map.conflictDialog.action.actionPerformed(null);
116 return;
117 }
118
119 final LinkedList<OsmPrimitive> add = new LinkedList<OsmPrimitive>();
120 final LinkedList<OsmPrimitive> update = new LinkedList<OsmPrimitive>();
121 final LinkedList<OsmPrimitive> delete = new LinkedList<OsmPrimitive>();
122 for (OsmPrimitive osm : Main.ds.allPrimitives()) {
123 if (osm.get("josm/ignore") != null)
124 continue;
125 if (osm.id == 0 && !osm.deleted)
126 add.addLast(osm);
127 else if (osm.modified && !osm.deleted)
128 update.addLast(osm);
129 else if (osm.deleted && osm.id != 0)
130 delete.addFirst(osm);
131 }
132
133 if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
134 JOptionPane.showMessageDialog(Main.parent,tr("No changes to upload."));
135 return;
136 }
137
138 // Call all upload hooks in sequence. The upload confirmation dialog
139 // is one of these.
140 for(UploadHook hook : uploadHooks)
141 if(!hook.checkUpload(add, update, delete))
142 return;
143
144 final OsmServerWriter server = new OsmServerWriter();
145 final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
146 all.addAll(add);
147 all.addAll(update);
148 all.addAll(delete);
149
150 PleaseWaitRunnable uploadTask = new PleaseWaitRunnable(tr("Uploading data")){
151 @Override protected void realRun() throws SAXException {
152 server.uploadOsm(all);
153 }
154 @Override protected void finish() {
155 Main.main.editLayer().cleanData(server.processed, !add.isEmpty());
156 }
157 @Override protected void cancel() {
158 server.cancel();
159 }
160 };
161 Main.worker.execute(uploadTask);
162 }
163}
Note: See TracBrowser for help on using the repository browser.