From cb90f91fc72f9edd72bd5266d210a37f70352099 Mon Sep 17 00:00:00 2001 From: Harrison Date: Fri, 5 Jun 2020 18:15:24 +0800 Subject: [PATCH] =?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=85=B3=E8=81=94=E5=9C=BA=E6=99=AF]=20https://kms.fineres.com?= =?UTF-8?q?/pages/viewpage.action=3FpageId=3D116725800=20[=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=96=B9=E6=A1=88]=201=E3=80=81=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=9B=AE=E6=A0=87=20creator=202=E3=80=81=E5=AF=B9=20creator=20?= =?UTF-8?q?=E7=9A=84=E5=90=8D=E5=AD=97=E8=BF=9B=E8=A1=8C=E5=A4=84=E7=90=86?= =?UTF-8?q?=E3=80=82=203=E3=80=81=E5=AD=98=E5=9C=A8=20widgetname=20?= =?UTF-8?q?=EF=BC=8C=20=E8=AF=B4=E6=98=8E=E5=B7=B2=E7=BB=8F=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E4=BA=86=EF=BC=8C=20=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20widgetname=20=E5=A4=84=E7=90=86=204?= =?UTF-8?q?=E3=80=81=E4=B8=8D=E5=AD=98=E5=9C=A8=EF=BC=8C=20=E5=88=99?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E8=B5=8B=E4=BA=88=E4=B8=80=E4=B8=AA=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E7=9A=84=20widgetname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/beans/models/AddingModel.java | 11 +-- .../designer/beans/models/ModelUtil.java | 60 ++++++++++++ .../beans/models/AddingModelTest.java | 94 +++++++++++++++++++ 3 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 designer-form/src/main/java/com/fr/design/designer/beans/models/ModelUtil.java 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 9285b35ca7..fd98adc184 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 @@ -26,9 +26,8 @@ public class AddingModel { 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(); @@ -37,11 +36,11 @@ public class AddingModel { /** * 待说明 * - * @param designer 设计器 - * @param creatorName 组件名 + * @param designer 设计器 */ - public void instantiateCreator(FormDesigner designer, String creatorName) { - creator.toData().setWidgetName(creatorName); + public void instantiateCreator(FormDesigner designer) { + + ModelUtil.renameWidgetName(designer.getTarget(), creator); ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator); adapter.initialize(); creator.addNotify(); diff --git a/designer-form/src/main/java/com/fr/design/designer/beans/models/ModelUtil.java b/designer-form/src/main/java/com/fr/design/designer/beans/models/ModelUtil.java new file mode 100644 index 0000000000..e1efd31416 --- /dev/null +++ b/designer-form/src/main/java/com/fr/design/designer/beans/models/ModelUtil.java @@ -0,0 +1,60 @@ +package com.fr.design.designer.beans.models; + +import com.fr.design.designer.creator.XCreator; +import com.fr.design.designer.creator.XWParameterLayout; +import com.fr.form.main.Form; +import com.fr.form.ui.Widget; +import com.fr.stable.StringUtils; + +import java.util.HashSet; +import java.util.Set; + +/** + * created by Harrison on 2020/06/05 + **/ +abstract class ModelUtil { + + public static void renameWidgetName(Form form, XCreator xCreator) { + + Set duplicated = new HashSet<>(); + recursiveRenameWidgetName(form, xCreator, duplicated); + } + + private static void recursiveRenameWidgetName(Form form, XCreator xCreator, Set duplicated) { + + //有可能并不需要对自己处理,而是对其他的组件处理。所以要找到目标值 + XCreator target = xCreator.getXCreator(); + String uniqueName = uniqueName(form, target, duplicated); + target.toData().setWidgetName(uniqueName); + //将名字加入重复集合中 + duplicated.add(uniqueName); + + int count = target.getComponentCount(); + for (int a = 0; a < count; a++) { + if (target.getComponent(a) instanceof XCreator) { + XCreator child = (XCreator) target.getComponent(a); + recursiveRenameWidgetName(form, child, duplicated); + } + } + } + + private static String uniqueName(Form form, XCreator xCreator, Set duplicated) { + + if (xCreator.acceptType(XWParameterLayout.class)) { + return xCreator.createDefaultName(); + } + Widget widget = xCreator.toData(); + String widgetName = widget.getWidgetName(); + if (StringUtils.isEmpty(widgetName)) { + widgetName = xCreator.createDefaultName(); + } + String raw = widgetName; + int i = 0; + while (form.isNameExist(widgetName) || duplicated.contains(widgetName)) { + widgetName = raw + i; + i++; + } + return widgetName; + } + +} 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..e7eca1c036 --- /dev/null +++ b/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java @@ -0,0 +1,94 @@ +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 com.fr.form.ui.Widget; +import com.fr.form.ui.container.WAbsoluteLayout; +import com.fr.form.ui.container.WTitleLayout; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +import javax.swing.JComponent; +import java.awt.Dimension; +import java.awt.Rectangle; +import java.beans.IntrospectionException; + +public class AddingModelTest { + + @Test + public void testInstantiateCreator() throws Exception { + + Dimension dimension = new Dimension(20, 20); + + ChartEditor chartEditor1 = new ChartEditor(); + XCreator xCreator1 = new DemoCreator(chartEditor1, dimension, "test"); + + ChartEditor chartEditor2 = new ChartEditor(); + chartEditor2.setWidgetName("test02"); + XCreator xCreator2 = new DemoCreator(chartEditor2, dimension, "test02"); + xCreator1.add(xCreator2); + + ChartEditor chartEditor3 = new ChartEditor(); + chartEditor3.setWidgetName("test03"); + WAbsoluteLayout.BoundsWidget boundsWidget = new WAbsoluteLayout.BoundsWidget(chartEditor3, new Rectangle(dimension)); + WTitleLayout wTitleLayout03 = new WTitleLayout(); + wTitleLayout03.addWidget(boundsWidget); + DemoCreator xCreator3 = new DemoCreator(wTitleLayout03, dimension, "test03"); + xCreator1.add(xCreator3); + + AddingModel addingModel = new AddingModel(xCreator1, 20, 20); + + Form form = EasyMock.mock(Form.class); + EasyMock.expect(form.isNameExist("test")).andReturn(true).once(); + EasyMock.expect(form.isNameExist("test03")).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); + //没有默认参数, 但已经存在 test + Assert.assertEquals("test0", xCreator1.toData().getWidgetName()); + //直接返回 + Assert.assertEquals("test02", xCreator2.toData().getWidgetName()); + //已经存在,后接0 + Assert.assertEquals("test030", xCreator3.toData().getWidgetName()); + } + + private static class DemoCreator extends XCreator { + + private String widgetName; + + public DemoCreator(Widget ob, Dimension initSize, String widgetName) { + super(ob, initSize); + this.widgetName = widgetName; + } + + @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 this.widgetName; + } + } + +} \ No newline at end of file