Files
VisualSapfor/src/Visual_DVM_2021/Passes/All/RenameDirectory.java

94 lines
4.1 KiB
Java
Raw Normal View History

package Visual_DVM_2021.Passes.All;
2024-10-09 23:37:58 +03:00
import Common.Current_;
2024-10-11 00:00:30 +03:00
import Common.Utils.Utils_;
2024-10-09 22:01:19 +03:00
import _VisualDVM.Current;
import _VisualDVM.Global;
2024-10-09 22:01:19 +03:00
import _VisualDVM.Visual.UI;
2024-10-08 22:33:49 +03:00
import Common.Visual.Windows.Dialog.Text.FileNameForm;
2024-10-09 22:01:19 +03:00
import _VisualDVM.Utils;
2024-10-09 22:21:57 +03:00
import _VisualDVM.ProjectData.Files.DBProjectFile;
import Visual_DVM_2021.Passes.ChangeFilePass;
2024-10-09 23:37:58 +03:00
import Visual_DVM_2021.Passes.PassCode;
2024-10-10 23:57:36 +03:00
import Common.Passes.PassException;
2023-09-17 22:13:42 +03:00
import org.apache.commons.io.FileUtils;
import javax.swing.tree.DefaultMutableTreeNode;
import java.io.File;
import java.nio.file.Paths;
import java.util.Vector;
public class RenameDirectory extends ChangeFilePass {
boolean current;
DBProjectFile old_current_file;
DBProjectFile new_current_file;
@Override
protected boolean canStart(Object... args) {
resetArgs();
current = false;
old_current_file = null;
new_current_file = null;
dst_node = Global.mainModule.getProjectNode();
2023-09-17 22:13:42 +03:00
if ((dst_node != null) && (dst_node.getUserObject() instanceof File)) {
target_dir = Global.mainModule.getSelectedDirectory();
if (current = Global.mainModule.HasFile() && Utils.isAnchestor(Global.mainModule.getFile().file, target_dir)) {
old_current_file = Global.mainModule.getFile();
2023-09-17 22:13:42 +03:00
}
if ((ff = new FileNameForm()).ShowDialog("Введите новое имя папки", target_dir.getName())) {
fileName = ff.Result;
dst = Paths.get(target_dir.getParentFile().getAbsolutePath(), fileName).toFile();
if (dst.exists()) {
2024-10-11 00:00:30 +03:00
Log.Writeln("Файл с именем " + Utils_.Brackets(fileName) + " уже существует");
2023-09-17 22:13:42 +03:00
return false;
}
if (target_dir.equals(project.Home)) {
Log.Writeln("Нельзя переименовывать домашнюю папку проекта.");
return false;
}
return true;
}
} else Log.Writeln("Папка не выделена.");
return false;
}
@Override
protected void performPreparation() throws Exception {
if (current)
Global.mainModule.getPass(PassCode.CloseCurrentFile).Do();
2023-09-17 22:13:42 +03:00
}
@Override
protected void body() throws Exception {
FileUtils.moveDirectory(target_dir, dst);
if (!dst.exists()) throw new PassException("Не удалось переименовать папку");
//теперь все файлы и подпапки которые были в этой папке должны быть переосмыслены.
Vector<DBProjectFile> to_rename = new Vector<>();
for (DBProjectFile file : project.db.files.Data.values()) {
if (Utils.isAnchestor(file.file, target_dir)) {
to_rename.add(file);
}
}
//так как имя первичный ключ приходится удалять их из бд проекта.
for (DBProjectFile file : to_rename) {
if (current && file.file.equals(old_current_file.file)) {
new_current_file = file;
}
file.CleanAll();
file.father.db.Delete(file);
file.file = Paths.get(dst.getAbsolutePath(), Utils.getRelativeAddress(file.file, target_dir)).toFile();
file.RefreshName();
file.father.db.Insert(file);
}
dst_node.setUserObject(dst);
for (int i = 0; i < dst_node.getChildCount(); ++i)
Utils.renameSubdirs_r((DefaultMutableTreeNode) dst_node.getChildAt(i), target_dir, dst);
}
@Override
protected void performDone() throws Exception {
Global.mainModule.getSapfor().ResetAllAnalyses();
Global.mainModule.set(Current.SelectedDirectory, dst);
2023-09-17 22:13:42 +03:00
if (current && new_current_file != null)
Global.mainModule.getPass(PassCode.OpenCurrentFile).Do(new_current_file);
2023-09-17 22:13:42 +03:00
}
@Override
protected void showDone() throws Exception {
UI.getMainWindow().getProjectWindow().getFilesTreeForm().getTree().RefreshNode(dst_node);
}
}