Browse Source

Merge pull request #12183 in DESIGN/design from release/11.0 to bugfix/11.0

* commit '65e3a0c43f47f85e5c6d015a56d1fa1490824ecb':
  REPORT-96012 移动文件夹到自己文件夹下,文件夹丢失
  REPORT-96012 移动文件夹到自己文件夹下,文件夹丢失 优化代码
  REPORT-96012 移动文件夹到自己文件夹下,文件夹丢失
bugfix/11.0
superman 2 years ago
parent
commit
144cff922d
  1. 22
      designer-base/src/main/java/com/fr/design/file/DefaultTemplateTreeDefineProcessor.java
  2. 40
      designer-base/src/main/java/com/fr/design/file/FileOperationHelper.java
  3. 49
      designer-base/src/test/java/com/fr/design/file/FileOperationHelperTest.java

22
designer-base/src/main/java/com/fr/design/file/DefaultTemplateTreeDefineProcessor.java

@ -28,7 +28,6 @@ import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils; import com.fr.stable.StringUtils;
import com.fr.stable.collections.CollectionUtils; import com.fr.stable.collections.CollectionUtils;
import com.fr.stable.project.ProjectConstants; import com.fr.stable.project.ProjectConstants;
import com.fr.workspace.WorkContext;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.JDialog; import javax.swing.JDialog;
@ -221,6 +220,7 @@ public class DefaultTemplateTreeDefineProcessor extends AbstractTemplateTreeDefi
/** /**
* 检测是否能够黏贴 * 检测是否能够黏贴
*
* @param treeNodeList * @param treeNodeList
* @return * @return
*/ */
@ -271,7 +271,7 @@ public class DefaultTemplateTreeDefineProcessor extends AbstractTemplateTreeDefi
} }
} }
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e,"Template paste failed.", e.getMessage()); FineLoggerFactory.getLogger().error(e, "Template paste failed.", e.getMessage());
FineJOptionPane.showConfirmDialog(null, FineJOptionPane.showConfirmDialog(null,
Toolkit.i18nText("Fine-Design_Basic_Paste_Failure"), Toolkit.i18nText("Fine-Design_Basic_Paste_Failure"),
Toolkit.i18nText("Fine-Design_Basic_Error"), Toolkit.i18nText("Fine-Design_Basic_Error"),
@ -433,6 +433,14 @@ public class DefaultTemplateTreeDefineProcessor extends AbstractTemplateTreeDefi
WARNING_MESSAGE); WARNING_MESSAGE);
return false; return false;
} }
// 检查移动的源文件夹是否为目标文件夹相同文件夹或子文件夹
if (FileOperationHelper.getInstance().isSubDirectoryOrSame(getFileTree().getSelectedTreeNodes(), getTargetFileNode())) {
FineJOptionPane.showMessageDialog(this,
Toolkit.i18nText("Fine-Design_Basic_Move_To_SubDirectory"),
Toolkit.i18nText("Fine-Design_Basic_Alert"),
WARNING_MESSAGE);
return false;
}
if (TemplateUtils.checkSelectedTemplateIsEditing()) { if (TemplateUtils.checkSelectedTemplateIsEditing()) {
return FineJOptionPane.showConfirmDialog(this, return FineJOptionPane.showConfirmDialog(this,
Toolkit.i18nText("Fine-Design_Basic_Template_Is_Editing"), Toolkit.i18nText("Fine-Design_Basic_Template_Is_Editing"),
@ -443,9 +451,7 @@ public class DefaultTemplateTreeDefineProcessor extends AbstractTemplateTreeDefi
} }
private boolean doMove() { private boolean doMove() {
FileNode fileNode = getDirTree().getSelectedFileNode(); FileNode fileNode = getTargetFileNode();
ExpandMutableTreeNode rootTreeNode = (ExpandMutableTreeNode) getDirTree().getModel().getRoot();
fileNode = fileNode == null ? (FileNode) rootTreeNode.getUserObject() : fileNode;
boolean moveSuccess = true; boolean moveSuccess = true;
try { try {
//待移动的文件可以有多个 //待移动的文件可以有多个
@ -465,6 +471,12 @@ public class DefaultTemplateTreeDefineProcessor extends AbstractTemplateTreeDefi
return moveSuccess; return moveSuccess;
} }
private FileNode getTargetFileNode() {
FileNode fileNode = getDirTree().getSelectedFileNode();
ExpandMutableTreeNode rootTreeNode = (ExpandMutableTreeNode) getDirTree().getModel().getRoot();
return fileNode == null ? (FileNode) rootTreeNode.getUserObject() : fileNode;
}
@Override @Override
public void dispose() { public void dispose() {
TemplateDirTreeSearchManager.getInstance().outOfSearchMode(); TemplateDirTreeSearchManager.getInstance().outOfSearchMode();

40
designer-base/src/main/java/com/fr/design/file/FileOperationHelper.java

@ -16,7 +16,10 @@ import com.fr.stable.StringUtils;
import com.fr.stable.project.ProjectConstants; import com.fr.stable.project.ProjectConstants;
import com.fr.workspace.WorkContext; import com.fr.workspace.WorkContext;
import com.fr.workspace.resource.ResourceIOException; import com.fr.workspace.resource.ResourceIOException;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import static javax.swing.JOptionPane.WARNING_MESSAGE; import static javax.swing.JOptionPane.WARNING_MESSAGE;
@ -32,6 +35,38 @@ public class FileOperationHelper {
return INSTANCE; return INSTANCE;
} }
/**
* 检查目标文件夹是否为多个源文件夹中的任一文件夹或者任一文件夹的子文件夹
*
* @param fileNodes 需移动的源文件
* @param targetNode 移动后的目标文件夹
* @return
*/
public boolean isSubDirectoryOrSame(@NotNull ExpandMutableTreeNode[] fileNodes, @NotNull FileNode targetNode) {
for (ExpandMutableTreeNode treeNode : fileNodes) {
FileNode sourceFileNode = (FileNode) treeNode.getUserObject();
if (isSubDirectoryOrSame(sourceFileNode, targetNode)) {
return true;
}
}
return false;
}
/**
* 检查目标文件夹是否为源文件夹或源文件夹的子文件夹
*
* @param sourceFileNode 需移动的源文件
* @param targetNode 移动后的目标文件夹
*/
public boolean isSubDirectoryOrSame(@NotNull FileNode sourceFileNode, @NotNull FileNode targetNode) {
if (!sourceFileNode.isDirectory() || !targetNode.isDirectory()) {
return false;
}
Path sourceDir = Paths.get(sourceFileNode.getEnvPath()).normalize();
Path targetDir = Paths.get(targetNode.getEnvPath()).normalize();
return targetDir.startsWith(sourceDir);
}
public String moveFile(FileNode sourceFileNode, String targetDir) { public String moveFile(FileNode sourceFileNode, String targetDir) {
String targetPath = copyFileAndVcs(sourceFileNode, targetDir); String targetPath = copyFileAndVcs(sourceFileNode, targetDir);
FileNodeFILE nodeFILE = new FileNodeFILE(sourceFileNode); FileNodeFILE nodeFILE = new FileNodeFILE(sourceFileNode);
@ -51,6 +86,7 @@ public class FileOperationHelper {
/** /**
* 拷贝文件的同时拷贝对应的版本控制文件 * 拷贝文件的同时拷贝对应的版本控制文件
*
* @param sourceFile 源文件或目录 * @param sourceFile 源文件或目录
* @param targetDir 目标目录 * @param targetDir 目标目录
* @return 复制后的目标文件的路径 * @return 复制后的目标文件的路径
@ -61,6 +97,7 @@ public class FileOperationHelper {
/** /**
* 只拷贝文件, 不拷贝对应的版本控制文件 * 只拷贝文件, 不拷贝对应的版本控制文件
*
* @param sourceFile 源文件或目录 * @param sourceFile 源文件或目录
* @param targetDir 目标目录 * @param targetDir 目标目录
* @return 复制后的目标文件的路径 * @return 复制后的目标文件的路径
@ -71,6 +108,7 @@ public class FileOperationHelper {
/** /**
* 检测节点是否被锁住了 * 检测节点是否被锁住了
*
* @param node 待检测节点 * @param node 待检测节点
* @param dNodes 没有锁住的节点集合 * @param dNodes 没有锁住的节点集合
* @param lNodes 锁住的节点集合 * @param lNodes 锁住的节点集合
@ -165,7 +203,7 @@ public class FileOperationHelper {
} else { } else {
if (!TemplateResourceManager.getResource().copy(sourcePath, targetPath)) { if (!TemplateResourceManager.getResource().copy(sourcePath, targetPath)) {
throw new ResourceIOException(String.format("copy file failed, from %s to %s", sourcePath, targetPath)); throw new ResourceIOException(String.format("copy file failed, from %s to %s", sourcePath, targetPath));
} else if (withCopyVcs){ } else if (withCopyVcs) {
sourcePath = sourcePath.replaceFirst(ProjectConstants.REPORTLETS_NAME, StringUtils.EMPTY); sourcePath = sourcePath.replaceFirst(ProjectConstants.REPORTLETS_NAME, StringUtils.EMPTY);
targetPath = targetPath.replaceFirst(ProjectConstants.REPORTLETS_NAME, StringUtils.EMPTY); targetPath = targetPath.replaceFirst(ProjectConstants.REPORTLETS_NAME, StringUtils.EMPTY);
VcsHelper.getInstance().moveVcs(sourcePath, targetPath); VcsHelper.getInstance().moveVcs(sourcePath, targetPath);

49
designer-base/src/test/java/com/fr/design/file/FileOperationHelperTest.java

@ -0,0 +1,49 @@
package com.fr.design.file;
import com.fr.design.gui.itree.refreshabletree.ExpandMutableTreeNode;
import com.fr.file.filetree.FileNode;
import org.junit.Assert;
import org.junit.Test;
/**
*
* @author Levy.Xie
* @version 11.0
* Created by Levy.Xie on 2023/05/23
*/
public class FileOperationHelperTest {
@Test
public void testIsSubDirectory() {
FileNode sourceNode1 = new FileNode("/usr/local/webroot/reportlets", true);
FileNode sourceNode2 = new FileNode("/usr/local/webroot/reportlets/demo", true);
FileNode sourceNode3 = new FileNode("/usr/local/webroot/reportlets/doc", true);
FileNode sourceNode4 = new FileNode("/usr/local/webroot/reportlets/doc/1.cpt", false);
FileNode sourceNode5 = new FileNode("/usr/local/webroot/reportlets/demo/test", true);
FileNode sourceNode6 = new FileNode("/usr/local/webroot/reportlets/demo/test", false);
FileNode sourceNode7 = new FileNode("/usr/local/webroot/reportlets/demo/test123", true);
FileNode sourceNode8 = new FileNode("/usr/local/webroot/reportlets/../reportlets/demo/test", true);
FileNode sourceNode9 = new FileNode("/usr/local/webroot/reportlets/../reportlets/demo/test/c", true);
FileNode targetNode = new FileNode("/usr/local/webroot/reportlets/demo/test", true);
Assert.assertTrue(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode1, targetNode));
Assert.assertTrue(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode2, targetNode));
Assert.assertFalse(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode3, targetNode));
Assert.assertFalse(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode4, targetNode));
Assert.assertTrue(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode5, targetNode));
Assert.assertFalse(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode6, targetNode));
Assert.assertFalse(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode7, targetNode));
Assert.assertTrue(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode8, targetNode));
Assert.assertFalse(FileOperationHelper.getInstance().isSubDirectoryOrSame(sourceNode9, targetNode));
ExpandMutableTreeNode treeNode1 = new ExpandMutableTreeNode(sourceNode1);
ExpandMutableTreeNode treeNode2 = new ExpandMutableTreeNode(sourceNode2);
ExpandMutableTreeNode treeNode3 = new ExpandMutableTreeNode(sourceNode3);
ExpandMutableTreeNode treeNode4 = new ExpandMutableTreeNode(sourceNode4);
Assert.assertTrue(FileOperationHelper.getInstance().isSubDirectoryOrSame(new ExpandMutableTreeNode[]{treeNode1, treeNode2, treeNode3}, targetNode));
Assert.assertFalse(FileOperationHelper.getInstance().isSubDirectoryOrSame(new ExpandMutableTreeNode[]{treeNode3, treeNode4}, targetNode));
}
}
Loading…
Cancel
Save