Browse Source

REPORT-54998 完善代码逻辑

zheng-1641779399395
方磊 3 years ago
parent
commit
ce0437869c
  1. 160
      designer-realize/src/main/java/com/fr/grid/GridMouseAdapter.java

160
designer-realize/src/main/java/com/fr/grid/GridMouseAdapter.java

@ -458,20 +458,15 @@ public class GridMouseAdapter implements MouseListener, MouseWheelListener, Mous
java.awt.Rectangle startRec = cs.toRectangle();
ColumnRow currentMouseCell = GridUtils.getAdjustEventColumnRow_withresolution(reportPane, evtX, evtY, resolution);
if (startRec.contains(currentMouseCell.getColumn(), currentMouseCell.getRow())) {
grid.getDragRectangle().setBounds(startRec);
} else {
if (isOutECBlockPane(evtX, evtY)) {
return;
}
int xDistance = evtX - this.oldEvtX;
int yDistance = evtY - this.oldEvtY;
int dragDirection = calculateDragDirection(xDistance, yDistance);
grid.getDragRectangle().x = calculateDragRectangleX(currentMouseCell, startRec, dragDirection);
grid.getDragRectangle().y = calculateDragRectangleY(currentMouseCell, startRec, dragDirection);
grid.getDragRectangle().width = calculateDragRectangleWidth(currentMouseCell, startRec, dragDirection);
grid.getDragRectangle().height = calculateDragRectangleHeight(currentMouseCell, startRec, dragDirection);
if (isOutECBlockPane(evtX, evtY)) {
return;
}
int xDistance = evtX - this.oldEvtX;
int yDistance = evtY - this.oldEvtY;
int dragDirection = calculateDragDirection(xDistance, yDistance);
ColumnRow realCell = calculateRealCell(currentMouseCell, startRec, dragDirection);
grid.getDragRectangle().setBounds(calculateDragRectangle(realCell, startRec, dragDirection));
reportPane.ensureColumnRowVisible(currentMouseCell.getColumn() + 1, currentMouseCell.getRow() + 1);
}
@ -491,97 +486,90 @@ public class GridMouseAdapter implements MouseListener, MouseWheelListener, Mous
}
}
private int calculateDragRectangleX(ColumnRow currentMouseCell, Rectangle startRec, int direction) {
int result = 0;
/**
* 计算当前十字标所在格子对应的真正参与计算拖拽边框的格子
* @param currentMouseCell 当前鼠标所在的格子
* @param startRec 起始格子
* @param direction 拖拽方向
* @return 当前十字标所在格子对应的真正参与计算拖拽边框的格子
*/
private ColumnRow calculateRealCell(ColumnRow currentMouseCell, Rectangle startRec, int direction) {
ColumnRow realCell;
switch(direction) {
case DIRECTION_DOWN:
case DIRECTION_UP:
case DIRECTION_RIGHT:
result = startRec.x;
realCell = ColumnRow.valueOf(startRec.x, currentMouseCell.row);
break;
case DIRECTION_LEFT:
if (isInsideSelectedCell(currentMouseCell, startRec, direction)) {
result = startRec.x;
} else {
result = currentMouseCell.getColumn();
}
break;
}
return result;
}
private int calculateDragRectangleY(ColumnRow currentMouseCell, Rectangle startRec, int direction) {
int result = 0;
switch(direction) {
case DIRECTION_RIGHT:
case DIRECTION_LEFT:
case DIRECTION_DOWN:
result = startRec.y;
realCell = ColumnRow.valueOf(currentMouseCell.column, startRec.y);
break;
case DIRECTION_UP:
if (isInsideSelectedCell(currentMouseCell, startRec, direction)) {
result = startRec.y;
} else {
result = currentMouseCell.getRow();
}
default:
realCell = currentMouseCell;
break;
}
return result;
return realCell;
}
private int calculateDragRectangleWidth(ColumnRow currentMouseCell, Rectangle startRec, int direction) {
int result = 0;
switch(direction) {
case DIRECTION_DOWN:
case DIRECTION_UP:
result = startRec.width;
break;
case DIRECTION_RIGHT:
result = currentMouseCell.getColumn() - startRec.x + 1;
break;
case DIRECTION_LEFT:
if (isInsideSelectedCell(currentMouseCell, startRec, direction)) {
result = startRec.width;
} else {
result = startRec.x - currentMouseCell.getColumn() + startRec.width;
}
break;
/**
* 计算拖拽的边框
* @param realCell 当前十字标所在格子对应的真正参与计算拖拽边框的格子
* @param startRec 起始格子
* @param direction 拖拽方向
* @return 拖拽的边框
*/
private Rectangle calculateDragRectangle(ColumnRow realCell, Rectangle startRec, int direction) {
Rectangle rectangle = new Rectangle();
if (startRec.contains(realCell.column, realCell.row)) {
rectangle.setBounds(startRec);
} else {
boolean clockWise = isClockWise(direction);
int x, y, width, height;
if (clockWise) {
x = startRec.x;
y = startRec.y;
width = Math.abs(realCell.column - startRec.x) + 1; // 加一是因为计算顺时针边框宽度或者高度的时候,没把realCell自身高度算进去
height = Math.abs(realCell.row - startRec.y) + 1;
} else {
x = realCell.column;
y = realCell.row;
width = Math.abs(realCell.column - startRec.x) + startRec.width;
height = Math.abs(realCell.row - startRec.y) + startRec.height;
}
// 上面的只计算了顺时针和逆时针的width,height,但是还要顺时针里面还要区分从左往右和从上往下,这里是用于修正
boolean isHorizontal = isHorizontal(direction);
if (isHorizontal) {
height = startRec.height; // 水平方向,绘制的边框肯定就等于起始格子的高度
} else {
width = startRec.width; // 垂直方向,绘制的边框肯定就等于起始给子的宽度
}
rectangle.setBounds(x, y, width, height);
}
return result;
return rectangle;
}
private int calculateDragRectangleHeight(ColumnRow currentMouseCell, Rectangle startRec, int direction) {
int result = 0;
switch(direction) {
case DIRECTION_DOWN:
result = currentMouseCell.getRow() - startRec.y + 1;
break;
case DIRECTION_UP:
if (isInsideSelectedCell(currentMouseCell, startRec, direction)) {
result = startRec.y - currentMouseCell.getRow() + startRec.height;
} else {
result = startRec.height;
}
break;
case DIRECTION_RIGHT:
case DIRECTION_LEFT:
result = startRec.height;
break;
}
return result;
/**
* 判断拖拽方向是否是顺时针 从左往右从上往下为顺时针从右往左从下往上为逆时针
* @param direction 方向
* @return 是否是顺时针
*/
private boolean isClockWise(int direction) {
return direction == DIRECTION_DOWN || direction == DIRECTION_RIGHT;
}
private boolean isOutECBlockPane(int evtX, int evtY) {
ElementCasePane reportPane = grid.getElementCasePane();
if (reportPane instanceof ECBlockPane) {
return (evtY > reportPane.getBounds().height - ECBlockGap) || (evtX > reportPane.getBounds().width - ECBlockGap);
}
return true;
/**
* 判断拖拽方向是否为水平还是垂直
* @param direction 方向
* @return 是否为水平
*/
private boolean isHorizontal(int direction) {
return direction == DIRECTION_RIGHT || direction == DIRECTION_LEFT;
}
private boolean isInsideSelectedCell(ColumnRow currentMouseCell, Rectangle startRec, int direction) {
return (direction == DIRECTION_UP && currentMouseCell.getRow() >= startRec.y && currentMouseCell.getRow() < startRec.y + startRec.height) ||
(direction == DIRECTION_LEFT && currentMouseCell.getColumn() >= startRec.x && currentMouseCell.getColumn() < startRec.x + startRec.width);
private boolean isOutECBlockPane(int evtX, int evtY) {
ElementCasePane reportPane = grid.getElementCasePane();
return reportPane instanceof ECBlockPane && ((evtY > reportPane.getBounds().height - ECBlockGap) || (evtX > reportPane.getBounds().width - ECBlockGap));
}
private void doShiftSelectCell(double evtX, double evtY) {

Loading…
Cancel
Save