Add a file to the sketch. Supported code files will be copied to the sketch folder. All other files will be copied to the "data" folder (which is created if it does not exist yet). @return true if successful.
(File sourceFile)
| 497 | * @return true if successful. |
| 498 | */ |
| 499 | public boolean addFile(File sourceFile) { |
| 500 | String filename = sourceFile.getName(); |
| 501 | File destFile = null; |
| 502 | boolean isData = false; |
| 503 | boolean replacement = false; |
| 504 | |
| 505 | if (FileUtils.hasExtension(sourceFile, Sketch.EXTENSIONS)) { |
| 506 | destFile = new File(sketch.getFolder(), filename); |
| 507 | } else { |
| 508 | sketch.prepareDataFolder(); |
| 509 | destFile = new File(sketch.getDataFolder(), filename); |
| 510 | isData = true; |
| 511 | } |
| 512 | |
| 513 | if (!sourceFile.equals(destFile)) { |
| 514 | // The typical case here is adding a file from somewhere else. |
| 515 | // This however fails if the source and destination are equal |
| 516 | |
| 517 | // check whether this file already exists |
| 518 | if (destFile.exists()) { |
| 519 | Object[] options = { tr("OK"), tr("Cancel") }; |
| 520 | String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename); |
| 521 | int result = JOptionPane.showOptionDialog(editor, |
| 522 | prompt, |
| 523 | tr("Replace"), |
| 524 | JOptionPane.YES_NO_OPTION, |
| 525 | JOptionPane.QUESTION_MESSAGE, |
| 526 | null, |
| 527 | options, |
| 528 | options[0]); |
| 529 | if (result == JOptionPane.YES_OPTION) { |
| 530 | replacement = true; |
| 531 | } else { |
| 532 | return false; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | // If it's a replacement, delete the old file first, |
| 537 | // otherwise case changes will not be preserved. |
| 538 | // http://dev.processing.org/bugs/show_bug.cgi?id=969 |
| 539 | if (replacement) { |
| 540 | if (!destFile.delete()) { |
| 541 | Base.showWarning(tr("Error adding file"), |
| 542 | I18n.format(tr("Could not delete the existing ''{0}'' file."), filename), |
| 543 | null); |
| 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // perform the copy |
| 549 | try { |
| 550 | Base.copyFile(sourceFile, destFile); |
| 551 | |
| 552 | } catch (IOException e) { |
| 553 | Base.showWarning(tr("Error adding file"), |
| 554 | I18n.format(tr("Could not add ''{0}'' to the sketch."), filename), |
| 555 | e); |
| 556 | return false; |
no test coverage detected