/** * @param path The relative path to the file in the .replicatorG directory * @param autoCopy If true, copy over the file of the same name in the application directory if * none is found in the prefs directory. * @return */ public static File getUserFile(String path, boolean autoCopy) { if (path.contains("..")) { Base.logger.info("Attempted to access parent directory in " + path + ", skipping"); return null; } // First look in the user's local .replicatorG directory for the path. File f = new File(getUserDirectory(), path); // Make the parent file if not already there File dir = f.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } if (autoCopy && !f.exists()) { // Check if there's an application-level version File original = getApplicationFile(path); // If so, copy it over if (original.exists()) { try { Base.copyFile(original, f); } catch (IOException ioe) { Base.logger.log( Level.SEVERE, "Couldn't copy " + path + " to your local .replicatorG directory", f); } } } return f; }
/** * Add a file to the sketch. * * <p>.gcode files will be added to the sketch folder. <br> * All other files will be added to the "data" folder. * * <p>If they don't exist already, the "code" or "data" folder will be created. * * <p> * * @return true if successful. */ public boolean addFile(File sourceFile) { String filename = sourceFile.getName(); File destFile = null; boolean addingCode = false; destFile = new File(this.folder, filename); addingCode = true; // make sure they aren't the same file if (!addingCode && sourceFile.equals(destFile)) { Base.showWarning( "You can't fool me", "This file has already been copied to the\n" + "location where you're trying to add it.\n" + "I ain't not doin nuthin'.", null); return false; } // in case the user is "adding" the code in an attempt // to update the sketch's tabs if (!sourceFile.equals(destFile)) { try { Base.copyFile(sourceFile, destFile); } catch (IOException e) { Base.showWarning("Error adding file", "Could not add '" + filename + "' to the sketch.", e); return false; } } // make the tabs update after this guy is added if (addingCode) { String newName = destFile.getName(); int newFlavor = -1; if (newName.toLowerCase().endsWith(".gcode")) { newName = newName.substring(0, newName.length() - 6); newFlavor = GCODE; } // see also "nameCode" for identical situation SketchCode newCode = new SketchCode(newName, destFile, newFlavor); insertCode(newCode); sortCode(); setCurrent(newName); editor.header.repaint(); } return true; }
public static void copyDir(File sourceDir, File targetDir) throws IOException { targetDir.mkdirs(); String files[] = sourceDir.list(); for (int i = 0; i < files.length; i++) { if (files[i].equals(".") || files[i].equals("..")) continue; File source = new File(sourceDir, files[i]); File target = new File(targetDir, files[i]); if (source.isDirectory()) { // target.mkdirs(); copyDir(source, target); target.setLastModified(source.lastModified()); } else { copyFile(source, target); } } }