Changeset 15828 in osm for applications/editors/josm
- Timestamp:
- 2009-06-10T16:30:04+02:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java
r14856 r15828 60 60 61 61 // Fields are declared here for easy access 62 private JMultilineLabel OutputDisplay = new JMultilineLabel(""); 63 private JTextField descriptionField = new JTextField(); 64 private JTextField tagsField = new JTextField(); 62 // Do not remove the space in JMultilineLabel. Otherwise the label will be empty 63 // as we don't know its contents yet and therefore have a height of 0. This will 64 // lead to unnecessary scrollbars. 65 private JMultilineLabel OutputDisplay = new JMultilineLabel(" "); 66 private JTextField descriptionField = new JTextField(50); 67 private JTextField tagsField = new JTextField(50); 65 68 private JCheckBox publicCheckbox = new JCheckBox(); 66 69 67 70 // Constants used when generating upload request 68 71 private static final String API_VERSION = "0.6"; … … 74 77 private String datename = new SimpleDateFormat("yyMMddHHmmss").format(new Date()); 75 78 private String filename = ""; 76 79 77 80 private boolean cancelled = false; 78 81 … … 86 89 JPanel content = initComponents(); 87 90 autoSelectTrace(); 88 89 contentConstraints = GBC.eol().fill().insets(5,10,5,0); 91 90 92 setupDialog(content, new String[] { "uploadtrace.png", "cancel.png" }); 91 93 92 94 buttons.get(0).setEnabled(!checkForGPXLayer()); 93 94 setSize(findMaxDialogSize()); 95 } 96 95 } 96 97 97 /** 98 98 * Sets up the dialog window elements … … 109 109 tagsField.setToolTipText(tr("Please enter tags about your trace.")); 110 110 111 JPanel p = new JPanel(); 112 p.setLayout(new GridBagLayout()); 111 JPanel p = new JPanel(new GridBagLayout()); 113 112 114 113 OutputDisplay.setMaxWidth(findMaxDialogSize().width-10); … … 125 124 return p; 126 125 } 127 126 128 127 /** 129 128 * This function will automatically select a GPX layer if it's the only one. … … 168 167 if(checkForErrors(username, password, description, gpxData)) 169 168 return; 170 169 171 170 // Clean description/tags from disallowed chars 172 171 description = description.replaceAll("[&?/\\\\]"," "); 173 172 tags = tags.replaceAll("[&?/\\\\.,;]"," "); 174 173 175 174 // Set progress dialog to indeterminate while connecting 176 175 Main.pleaseWaitDlg.progress.setValue(0); 177 Main.pleaseWaitDlg.setIndeterminate(true); 176 Main.pleaseWaitDlg.setIndeterminate(true); 178 177 Main.pleaseWaitDlg.currentAction.setText(tr("Connecting...")); 179 178 180 179 try { 181 180 // Generate data for upload 182 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 181 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 183 182 writeGpxFile(baos, "file", gpxData); 184 183 writeField(baos, "description", description); … … 186 185 writeField(baos, "public", isPublic ? "1" : "0"); 187 186 writeString(baos, "--" + BOUNDARY + "--" + LINE_END); 188 189 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 187 188 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 190 189 HttpURLConnection conn = setupConnection(baos.size()); 191 190 192 191 Main.pleaseWaitDlg.progress.setMaximum(baos.size()); 193 Main.pleaseWaitDlg.setIndeterminate(false); 194 192 Main.pleaseWaitDlg.setIndeterminate(false); 193 195 194 try { 196 195 flushToServer(bais, conn.getOutputStream()); 197 196 } catch(Exception e) {} 198 197 199 198 if(cancelled) { 200 199 conn.disconnect(); … … 203 202 cancelled = false; 204 203 } else { 205 boolean success = finishUpConnection(conn); 204 boolean success = finishUpConnection(conn); 206 205 buttons.get(0).setEnabled(!success); 207 206 if(success) … … 213 212 } 214 213 } 215 214 216 215 /** 217 216 * This function sets up the upload URL and logs in using the username and password given … … 225 224 String auth = username + ":" + password; 226 225 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth)); 227 226 228 227 // Upload URL 229 228 URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create"); 230 229 231 230 // Set up connection and log in 232 231 HttpURLConnection c = (HttpURLConnection) url.openConnection(); … … 235 234 c.setRequestMethod("POST"); 236 235 c.setDoOutput(true); 237 c.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes)); 236 c.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes)); 238 237 c.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); 239 238 c.addRequestProperty("Connection", "close"); // counterpart of keep-alive 240 239 c.addRequestProperty("Expect", ""); 241 240 c.connect(); 242 241 243 242 return c; 244 243 } 245 244 246 245 /** 247 246 * This function checks if the given connection finished up fine, closes it and returns the result. 248 247 * It also posts the result (or errors) to OutputDisplay. 249 248 250 249 * @param HttpURLConnection The connection to check/finish up 251 250 */ … … 253 252 String returnMsg = c.getResponseMessage(); 254 253 boolean success = returnMsg.equals("OK"); 255 254 256 255 if (c.getResponseCode() != 200) { 257 256 if (c.getHeaderField("Error") != null) 258 257 returnMsg += "\n" + c.getHeaderField("Error"); 259 258 } 260 259 261 260 OutputDisplay.setText(success 262 261 ? tr("GPX upload was successful") … … 266 265 return success; 267 266 } 268 267 269 268 /** 270 269 * Uploads a given InputStream to a given OutputStream and sets progress … … 274 273 private void flushToServer(InputStream in, OutputStream out) throws Exception { 275 274 // Upload in 10 kB chunks 276 277 278 279 280 281 282 275 byte[] buffer = new byte[10000]; 276 int nread; 277 int cur = 0; 278 synchronized (in) { 279 while ((nread = in.read(buffer, 0, buffer.length)) >= 0) { 280 out.write(buffer, 0, nread); 281 cur += nread; 283 282 out.flush(); 284 283 Main.pleaseWaitDlg.progress.setValue(cur); 285 284 Main.pleaseWaitDlg.currentAction.setText(getProgressText(cur)); 286 285 287 286 if(cancelled) 288 287 break; 289 290 291 288 } 289 } 290 if(!cancelled) 292 291 out.flush(); 293 292 Main.pleaseWaitDlg.currentAction.setText("Waiting for server reply..."); 294 295 296 293 buffer = null; 294 } 295 297 296 /** 298 297 * Generates the output string displayed in the PleaseWaitDialog. … … 306 305 percent, formatBytes(cur), formatBytes(max)); 307 306 } 308 307 309 308 /** 310 309 * Nicely calculates given bytes into MB, kB and B (with units) … … 351 350 352 351 /** 353 * Checks if a GPX layer is selected and returns the result. Also writes an error 352 * Checks if a GPX layer is selected and returns the result. Also writes an error 354 353 * message to OutputDisplay if result is false. 355 354 * @return boolean True, if /no/ GPX layer is selected … … 366 365 } 367 366 368 367 369 368 /** 370 369 * This creates the uploadTask that does the actual work and hands it to the main.worker to be executed. … … 372 371 private void setupUpload() { 373 372 if(checkForGPXLayer()) return; 374 373 375 374 // Disable Upload button so users can't just upload that track again 376 375 buttons.get(0).setEnabled(false); … … 389 388 } 390 389 }; 391 390 392 391 Main.worker.execute(uploadTask); 393 392 } 394 393 395 394 /** 396 395 * Writes textfields (like in webbrowser) to the given ByteArrayOutputStream … … 425 424 writeLineEnd(baos); 426 425 } 427 426 428 427 /** 429 428 * Writes a String to the given ByteArrayOutputStream … … 436 435 } catch(Exception e) {} 437 436 } 438 437 439 438 /** 440 439 * Writes a newline to the given ByteArrayOutputStream … … 444 443 writeString(baos, LINE_END); 445 444 } 446 445 447 446 /** 448 447 * Writes a boundary line to the given ByteArrayOutputStream … … 453 452 writeLineEnd(baos); 454 453 } 455 454 456 455 /** 457 456 * Returns the filename of the GPX file to be upload. If not available, returns current date … … 462 461 return filename.equals("") ? datename : filename; 463 462 } 464 465 /** 466 * Defines a default size for the dialog 467 */ 468 @Override protected Dimension findMaxDialogSize() { 469 setResizable(false); 470 return new Dimension(300, 230); 471 } 472 463 473 464 /** 474 465 * Overrides the default actions. Will not close the window when upload trace is clicked
Note:
See TracChangeset
for help on using the changeset viewer.