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

74 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 _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.Project.ChangeFilePass;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Files.FileType;
import _VisualDVM.Utils;
import _VisualDVM.Visual.Windows.FileNameForm;
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 (Utils_.ContainsCyrillic(fileName)) {
Log.Writeln_("Имя файла " + Utils_.Brackets(fileName) + " содержит кириллицу.");
return false;
}
if (Utils_.ContainsForbiddenName(fileName)) {
Log.Writeln_("Имя файла " + Utils_.Brackets(fileName)
+ " содержит запрещенные символы." +
"\n" + Utils_.printAllForbiddenCharacters());
return false;
}
} else {
(ff = new FileNameForm()).ShowDialog("Введите имя создаваемого файла");
fileName = ff.Result;
//тут имена фильтруются уже на этапе формы.
}
if (fileName != null) {
//->
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.equals(FileType.forbidden)) {
Log.Writeln_("Расширение " + Utils_.Brackets(Utils_.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);
Global.mainModule.getUI().getMainWindow().getProjectWindow().getFilesTreeForm().getTree().AddNode(parent_node,
dst_node = target.node = new DefaultMutableTreeNode(target)
);
}
enum Mode {
Dialog,
Import
}
}