Exemplo n.º 1
0
  /** Remove a piece of code from the sketch and from the disk. */
  public void deleteCode() {
    // make sure the user didn't hide the sketch folder
    ensureExistence();

    // if read-only, give an error
    if (isReadOnly()) {
      // if the files are read-only, need to first do a "save as".
      Base.showMessage(
          "Sketch is Read-Only",
          "Some files are marked \"read-only\", so you'll\n"
              + "need to re-save the sketch in another location,\n"
              + "and try again.");
      return;
    }

    // confirm deletion with user, yes/no
    Object[] options = {"OK", "Cancel"};
    String prompt =
        (currentIndex == 0)
            ? "Are you sure you want to delete this sketch?"
            : "Are you sure you want to delete \""
                + current.name
                + flavorExtensionsShown[current.flavor]
                + "\"?";
    int result =
        JOptionPane.showOptionDialog(
            editor,
            prompt,
            "Delete",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);
    if (result == JOptionPane.YES_OPTION) {
      if (currentIndex == 0) {
        // need to unset all the modified flags, otherwise tries
        // to do a save on the handleNew()

        // delete the entire sketch
        Base.removeDir(folder);

        // get the changes into the sketchbook menu
        // sketchbook.rebuildMenus();

        // make a new sketch, and i think this will rebuild the sketch
        // menu
        editor.handleNewUnchecked();

      } else {
        // delete the file
        if (!current.file.delete()) {
          Base.showMessage("Couldn't do it", "Could not delete \"" + current.name + "\".");
          return;
        }

        // remove code from the list
        removeCode(current);

        // just set current tab to the main tab
        setCurrent(0);

        // update the tabs
        editor.header.repaint();
      }
    }
  }