Browse Source
[关联场景] https://kms.fineres.com/pages/viewpage.action?pageId=116725800 [处理方案] 1、获取目标 creator 2、对 creator 的名字进行处理。 3、存在 widgetname , 说明已经初始化了, 直接通过 widgetname 处理 4、不存在, 则需要赋予一个默认的 widgetnamefeature/big-screen
Harrison
5 years ago
3 changed files with 159 additions and 6 deletions
@ -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<String> duplicated = new HashSet<>(); |
||||||
|
recursiveRenameWidgetName(form, xCreator, duplicated); |
||||||
|
} |
||||||
|
|
||||||
|
private static void recursiveRenameWidgetName(Form form, XCreator xCreator, Set<String> 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<String> 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; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue