Browse Source

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

[场景]
拖入组件时,组件时绝对画布块,内部有多个组件。此时,不会对内部的多个组件进行检测。
[solution]
递归的对所有组件进行处理。
feature/big-screen
Harrison 5 years ago
parent
commit
04f474543f
  1. 75
      designer-form/src/main/java/com/fr/design/designer/beans/models/AddingModel.java
  2. 52
      designer-form/src/test/java/com/fr/design/designer/beans/models/AddingModelTest.java

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

@ -1,23 +1,30 @@
package com.fr.design.designer.beans.models; 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.AdapterBus;
import com.fr.design.designer.beans.ComponentAdapter; import com.fr.design.designer.beans.ComponentAdapter;
import com.fr.design.designer.beans.adapters.component.CompositeComponentAdapter; import com.fr.design.designer.beans.adapters.component.CompositeComponentAdapter;
import com.fr.design.designer.creator.XCreator; import com.fr.design.designer.creator.XCreator;
import com.fr.design.designer.creator.XLayoutContainer; 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.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 com.fr.general.ComparatorUtils;
import java.awt.Rectangle;
import java.util.HashSet;
import java.util.Set;
/** /**
* 添加状态下的model * 添加状态下的model
*/ */
public class AddingModel { public class AddingModel {
private static final String POSTFIX = "_a";
// 当前要添加的组件 // 当前要添加的组件
private XCreator creator; private XCreator creator;
// 记录当前鼠标的位置信息 // 记录当前鼠标的位置信息
@ -26,22 +33,20 @@ public class AddingModel {
private boolean added; private boolean added;
public AddingModel(FormDesigner designer, XCreator xCreator) { public AddingModel(FormDesigner designer, XCreator xCreator) {
String creatorName = getXCreatorName(designer, xCreator);
this.creator = xCreator; this.creator = xCreator;
instantiateCreator(designer, creatorName); instantiateCreator(designer);
// 初始的时候隐藏该组件的图标 // 初始的时候隐藏该组件的图标
currentY = -this.creator.getWidth(); currentY = -this.creator.getWidth();
currentX = -this.creator.getHeight(); currentX = -this.creator.getHeight();
} }
/** /**
* 待说明 * 初始化创建器
* *
* @param designer 设计器 * @param designer 设计器
* @param creatorName 组件名
*/ */
public void instantiateCreator(FormDesigner designer, String creatorName) { public void instantiateCreator(FormDesigner designer) {
creator.toData().setWidgetName(creatorName); renameWidgetName(designer.getTarget(), creator);
ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator); ComponentAdapter adapter = new CompositeComponentAdapter(designer, creator);
adapter.initialize(); adapter.initialize();
creator.addNotify(); creator.addNotify();
@ -65,16 +70,48 @@ public class AddingModel {
currentY = -this.creator.getHeight(); currentY = -this.creator.getHeight();
} }
public String getXCreatorName(FormDesigner designer, XCreator x) { /**
String def = x.createDefaultName(); * 防止冲突重命名 XCreator 内部的 widget
if (x.acceptType(XWParameterLayout.class)) { *
return def; * @param form 表单
* @param xCreator 创建器
*/
private static void renameWidgetName(Form form, XCreator xCreator) {
Set<String> nameSpace = new HashSet<>(8);
recursiveRenameWidgetName(form, nameSpace, xCreator);
}
private static void recursiveRenameWidgetName(Form form, Set<String> 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; xCreator.resetCreatorName(copyName);
while (designer.getTarget().isNameExist(def + i)) { int count = xCreator.getComponentCount();
i++; 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<String> 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() { public int getCurrentX() {

52
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());
}
}
Loading…
Cancel
Save