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

Last change on this file since 1628 was 1628, checked in by stoecker, 15 years ago

cleanup group settings in preferences

  • Property svn:eol-style set to native
File size: 7.9 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;
11import java.util.List;
12
13import javax.swing.JLabel;
14import javax.swing.JList;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.gui.ExtendedDialog;
22import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
23import org.openstreetmap.josm.gui.PleaseWaitRunnable;
24import org.openstreetmap.josm.gui.historycombobox.SuggestingJHistoryComboBox;
25import org.openstreetmap.josm.io.OsmServerWriter;
26import org.openstreetmap.josm.tools.GBC;
27import org.openstreetmap.josm.tools.Shortcut;
28import org.xml.sax.SAXException;
29
30/**
31 * Action that opens a connection to the osm server and uploads all changes.
32 *
33 * An dialog is displayed asking the user to specify a rectangle to grab.
34 * The url and account settings from the preferences are used.
35 *
36 * @author imi
37 */
38public class UploadAction extends JosmAction {
39
40 public static final String HISTORY_KEY = "upload.comment.history";
41
42 /** Upload Hook */
43 public interface UploadHook {
44 /**
45 * Checks the upload.
46 * @param add The added primitives
47 * @param update The updated primitives
48 * @param delete The deleted primitives
49 * @return true, if the upload can continue
50 */
51 public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete);
52 }
53
54 /**
55 * The list of upload hooks. These hooks will be called one after the other
56 * when the user wants to upload data. Plugins can insert their own hooks here
57 * if they want to be able to veto an upload.
58 *
59 * Be default, the standard upload dialog is the only element in the list.
60 * Plugins should normally insert their code before that, so that the upload
61 * dialog is the last thing shown before upload really starts; on occasion
62 * however, a plugin might also want to insert something after that.
63 */
64 public final LinkedList<UploadHook> uploadHooks = new LinkedList<UploadHook>();
65
66 public UploadAction() {
67 super(tr("Upload to OSM..."), "upload", tr("Upload all changes to the OSM server."),
68 Shortcut.registerShortcut("file:upload", tr("File: {0}", tr("Upload to OSM...")), KeyEvent.VK_U, Shortcut.GROUPS_ALT1+Shortcut.GROUP_HOTKEY), true);
69
70 /**
71 * Displays a screen where the actions that would be taken are displayed and
72 * give the user the possibility to cancel the upload.
73 */
74 uploadHooks.add(new UploadHook() {
75 public boolean checkUpload(Collection<OsmPrimitive> add, Collection<OsmPrimitive> update, Collection<OsmPrimitive> delete) {
76
77 JPanel p = new JPanel(new GridBagLayout());
78
79 OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
80
81 if (!add.isEmpty()) {
82 p.add(new JLabel(tr("Objects to add:")), GBC.eol());
83 JList l = new JList(add.toArray());
84 l.setCellRenderer(renderer);
85 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
86 p.add(new JScrollPane(l), GBC.eol().fill());
87 }
88
89 if (!update.isEmpty()) {
90 p.add(new JLabel(tr("Objects to modify:")), GBC.eol());
91 JList l = new JList(update.toArray());
92 l.setCellRenderer(renderer);
93 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
94 p.add(new JScrollPane(l), GBC.eol().fill());
95 }
96
97 if (!delete.isEmpty()) {
98 p.add(new JLabel(tr("Objects to delete:")), GBC.eol());
99 JList l = new JList(delete.toArray());
100 l.setCellRenderer(renderer);
101 l.setVisibleRowCount(l.getModel().getSize() < 6 ? l.getModel().getSize() : 10);
102 p.add(new JScrollPane(l), GBC.eol().fill());
103 }
104
105 p.add(new JLabel(tr("Provide a brief comment for the changes you are uploading:")), GBC.eol().insets(0, 5, 10, 3));
106 SuggestingJHistoryComboBox cmt = new SuggestingJHistoryComboBox();
107 List<String> cmtHistory = new LinkedList<String>(Main.pref.getCollection(HISTORY_KEY, null));
108 cmt.setHistory(cmtHistory);
109 //final JTextField cmt = new JTextField(lastCommitComment);
110 p.add(cmt, GBC.eol().fill(GBC.HORIZONTAL));
111
112 while(true) {
113 int result = new ExtendedDialog(Main.parent,
114 tr("Upload these changes?"),
115 p,
116 new String[] {tr("Upload Changes"), tr("Cancel")},
117 new String[] {"upload.png", "cancel.png"}).getValue();
118
119 // cancel pressed
120 if (result != 1) return false;
121
122 // don't allow empty commit message
123 if (cmt.getText().trim().length() < 3) continue;
124
125 // store the history of comments
126 cmt.addCurrentItemToHistory();
127 Main.pref.putCollection(HISTORY_KEY, cmt.getHistory());
128
129 break;
130 }
131 return true;
132 }
133 });
134 }
135
136 public void actionPerformed(ActionEvent e) {
137 if (Main.map == null) {
138 JOptionPane.showMessageDialog(Main.parent,tr("Nothing to upload. Get some data first."));
139 return;
140 }
141
142 if (!Main.map.conflictDialog.conflicts.isEmpty()) {
143 JOptionPane.showMessageDialog(Main.parent,tr("There are unresolved conflicts. You have to resolve these first."));
144 Main.map.conflictDialog.action.button.setSelected(true);
145 Main.map.conflictDialog.action.actionPerformed(null);
146 return;
147 }
148
149 final LinkedList<OsmPrimitive> add = new LinkedList<OsmPrimitive>();
150 final LinkedList<OsmPrimitive> update = new LinkedList<OsmPrimitive>();
151 final LinkedList<OsmPrimitive> delete = new LinkedList<OsmPrimitive>();
152 for (OsmPrimitive osm : Main.ds.allPrimitives()) {
153 if (osm.get("josm/ignore") != null)
154 continue;
155 if (osm.id == 0 && !osm.deleted)
156 add.addLast(osm);
157 else if (osm.modified && !osm.deleted)
158 update.addLast(osm);
159 else if (osm.deleted && osm.id != 0)
160 delete.addFirst(osm);
161 }
162
163 if (add.isEmpty() && update.isEmpty() && delete.isEmpty()) {
164 JOptionPane.showMessageDialog(Main.parent,tr("No changes to upload."));
165 return;
166 }
167
168 // Call all upload hooks in sequence. The upload confirmation dialog
169 // is one of these.
170 for(UploadHook hook : uploadHooks)
171 if(!hook.checkUpload(add, update, delete))
172 return;
173
174 final OsmServerWriter server = new OsmServerWriter();
175 final Collection<OsmPrimitive> all = new LinkedList<OsmPrimitive>();
176 all.addAll(add);
177 all.addAll(update);
178 all.addAll(delete);
179
180 PleaseWaitRunnable uploadTask = new PleaseWaitRunnable(tr("Uploading data")){
181 @Override protected void realRun() throws SAXException {
182 server.uploadOsm(Main.ds.version, all);
183 }
184 @Override protected void finish() {
185 Main.main.editLayer().cleanData(server.processed, !add.isEmpty());
186 }
187 @Override protected void cancel() {
188 // FIXME server.cancel();
189 }
190 };
191 Main.worker.execute(uploadTask);
192 }
193}
Note: See TracBrowser for help on using the repository browser.