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.
(String newName)
| 134 | * where they diverge. |
| 135 | */ |
| 136 | protected void nameCode(String newName) { |
| 137 | // make sure the user didn't hide the sketch folder |
| 138 | ensureExistence(); |
| 139 | |
| 140 | newName = newName.trim(); |
| 141 | if (newName.equals("")) return; |
| 142 | |
| 143 | if (newName.charAt(0) == '.') { |
| 144 | Base.showWarning(tr("Problem with rename"), |
| 145 | tr("The name cannot start with a period."), null); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | FileUtils.SplitFile split = FileUtils.splitFilename(newName); |
| 150 | if (split.extension.equals("")) |
| 151 | split.extension = Sketch.DEFAULT_SKETCH_EXTENSION; |
| 152 | |
| 153 | if (!Sketch.EXTENSIONS.contains(split.extension.toLowerCase())) { |
| 154 | String msg = I18n.format(tr("\".{0}\" is not a valid extension."), |
| 155 | split.extension); |
| 156 | Base.showWarning(tr("Problem with rename"), msg, null); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // Sanitize name |
| 161 | split.basename = BaseNoGui.sanitizeName(split.basename); |
| 162 | newName = split.join(); |
| 163 | |
| 164 | if (renamingCode) { |
| 165 | SketchFile current = editor.getCurrentTab().getSketchFile(); |
| 166 | |
| 167 | if (current.isPrimary()) { |
| 168 | if (!split.extension.equals(Sketch.DEFAULT_SKETCH_EXTENSION)) { |
| 169 | Base.showWarning(tr("Problem with rename"), |
| 170 | tr("The main file cannot use an extension"), null); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | // Primary file, rename the entire sketch |
| 175 | final File parent = sketch.getFolder().getParentFile(); |
| 176 | File newFolder = new File(parent, split.basename); |
| 177 | try { |
| 178 | sketch.renameTo(newFolder); |
| 179 | } catch (IOException e) { |
| 180 | // This does not pass on e, to prevent showing a backtrace for |
| 181 | // "normal" errors. |
| 182 | Base.showWarning(tr("Error"), e.getMessage(), null); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | editor.base.rebuildSketchbookMenus(); |
| 187 | } else { |
| 188 | // Non-primary file, rename just that file |
| 189 | try { |
| 190 | current.renameTo(newName); |
| 191 | } catch (IOException e) { |
| 192 | // This does not pass on e, to prevent showing a backtrace for |
| 193 | // "normal" errors. |
no test coverage detected