You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
6.6 KiB
171 lines
6.6 KiB
package com.fr.design.utils; |
|
|
|
import com.fr.base.extension.FileExtension; |
|
import com.fr.design.file.HistoryTemplateListCache; |
|
import com.fr.design.file.TemplateTreePane; |
|
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.JTemplate; |
|
import com.fr.design.worker.save.SaveFailureHandler; |
|
import com.fr.file.FILE; |
|
import com.fr.file.FILEChooserPane; |
|
import com.fr.file.filetree.FileNode; |
|
import com.fr.file.filter.ChooseFileFilter; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.ArrayUtils; |
|
import com.fr.stable.CoreConstants; |
|
import com.fr.stable.ProductConstants; |
|
import com.fr.workspace.WorkContext; |
|
import com.fr.workspace.server.lock.TplOperator; |
|
|
|
import javax.swing.SwingWorker; |
|
import java.io.OutputStream; |
|
import java.util.List; |
|
import java.util.stream.Collectors; |
|
|
|
/** |
|
* @author hades |
|
* @version 11.0 |
|
* Created by hades on 2021/12/5 |
|
*/ |
|
public class TemplateUtils { |
|
|
|
/** |
|
* 创建新的模板文件并打开模板 |
|
* @param prefix 模板文件名称前缀 |
|
* @param file 模板文件 |
|
* @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 |
|
* 为true时以CurrentEditingTemplate为准创建新模板 |
|
* 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 |
|
* @param openNewTemplate 是否需要在创建后打开模板 |
|
*/ |
|
public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate) { |
|
String fileName = file.getName(); |
|
String oldPath = file.getPath(); |
|
int indexOfLastDot = fileName.lastIndexOf(CoreConstants.DOT); |
|
if (indexOfLastDot < 0) { |
|
return; |
|
} |
|
String suffix = fileName.substring(indexOfLastDot + 1); |
|
FILEChooserPane fileChooserPane = FILEChooserPane.getInstance(true, true); |
|
fileChooserPane.setFileNameTextField(prefix + fileName, suffix); |
|
FileExtension fileExtension = FileExtension.parse(suffix); |
|
fileChooserPane.addChooseFILEFilter(new ChooseFileFilter(fileExtension, ProductConstants.APP_NAME + Toolkit.i18nText("Fine-Design_Report_Template_File"))); |
|
fileChooserPane.disableFileNameTextFiled(); |
|
int result = fileChooserPane.showSaveDialog(DesignerContext.getDesignerFrame(), suffix); |
|
fileChooserPane.enableFileNameTextFiled(); |
|
|
|
if (isCancel(result)) { |
|
return; |
|
} |
|
|
|
if (isOk(result)) { |
|
file = fileChooserPane.getSelectedFILE(); |
|
_createAndOpenTemplate(file, oldPath, createByEditingTemplate, openNewTemplate); |
|
} |
|
} |
|
|
|
|
|
private static void _createAndOpenTemplate(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate){ |
|
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 { |
|
// 加锁 |
|
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()); |
|
} |
|
|
|
} |
|
return null; |
|
} |
|
|
|
@Override |
|
protected void done() { |
|
try { |
|
get(); |
|
if (openNewTemplate) { |
|
DesignerContext.getDesignerFrame().openTemplate(file); |
|
} |
|
// 备份成功刷新下目录树 展示出来备份的模板 |
|
TemplateTreePane.getInstance().refresh(); |
|
} catch (Exception e) { |
|
SaveFailureHandler.getInstance().process(e); |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
} |
|
}.execute(); |
|
|
|
} |
|
|
|
private static boolean isCancel(int result) { |
|
return result == FILEChooserPane.CANCEL_OPTION || |
|
result == FILEChooserPane.JOPTIONPANE_CANCEL_OPTION; |
|
} |
|
|
|
private static boolean isOk(int result) { |
|
return result == FILEChooserPane.JOPTIONPANE_OK_OPTION || |
|
result == FILEChooserPane.OK_OPTION; |
|
} |
|
|
|
public static String createLockeTemplatedName(JTemplate<?, ?> template, String name) { |
|
int index = name.lastIndexOf(CoreConstants.DOT); |
|
if (index < 0 || !template.isForbidden()) { |
|
return name; |
|
} |
|
return name.substring(0, index) + Toolkit.i18nText("Fine_Design_Template_Has_Been_Locked") + name.substring(index); |
|
} |
|
|
|
/** |
|
* 检测当前所选模板是否已经被打开 |
|
* |
|
* @return |
|
*/ |
|
public static boolean checkSelectedTemplateIsEditing() { |
|
return checkTemplateIsEditing(TemplateTreePane.getInstance().getTemplateFileTree().getSelectedTreeNodes()); |
|
} |
|
|
|
/** |
|
* 检测指定模板节点是否已经被打开 |
|
* |
|
* @param treeNodes |
|
* @return |
|
*/ |
|
public static boolean checkTemplateIsEditing(ExpandMutableTreeNode[] treeNodes) { |
|
if (ArrayUtils.isEmpty(treeNodes)) { |
|
return false; |
|
} |
|
List<JTemplate<?, ?>> jTemplates = HistoryTemplateListCache.getInstance().getHistoryList(); |
|
List<String> openedFile = jTemplates.stream().map(JTemplate::getPath).collect(Collectors.toList()); |
|
for (ExpandMutableTreeNode treeNode : treeNodes) { |
|
String templatePath = ((FileNode) (treeNode.getUserObject())).getEnvPath(); |
|
if (openedFile.contains(templatePath)) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
}
|
|
|