|
|
@ -16,6 +16,7 @@ import com.fr.log.FineLoggerFactory; |
|
|
|
import com.fr.stable.ArrayUtils; |
|
|
|
import com.fr.stable.ArrayUtils; |
|
|
|
import com.fr.stable.CoreConstants; |
|
|
|
import com.fr.stable.CoreConstants; |
|
|
|
import com.fr.stable.ProductConstants; |
|
|
|
import com.fr.stable.ProductConstants; |
|
|
|
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
import com.fr.workspace.WorkContext; |
|
|
|
import com.fr.workspace.WorkContext; |
|
|
|
import com.fr.workspace.server.lock.TplOperator; |
|
|
|
import com.fr.workspace.server.lock.TplOperator; |
|
|
|
|
|
|
|
|
|
|
@ -33,6 +34,7 @@ public class TemplateUtils { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 创建新的模板文件并打开模板 |
|
|
|
* 创建新的模板文件并打开模板 |
|
|
|
|
|
|
|
* |
|
|
|
* @param prefix 模板文件名称前缀 |
|
|
|
* @param prefix 模板文件名称前缀 |
|
|
|
* @param file 模板文件 |
|
|
|
* @param file 模板文件 |
|
|
|
* @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 |
|
|
|
* @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 |
|
|
@ -41,6 +43,21 @@ public class TemplateUtils { |
|
|
|
* @param openNewTemplate 是否需要在创建后打开模板 |
|
|
|
* @param openNewTemplate 是否需要在创建后打开模板 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean 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 fileName = file.getName(); |
|
|
|
String oldPath = file.getPath(); |
|
|
|
String oldPath = file.getPath(); |
|
|
|
int indexOfLastDot = fileName.lastIndexOf(CoreConstants.DOT); |
|
|
|
int indexOfLastDot = fileName.lastIndexOf(CoreConstants.DOT); |
|
|
@ -62,31 +79,26 @@ public class TemplateUtils { |
|
|
|
|
|
|
|
|
|
|
|
if (isOk(result)) { |
|
|
|
if (isOk(result)) { |
|
|
|
file = fileChooserPane.getSelectedFILE(); |
|
|
|
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>() { |
|
|
|
new SwingWorker<Boolean, Void>() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected Void doInBackground() throws Exception { |
|
|
|
protected Boolean doInBackground() throws Exception { |
|
|
|
byte[] content = new byte[0]; |
|
|
|
// 读取模板数据
|
|
|
|
if (createByEditingTemplate) { |
|
|
|
byte[] content = getTemplateData(createByEditingTemplate, oldPath); |
|
|
|
// 从当前编辑模板中生成备份文件
|
|
|
|
|
|
|
|
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; |
|
|
|
OutputStream out = null; |
|
|
|
try { |
|
|
|
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 = file.asOutputStream(); |
|
|
|
out.write(content); |
|
|
|
out.write(content); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
@ -98,15 +110,18 @@ public class TemplateUtils { |
|
|
|
// 解锁
|
|
|
|
// 解锁
|
|
|
|
WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); |
|
|
|
WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(file.getPath()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void done() { |
|
|
|
protected void done() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
get(); |
|
|
|
if (!get()) { |
|
|
|
|
|
|
|
throw new Exception("[RemoteDesign] back up template file failed"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 创建备份成功后,关闭原模板
|
|
|
|
|
|
|
|
doAfterCreateTemplate.run(); |
|
|
|
if (openNewTemplate) { |
|
|
|
if (openNewTemplate) { |
|
|
|
DesignerContext.getDesignerFrame().openTemplate(file); |
|
|
|
DesignerContext.getDesignerFrame().openTemplate(file); |
|
|
|
} |
|
|
|
} |
|
|
@ -118,7 +133,31 @@ public class TemplateUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}.execute(); |
|
|
|
}.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) { |
|
|
|
private static boolean isCancel(int result) { |
|
|
|