|
|
|
@ -85,28 +85,42 @@ public class TemplateUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void createAndOpenTemplate0(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { |
|
|
|
|
new SwingWorker<Void, Void>() { |
|
|
|
|
new SwingWorker<Boolean, Void>() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected Void doInBackground() throws Exception { |
|
|
|
|
protected Boolean doInBackground() throws Exception { |
|
|
|
|
// 读取模板数据
|
|
|
|
|
byte[] content = getTemplateData(createByEditingTemplate, oldPath); |
|
|
|
|
try (OutputStream out = file.asOutputStream()) { |
|
|
|
|
OutputStream out = null; |
|
|
|
|
try { |
|
|
|
|
// 加锁
|
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); |
|
|
|
|
boolean saveAsLock = WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); |
|
|
|
|
if (!saveAsLock) { |
|
|
|
|
// 加锁失败时,直接返回
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
out = file.asOutputStream(); |
|
|
|
|
out.write(content); |
|
|
|
|
} finally { |
|
|
|
|
// 解锁
|
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); |
|
|
|
|
try { |
|
|
|
|
if (out != null) { |
|
|
|
|
out.close(); |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
// 解锁
|
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void done() { |
|
|
|
|
try { |
|
|
|
|
get(); |
|
|
|
|
// 创建备份成功后
|
|
|
|
|
if (!get()) { |
|
|
|
|
throw new Exception("[RemoteDesign] back up template file failed"); |
|
|
|
|
} |
|
|
|
|
// 创建备份成功后,关闭原模板
|
|
|
|
|
doAfterCreateTemplate.run(); |
|
|
|
|
if (openNewTemplate) { |
|
|
|
|
DesignerContext.getDesignerFrame().openTemplate(file); |
|
|
|
@ -126,7 +140,7 @@ public class TemplateUtils {
|
|
|
|
|
* |
|
|
|
|
* @param readCurrentEditingTemplate 是否读取当前编辑模板 |
|
|
|
|
* @param path 模板路径 |
|
|
|
|
* @return 模板文件数据 |
|
|
|
|
* @return 模板文件数据 |
|
|
|
|
*/ |
|
|
|
|
private static byte[] getTemplateData(boolean readCurrentEditingTemplate, String path) throws Exception { |
|
|
|
|
byte[] content = new byte[0]; |
|
|
|
|