diff --git a/designer-base/src/main/java/com/fr/design/data/DesignTableDataManager.java b/designer-base/src/main/java/com/fr/design/data/DesignTableDataManager.java index 25492c55e..a84e1337b 100644 --- a/designer-base/src/main/java/com/fr/design/data/DesignTableDataManager.java +++ b/designer-base/src/main/java/com/fr/design/data/DesignTableDataManager.java @@ -114,6 +114,7 @@ public abstract class DesignTableDataManager { public static void closeTemplate(JTemplate template) { if (template != null) { + template.whenClose(); columnCache.remove(getEditingTableDataSource()); dsListenersMap.remove(template.getPath()); } diff --git a/designer-base/src/main/java/com/fr/design/event/RemoveListener.java b/designer-base/src/main/java/com/fr/design/event/RemoveListener.java new file mode 100644 index 000000000..ff3eae305 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/event/RemoveListener.java @@ -0,0 +1,11 @@ +package com.fr.design.event; + +/** + * @author hades + * @version 10.0 + * Created by hades on 2020/3/19 + */ +public interface RemoveListener { + + void doRemoveAction(); +} diff --git a/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java b/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java index 2f9a69b31..6568f458d 100644 --- a/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java +++ b/designer-base/src/main/java/com/fr/design/file/HistoryTemplateListCache.java @@ -178,15 +178,28 @@ public class HistoryTemplateListCache implements CallbackEvent { * @return 位置 */ public int contains(JTemplate jt) { + return contains(jt, null); + } + + public int contains(FILE file) { + return contains(null, file); + } + + private int contains(JTemplate jt, FILE file) { + FILE item = null; + if (jt != null) { + item = jt.getEditingFILE(); + } else { + item = file; + } for (int i = 0; i < historyList.size(); i++) { - if (ComparatorUtils.equals(historyList.get(i).getEditingFILE(), jt.getEditingFILE())) { + if (ComparatorUtils.equals(historyList.get(i).getEditingFILE(), item)) { return i; } } return -1; } - /** * 判断是否打开过该模板 * 由于切换环境不会关闭模板,可能存在同名的模板,所以该方法不能准确找到所选的模板, diff --git a/designer-base/src/main/java/com/fr/design/mainframe/DesignerFrame.java b/designer-base/src/main/java/com/fr/design/mainframe/DesignerFrame.java index b7fbae773..dd31d8322 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/DesignerFrame.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/DesignerFrame.java @@ -1085,6 +1085,14 @@ public class DesignerFrame extends JFrame implements JTemplateActionListener, Ta */ private void openFile(FILE tplFile) { + if (tplFile != null) { + int index = HistoryTemplateListCache.getInstance().contains(tplFile); + if (index != -1) { + HistoryTemplateListCache.getInstance().getHistoryList().get(index).activeOldJTemplate(); + return; + } + } + JTemplate jt = JTemplateFactory.createJTemplate(tplFile); if (jt == null) { return; @@ -1094,23 +1102,6 @@ public class DesignerFrame extends JFrame implements JTemplateActionListener, Ta this.addAndActivateJTemplate(); MutilTempalteTabPane.getInstance().setTemTemplate( HistoryTemplateListCache.getInstance().getCurrentEditingTemplate()); - } else { - activeTemplate(jt); - } - } - - /** - * 激活指定的模板 - * - * @param jt 当前报表 - * @date 2014-10-14-下午6:31:23 - */ - private void activeTemplate(JTemplate jt) { - // 如果该模板已经打开,则进行激活就可以了 - int index = HistoryTemplateListCache.getInstance().contains(jt); - List> historyList = HistoryTemplateListCache.getInstance().getHistoryList(); - if (index != -1) { - historyList.get(index).activeJTemplate(index, jt); } else { this.addAndActivateJTemplate(jt); } diff --git a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java index 482b0d337..29c1dcb53 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java @@ -93,6 +93,7 @@ public abstract class JTemplate> protected U authorityUndoState = null; protected T template; // 当前模板 protected TemplateProcessInfo processInfo; // 模板过程的相关信息 + private JComponent centerPane; private static short currentIndex = 0;// 此变量用于多次新建模板时,让名字不重复 private DesignModelAdapter designModel; private PreviewProvider previewType; @@ -123,13 +124,13 @@ public abstract class JTemplate> this.editingFILE = file; this.setLayout(FRGUIPaneFactory.createBorderLayout()); this.setBorder(BorderFactory.createEmptyBorder()); - this.add(createCenterPane(), BorderLayout.CENTER); + addCenterPane(); this.undoState = createUndoState(); designModel = createDesignModel(); consumeTimer.setEnabled(shouldInitForCollectInfo(isNewFile)); } - + void onGetFocus() { consumeTimer.start(); } @@ -246,19 +247,47 @@ public abstract class JTemplate> } + /** + * 模板关闭时 + */ + public void whenClose() { + // do nothing + } + /** * 刷新内部资源 + * + * 刷新资源的同时。 + * CenterPane 负责监听改动。 + * 所以需要同步处理 */ public void refreshResource() { try { this.template = JTemplateFactory.asIOFile(this.editingFILE); setTarget(this.template); + + // 先移除旧的。 + removeCenterPane(); + // 加入新的 + addCenterPane(); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } } - + + private void addCenterPane() { + + this.centerPane = createCenterPane(); + this.add(centerPane, BorderLayout.CENTER); + } + + private void removeCenterPane() { + + JComponent centerPane = this.centerPane; + this.remove(centerPane); + } + /** * 刷新容器 */ diff --git a/designer-base/src/test/java/com/fr/design/file/HistoryTemplateListCacheTest.java b/designer-base/src/test/java/com/fr/design/file/HistoryTemplateListCacheTest.java new file mode 100644 index 000000000..bd9f8484d --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/file/HistoryTemplateListCacheTest.java @@ -0,0 +1,33 @@ +package com.fr.design.file; + +import com.fr.design.mainframe.JTemplate; +import com.fr.file.FILE; +import com.fr.invoke.Reflect; +import junit.framework.TestCase; +import org.easymock.EasyMock; +import org.junit.Assert; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * @author hades + * @version 10.0 + * Created by hades on 2020/3/23 + */ +public class HistoryTemplateListCacheTest extends TestCase { + + public void testContains() { + JTemplate jTemplate = EasyMock.mock(JTemplate.class); + FILE file = EasyMock.mock(FILE.class); + EasyMock.expect(jTemplate.getEditingFILE()).andReturn(file).anyTimes(); + EasyMock.replay(file); + EasyMock.replay(jTemplate); + Assert.assertEquals(-1, HistoryTemplateListCache.getInstance().contains(file)); + Assert.assertEquals(-1, HistoryTemplateListCache.getInstance().contains(jTemplate)); + Reflect.on(HistoryTemplateListCache.getInstance()).set("historyList", new ArrayList<>(Arrays.asList(jTemplate))); + Assert.assertEquals(0, HistoryTemplateListCache.getInstance().contains(file)); + Assert.assertEquals(0, HistoryTemplateListCache.getInstance().contains(jTemplate)); + } + +} \ No newline at end of file diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java b/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java index 621d3f40b..0759ddf98 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java @@ -1188,4 +1188,9 @@ public class JWorkBook extends JTemplate { provider.addChooseFileFilter(fileChooser, this.suffix()); } } + + @Override + public void whenClose() { + reportComposite.doRemoveAction(); + } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/ReportComponentComposite.java b/designer-realize/src/main/java/com/fr/design/mainframe/ReportComponentComposite.java index 71fb27502..0299899a0 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/ReportComponentComposite.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/ReportComponentComposite.java @@ -3,6 +3,7 @@ package com.fr.design.mainframe; import com.fr.base.ScreenResolution; import com.fr.common.inputevent.InputEventBaseOnOS; import com.fr.design.designer.EditingState; +import com.fr.design.event.RemoveListener; import com.fr.design.event.TargetModifiedListener; import com.fr.design.file.HistoryTemplateListPane; import com.fr.design.gui.icontainer.UIModeControlContainer; @@ -30,7 +31,7 @@ import java.util.ArrayList; * @editor zhou * @since 2012-3-27下午12:12:05 */ -public class ReportComponentComposite extends JComponent { +public class ReportComponentComposite extends JComponent implements RemoveListener { private static final int MAX = 400; private static final int HUND = 100; @@ -250,4 +251,9 @@ public class ReportComponentComposite extends JComponent { public void fireTargetModified() { parent.fireTargetModified(); } + + @Override + public void doRemoveAction() { + sheetNameTab.doRemoveAction(); + } } 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 747789cdb..74443e831 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 @@ -5,6 +5,7 @@ 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; @@ -27,6 +28,7 @@ import com.fr.base.vcs.DesignerMode; import com.fr.design.actions.UpdateAction; import com.fr.design.constants.UIConstants; import com.fr.design.dialog.FineJOptionPane; +import com.fr.design.event.RemoveListener; import com.fr.design.file.HistoryTemplateListCache; import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.ibutton.UIButtonUI; @@ -44,7 +46,6 @@ import com.fr.poly.PolyDesigner; import com.fr.report.poly.PolyWorkSheet; import com.fr.report.report.TemplateReport; import com.fr.report.worksheet.WorkSheet; -import com.fr.stable.ProductConstants; /** * NameTabPane of sheets @@ -52,7 +53,7 @@ import com.fr.stable.ProductConstants; * @editor zhou * @since 2012-3-26下午1:45:53 */ -public class SheetNameTabPane extends JComponent implements MouseListener, MouseMotionListener { +public class SheetNameTabPane extends JComponent implements MouseListener, MouseMotionListener, RemoveListener { private static final Color LINE_COLOR = new Color(0xababab); @@ -140,6 +141,10 @@ public class SheetNameTabPane extends JComponent implements MouseListener, Mouse private boolean isAuthorityEditing = false; + private ComponentListener listener; + + private DesignerFrame designerFrame; + public SheetNameTabPane(ReportComponentComposite reportCompositeX) { this.reportComposite = reportCompositeX; this.setLayout(new BorderLayout(0, 0)); @@ -203,14 +208,20 @@ public class SheetNameTabPane extends JComponent implements MouseListener, Mouse } } }); - - DesignerContext.getDesignerFrame().addComponentListener(new ComponentAdapter(){ + listener = new ComponentAdapter(){ @Override public void componentResized(ComponentEvent e) { for (int i = 0; i < lastOneIndex * NUM; i++) { moveLeft(); } } - }); + }; + designerFrame = DesignerContext.getDesignerFrame(); + designerFrame.addComponentListener(listener); + } + + @Override + public void doRemoveAction() { + designerFrame.removeComponentListener(listener); } private ActionListener createLeftButtonActionListener() {