Files
VisualSapfor/src/Visual_DVM_2021/Passes/All/RenameFile.java
2024-10-08 22:33:49 +03:00

71 lines
2.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package Visual_DVM_2021.Passes.All;
import Common.CurrentAnchestor;
import Common.Utils.CommonUtils;
import Common_old.Current;
import Common_old.UI.UI;
import Common.Visual.Windows.Dialog.Text.FileNameForm;
import ProjectData.Files.DBProjectFile;
import ProjectData.Files.FileType;
import Visual_DVM_2021.Passes.ChangeFilePass;
import Visual_DVM_2021.Passes.PassCode_2021;
import Visual_DVM_2021.Passes.PassException;
import java.io.File;
import java.nio.file.Paths;
public class RenameFile extends ChangeFilePass<DBProjectFile> {
DBProjectFile old;
boolean current;
@Override
protected boolean canStart(Object... args) {
resetArgs();
current = false;
if (CurrentAnchestor.Check(Log, Current.SelectedFile)) {
old = Current.getSelectedFile();
current = Current.HasFile() && Current.getFile().file.equals(old.file);
if ((ff = new FileNameForm()).ShowDialog("Введите новое имя файла", old.file.getName())) {
fileName = ff.Result;
//->
parent_node = Current.getProjectCurrentParentNode();
target_dir = (File) parent_node.getUserObject();
//->
dst = Paths.get(target_dir.getAbsolutePath(), fileName).toFile();
if (dst.exists()) {
Log.Writeln_("Файл с именем " + CommonUtils.Brackets(fileName) + " уже существует");
return false;
}
target = new DBProjectFile(dst, project);
if (target.fileType == FileType.forbidden)
Log.Writeln_("Расширение " + CommonUtils.Brackets(CommonUtils.getExtension(dst)) + " недопустимо");
return true;
}
}
return false;
}
@Override
protected void performPreparation() throws Exception {
if (current)
passes.get(PassCode_2021.CloseCurrentFile).Do();
}
@Override
protected void body() throws Exception {
if (!old.file.renameTo(dst)) throw new PassException("Не удалось переименовать файл");
project.db.Delete(old);
old.node.setUserObject(target);
target.node = old.node;
target.file = dst;
target.RefreshName();
project.db.Insert(target);
}
@Override
protected void performDone() throws Exception {
Current.getSapfor().ResetAllAnalyses();
CurrentAnchestor.set(Current.SelectedFile, target);
if (current)
passes.get(PassCode_2021.OpenCurrentFile).Do(target);
}
@Override
protected void showDone() throws Exception {
UI.getMainWindow().getProjectWindow().getFilesTreeForm().getTree().RefreshNode(target.node);
}
}