diff --git a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java index f3d062946d..9b426b5c87 100644 --- a/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java +++ b/designer-base/src/main/java/com/fr/design/utils/TemplateUtils.java @@ -24,6 +24,8 @@ import org.jetbrains.annotations.Nullable; import javax.swing.SwingWorker; import java.io.OutputStream; import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -44,7 +46,7 @@ public class TemplateUtils { * @param openNewTemplate 是否需要在创建后打开模板 */ public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate) { - createAndOpenTemplate(prefix, file, createByEditingTemplate, openNewTemplate, () -> {}); + createAndOpenTemplate(prefix, file, createByEditingTemplate, openNewTemplate, null); } /** @@ -66,15 +68,33 @@ public class TemplateUtils { Runnable doAfterCreateTemplate; //判断一下要保存的文件是否已打开 int index = HistoryTemplateListCache.getInstance().contains(file); - if (index == -1) { - doAfterCreateTemplate = () -> {}; - } else { - JTemplate template = HistoryTemplateListCache.getInstance().getHistoryList().get(index); - doAfterCreateTemplate = () -> HistoryTemplateListCache.getInstance().closeSelectedReport(template); + JTemplate template = null; + if (index != -1) { + template = HistoryTemplateListCache.getInstance().getHistoryList().get(index); } - createAndOpenTemplate0(file, oldPath, createByEditingTemplate, openNewTemplate, doAfterCreateTemplate); + createAndOpenTemplate0(file, oldPath, createByEditingTemplate, openNewTemplate, template); } + /** + * 创建新的模板文件并打开模板,并在创建备份模板成功后执行doAfterCreate + * + * @param prefix 模板文件名称前缀 + * @param file 模板文件 + * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 + * 为true时以CurrentEditingTemplate为准创建新模板 + * 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 + * @param openNewTemplate 是否需要在创建后打开模板 + * @param template 备份成功后需要关闭的模板 + */ + public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate, @Nullable JTemplate template) { + String oldPath = file.getPath(); + file = getSavedFile(prefix, file); + if (file == null) { + return; + } + + createAndOpenTemplate0(file, oldPath, createByEditingTemplate, openNewTemplate, template); + } /** * 返回值可以为null, 为null表示没有点击保存按钮或者传递进来的文件file本身不满足格式要求 @@ -111,31 +131,17 @@ public class TemplateUtils { } /** - * 创建新的模板文件并打开模板,并在创建备份模板成功后执行doAfterCreate + * 生成备份模板 * - * @param prefix 模板文件名称前缀 - * @param file 模板文件 + * @param file saveAs的模板文件 * @param createByEditingTemplate 是否根据 当前编辑模板 来创建新模板 * 为true时以CurrentEditingTemplate为准创建新模板 * 为false时以传入的File文件为准创建新模板,此文件可以不是编辑状态 - * @param openNewTemplate 是否需要在创建后打开模板 - * @param doAfterCreateTemplate 创建备份模板成功后调用 + * @param oldPath 被saveAs的文件路径 */ - public static void createAndOpenTemplate(String prefix, FILE file, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { - String oldPath = file.getPath(); - file = getSavedFile(prefix, file); - if (file == null) { - return; - } - - createAndOpenTemplate0(file, oldPath, createByEditingTemplate, openNewTemplate, doAfterCreateTemplate); - } - - private static void createAndOpenTemplate0(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate, Runnable doAfterCreateTemplate) { - new SwingWorker() { - - @Override - protected Boolean doInBackground() throws Exception { + private static CompletableFuture createTemplate(FILE file, String oldPath, boolean createByEditingTemplate) { + return CompletableFuture.supplyAsync(() -> { + try { // 读取模板数据 byte[] content = getTemplateData(createByEditingTemplate, oldPath); OutputStream out = null; @@ -144,7 +150,7 @@ public class TemplateUtils { boolean saveAsLock = WorkContext.getCurrent().get(TplOperator.class).saveAs(file.getPath()); if (!saveAsLock) { // 加锁失败时,直接返回 - return false; + throw new RuntimeException("[RemoteDesign] back up template file failed"); } out = file.asOutputStream(); out.write(content); @@ -159,20 +165,43 @@ public class TemplateUtils { } } return true; + } catch (Exception e) { + SaveFailureHandler.getInstance().process(e); + FineLoggerFactory.getLogger().error(e.getMessage(), e); + return false; } + }); + } + /** + * 打开saveAs的新模板并关闭指定的旧模板 + * + * @param file 模板文件 + * @param openNewTemplate 是否需要打开新模板 + * @param template 需要关闭的模板 + */ + private static void openNewTemplateAndCloseOldTemplate(FILE file, boolean openNewTemplate, @Nullable JTemplate template) { + new SwingWorker() { + @Override + protected Boolean doInBackground() throws Exception { + if (JTemplate.isValid(template)) { + //给要关闭的模板解锁 + WorkContext.getCurrent().get(TplOperator.class).closeAndFreeFile(template.getPath()); + return true; + } + return false; + } @Override protected void done() { try { - if (!get()) { - throw new Exception("[RemoteDesign] back up template file failed"); + if (get()) { + //模板释放锁成功后关闭该模板 + HistoryTemplateListCache.getInstance().closeSelectedReport(template); } - // 创建备份成功后,关闭原模板 - doAfterCreateTemplate.run(); if (openNewTemplate) { DesignerContext.getDesignerFrame().openTemplate(file); } - // 备份成功刷新下目录树 展示出来备份的模板 + // 刷新下目录树 TemplateTreePane.getInstance().refresh(); } catch (Exception e) { SaveFailureHandler.getInstance().process(e); @@ -182,6 +211,15 @@ public class TemplateUtils { }.execute(); } + private static void createAndOpenTemplate0(FILE file, String oldPath, boolean createByEditingTemplate, boolean openNewTemplate, @Nullable JTemplate template) { + createTemplate(file, oldPath, createByEditingTemplate).thenApply((Function) aBoolean -> { + if (aBoolean) { + openNewTemplateAndCloseOldTemplate(file, openNewTemplate, template); + } + return null; + }); + } + /** * 读取模板文件数据 * diff --git a/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java b/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java index 42ef95978a..9ab897d7e8 100644 --- a/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java +++ b/designer-base/src/main/java/com/fr/design/worker/save/SaveFailureHandler.java @@ -119,8 +119,8 @@ public class SaveFailureHandler implements ThrowableHandler { new FileNodeFILE(new FileNode(template.getPath(), false)), true, true, - // 创建并打开备份模板后,关闭原模板,无需释放原模板锁(因为已经被超管手动清除了) - () -> HistoryTemplateListCache.getInstance().closeSelectedReport(template)); + //另存之后需要关闭的模板 + template); } } } @@ -141,8 +141,8 @@ public class SaveFailureHandler implements ThrowableHandler { new FileNodeFILE(new FileNode(template.getPath(), false)), true, true, - // 创建并打开备份模板后,关闭原模板,无需释放原模板锁(锁定信息不一致 = 此用户模板锁信息已被清除) - () -> HistoryTemplateListCache.getInstance().closeSelectedReport(template)); + //另存之后需要关闭的模板 + template); } } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/SheetNameTabPane.java b/designer-realize/src/main/java/com/fr/design/mainframe/SheetNameTabPane.java index c8c1b7172e..f672b9a232 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/SheetNameTabPane.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/SheetNameTabPane.java @@ -1,30 +1,7 @@ package com.fr.design.mainframe; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; -import java.awt.event.ComponentListener; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; -import java.awt.geom.GeneralPath; -import java.awt.geom.Path2D; -import java.util.ArrayList; -import java.util.List; - -import javax.swing.Icon; -import javax.swing.JComponent; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JPopupMenu; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; - import com.fr.base.BaseUtils; import com.fr.base.GraphHelper; - import com.fr.base.svg.IconUtils; import com.fr.base.theme.ReportTheme; import com.fr.base.theme.TemplateTheme; @@ -44,7 +21,6 @@ import com.fr.design.roleAuthority.RolesAlreadyEditedPane; import com.fr.design.utils.gui.GUICoreUtils; import com.fr.design.utils.gui.GUIPaintUtils; import com.fr.general.ComparatorUtils; - import com.fr.general.IOUtils; import com.fr.main.impl.WorkBook; import com.fr.poly.PolyDesigner; @@ -52,6 +28,34 @@ import com.fr.report.poly.PolyWorkSheet; import com.fr.report.report.TemplateReport; import com.fr.report.worksheet.WorkSheet; +import javax.swing.Icon; +import javax.swing.JComponent; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.FontMetrics; +import java.awt.GradientPaint; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.geom.GeneralPath; +import java.awt.geom.Path2D; +import java.util.ArrayList; +import java.util.List; + /** * NameTabPane of sheets * @@ -67,7 +71,7 @@ public class SheetNameTabPane extends JComponent implements MouseListener, Mouse private static final Icon WORK_SHEET_ICON = IconUtils.readIcon("/com/fr/design/standard/worksheet"); private static final Icon POLY_SHEET_ICON = IconUtils.readIcon("/com/fr/design/standard/polysheet"); private static final Icon LEFT_ICON = IconUtils.readIcon("/com/fr/design/standard/prepage/pre_page"); - private static final Icon RIGHT_ICON = IconUtils.readIcon("/com/fr/design/standard/nextpage"); + private static final Icon RIGHT_ICON = IconUtils.readIcon("/com/fr/design/standard/nextpage/next_page"); private static final Icon DISABLED_LEFT_ICON = IconUtils.readIcon("/com/fr/design/standard/prepage/pre_page_disabled.svg"); private static final Icon DISABLED_RIGHT_ICON = IconUtils.readIcon("/com/fr/design/standard/nextpage/next_page_disabled.svg"); private static final int NUM = 10;