From 04f474543ff0dd4759027ce66de92c31c6a334f5 Mon Sep 17 00:00:00 2001 From: Harrison Date: Tue, 2 Jun 2020 11:15:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?REPORT-32149=E3=80=90=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E3=80=91@harrison=E4=B8=A4=E4=B8=AA=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E8=BF=9B=E8=A1=8C=E6=8B=96=E5=85=A5=EF=BC=8C=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E9=A2=84=E8=A7=88=E5=BE=88=E5=A5=87=E6=80=AA=20[?= =?UTF-8?q?=E5=9C=BA=E6=99=AF]=20=E6=8B=96=E5=85=A5=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E7=BB=84=E4=BB=B6=E6=97=B6=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E7=94=BB=E5=B8=83=E5=9D=97=EF=BC=8C=E5=86=85=E9=83=A8=E6=9C=89?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E7=BB=84=E4=BB=B6=E3=80=82=E6=AD=A4=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E4=BC=9A=E5=AF=B9=E5=86=85=E9=83=A8=E7=9A=84?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E7=BB=84=E4=BB=B6=E8=BF=9B=E8=A1=8C=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E3=80=82=20[solution]=20=E9=80=92=E5=BD=92=E7=9A=84?= =?UTF-8?q?=E5=AF=B9=E6=89=80=E6=9C=89=E7=BB=84=E4=BB=B6=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/beans/models/AddingModel.java | 95 +++++++++++++------ .../beans/models/AddingModelTest.java | 52 ++++++++++ 2 files changed, 118 insertions(+), 29 deletions(-) create mode 100644 designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java b/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java index 9d308b9d4b..7bf623db69 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java @@ -1,47 +1,52 @@ package com.fr.design.designer.beans.models; -import java.awt.Rectangle; - -import com.fr.design.designer.creator.XWAbsoluteLayout; -import com.fr.design.mainframe.FormDesigner; import com.fr.design.designer.beans.AdapterBus; import com.fr.design.designer.beans.ComponentAdapter; import com.fr.design.designer.beans.adapters.component.CompositeComponentAdapter; import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.creator.XLayoutContainer; -import com.fr.design.designer.creator.XWParameterLayout; +import com.fr.design.designer.creator.XWAbsoluteLayout; +import com.fr.design.designer.creator.XWTitleLayout; +import com.fr.design.mainframe.FormDesigner; import com.fr.design.utils.ComponentUtils; +import com.fr.form.main.Form; +import com.fr.form.ui.Widget; +import com.fr.form.ui.container.WTitleLayout; import com.fr.general.ComparatorUtils; +import java.awt.Rectangle; +import java.util.HashSet; +import java.util.Set; + /** * 添加状态下的model */ public class AddingModel { - + + private static final String POSTFIX = "_a"; + // 当前要添加的组件 private XCreator creator; // 记录当前鼠标的位置信息 private int currentX; private int currentY; private boolean added; - + public AddingModel(FormDesigner designer, XCreator xCreator) { - String creatorName = getXCreatorName(designer, xCreator); this.creator = xCreator; - instantiateCreator(designer, creatorName); + instantiateCreator(designer); // 初始的时候隐藏该组件的图标 currentY = -this.creator.getWidth(); currentX = -this.creator.getHeight(); } - + /** - * 待说明 + * 初始化创建器 * - * @param designer 设计器 - * @param creatorName 组件名 + * @param designer 设计器 */ - public void instantiateCreator(FormDesigner designer, String creatorName) { - creator.toData().setWidgetName(creatorName); + public void instantiateCreator(FormDesigner designer) { + renameWidgetName(designer.getTarget(), creator); ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator); adapter.initialize(); creator.addNotify(); @@ -56,7 +61,7 @@ public class AddingModel { currentX = x - (xCreator.getWidth() / 2); currentY = y - (xCreator.getHeight() / 2); } - + /** * 隐藏当前组件的图标 */ @@ -64,28 +69,60 @@ public class AddingModel { currentX = -this.creator.getWidth(); currentY = -this.creator.getHeight(); } - - public String getXCreatorName(FormDesigner designer, XCreator x) { - String def = x.createDefaultName(); - if (x.acceptType(XWParameterLayout.class)) { - return def; + + /** + * 防止冲突,重命名 XCreator 内部的 widget + * + * @param form 表单 + * @param xCreator 创建器 + */ + private static void renameWidgetName(Form form, XCreator xCreator) { + + Set nameSpace = new HashSet<>(8); + recursiveRenameWidgetName(form, nameSpace, xCreator); + } + + private static void recursiveRenameWidgetName(Form form, Set nameSpace, XCreator xCreator) { + + String copyName = getAddWidgetName(form, xCreator.toData(), nameSpace); + if (xCreator.toData() instanceof WTitleLayout) { + XWTitleLayout xwTitleLayout = new XWTitleLayout((WTitleLayout) xCreator.toData(), xCreator.getSize()); + xwTitleLayout.resetCreatorName(copyName); + return; } - int i = 0; - while (designer.getTarget().isNameExist(def + i)) { - i++; + xCreator.resetCreatorName(copyName); + int count = xCreator.getComponentCount(); + for (int a = 0; a < count; a++) { + if (xCreator.getComponent(a) instanceof XCreator) { + XCreator child = (XCreator) xCreator.getComponent(a); + recursiveRenameWidgetName(form, nameSpace, child); + } + } - return def + i; } - + + /** + * 组件拷贝命名规则 + */ + private static String getAddWidgetName(Form form, Widget added, Set nameSpace) { + + StringBuilder name = new StringBuilder(added.getWidgetName()); + do { + name.append(POSTFIX); + } while (form.isNameExist(name.toString()) || nameSpace.contains(name.toString())); + nameSpace.add(name.toString()); + return name.toString(); + } + public int getCurrentX() { return currentX; } - + public int getCurrentY() { return currentY; } - - + + /** * 移动组件图标到鼠标事件发生的位置 * diff --git a/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java b/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java new file mode 100644 index 0000000000..64084844d1 --- /dev/null +++ b/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java @@ -0,0 +1,52 @@ +package com.fr.design.designer.beans.models; + +import com.fr.design.designer.creator.CRPropertyDescriptor; +import com.fr.design.designer.creator.XCreator; +import com.fr.design.mainframe.FormDesigner; +import com.fr.form.main.Form; +import com.fr.form.ui.ChartEditor; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +import javax.swing.JComponent; +import java.awt.Dimension; +import java.beans.IntrospectionException; + +public class AddingModelTest { + + @Test + public void testInstantiateCreator() throws Exception { + + ChartEditor chartEditor = new ChartEditor(); + chartEditor.setWidgetName("test"); + XCreator xCreator = new XCreator(chartEditor, new Dimension(20, 20)) { + @Override + public CRPropertyDescriptor[] supportedDescriptor() throws IntrospectionException { + return new CRPropertyDescriptor[0]; + } + + @Override + protected JComponent initEditor() { + return null; + } + + @Override + protected void initXCreatorProperties() { + } + }; + AddingModel addingModel = new AddingModel(xCreator, 20, 20); + + Form form = EasyMock.mock(Form.class); + EasyMock.expect(form.isNameExist("test_a")).andReturn(true).once(); + EasyMock.expect(form.isNameExist(EasyMock.anyString())).andReturn(false).anyTimes(); + EasyMock.replay(form); + + FormDesigner mock = EasyMock.mock(FormDesigner.class); + EasyMock.expect(mock.getTarget()).andReturn(form).anyTimes(); + EasyMock.replay(mock); + + addingModel.instantiateCreator(mock); + Assert.assertEquals("test_a_a", xCreator.toData().getWidgetName()); + } +} \ No newline at end of file From c5b4410d9a231d384cd4bfe23b939351fe739f05 Mon Sep 17 00:00:00 2001 From: Harrison Date: Tue, 2 Jun 2020 17:07:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B2=BF=E7=94=A8=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E5=91=BD=E5=90=8D=E8=A7=84=E5=88=99=E3=80=82=20?= =?UTF-8?q?=E5=8F=AA=E8=BF=9B=E8=A1=8C=20n=20=E5=AF=B9=20n=20=E7=9A=84?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/beans/models/AddingModel.java | 41 ++++++++++--------- .../beans/models/AddingModelTest.java | 23 +++++++---- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java b/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java index 7bf623db69..833101750b 100644 --- a/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java +++ b/designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java @@ -6,11 +6,11 @@ import com.fr.design.designer.beans.adapters.component.CompositeComponentAdapter import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.creator.XLayoutContainer; import com.fr.design.designer.creator.XWAbsoluteLayout; +import com.fr.design.designer.creator.XWParameterLayout; import com.fr.design.designer.creator.XWTitleLayout; import com.fr.design.mainframe.FormDesigner; import com.fr.design.utils.ComponentUtils; import com.fr.form.main.Form; -import com.fr.form.ui.Widget; import com.fr.form.ui.container.WTitleLayout; import com.fr.general.ComparatorUtils; @@ -23,8 +23,6 @@ import java.util.Set; */ public class AddingModel { - private static final String POSTFIX = "_a"; - // 当前要添加的组件 private XCreator creator; // 记录当前鼠标的位置信息 @@ -77,41 +75,46 @@ public class AddingModel { * @param xCreator 创建器 */ private static void renameWidgetName(Form form, XCreator xCreator) { - - Set nameSpace = new HashSet<>(8); - recursiveRenameWidgetName(form, nameSpace, xCreator); + + Set nameSpace = new HashSet<>(); + recursiveRenameWidgetName(form, xCreator, nameSpace); } - private static void recursiveRenameWidgetName(Form form, Set nameSpace, XCreator xCreator) { + private static void recursiveRenameWidgetName(Form form, XCreator xCreator, Set nameSpace) { - String copyName = getAddWidgetName(form, xCreator.toData(), nameSpace); + String creatorName = uniqueName(form, xCreator, nameSpace); if (xCreator.toData() instanceof WTitleLayout) { XWTitleLayout xwTitleLayout = new XWTitleLayout((WTitleLayout) xCreator.toData(), xCreator.getSize()); - xwTitleLayout.resetCreatorName(copyName); + xwTitleLayout.resetCreatorName(creatorName); return; } - xCreator.resetCreatorName(copyName); + xCreator.resetCreatorName(creatorName); int count = xCreator.getComponentCount(); for (int a = 0; a < count; a++) { if (xCreator.getComponent(a) instanceof XCreator) { XCreator child = (XCreator) xCreator.getComponent(a); - recursiveRenameWidgetName(form, nameSpace, child); + recursiveRenameWidgetName(form, child, nameSpace); } } } /** - * 组件拷贝命名规则 + * 组件命名规则 */ - private static String getAddWidgetName(Form form, Widget added, Set nameSpace) { + public static String uniqueName(Form form, XCreator x, Set nameSpace) { - StringBuilder name = new StringBuilder(added.getWidgetName()); - do { - name.append(POSTFIX); - } while (form.isNameExist(name.toString()) || nameSpace.contains(name.toString())); - nameSpace.add(name.toString()); - return name.toString(); + String def = x.createDefaultName(); + if (x.acceptType(XWParameterLayout.class)) { + return def; + } + int i = 0; + String name = def + i; + while (form.isNameExist(name) || nameSpace.contains(name)) { + i++; + name = def + i; + } + return name; } public int getCurrentX() { diff --git a/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java b/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java index 64084844d1..1487eb574a 100644 --- a/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java +++ b/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java @@ -17,36 +17,41 @@ public class AddingModelTest { @Test public void testInstantiateCreator() throws Exception { - + ChartEditor chartEditor = new ChartEditor(); - chartEditor.setWidgetName("test"); + chartEditor.setWidgetName("test01"); XCreator xCreator = new XCreator(chartEditor, new Dimension(20, 20)) { @Override public CRPropertyDescriptor[] supportedDescriptor() throws IntrospectionException { return new CRPropertyDescriptor[0]; } - + @Override protected JComponent initEditor() { return null; } - + @Override protected void initXCreatorProperties() { } + + @Override + public String createDefaultName() { + return "test"; + } }; AddingModel addingModel = new AddingModel(xCreator, 20, 20); - + Form form = EasyMock.mock(Form.class); - EasyMock.expect(form.isNameExist("test_a")).andReturn(true).once(); + EasyMock.expect(form.isNameExist("test0")).andReturn(true).once(); EasyMock.expect(form.isNameExist(EasyMock.anyString())).andReturn(false).anyTimes(); EasyMock.replay(form); - + FormDesigner mock = EasyMock.mock(FormDesigner.class); EasyMock.expect(mock.getTarget()).andReturn(form).anyTimes(); EasyMock.replay(mock); - + addingModel.instantiateCreator(mock); - Assert.assertEquals("test_a_a", xCreator.toData().getWidgetName()); + Assert.assertEquals("test1", xCreator.toData().getWidgetName()); } } \ No newline at end of file