|
|
|
@ -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 { |
|
|
|
|
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); |
|
|
|
|