Files
VisualSapfor/src/Visual_DVM_2021/Passes/All/AddFile.java
2024-10-07 22:04:09 +03:00

76 lines
2.9 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.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;
import ProjectData.Files.DBProjectFile;
import ProjectData.Files.FileType;
import Visual_DVM_2021.Passes.ChangeFilePass;
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();
if (CommonUtils.ContainsCyrillic(fileName)) {
Log.Writeln_("Имя файла " + CommonUtils.Brackets(fileName) + " содержит кириллицу.");
return false;
}
if (CommonUtils.ContainsForbiddenName(fileName)) {
Log.Writeln_("Имя файла " + CommonUtils.Brackets(fileName)
+ " содержит запрещенные символы." +
"\n" + CommonUtils.printAllForbiddenCharacters());
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()) {
Log.Writeln_("Файл с именем " + CommonUtils.Brackets(fileName) + " уже существует");
return false;
}
target = new DBProjectFile(dst, project);
if (target.fileType.equals(FileType.forbidden)) {
Log.Writeln_("Расширение " + CommonUtils.Brackets(CommonUtils.getExtension(dst)) + " недопустимо");
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
}
}