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 833101750b..9285b35ca7 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 @@ -7,16 +7,11 @@ 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.container.WTitleLayout; import com.fr.general.ComparatorUtils; import java.awt.Rectangle; -import java.util.HashSet; -import java.util.Set; /** * 添加状态下的model @@ -31,20 +26,22 @@ public class AddingModel { private boolean added; public AddingModel(FormDesigner designer, XCreator xCreator) { + String creatorName = getXCreatorName(designer, xCreator); this.creator = xCreator; - instantiateCreator(designer); + instantiateCreator(designer, creatorName); // 初始的时候隐藏该组件的图标 currentY = -this.creator.getWidth(); currentX = -this.creator.getHeight(); } /** - * 初始化创建器 + * 待说明 * - * @param designer 设计器 + * @param designer 设计器 + * @param creatorName 组件名 */ - public void instantiateCreator(FormDesigner designer) { - renameWidgetName(designer.getTarget(), creator); + public void instantiateCreator(FormDesigner designer, String creatorName) { + creator.toData().setWidgetName(creatorName); ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator); adapter.initialize(); creator.addNotify(); @@ -68,55 +65,19 @@ public class AddingModel { currentY = -this.creator.getHeight(); } - /** - * 防止冲突,重命名 XCreator 内部的 widget - * - * @param form 表单 - * @param xCreator 创建器 - */ - private static void renameWidgetName(Form form, XCreator xCreator) { - - Set nameSpace = new HashSet<>(); - recursiveRenameWidgetName(form, xCreator, nameSpace); - } - - private static void recursiveRenameWidgetName(Form form, XCreator xCreator, Set nameSpace) { - - String creatorName = uniqueName(form, xCreator, nameSpace); - if (xCreator.toData() instanceof WTitleLayout) { - XWTitleLayout xwTitleLayout = new XWTitleLayout((WTitleLayout) xCreator.toData(), xCreator.getSize()); - xwTitleLayout.resetCreatorName(creatorName); - return; - } - 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, child, nameSpace); - } - - } - } - - /** - * 组件命名规则 - */ - public static String uniqueName(Form form, XCreator x, Set nameSpace) { - + public String getXCreatorName(FormDesigner designer, XCreator x) { 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)) { + while (designer.getTarget().isNameExist(def + i)) { i++; - name = def + i; } - return name; + return def + i; } + public int getCurrentX() { return currentX; } 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 deleted file mode 100644 index 1487eb574a..0000000000 --- a/designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java +++ /dev/null @@ -1,57 +0,0 @@ -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("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("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("test1", xCreator.toData().getWidgetName()); - } -} \ No newline at end of file