Browse Source
Merge in DESIGN/design from ~ROGER.CHEN/design:release/11.0 to release/11.0 * commit '400df5f2663934bbd6cdb3d50673c8b548563aff': 代码规范及右键增加定位模板功能 添加注释 REPORT-79128 触发两次粘贴,页面卡死,等不卡了在看目录层级,粘贴了好多层release/11.0
Roger.Chen
2 years ago
12 changed files with 372 additions and 243 deletions
@ -0,0 +1,195 @@
|
||||
package com.fr.design.file; |
||||
|
||||
import com.fr.design.dialog.FineJOptionPane; |
||||
import com.fr.design.gui.itree.refreshabletree.ExpandMutableTreeNode; |
||||
import com.fr.design.i18n.Toolkit; |
||||
import com.fr.design.mainframe.DesignerContext; |
||||
import com.fr.design.mainframe.DesignerFrameFileDealerPane; |
||||
import com.fr.design.mainframe.vcs.common.VcsHelper; |
||||
import com.fr.file.FileNodeFILE; |
||||
import com.fr.file.filetree.FileNode; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.ArrayUtils; |
||||
import com.fr.stable.StableUtils; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.project.ProjectConstants; |
||||
import com.fr.workspace.WorkContext; |
||||
import com.fr.workspace.resource.ResourceIOException; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
import static javax.swing.JOptionPane.WARNING_MESSAGE; |
||||
|
||||
/** |
||||
* 文件操作的辅助类 |
||||
*/ |
||||
public class FileOperationHelper { |
||||
|
||||
private static final FileOperationHelper INSTANCE = new FileOperationHelper(); |
||||
|
||||
public static FileOperationHelper getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
public String moveFile(FileNode sourceFileNode, String targetDir) { |
||||
String targetPath = copyFileAndVcs(sourceFileNode, targetDir); |
||||
FileNodeFILE nodeFILE = new FileNodeFILE(sourceFileNode); |
||||
if (nodeFILE.exists()) { |
||||
if (TemplateResourceManager.getResource().delete(nodeFILE)) { |
||||
HistoryTemplateListCache.getInstance().deleteFile(nodeFILE); |
||||
FineLoggerFactory.getLogger().info("template {} move to {} success.", sourceFileNode.getEnvPath(), targetDir); |
||||
} else { |
||||
//删除失败,将复制过去的文件删掉
|
||||
TemplateResourceManager.getResource().delete(new FileNodeFILE(targetPath)); |
||||
FineLoggerFactory.getLogger().error("template {} move to {} failed.", sourceFileNode.getEnvPath(), targetDir); |
||||
targetPath = StringUtils.EMPTY; |
||||
} |
||||
} |
||||
return targetPath; |
||||
} |
||||
|
||||
/** |
||||
* 拷贝文件的同时拷贝对应的版本控制文件 |
||||
* @param sourceFile 源文件或目录 |
||||
* @param targetDir 目标目录 |
||||
* @return 复制后的目标文件的路径 |
||||
*/ |
||||
public String copyFileAndVcs(FileNode sourceFile, String targetDir) { |
||||
return copyFile(sourceFile, targetDir, true); |
||||
} |
||||
|
||||
/** |
||||
* 只拷贝文件, 不拷贝对应的版本控制文件 |
||||
* @param sourceFile 源文件或目录 |
||||
* @param targetDir 目标目录 |
||||
* @return 复制后的目标文件的路径 |
||||
*/ |
||||
public String copyFile(FileNode sourceFile, String targetDir) { |
||||
return copyFile(sourceFile, targetDir, false); |
||||
} |
||||
|
||||
/** |
||||
* 检测节点是否被锁住了 |
||||
* @param node 待检测节点 |
||||
* @param dNodes 没有锁住的节点集合 |
||||
* @param lNodes 锁住的节点集合 |
||||
* @return 是否存在被锁住的文件 |
||||
*/ |
||||
public boolean checkFreeOrLock(ExpandMutableTreeNode node, ArrayList<ExpandMutableTreeNode> dNodes, ArrayList<ExpandMutableTreeNode> lNodes) { |
||||
// 自己没锁
|
||||
boolean selfEmptyLock = false; |
||||
Object userObj = node.getUserObject(); |
||||
if (userObj instanceof FileNode) { |
||||
String lock = ((FileNode) userObj).getLock(); |
||||
selfEmptyLock = lock == null || ((FileNode) userObj).getUserID().equals(lock); |
||||
} |
||||
|
||||
if (node.isLeaf()) { |
||||
if (selfEmptyLock) { |
||||
dNodes.add(node); |
||||
} else { |
||||
lNodes.add(node); |
||||
} |
||||
return selfEmptyLock; |
||||
} |
||||
|
||||
return checkChildNode(node, dNodes, lNodes, selfEmptyLock); |
||||
} |
||||
|
||||
private boolean checkChildNode(ExpandMutableTreeNode node, ArrayList<ExpandMutableTreeNode> dNodes, ArrayList<ExpandMutableTreeNode> lNodes, boolean selfEmptyLock) { |
||||
ExpandMutableTreeNode[] children = TemplateTreePane.getInstance().getTemplateFileTree().loadChildTreeNodes(node); |
||||
|
||||
boolean childrenEmptyLock = true; |
||||
|
||||
for (ExpandMutableTreeNode child : children) { |
||||
childrenEmptyLock = checkFreeOrLock(child, dNodes, lNodes) && childrenEmptyLock; |
||||
} |
||||
|
||||
boolean emptyLock = childrenEmptyLock && selfEmptyLock; |
||||
if (emptyLock) { |
||||
dNodes.add(node); |
||||
} else { |
||||
lNodes.add(node); |
||||
} |
||||
return emptyLock; |
||||
} |
||||
|
||||
private String copyFile(FileNode sourceFile, String targetDir, boolean withCopyVcs) { |
||||
String name = getNoRepeatedName4Paste(targetDir, sourceFile.getName()); |
||||
String targetFile = StableUtils.pathJoin(targetDir, name); |
||||
if (sourceFile.isDirectory()) { |
||||
copyDir(sourceFile.getEnvPath(), targetFile, withCopyVcs); |
||||
} else { |
||||
copyFile(sourceFile.getEnvPath(), targetFile, withCopyVcs); |
||||
} |
||||
return targetFile; |
||||
} |
||||
|
||||
private void copyDir(String sourceDir, String targetDir, boolean withCopyVcs) { |
||||
FileNode[] fileNodes = TemplateTreePane.getInstance().getTemplateFileTree().listFile(sourceDir); |
||||
if (ArrayUtils.isEmpty(fileNodes)) { |
||||
//空目录:相当于新建一个目录
|
||||
DesignerFrameFileDealerPane.getInstance().getSelectedOperation().mkdir(targetDir); |
||||
return; |
||||
} |
||||
for (FileNode fileNode : fileNodes) { |
||||
if (fileNode.isDirectory()) { |
||||
copyDir(StableUtils.pathJoin(fileNode.getParent(), fileNode.getName()), StableUtils.pathJoin(targetDir, fileNode.getName()), withCopyVcs); |
||||
} else { |
||||
copyFile(StableUtils.pathJoin(sourceDir, fileNode.getName()), StableUtils.pathJoin(targetDir, fileNode.getName()), withCopyVcs); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void copyFile(String sourcePath, String targetPath, boolean withCopyVcs) { |
||||
//检查源文件是不是还存在
|
||||
if (!WorkContext.getWorkResource().exist(sourcePath)) { |
||||
FineJOptionPane.showMessageDialog(DesignerContext.getDesignerFrame(), |
||||
Toolkit.i18nText("Fine-Design_Basic_Source_File_Not_Exist", sourcePath), |
||||
Toolkit.i18nText("Fine-Design_Basic_Alert"), |
||||
WARNING_MESSAGE); |
||||
} else { |
||||
if (!TemplateResourceManager.getResource().copy(sourcePath, targetPath)) { |
||||
throw new ResourceIOException(String.format("copy file failed, from %s to %s", sourcePath, targetPath)); |
||||
} else if (withCopyVcs){ |
||||
sourcePath = sourcePath.replaceFirst(ProjectConstants.REPORTLETS_NAME, StringUtils.EMPTY); |
||||
targetPath = targetPath.replaceFirst(ProjectConstants.REPORTLETS_NAME, StringUtils.EMPTY); |
||||
VcsHelper.getInstance().moveVcs(sourcePath, targetPath); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 重名处理 |
||||
* |
||||
* @param targetDir |
||||
* @param oldName |
||||
* @return |
||||
*/ |
||||
private String getNoRepeatedName4Paste(String targetDir, String oldName) { |
||||
while (isNameRepeaded(targetDir, oldName)) { |
||||
int index = oldName.lastIndexOf("."); |
||||
if (index > 0) { |
||||
String oName = oldName.substring(0, index); |
||||
oName = oName + Toolkit.i18nText("Fine-Design_Table_Data_Copy_Of_Table_Data"); |
||||
oldName = oName.concat(oldName.substring(index)); |
||||
} else { |
||||
//目录重名
|
||||
oldName = oldName + Toolkit.i18nText("Fine-Design_Table_Data_Copy_Of_Table_Data"); |
||||
} |
||||
} |
||||
return oldName; |
||||
} |
||||
|
||||
private boolean isNameRepeaded(String targetDir, String name) { |
||||
FileNode[] fileNodes = TemplateTreePane.getInstance().getTemplateFileTree().listFile(targetDir); |
||||
for (int i = 0; i < fileNodes.length; i++) { |
||||
if (ComparatorUtils.equals(name, fileNodes[i].getName())) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue