Changeset 16582 in osm for applications/editors/josm/plugins
- Timestamp:
- 2009-07-19T18:32:45+02:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/DirectUpload
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java
r15828 r16582 11 11 import static org.openstreetmap.josm.tools.I18n.tr; 12 12 13 import java.awt. Dimension;13 import java.awt.GridBagLayout; 14 14 import java.awt.event.ActionEvent; 15 import java.awt.GridBagLayout;16 import java.io.BufferedOutputStream;17 15 import java.io.ByteArrayInputStream; 18 16 import java.io.ByteArrayOutputStream; 17 import java.io.IOException; 19 18 import java.io.InputStream; 20 import java.io.IOException;21 19 import java.io.OutputStream; 22 import java.lang.String;23 20 import java.net.HttpURLConnection; 24 21 import java.net.URL; … … 27 24 import java.nio.charset.Charset; 28 25 import java.nio.charset.CharsetEncoder; 29 import java.text.DateFormat;30 26 import java.text.DecimalFormat; 31 27 import java.text.SimpleDateFormat; 32 28 import java.util.Date; 33 29 34 import javax.swing.JButton;35 30 import javax.swing.JCheckBox; 36 31 import javax.swing.JLabel; … … 38 33 import javax.swing.JTextField; 39 34 35 import org.openstreetmap.josm.Main; 40 36 import org.openstreetmap.josm.data.gpx.GpxData; 41 37 import org.openstreetmap.josm.gui.ExtendedDialog; 42 38 import org.openstreetmap.josm.gui.JMultilineLabel; 39 import org.openstreetmap.josm.gui.MapView; 40 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 43 41 import org.openstreetmap.josm.gui.layer.GpxLayer; 44 42 import org.openstreetmap.josm.gui.layer.Layer; 45 import org.openstreetmap.josm.gui.MapView; 46 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 43 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 47 44 import org.openstreetmap.josm.io.GpxWriter; 48 import org.openstreetmap.josm.Main;49 45 import org.openstreetmap.josm.tools.Base64; 50 46 import org.openstreetmap.josm.tools.GBC; … … 164 160 * @param GpxData The GPX Data to upload 165 161 */ 166 private void upload(String description, String tags, Boolean isPublic, GpxData gpxData) throws IOException { 167 if(checkForErrors(username, password, description, gpxData)) 168 return; 169 170 // Clean description/tags from disallowed chars 171 description = description.replaceAll("[&?/\\\\]"," "); 172 tags = tags.replaceAll("[&?/\\\\.,;]"," "); 173 174 // Set progress dialog to indeterminate while connecting 175 Main.pleaseWaitDlg.progress.setValue(0); 176 Main.pleaseWaitDlg.setIndeterminate(true); 177 Main.pleaseWaitDlg.currentAction.setText(tr("Connecting...")); 178 179 try { 180 // Generate data for upload 181 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 182 writeGpxFile(baos, "file", gpxData); 183 writeField(baos, "description", description); 184 writeField(baos, "tags", (tags != null && tags.length() > 0) ? tags : ""); 185 writeField(baos, "public", isPublic ? "1" : "0"); 186 writeString(baos, "--" + BOUNDARY + "--" + LINE_END); 187 188 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 189 HttpURLConnection conn = setupConnection(baos.size()); 190 191 Main.pleaseWaitDlg.progress.setMaximum(baos.size()); 192 Main.pleaseWaitDlg.setIndeterminate(false); 193 194 try { 195 flushToServer(bais, conn.getOutputStream()); 196 } catch(Exception e) {} 197 198 if(cancelled) { 199 conn.disconnect(); 200 OutputDisplay.setText(tr("Upload cancelled")); 201 buttons.get(0).setEnabled(true); 202 cancelled = false; 203 } else { 204 boolean success = finishUpConnection(conn); 205 buttons.get(0).setEnabled(!success); 206 if(success) 207 buttons.get(1).setText(tr("Close")); 208 } 209 } catch(Exception e) { 210 OutputDisplay.setText(tr("Error while uploading")); 211 e.printStackTrace(); 212 } 162 private void upload(String description, String tags, Boolean isPublic, GpxData gpxData, ProgressMonitor progressMonitor) throws IOException { 163 progressMonitor.beginTask(null); 164 try { 165 if(checkForErrors(username, password, description, gpxData)) 166 return; 167 168 // Clean description/tags from disallowed chars 169 description = description.replaceAll("[&?/\\\\]"," "); 170 tags = tags.replaceAll("[&?/\\\\.,;]"," "); 171 172 // Set progress dialog to indeterminate while connecting 173 progressMonitor.indeterminateSubTask(tr("Connecting...")); 174 175 try { 176 // Generate data for upload 177 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 178 writeGpxFile(baos, "file", gpxData); 179 writeField(baos, "description", description); 180 writeField(baos, "tags", (tags != null && tags.length() > 0) ? tags : ""); 181 writeField(baos, "public", isPublic ? "1" : "0"); 182 writeString(baos, "--" + BOUNDARY + "--" + LINE_END); 183 184 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 185 HttpURLConnection conn = setupConnection(baos.size()); 186 187 progressMonitor.setTicksCount(baos.size()); 188 progressMonitor.subTask(null); 189 190 try { 191 flushToServer(bais, conn.getOutputStream(), progressMonitor); 192 } catch(Exception e) {} 193 194 if(cancelled) { 195 conn.disconnect(); 196 OutputDisplay.setText(tr("Upload cancelled")); 197 buttons.get(0).setEnabled(true); 198 cancelled = false; 199 } else { 200 boolean success = finishUpConnection(conn); 201 buttons.get(0).setEnabled(!success); 202 if(success) 203 buttons.get(1).setText(tr("Close")); 204 } 205 } catch(Exception e) { 206 OutputDisplay.setText(tr("Error while uploading")); 207 e.printStackTrace(); 208 } 209 } finally { 210 progressMonitor.finishTask(); 211 } 213 212 } 214 213 … … 271 270 * @param OutputStream 272 271 */ 273 private void flushToServer(InputStream in, OutputStream out ) throws Exception {272 private void flushToServer(InputStream in, OutputStream out, ProgressMonitor progressMonitor) throws Exception { 274 273 // Upload in 10 kB chunks 275 274 byte[] buffer = new byte[10000]; … … 281 280 cur += nread; 282 281 out.flush(); 283 Main.pleaseWaitDlg.progress.setValue(cur);284 Main.pleaseWaitDlg.currentAction.setText(getProgressText(cur));282 progressMonitor.worked(nread); 283 progressMonitor.subTask(getProgressText(cur, progressMonitor)); 285 284 286 285 if(cancelled) … … 290 289 if(!cancelled) 291 290 out.flush(); 292 Main.pleaseWaitDlg.currentAction.setText("Waiting for server reply...");291 progressMonitor.subTask("Waiting for server reply..."); 293 292 buffer = null; 294 293 } … … 299 298 * @return String Message 300 299 */ 301 private String getProgressText(int cur ) {302 int max = Main.pleaseWaitDlg.progress.getMaximum();300 private String getProgressText(int cur, ProgressMonitor progressMonitor) { 301 int max = progressMonitor.getTicksCount(); 303 302 int percent = Math.round(cur * 100 / max); 304 303 return tr("Uploading GPX track: {0}% ({1} of {2})", … … 380 379 tagsField.getText(), 381 380 publicCheckbox.isSelected(), 382 ((GpxLayer)Main.map.mapView.getActiveLayer()).data 381 ((GpxLayer)Main.map.mapView.getActiveLayer()).data, 382 progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false) 383 383 ); 384 384 }
Note:
See TracChangeset
for help on using the changeset viewer.