Files
VisualSapfor/src/_VisualDVM/Passes/All/RenameFile.java

70 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 _VisualDVM.Passes.All;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Project.ChangeFilePass;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Files.FileType;
import _VisualDVM.Visual.Windows.FileNameForm;
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 (Global.mainModule.Check(Log, Current.SelectedFile)) {
old = Global.mainModule.getSelectedFile();
current = Global.mainModule.HasFile() && Global.mainModule.getFile().file.equals(old.file);
if ((ff = new FileNameForm()).ShowDialog("Введите новое имя файла", old.file.getName())) {
fileName = ff.Result;
//->
parent_node = Global.mainModule.getProjectCurrentParentNode();
target_dir = (File) parent_node.getUserObject();
//->
dst = Paths.get(target_dir.getAbsolutePath(), fileName).toFile();
if (dst.exists()) {
Log.Writeln_("Файл с именем " + Utils_.Brackets(fileName) + " уже существует");
return false;
}
target = new DBProjectFile(dst, project);
if (target.fileType == FileType.forbidden)
Log.Writeln_("Расширение " + Utils_.Brackets(Utils_.getExtension(dst)) + " недопустимо");
return true;
}
}
return false;
}
@Override
protected void performPreparation() throws Exception {
if (current)
Global.mainModule.getPass(PassCode.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 {
Global.mainModule.getSapfor().ResetAllAnalyses();
Global.mainModule.set(Current.SelectedFile, target);
if (current)
Global.mainModule.getPass(PassCode.OpenCurrentFile).Do(target);
}
@Override
protected void showDone() throws Exception {
Global.mainModule.getUI().getMainWindow().getProjectWindow().getFilesTreeForm().getTree().RefreshNode(target.node);
}
}