|
|
|
@ -16,6 +16,7 @@ import com.fr.log.FineLoggerFactory;
|
|
|
|
|
import com.fr.stable.ArrayUtils; |
|
|
|
|
import com.fr.stable.CoreConstants; |
|
|
|
|
import com.fr.stable.ProductConstants; |
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
|
import com.fr.workspace.WorkContext; |
|
|
|
|
import com.fr.workspace.server.lock.TplOperator; |
|
|
|
|
|
|
|
|
@ -33,6 +34,7 @@ public class TemplateUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建新的模板文件并打开模板 |
|
|
|
|
* |
|
|
|
|
* @param prefix 模板文件名称前缀 |
|
|
|
|
* @param file 模板文件 |
|
|
|
|
* @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 |
|
|
|
@ -41,6 +43,21 @@ public class TemplateUtils {
|
|
|
|
|
* @param openNewTemplate 是否需要在创建后打开模板 |
|
|
|
|
*/ |
|
|
|
|
public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate) { |
|
|
|
|
createAndOpenTemplate(prefix, file, createByEditingTemplate, openNewTemplate, () -> {}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建新的模板文件并打开模板,并在创建备份模板成功后执行doAfterCreate |
|
|
|
|
* |
|
|
|
|
* @param prefix 模板文件名称前缀 |
|
|
|
|
* @param file 模板文件 |
|
|
|
|
* @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 |
|
|
|
|
* 为true时以CurrentEditingTemplate为准创建新模板 |
|
|
|
|
* 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 |
|
|
|
|
* @param openNewTemplate 是否需要在创建后打开模板 |
|
|
|
|
* @param doAfterCreateTemplate 创建备份模板成功后调用 |
|
|
|
|
*/ |
|
|
|
|
public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { |
|
|
|
|
String fileName = file.getName(); |
|
|
|
|
String oldPath = file.getPath(); |
|
|
|
|
int indexOfLastDot = fileName.lastIndexOf(CoreConstants.DOT); |
|
|
|
@ -62,43 +79,25 @@ public class TemplateUtils {
|
|
|
|
|
|
|
|
|
|
if (isOk(result)) { |
|
|
|
|
file = fileChooserPane.getSelectedFILE(); |
|
|
|
|
_createAndOpenTemplate(file, oldPath, createByEditingTemplate, openNewTemplate); |
|
|
|
|
createAndOpenTemplate0(file, oldPath, createByEditingTemplate, openNewTemplate, doAfterCreateTemplate); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void _createAndOpenTemplate(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate){ |
|
|
|
|
private static void createAndOpenTemplate0(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { |
|
|
|
|
new SwingWorker<Void, Void>() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected Void doInBackground() throws Exception { |
|
|
|
|
byte[] content = new byte[0]; |
|
|
|
|
if (createByEditingTemplate) { |
|
|
|
|
// 从当前编辑模板中生成备份文件
|
|
|
|
|
JTemplate<?, ?> template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
|
|
|
|
content = template.exportData(); |
|
|
|
|
} else { |
|
|
|
|
content = WorkContext.getWorkResource().readFully(oldPath); |
|
|
|
|
} |
|
|
|
|
if (ArrayUtils.isEmpty(content)) { |
|
|
|
|
throw new Exception(oldPath + " content is empty" ); |
|
|
|
|
} |
|
|
|
|
OutputStream out = null; |
|
|
|
|
try { |
|
|
|
|
// 读取模板数据
|
|
|
|
|
byte[] content = getTemplateData(createByEditingTemplate, oldPath); |
|
|
|
|
try(OutputStream out = file.asOutputStream()) { |
|
|
|
|
// 加锁
|
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); |
|
|
|
|
out = file.asOutputStream(); |
|
|
|
|
out.write(content); |
|
|
|
|
} finally { |
|
|
|
|
try { |
|
|
|
|
if (out != null) { |
|
|
|
|
out.close(); |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
// 解锁
|
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 解锁
|
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
@ -107,6 +106,8 @@ public class TemplateUtils {
|
|
|
|
|
protected void done() { |
|
|
|
|
try { |
|
|
|
|
get(); |
|
|
|
|
// 创建备份成功后
|
|
|
|
|
doAfterCreateTemplate.run(); |
|
|
|
|
if (openNewTemplate) { |
|
|
|
|
DesignerContext.getDesignerFrame().openTemplate(file); |
|
|
|
|
} |
|
|
|
@ -118,7 +119,30 @@ public class TemplateUtils {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}.execute(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 读取模板文件数据 |
|
|
|
|
* |
|
|
|
|
* @param readCurrentEditingTemplate 是否读取当前编辑模板 |
|
|
|
|
* @param path 模板路径 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private static byte[] getTemplateData(boolean readCurrentEditingTemplate, String path) throws Exception { |
|
|
|
|
byte[] content = new byte[0]; |
|
|
|
|
if (readCurrentEditingTemplate) { |
|
|
|
|
// 从当前编辑模板中生成备份文件
|
|
|
|
|
JTemplate<?, ?> template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
|
|
|
|
if (JTemplate.isValid(template)) { |
|
|
|
|
content = template.exportData(); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
content = WorkContext.getWorkResource().readFully(path); |
|
|
|
|
} |
|
|
|
|
if (ArrayUtils.isEmpty(content)) { |
|
|
|
|
throw new Exception(StringUtils.messageFormat("{} content is empty", path)); |
|
|
|
|
} |
|
|
|
|
return content; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static boolean isCancel(int result) { |
|
|
|
|