Browse Source

REPORT-32149【组件加密】@harrison两个组件进行拖入,模板预览很奇怪

组件的默认名字不满足要求。
一些组件外面会嵌套 TitileLayout
所以根组件依旧保持之前的逻辑。
子组件使用组件名字递增的方式。
feature/big-screen
Harrison 5 years ago
parent
commit
0d2ef6de03
  1. 57
      designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java
  2. 101
      designer-form/src/main/java/com/fr/design/designer/beans/models/ModelUtils.java
  3. 95
      designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java

57
designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java

@ -6,17 +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.container.WTitleLayout;
import com.fr.general.ComparatorUtils;
import java.awt.Rectangle;
import java.util.HashSet;
import java.util.Set;
/**
* 添加状态下的model
@ -44,7 +38,7 @@ public class AddingModel {
* @param designer 设计器
*/
public void instantiateCreator(FormDesigner designer) {
renameWidgetName(designer.getTarget(), creator);
ModelUtils.recursiveRenameWidgetName(designer.getTarget(), creator);
ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator);
adapter.initialize();
creator.addNotify();
@ -68,55 +62,6 @@ public class AddingModel {
currentY = -this.creator.getHeight();
}
/**
* 防止冲突重命名 XCreator 内部的 widget
*
* @param form 表单
* @param xCreator 创建器
*/
private static void renameWidgetName(Form form, XCreator xCreator) {
Set<String> nameSpace = new HashSet<>();
recursiveRenameWidgetName(form, xCreator, nameSpace);
}
private static void recursiveRenameWidgetName(Form form, XCreator xCreator, Set<String> 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<String> nameSpace) {
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() {
return currentX;
}

101
designer-form/src/main/java/com/fr/design/designer/beans/models/ModelUtils.java

@ -0,0 +1,101 @@
package com.fr.design.designer.beans.models;
import com.fr.design.designer.creator.XCreator;
import com.fr.design.designer.creator.XWParameterLayout;
import com.fr.design.designer.creator.XWTitleLayout;
import com.fr.form.main.Form;
import com.fr.form.ui.Widget;
import com.fr.form.ui.container.WTitleLayout;
import com.fr.stable.StringUtils;
import java.util.HashSet;
import java.util.Set;
/**
* created by Harrison on 2020/06/04
**/
public class ModelUtils {
/**
* 防止冲突重命名 XCreator 内部的 widget
*
* @param form 表单
* @param xCreator 创建器
*/
public static void recursiveRenameWidgetName(Form form, XCreator xCreator) {
Set<String> nameSpace = new HashSet<>();
//重命名根名字
//这里主要是为了兼容老的命名规则。
renameRootName(form, xCreator, nameSpace);
//这里要用新的命名规则
recursiveRenameChildName(form, xCreator, nameSpace);
}
private static void renameRootName(Form form, XCreator xCreator, Set<String> nameSpace) {
String creatorName = getNameByXCreator(form, xCreator, nameSpace);
xCreator.resetCreatorName(creatorName);
}
private static void recursiveRenameChildName(Form form, XCreator xCreator, Set<String> nameSpace) {
int count = xCreator.getComponentCount();
for (int a = 0; a < count; a++) {
if (xCreator.getComponent(a) instanceof XCreator) {
XCreator child = (XCreator) xCreator.getComponent(a);
String childName = getNameByWidget(form, child, nameSpace);
if (child.toData() instanceof WTitleLayout) {
XWTitleLayout xwTitleLayout = new XWTitleLayout((WTitleLayout) child.toData(), child.getSize());
xwTitleLayout.resetCreatorName(childName);
continue;
}
child.resetCreatorName(childName);
recursiveRenameChildName(form, child, nameSpace);
}
}
}
/**
* 组件命名规则
* 根据组件的名字如果组件没有名字就用 XCreator 的对应名字
*/
private static String getNameByWidget(Form form, XCreator xCreator, Set<String> nameSpace) {
Widget widget = xCreator.toData();
String name = widget.getWidgetName();
//没有 widgetName, 就创建默认的
if (StringUtils.isEmpty(name)) {
return getNameByXCreator(form, xCreator, nameSpace);
}
return getNameByDefault(form, xCreator, nameSpace, name);
}
/**
* 组件命名规则
* 根据组件的默认名
*/
private static String getNameByXCreator(Form form, XCreator x, Set<String> nameSpace) {
String defaultName = x.createDefaultName();
return getNameByDefault(form, x, nameSpace, defaultName);
}
private static String getNameByDefault(Form form, XCreator xCreator, Set<String> nameSpace, String defaultName) {
String name = defaultName;
if (xCreator.acceptType(XWParameterLayout.class)) {
return name;
}
int i = 0;
//保留第一次的值
String raw = name;
while (form.isNameExist(name) || nameSpace.contains(name)) {
name = raw + i;
i++;
}
nameSpace.add(name);
return name;
}
}

95
designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java

@ -5,53 +5,90 @@ 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 {
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);
Dimension dimension = new Dimension(20, 20);
ChartEditor chartEditor1 = new ChartEditor();
chartEditor1.setWidgetName("test01");
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("test0")).andReturn(true).once();
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);
Assert.assertEquals("test1", xCreator.toData().getWidgetName());
//后接0
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;
}
}
}
Loading…
Cancel
Save