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

76 lines
2.9 KiB
Java
Raw Normal View History

package Visual_DVM_2021.Passes.All;
2024-10-07 14:22:52 +03:00
import Common.Utils.CommonUtils;
import Common_old.Constants;
import Common_old.Current;
import Common_old.UI.UI;
import Common_old.UI.Windows.Dialog.Text.FileNameForm;
import Common_old.Utils.Utils;
2023-09-17 22:13:42 +03:00
import ProjectData.Files.DBProjectFile;
import ProjectData.Files.FileType;
import Visual_DVM_2021.Passes.ChangeFilePass;
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;
public class AddFile extends ChangeFilePass<DBProjectFile> {
Mode mode;
File src;
@Override
protected boolean canStart(Object... args) throws Exception {
resetArgs();
mode = Mode.Dialog;
src = null;
if (args.length > 0) {
src = (File) args[0];
mode = Mode.Import;
fileName = src.getName();
2024-10-07 14:22:52 +03:00
if (CommonUtils.ContainsCyrillic(fileName)) {
Log.Writeln_("Имя файла " + CommonUtils.Brackets(fileName) + " содержит кириллицу.");
2023-09-17 22:13:42 +03:00
return false;
}
if (Utils.ContainsForbiddenName(fileName)) {
2024-10-07 14:22:52 +03:00
Log.Writeln_("Имя файла " + CommonUtils.Brackets(fileName)
2023-09-17 22:13:42 +03:00
+ " содержит запрещенные символы." +
2023-09-29 21:46:08 +03:00
"\n" + Constants.all_forbidden_characters_string);
2023-09-17 22:13:42 +03:00
return false;
}
} else {
(ff = new FileNameForm()).ShowDialog("Введите имя создаваемого файла");
fileName = ff.Result;
//тут имена фильтруются уже на этапе формы.
}
if (fileName != null) {
//->
parent_node = Current.getProjectCurrentParentNode();
target_dir = (File) parent_node.getUserObject();
//->
dst = Paths.get(target_dir.getAbsolutePath(), fileName).toFile();
if (dst.exists()) {
2024-10-07 14:22:52 +03:00
Log.Writeln_("Файл с именем " + CommonUtils.Brackets(fileName) + " уже существует");
2023-09-17 22:13:42 +03:00
return false;
}
target = new DBProjectFile(dst, project);
if (target.fileType.equals(FileType.forbidden)) {
2024-10-07 14:22:52 +03:00
Log.Writeln_("Расширение " + CommonUtils.Brackets(CommonUtils.getExtension(dst)) + " недопустимо");
2023-09-17 22:13:42 +03:00
return false;
}
return true;
}
return false;
}
@Override
protected void body() throws Exception {
if (mode == Mode.Dialog) Utils.WriteToFile(target.file, "");
else FileUtils.copyFile(src, dst);
project.db.Insert(target);
UI.getMainWindow().getProjectWindow().getFilesTreeForm().getTree().AddNode(parent_node,
dst_node = target.node = new DefaultMutableTreeNode(target)
);
}
enum Mode {
Dialog,
Import
}
}