Skip to content

Commit

Permalink
改进TreeTableLayout,使用flex属性来指定可扩展的单元格
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 3, 2025
1 parent b7c12ad commit 7a85fc8
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 78 deletions.
36 changes: 36 additions & 0 deletions nop-core/src/main/java/io/nop/core/model/table/tree/TreeCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class TreeCell extends AbstractFreezable implements ITreeStructure, ICell
private int rowIndex = -1;
private int colIndex = -1;

/**
* 如果不为0,则表示允许自动延展。flex表示自动延展时的占比
*/
private int flex;

/**
* 树节点所处的层次,顶层的treeLevel为0,子层加1
*/
Expand All @@ -69,6 +74,14 @@ public String getFormula() {
return null;
}

public int getFlex() {
return flex;
}

public void setFlex(int flex) {
this.flex = flex;
}

public String getComment() {
return comment;
}
Expand Down Expand Up @@ -109,6 +122,7 @@ public TreeCell cloneInstance() {
ret.colIndex = colIndex;
ret.leafIndex = leafIndex;
ret.treeLevel = treeLevel;
ret.flex = flex;
return ret;
}

Expand Down Expand Up @@ -137,6 +151,18 @@ public void setLeafIndex(int leafIndex) {
this.leafIndex = leafIndex;
}

public TreeCell getFirstChild() {
if (children == null || children.isEmpty())
return null;
return children.get(0);
}

public TreeCell getLastChild() {
if (children == null || children.isEmpty())
return null;
return children.get(children.size() - 1);
}

public void addChild(TreeCell cell) {
Guard.checkArgument(cell.getParent() == null);
if (this.children == null) {
Expand Down Expand Up @@ -185,6 +211,16 @@ public int getRowSpan() {
return mergeDown + 1;
}

public void setColSpan(int colSpan) {
Guard.positiveInt(colSpan, "colSpan");
this.mergeAcross = colSpan - 1;
}

public void setRowSpan(int rowSpan) {
Guard.positiveInt(rowSpan, "rowSpan");
this.mergeDown = rowSpan - 1;
}

public Object getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.nop.api.core.exceptions.NopException;
import io.nop.core.model.table.IRow;
import io.nop.core.model.table.ITable;
import io.nop.core.model.tree.TreeVisitors;

import java.util.List;

Expand Down Expand Up @@ -49,11 +48,16 @@ public TreeCell calcLayout(List<TreeCell> cells, boolean bVer) {

public void assignToTable(List<TreeCell> cells, ITable<IRow> table) {
for (TreeCell cell : cells) {
for (TreeCell c : TreeVisitors.depthFirstIterator(cell, true)) {
if (c.isVirtual())
continue;
table.setCell(c.getRowIndex(), c.getColIndex(), cell);
if (cell.isVirtual()) {
if (cell.getChildren() != null)
assignToTable(cell.getChildren(), table);
continue;
}


table.setCell(cell.getRowIndex(), cell.getColIndex(), cell);
if (cell.getChildren() != null)
assignToTable(cell.getChildren(), table);
}
}

Expand Down Expand Up @@ -95,7 +99,7 @@ void calcBbox(TreeCell cell) {
case right_ver:
case left_ver: {
int w = maxBboxWidth(children);
int h = sumBboxWidth(children);
int h = sumBboxHeight(children);
cell.setMergeDown(Math.max(cell.getMergeDown(), h - 1));
cell.setBboxWidth(cell.getColSpan() + w);
cell.setBboxHeight(cell.getRowSpan());
Expand All @@ -113,7 +117,7 @@ void calcBbox(TreeCell cell) {
case bottom_ver:
case top_ver: {
int w = maxBboxWidth(children);
int h = sumBboxWidth(children);
int h = sumBboxHeight(children);
cell.setMergeAcross(Math.max(cell.getMergeAcross(), w - 1));
cell.setBboxWidth(cell.getColSpan());
cell.setBboxHeight(cell.getRowSpan() + h);
Expand All @@ -122,17 +126,17 @@ void calcBbox(TreeCell cell) {
case hor: {
int w = sumBboxWidth(children);
int h = maxBboxHeight(children);
cell.setMergeAcross(0);
cell.setMergeDown(0);
cell.setColSpan(w);
cell.setRowSpan(h);
cell.setBboxWidth(w);
cell.setBboxHeight(h);
break;
}
case ver: {
int w = maxBboxWidth(children);
int h = sumBboxWidth(children);
cell.setMergeAcross(0);
cell.setMergeDown(0);
int h = sumBboxHeight(children);
cell.setColSpan(w);
cell.setRowSpan(h);
cell.setBboxWidth(w);
cell.setBboxHeight(h);
break;
Expand Down Expand Up @@ -191,53 +195,107 @@ void adjustBbox(TreeCell cell, int w, int h) {

int bboxWidth = cell.getBboxWidth();
int bboxHeight = cell.getBboxHeight();
int sumFlex = childrenFlex(cell);

cell.setBboxWidth(w);
cell.setBboxHeight(h);

switch (cell.getChildPos()) {
TreeCellChildPosition pos = cell.getChildPos();
if (pos == null)
pos = TreeCellChildPosition.ver;

switch (pos) {
case right_hor:
case left_hor: {
// 如果边框宽度增加了,则延展最后一个child
lastChild(children).incWidth(w - bboxWidth);
adjustChildrenHor(children, h);
cell.setMergeDown(h - 1);
distributeWidth(children, sumFlex, w - bboxWidth);
adjustChildrenHor(children, h);
break;
}
case right_ver:
case left_ver: {
lastChild(children).incHeight(h - bboxHeight);
adjustChildrenVer(children, w - cell.getColSpan());
cell.setMergeDown(h - 1);
distributeHeight(children, sumFlex, h - bboxHeight);
adjustChildrenVer(children, w - cell.getColSpan());
break;
}
case bottom_hor:
case top_hor: {
lastChild(children).incWidth(w - bboxWidth);
adjustChildrenHor(children, h - cell.getRowSpan());
cell.setMergeAcross(w - 1);
distributeWidth(children, sumFlex, w - bboxWidth);
adjustChildrenHor(children, h - cell.getRowSpan());
break;
}
case bottom_ver:
case top_ver: {
cell.setMergeAcross(w - 1);
lastChild(children).incHeight(h - bboxHeight);
distributeHeight(children, sumFlex, h - bboxHeight);
adjustChildrenVer(children, w);
break;
}
case hor:
lastChild(children).incWidth(w - bboxWidth);
cell.setMergeDown(h - 1);
cell.setMergeAcross(w - 1);
distributeWidth(children, sumFlex, w - bboxWidth);
adjustChildrenHor(children, h);
break;
case ver:
lastChild(children).incHeight(h - bboxHeight);
cell.setMergeDown(h - 1);
cell.setMergeAcross(w - 1);
distributeHeight(children, sumFlex, h - bboxHeight);
adjustChildrenVer(children, w);
break;
default:
throw new NopException(ERR_TREE_TABLE_UNSUPPORTED_CHILD_POS).param(ARG_POS, cell.getChildPos());
}
}

void distributeWidth(List<TreeCell> children, int sumFlex, int deltaWidth) {
if (sumFlex == 0 || deltaWidth <= 0)
return;

for (TreeCell child : children) {
int flex = child.getFlex();
if (flex > 0) {
if (flex == sumFlex) {
child.incWidth(deltaWidth);
} else {
int dw = deltaWidth * flex / sumFlex;
child.incWidth(dw);
deltaWidth -= dw;
sumFlex -= flex;
}
}
}
}

void distributeHeight(List<TreeCell> children, int sumFlex, int deltaHeight) {
if (sumFlex == 0 || deltaHeight <= 0)
return;

for (TreeCell child : children) {
int flex = child.getFlex();
if (flex > 0) {
if (flex == sumFlex) {
child.incHeight(deltaHeight);
} else {
int dh = deltaHeight * flex / sumFlex;
child.incHeight(dh);
deltaHeight -= dh;
sumFlex -= flex;
}
}
}
}

int childrenFlex(TreeCell cell) {
int total = 0;
for (TreeCell child : cell.getChildren()) {
total += child.getFlex();
}
return total;
}

TreeCell lastChild(List<TreeCell> children) {
return children.get(children.size() - 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ public void testOneToManyRelation() {
// List<?> items = BeanTool.castBeanToType(response.getData(), List.class);
// assertEquals(1, items.size());

// {deptName=2222, deptUsers=[{userId=7ab81}, {id=dasda2}]}

Map<Object, Object> mapData = new HashMap<>();
mapData.put("roleKey", "test1");
mapData.put("roleName", "test1");
Map<Object, Object> deptUserData = new HashMap<>();
deptUserData.put("id", "1");
mapData.put("roleUsers", List.of(deptUserData));

ApiRequest<?> request = ApiRequest.build(Map.of("data", mapData));
request.setSelection(FieldSelectionBean.fromProp("roleUsers"));
IGraphQLExecutionContext context = graphQLEngine.newRpcContext(GraphQLOperationType.mutation,
"RoleEntity__save", request);
Object result = FutureHelper.syncGet(graphQLEngine.executeRpcAsync(context));
System.out.println(result);
// {deptName=2222, deptUsers=[{id=7ab81}, {id=dasda2}]}

Map<Object, Object> mapData = new HashMap<>();
mapData.put("roleKey", "test1");
mapData.put("roleName", "test1");
Map<Object, Object> deptUserData = new HashMap<>();
deptUserData.put("id", "1");
mapData.put("roleUsers", List.of(deptUserData));

ApiRequest<?> request = ApiRequest.build(Map.of("data", mapData));
request.setSelection(FieldSelectionBean.fromProp("roleUsers.userName"));
IGraphQLExecutionContext context =
graphQLEngine.newRpcContext(GraphQLOperationType.mutation,"RoleEntity__save", request);
ApiResponse<?> response = FutureHelper.syncGet(graphQLEngine.executeRpcAsync(context));
assertEquals(true, response.isOk());
List roleUsers = (List)BeanTool.getProperty(response.getData(), "roleUsers");
assertEquals(1, roleUsers.size());
}

@EnableSnapshot
@Test
public void testManyToManyRelation() {

testGen(OrmRelationType.m2m);
Expand Down Expand Up @@ -206,7 +206,7 @@ private void addRelation(OrmRelationType relationType, String relationName, Stri
relationMeta.setLeftPropName(leftPropName);
relationMeta.setRightPropName(rightPropName);
relationMeta.setStatus(1);
relationMeta.setTagsText("pub");
relationMeta.setTagsText("pub,insertable");
// relationMeta.setMiddleTableName(middleTableName);
leftEntity.getRelationMetasForEntity().add(relationMeta);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface XlsxConstants {

String SHEET_DATA = "Data";

String EXT_PROP_XPT_SINGLE_ROW = "xpt:singleRow";
String EXT_PROP_XPT_SINGLE_COL_LAYOUT = "xpt:singleColLayout";
String EXT_PROP_XPT_LABEL_COL_SIZE = "xpt:labelColSize";
String EXT_PROP_XPT_VALUE_COL_SIZE = "xpt:valueColSize";

Expand Down
Loading

0 comments on commit 7a85fc8

Please sign in to comment.