Browse Source
组件的默认名字不满足要求。 一些组件外面会嵌套 TitileLayout 所以根组件依旧保持之前的逻辑。 子组件使用组件名字递增的方式。feature/big-screen
Harrison
5 years ago
3 changed files with 168 additions and 85 deletions
@ -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; |
||||
} |
||||
} |
Loading…
Reference in new issue