/** Convert to sanitized name and alert the user if changes were made. */ private static String checkName(String origName) { String newName = BaseNoGui.sanitizeName(origName); if (!newName.equals(origName)) { String msg = tr( "The sketch name had to be modified. Sketch names can only consist\n" + "of ASCII characters and numbers (but cannot start with a number).\n" + "They should also be less than 64 characters long."); System.out.println(msg); } return newName; }
/** * 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. */ protected void nameCode(String newName) { // make sure the user didn't hide the sketch folder ensureExistence(); newName = newName.trim(); if (newName.equals("")) return; if (newName.charAt(0) == '.') { Base.showWarning(tr("Problem with rename"), tr("The name cannot start with a period."), null); return; } FileUtils.SplitFile split = FileUtils.splitFilename(newName); if (split.extension.equals("")) split.extension = Sketch.DEFAULT_SKETCH_EXTENSION; if (!Sketch.EXTENSIONS.contains(split.extension)) { String msg = I18n.format(tr("\".{0}\" is not a valid extension."), split.extension); Base.showWarning(tr("Problem with rename"), msg, null); return; } // Sanitize name split.basename = BaseNoGui.sanitizeName(split.basename); newName = split.join(); if (renamingCode) { SketchFile current = editor.getCurrentTab().getSketchFile(); if (current.isPrimary()) { if (!split.extension.equals(Sketch.DEFAULT_SKETCH_EXTENSION)) { Base.showWarning( tr("Problem with rename"), tr("The main file cannot use an extension"), null); return; } // Primary file, rename the entire sketch final File parent = sketch.getFolder().getParentFile(); File newFolder = new File(parent, split.basename); try { sketch.renameTo(newFolder); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return; } editor.base.rebuildSketchbookMenus(); } else { // Non-primary file, rename just that file try { current.renameTo(newName); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return; } } } else { // creating a new file SketchFile file; try { file = sketch.addFile(newName); editor.addTab(file, ""); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return; } editor.selectTab(editor.findTabIndex(file)); } // update the tabs editor.header.rebuild(); }