Example #1
0
  /** Save all code in the current sketch. */
  public boolean save() throws IOException {
    // make sure the user didn't hide the sketch folder
    ensureExistence();

    // first get the contents of the editor text area
    if (current.modified) {
      current.program = editor.getText();
    }

    // don't do anything if not actually modified
    // if (!modified) return false;

    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 this sketch to another location.");
      // if the user cancels, give up on the save()
      if (!saveAs()) return false;
    }

    for (int i = 0; i < codeCount; i++) {
      if (code[i].modified) code[i].save();
    }
    calcModified();
    return true;
  }
Example #2
0
  /** Save all code in the current sketch. */
  public boolean save() throws IOException {
    if (mainFilename == null) {
      return saveAs();
    }

    if (isReadOnly()) {
      // if the files are read-only, need to first do a "save as".
      Base.showMessage(
          "File is read-only",
          "This file is marked \"read-only\", so you'll\n"
              + "need to re-save this file to another location.");
      // if the user cancels, give up on the save()
      if (!saveAs()) return false;
      return true;
    }

    BuildCode code = getCode();
    if (code != null) {
      if (hasMainWindow) {
        if (code.isModified()) {
          code.program = editor.getText();
          code.save();
        }
      }
    }
    BuildModel model = getModel();
    if (model != null) {
      if (model.isModified()) {
        model.save();
      }
    }
    return true;
  }
Example #3
0
  public void hideCode() {
    // 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;
    }

    // don't allow hide of the main code
    // TODO maybe gray out the menu on setCurrent(0)
    if (currentIndex == 0) {
      Base.showMessage(
          "Can't do that", "You cannot hide the main " + ".gcode file from a sketch\n");
      return;
    }

    // rename the file
    File newFile = new File(current.file.getAbsolutePath() + ".x");
    if (!current.file.renameTo(newFile)) {
      Base.showWarning("Error", "Could not hide " + "\"" + current.file.getName() + "\".", null);
      return;
    }
    current.file = newFile;

    // move it to the hidden list
    if (hiddenCount == hidden.length) {
      SketchCode temp[] = new SketchCode[hiddenCount + 1];
      System.arraycopy(hidden, 0, temp, 0, hiddenCount);
      hidden = temp;
    }
    hidden[hiddenCount++] = current;

    // remove it from the main list
    removeCode(current);

    // update the tabs
    setCurrent(0);
    editor.header.repaint();
  }
Example #4
0
  public void unhideCode(String what) {
    SketchCode unhideCode = null;
    String name = what.substring(0, (what.indexOf(".") == -1 ? what.length() : what.indexOf(".")));
    String extension = what.indexOf(".") == -1 ? "" : what.substring(what.indexOf("."));

    for (int i = 0; i < hiddenCount; i++) {
      if (hidden[i].name.equals(name)
          && Sketch.flavorExtensionsShown[hidden[i].flavor].equals(extension)) {
        // unhideIndex = i;
        unhideCode = hidden[i];

        // remove from the 'hidden' list
        for (int j = i; j < hiddenCount - 1; j++) {
          hidden[j] = hidden[j + 1];
        }
        hiddenCount--;
        break;
      }
    }
    // if (unhideIndex == -1) {
    if (unhideCode == null) {
      System.err.println("internal error: could find " + what + " to unhide.");
      return;
    }
    if (!unhideCode.file.exists()) {
      Base.showMessage("Can't unhide", "The file \"" + what + "\" no longer exists.");
      // System.out.println(unhideCode.file);
      return;
    }
    String unhidePath = unhideCode.file.getAbsolutePath();
    File unhideFile = new File(unhidePath.substring(0, unhidePath.length() - 2));

    if (!unhideCode.file.renameTo(unhideFile)) {
      Base.showMessage(
          "Can't unhide", "The file \"" + what + "\" could not be" + "renamed and unhidden.");
      return;
    }
    unhideCode.file = unhideFile;
    insertCode(unhideCode);
    sortCode();
    setCurrent(unhideCode.name);
    editor.header.repaint();
  }
Example #5
0
  public void newCode() {
    // 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;
    }

    renamingCode = false;
    // editor.status.edit("Name for new file:", "");
  }
Example #6
0
  public void renameCode() {
    // 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;
    }

    // ask for new name of file (internal to window)
    // TODO maybe just popup a text area?
    renamingCode = true;
    // editor.status.edit(prompt, oldName);
  }
Example #7
0
  public static void main(String args[]) {

    // make sure that this is running on java 1.5 or better.
    if (Base.javaVersion < 1.5f) {
      Base.quitWithError(
          "Need to install Java 1.5",
          "This version of ReplicatorG requires\n"
              + "Java 1.5 or later to run properly.\n"
              + "Please visit java.com to upgrade.",
          null);
    }

    if (Base.isMacOS()) {
      // Default to sun's XML parser, PLEASE.  Some apps are installing some janky-ass xerces.
      System.setProperty(
          "javax.xml.parsers.DocumentBuilderFactory",
          "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
      System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ReplicatorG");
    }

    // parse command line input
    for (int i = 0; i < args.length; i++) {
      // grab any opened file from the command line
      if (supportedExtension(args[i])) {
        Base.openedAtStartup = args[i];
      }

      // Allow for [--debug] [DEBUGLEVEL]
      if (args[i].equals("--debug")) {
        int debugLevelArg = 2;
        if ((i + 1) < args.length) {
          try {
            debugLevelArg = Integer.parseInt(args[i + 1]);
          } catch (NumberFormatException e) {
          }
          ;
        }
        if (debugLevelArg == 0) {
          logger.setLevel(Level.INFO);
          logger.info("Debug level is 'INFO'");
        } else if (debugLevelArg == 1) {
          logger.setLevel(Level.FINE);
          logger.info("Debug level is 'FINE'");
        } else if (debugLevelArg == 2) {
          logger.setLevel(Level.FINER);
          logger.info("Debug level is 'FINER'");
        } else if (debugLevelArg == 3) {
          logger.setLevel(Level.FINEST);
          logger.info("Debug level is 'FINEST'");
        } else if (debugLevelArg >= 4) {
          logger.setLevel(Level.ALL);
          logger.info("Debug level is 'ALL'");
        }
      } else if (args[i].startsWith("-")) {
        System.out.println("Usage: ./replicatorg [[--debug] [DEBUGLEVEL]] [filename.stl]");
        System.exit(1);
      }
    }

    // Warn about read-only directories
    {
      File userDir = getUserDirectory();
      String header = null;
      if (!userDir.exists()) header = new String("Unable to create user directory");
      else if (!userDir.canWrite()) header = new String("Unable to write to user directory");
      else if (!userDir.isDirectory()) header = new String("User directory must be a directory");
      if (header != null) {
        Base.showMessage(
            header,
            "<html><body>ReplicatorG can not write to the directory "
                + userDir.getAbsolutePath()
                + ".<br>"
                + "Some functions of ReplicatorG, like toolpath generation and firmware updates,<br>"
                + "require ReplicatorG to write data to this directory.  You should end this<br>"
                + "session, change the permissions on this directory, and start again.");
      }
    }

    // Use the default system proxy settings
    System.setProperty("java.net.useSystemProxies", "true");
    // Use antialiasing implicitly
    System.setProperty("j3d.implicitAntialiasing", "true");

    // Start the firmware check thread.
    FirmwareUploader.checkFirmware();

    // MAC OS X ONLY:
    // register a temporary/early version of the mrj open document handler,
    // because the event may be lost (sometimes, not always) by the time
    // that MainWindow is properly constructed.
    MRJOpenDocumentHandler startupOpen =
        new MRJOpenDocumentHandler() {
          public void handleOpenFile(File file) {
            // this will only get set once.. later will be handled
            // by the MainWindow version of this fella
            if (Base.openedAtStartup == null) {
              Base.openedAtStartup = file.getAbsolutePath();
            }
          }
        };
    MRJApplicationUtils.registerOpenDocumentHandler(startupOpen);

    // Create the new application "Base" class.
    new Base();
  }
Example #8
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();
      }
    }
  }
Example #9
0
  /**
   * This is called upon return from entering a new file name. (that is, from either newCode or
   * renameCode after the prompt) This code is almost identical for both the newCode and renameCode
   * cases, so they're kept merged except for right in the middle where they diverge.
   */
  public void nameCode(String newName) {
    // make sure the user didn't hide the sketch folder
    ensureExistence();

    // if renaming to the same thing as before, just ignore.
    // also ignoring case here, because i don't want to write
    // a bunch of special stuff for each platform
    // (osx is case insensitive but preserving, windows insensitive,
    // *nix is sensitive and preserving.. argh)
    if (renamingCode && newName.equalsIgnoreCase(current.name)) {
      // exit quietly for the 'rename' case.
      // if it's a 'new' then an error will occur down below
      return;
    }

    // don't allow blank names
    if (newName.trim().equals("")) {
      return;
    }

    if (newName.trim().equals(".gcode")) {
      return;
    }

    String newFilename = null;
    int newFlavor = 0;

    // separate into newName (no extension) and newFilename (with ext)
    // add .gcode to file if it has no extension
    if (newName.endsWith(".gcode")) {
      newFilename = newName;
      newName = newName.substring(0, newName.length() - 6);
      newFlavor = GCODE;

    } else {
      newFilename = newName + ".gcode";
      newFlavor = GCODE;
    }

    // dots are allowed for the .gcode and .java, but not in the name
    // make sure the user didn't name things poo.time.gcode
    // or something like that (nothing against poo time)
    if (newName.indexOf('.') != -1) {
      newFilename = newName + ".gcode";
    }

    // create the new file, new SketchCode object and load it
    File newFile = new File(folder, newFilename);
    if (newFile.exists()) { // yay! users will try anything
      Base.showMessage(
          "Nope",
          "A file named \""
              + newFile
              + "\" already exists\n"
              + "in \""
              + folder.getAbsolutePath()
              + "\"");
      return;
    }

    File newFileHidden = new File(folder, newFilename + ".x");
    if (newFileHidden.exists()) {
      // don't let them get away with it if they try to create something
      // with the same name as something hidden
      Base.showMessage(
          "No Way",
          "A hidden tab with the same name already exists.\n" + "Use \"Unhide\" to bring it back.");
      return;
    }

    if (renamingCode) {
      if (currentIndex == 0) {
        // get the new folder name/location
        File newFolder = new File(folder.getParentFile(), newName);
        if (newFolder.exists()) {
          Base.showWarning(
              "Cannot Rename",
              "Sorry, a sketch (or folder) named " + "\"" + newName + "\" already exists.",
              null);
          return;
        }

        // unfortunately this can't be a "save as" because that
        // only copies the sketch files and the data folder
        // however this *will* first save the sketch, then rename

        // first get the contents of the editor text area
        if (current.modified) {
          current.program = editor.getText();
          try {
            // save this new SketchCode
            current.save();
          } catch (Exception e) {
            Base.showWarning("Error", "Could not rename the sketch. (0)", e);
            return;
          }
        }

        if (!current.file.renameTo(newFile)) {
          Base.showWarning(
              "Error",
              "Could not rename \""
                  + current.file.getName()
                  + "\" to \""
                  + newFile.getName()
                  + "\"",
              null);
          return;
        }

        // save each of the other tabs because this is gonna be
        // re-opened
        try {
          for (int i = 1; i < codeCount; i++) {
            // if (code[i].modified) code[i].save();
            code[i].save();
          }
        } catch (Exception e) {
          Base.showWarning("Error", "Could not rename the sketch. (1)", e);
          return;
        }

        // now rename the sketch folder and re-open
        boolean success = folder.renameTo(newFolder);
        if (!success) {
          Base.showWarning("Error", "Could not rename the sketch. (2)", null);
          return;
        }
        // if successful, set base properties for the sketch

        File mainFile = new File(newFolder, newName + ".gcode");
        mainFilename = mainFile.getAbsolutePath();

        // having saved everything and renamed the folder and the main
        // .gcode,
        // use the editor to re-open the sketch to re-init state
        // (unfortunately this will kill positions for carets etc)
        editor.handleOpenUnchecked(
            mainFilename,
            currentIndex,
            editor.textarea.getSelectionStart(),
            editor.textarea.getSelectionEnd(),
            editor.textarea.getScrollPosition());

        // get the changes into the sketchbook menu
        // (re-enabled in 0115 to fix bug #332)
        // editor.sketchbook.rebuildMenus();

      } else { // else if something besides code[0]
        if (!current.file.renameTo(newFile)) {
          Base.showWarning(
              "Error",
              "Could not rename \""
                  + current.file.getName()
                  + "\" to \""
                  + newFile.getName()
                  + "\"",
              null);
          return;
        }

        // just reopen the class itself
        current.name = newName;
        current.file = newFile;
        current.flavor = newFlavor;
      }

    } else { // creating a new file
      try {
        newFile.createNewFile(); // TODO returns a boolean
      } catch (IOException e) {
        Base.showWarning(
            "Error",
            "Could not create the file \""
                + newFile
                + "\"\n"
                + "in \""
                + folder.getAbsolutePath()
                + "\"",
            e);
        return;
      }
      SketchCode newCode = new SketchCode(newName, newFile, newFlavor);
      insertCode(newCode);
    }

    // sort the entries
    sortCode();

    // set the new guy as current
    setCurrent(newName + flavorExtensionsShown[newFlavor]);

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

    editor.header.rebuild();

    // force the update on the mac?
    Toolkit.getDefaultToolkit().sync();
    // editor.header.getToolkit().sync();
  }