Browse Source

REPORT-54885 优化重复代码

feature/10.0
方磊 3 years ago
parent
commit
3c5a53d2a2
  1. 51
      designer-form/src/main/java/com/fr/design/mainframe/AbstractFormParallelLine.java
  2. 45
      designer-form/src/main/java/com/fr/design/mainframe/FormHorizontalParallelLine.java
  3. 216
      designer-form/src/main/java/com/fr/design/mainframe/FormSpacingLineDrawer.java
  4. 45
      designer-form/src/main/java/com/fr/design/mainframe/FormVerticalParallelLine.java

51
designer-form/src/main/java/com/fr/design/mainframe/AbstractFormParallelLine.java

@ -0,0 +1,51 @@
package com.fr.design.mainframe;
import java.awt.Point;
public abstract class AbstractFormParallelLine {
protected int parallelValue;
protected int startPosition;
protected int endPosition;
public AbstractFormParallelLine(int parallelValue, int startPosition, int endPosition) {
this.parallelValue = parallelValue;
this.startPosition = startPosition;
this.endPosition = endPosition;
}
public int getCenterPosition() {
return (startPosition + endPosition) / 2;
}
abstract public Point getCenterPoint();
abstract public Point getPerpendicularPoint(int parallelValue);
public boolean isBeforeParallelLine(AbstractFormParallelLine parallelLine) {
return this.getCenterPosition() < parallelLine.getStartPosition();
}
public boolean isBehindParallelLine(AbstractFormParallelLine parallelLine) {
return this.getCenterPosition() > parallelLine.getEndPosition();
}
abstract public Point getExtendedLineStartPoint(AbstractFormParallelLine parallelLine);
abstract public Point getExtendedLineEndPoint(AbstractFormParallelLine parallelLine);
public int getDistanceWithLine(AbstractFormParallelLine parallelLine) {
return Math.abs(this.getParallelValue() - parallelLine.getParallelValue());
}
public int getParallelValue() {
return parallelValue;
}
public int getStartPosition() {
return startPosition;
}
public int getEndPosition() {
return endPosition;
}
}

45
designer-form/src/main/java/com/fr/design/mainframe/FormHorizontalParallelLine.java

@ -0,0 +1,45 @@
package com.fr.design.mainframe;
import java.awt.Point;
public class FormHorizontalParallelLine extends AbstractFormParallelLine {
public FormHorizontalParallelLine(int parallelValue, int startPosition, int endPosition) {
super(parallelValue, startPosition, endPosition);
}
@Override
public Point getCenterPoint() {
Point point = new Point();
point.setLocation(getCenterPosition(), parallelValue);
return point;
}
@Override
public Point getPerpendicularPoint(int parallelValue) {
Point point = new Point();
point.setLocation(getCenterPosition(), parallelValue);
return point;
}
@Override
public Point getExtendedLineStartPoint(AbstractFormParallelLine parallelLine) {
Point point = new Point();
if (isBeforeParallelLine(parallelLine)) {
point.setLocation(parallelLine.getStartPosition(), parallelLine.getParallelValue());
} else if (isBehindParallelLine(parallelLine)) {
point.setLocation(parallelLine.getEndPosition(), parallelLine.getParallelValue());
}
return point;
}
@Override
public Point getExtendedLineEndPoint(AbstractFormParallelLine parallelLine) {
Point point = new Point();
if (isBeforeParallelLine(parallelLine)) {
point.setLocation(getStartPosition(), parallelLine.getParallelValue());
} else if (isBehindParallelLine(parallelLine)) {
point.setLocation(getEndPosition(), parallelLine.getParallelValue());
}
return point;
}
}

216
designer-form/src/main/java/com/fr/design/mainframe/FormSpacingLineDrawer.java

@ -48,50 +48,39 @@ public class FormSpacingLineDrawer {
drawVerticalSpacingLine(g); drawVerticalSpacingLine(g);
} }
private void drawHorizontalSpacingLine(Graphics g) {
AbstractFormParallelLine[] lines = getNearestHorizontalSide();
drawSpacingLine(g, lines);
}
private void drawVerticalSpacingLine(Graphics g) {
AbstractFormParallelLine[] lines = getNearestVerticalSide();
drawSpacingLine(g, lines);
}
private void drawSpacingLine(Graphics g, int startX, int startY, int endX, int endY) { private void drawSpacingLine(Graphics g, int startX, int startY, int endX, int endY) {
Graphics2D g2d = (Graphics2D) g.create(); Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(LINE_COLOR); g2d.setColor(LINE_COLOR);
GraphDrawHelper.drawLine(g2d, startX, startY, endX, endY, Constants.LINE_THIN); GraphDrawHelper.drawLine(g2d, startX, startY, endX, endY, Constants.LINE_THIN);
} }
private void drawHorizontalSpacingLine(Graphics g) { private void drawSpacingLine(Graphics g, AbstractFormParallelLine[] nearestSides) {
int gap = calculateNearestXGap(); if (nearestSides.length != 2) {
if (gap <= MIN_SPACING) {
return; return;
} }
int[] nearestXSides = calculateNearestXSide(); int gap = nearestSides[0].getDistanceWithLine(nearestSides[1]);
if (nearestXSides.length != 2) {
return;
}
int startX = nearestXSides[0];
int startY = selectedRec.y + selectedRec.height / 2;
int endX = nearestXSides[1];
int endY = selectedRec.y + selectedRec.height / 2;
drawSpacingLine(g, startX, startY, endX, endY);
drawSpacingText(g, String.valueOf(gap), (startX + endX) / 2, (startY + endY) / 2);
if (isNeedVerticalExtendedLine()) {
drawVerticalExtendedLine(g, nearestXSides);
}
}
private void drawVerticalSpacingLine(Graphics g) {
int gap = calculateNearestYGap();
if (gap <= MIN_SPACING) { if (gap <= MIN_SPACING) {
return; return;
} }
int[] nearestYSides = calculateNearestYSide();
if (nearestYSides.length != 2) { int startX = (int) nearestSides[0].getCenterPoint().getX();
return; int startY = (int) nearestSides[0].getCenterPoint().getY();
} int endX = (int) nearestSides[0].getPerpendicularPoint(nearestSides[1].getParallelValue()).getX();
int startX = selectedRec.x + selectedRec.width / 2; int endY = (int) nearestSides[0].getPerpendicularPoint(nearestSides[1].getParallelValue()).getY();
int startY = nearestYSides[0];
int endX = selectedRec.x + selectedRec.width / 2;
int endY = nearestYSides[1];
drawSpacingLine(g, startX, startY, endX, endY); drawSpacingLine(g, startX, startY, endX, endY);
drawSpacingText(g, String.valueOf(gap), (startX + endX) / 2, (startY + endY) / 2); drawSpacingText(g, String.valueOf(gap), (startX + endX) / 2, (startY + endY) / 2);
if (isNeedHorizontalExtendedLine()) { drawExtendedLine(g, nearestSides);
drawHorizontalExtendedLine(g, nearestYSides);
}
} }
private void drawExtendedLine(Graphics g, int startX, int startY, int endX, int endY) { private void drawExtendedLine(Graphics g, int startX, int startY, int endX, int endY) {
@ -100,32 +89,14 @@ public class FormSpacingLineDrawer {
GraphDrawHelper.drawLine(g2d, startX, startY, endX, endY, Constants.LINE_DASH); GraphDrawHelper.drawLine(g2d, startX, startY, endX, endY, Constants.LINE_DASH);
} }
private void drawHorizontalExtendedLine(Graphics g, int[] nearestYSides) { private void drawExtendedLine(Graphics g, AbstractFormParallelLine[] nearestSides) {
int startX = 0, endX = 0; if (isNeedExtendedLine(nearestSides)) {
int startY = nearestYSides[1]; int startX = (int) nearestSides[0].getExtendedLineStartPoint(nearestSides[1]).getX();
int endY = nearestYSides[1]; int startY = (int) nearestSides[0].getExtendedLineStartPoint(nearestSides[1]).getY();
if (isHoveredCreatorLeftYSpacingLine()) { int endX = (int) nearestSides[0].getExtendedLineEndPoint(nearestSides[1]).getX();
startX = hoverCreator.getX() + hoverCreator.getWidth(); int endY = (int) nearestSides[0].getExtendedLineEndPoint(nearestSides[1]).getY();
endX = selectedRec.x + selectedRec.width; drawExtendedLine(g, startX, startY, endX, endY);
} else if (isHoveredCreatorRightYSpacingLine()) {
startX = hoverCreator.getX();
endX = selectedRec.x;
} }
drawExtendedLine(g, startX, startY, endX, endY);
}
private void drawVerticalExtendedLine(Graphics g, int[] nearestXSides) {
int startX = nearestXSides[1];
int endX = nearestXSides[1];
int startY = 0, endY = 0;
if (isHoveredCreatorAboveXSpacingLine()) {
startY = hoverCreator.getY() + hoverCreator.getHeight();
endY = selectedRec.y + selectedRec.height;
} else if (isHoveredCreatorBottomXSpacingLine()) {
startY = hoverCreator.getY();
endY = selectedRec.y;
}
drawExtendedLine(g, startX, startY, endX, endY);
} }
private void drawSpacingText(Graphics g, String text, int x, int y) { private void drawSpacingText(Graphics g, String text, int x, int y) {
@ -150,74 +121,6 @@ public class FormSpacingLineDrawer {
GraphDrawHelper.drawString(g2d, text, labelX, labelY); GraphDrawHelper.drawString(g2d, text, labelX, labelY);
} }
private int[] calculateNearestXSide() {
return calculateNearestSide(
selectedRec.x,
selectedRec.x + selectedRec.width,
hoverCreator.getX(),
hoverCreator.getX() + hoverCreator.getWidth()
);
}
private int[] calculateNearestYSide() {
return calculateNearestSide(selectedRec.y,
selectedRec.y + selectedRec.height,
hoverCreator.getY(),
hoverCreator.getY() + hoverCreator.getHeight());
}
private int calculateNearestXGap() {
return calculateNearestGap(
selectedRec.x,
selectedRec.x + selectedRec.width,
hoverCreator.getX(),
hoverCreator.getX() + hoverCreator.getWidth()
);
}
private int calculateNearestYGap() {
return calculateNearestGap(
selectedRec.y,
selectedRec.y + selectedRec.height,
hoverCreator.getY(),
hoverCreator.getY() + hoverCreator.getHeight()
);
}
private int[] calculateNearestSide(int selectedRecSide1, int selectedRecSide2, int hoveredSide1, int hoveredSide2) {
int minGap = calculateNearestGap(selectedRecSide1, selectedRecSide2, hoveredSide1, hoveredSide2);
int[] nearestSides = new int[2];
int[] selectedRecSides = new int[] {selectedRecSide1, selectedRecSide2};
int[] hoveredSides = new int[] {hoveredSide1, hoveredSide2};
for (int i = 0; i < selectedRecSides.length; i++) {
for (int j = 0; j < hoveredSides.length; j++) {
if (Math.abs(selectedRecSides[i] - hoveredSides[j]) == minGap) {
nearestSides[0] = selectedRecSides[i];
nearestSides[1] = hoveredSides[j];
}
}
}
return nearestSides;
}
private int calculateNearestGap(int selectedRecSide1, int selectedRecSide2, int hoveredSide1, int hoveredSide2) {
int[] selectedRecSides = new int[] {selectedRecSide1, selectedRecSide2};
int[] hoveredSides = new int[] {hoveredSide1, hoveredSide2};
int minDistance = 0;
for (int i = 0; i < selectedRecSides.length; i++) {
for (int j = 0; j < hoveredSides.length; j++) {
int distance = Math.abs(selectedRecSides[i] - hoveredSides[j]);
if (i == 0 && j == 0) {
minDistance = distance;
} else {
minDistance = Math.min(distance, minDistance);
}
}
}
return minDistance;
}
private boolean isSelectedRootComponent() { private boolean isSelectedRootComponent() {
return designer.getSelectionModel().getSelection().size() == 1 && return designer.getSelectionModel().getSelection().size() == 1 &&
designer.isRoot(designer.getSelectionModel().getSelection().getSelectedCreator()); designer.isRoot(designer.getSelectionModel().getSelection().getSelectedCreator());
@ -236,28 +139,8 @@ public class FormSpacingLineDrawer {
return hoverCreator.getParent() == null; return hoverCreator.getParent() == null;
} }
private boolean isNeedVerticalExtendedLine() { private boolean isNeedExtendedLine(AbstractFormParallelLine[] nearestSides) {
return isHoveredCreatorAboveXSpacingLine() || isHoveredCreatorBottomXSpacingLine(); return nearestSides[0].isBeforeParallelLine(nearestSides[1]) || nearestSides[0].isBehindParallelLine(nearestSides[1]);
}
private boolean isHoveredCreatorAboveXSpacingLine() {
return hoverCreator.getY() + hoverCreator.getHeight() < selectedRec.y + selectedRec.height / 2;
}
private boolean isHoveredCreatorBottomXSpacingLine() {
return hoverCreator.getY() > selectedRec.y + selectedRec.height / 2;
}
private boolean isNeedHorizontalExtendedLine() {
return isHoveredCreatorLeftYSpacingLine() || isHoveredCreatorRightYSpacingLine();
}
private boolean isHoveredCreatorLeftYSpacingLine() {
return hoverCreator.getX() + hoverCreator.getWidth() < selectedRec.x + selectedRec.width / 2;
}
private boolean isHoveredCreatorRightYSpacingLine() {
return hoverCreator.getX() > selectedRec.x + selectedRec.width / 2;
} }
private boolean isSelectedRootPane() { private boolean isSelectedRootPane() {
@ -271,4 +154,45 @@ public class FormSpacingLineDrawer {
private boolean isDrawSpacingLine() { private boolean isDrawSpacingLine() {
return !isSelectedRootPane() && !isHoveredRootPane() && isMouseMoveEvent; return !isSelectedRootPane() && !isHoveredRootPane() && isMouseMoveEvent;
} }
private AbstractFormParallelLine[] getNearestHorizontalSide() {
AbstractFormParallelLine[] selectedRecSides = new AbstractFormParallelLine[] {
new FormHorizontalParallelLine(selectedRec.y, selectedRec.x, selectedRec.x + selectedRec.width),
new FormHorizontalParallelLine(selectedRec.y + selectedRec.height, selectedRec.x, selectedRec.x + selectedRec.width)
};
AbstractFormParallelLine[] hoveredCreatorSides = new AbstractFormParallelLine[] {
new FormHorizontalParallelLine(hoverCreator.getY(), hoverCreator.getX(), hoverCreator.getX() + hoverCreator.getWidth()),
new FormHorizontalParallelLine(hoverCreator.getY() + hoverCreator.getHeight(), hoverCreator.getX(), hoverCreator.getX() + hoverCreator.getWidth())
};
return getNearestSide(selectedRecSides, hoveredCreatorSides);
}
private AbstractFormParallelLine[] getNearestVerticalSide() {
AbstractFormParallelLine[] selectedRecSides = new AbstractFormParallelLine[] {
new FormVerticalParallelLine(selectedRec.x, selectedRec.y, selectedRec.y + selectedRec.height),
new FormVerticalParallelLine(selectedRec.x + selectedRec.width, selectedRec.y, selectedRec.y + selectedRec.height)
};
AbstractFormParallelLine[] hoveredCreatorSides = new AbstractFormParallelLine[] {
new FormVerticalParallelLine(hoverCreator.getX(), hoverCreator.getY(), hoverCreator.getY() + hoverCreator.getHeight()),
new FormVerticalParallelLine(hoverCreator.getX() + hoverCreator.getWidth(), hoverCreator.getY(), hoverCreator.getY() + hoverCreator.getHeight())
};
return getNearestSide(selectedRecSides, hoveredCreatorSides);
}
private AbstractFormParallelLine[] getNearestSide(AbstractFormParallelLine[] lines1, AbstractFormParallelLine[] lines2) {
AbstractFormParallelLine[] nearestSides = new AbstractFormParallelLine[2];
int minDistance = lines1[0].getDistanceWithLine(lines2[0]);
for (int i = 0; i < lines1.length; i++) {
for (int j = 0; j < lines2.length; j++) {
int distance = lines1[i].getDistanceWithLine(lines2[j]);
if (distance <= minDistance) {
nearestSides[0] = lines1[i];
nearestSides[1] = lines2[j];
}
}
}
return nearestSides;
}
} }

45
designer-form/src/main/java/com/fr/design/mainframe/FormVerticalParallelLine.java

@ -0,0 +1,45 @@
package com.fr.design.mainframe;
import java.awt.Point;
public class FormVerticalParallelLine extends AbstractFormParallelLine {
public FormVerticalParallelLine(int parallelValue, int startPosition, int endPosition) {
super(parallelValue, startPosition, endPosition);
}
@Override
public Point getCenterPoint() {
Point point = new Point();
point.setLocation(parallelValue, getCenterPosition());
return point;
}
@Override
public Point getPerpendicularPoint(int parallelValue) {
Point point = new Point();
point.setLocation(parallelValue, getCenterPosition());
return point;
}
@Override
public Point getExtendedLineStartPoint(AbstractFormParallelLine parallelLine) {
Point point = new Point();
if (isBeforeParallelLine(parallelLine)) {
point.setLocation(parallelLine.getParallelValue(), parallelLine.getStartPosition());
} else if (isBehindParallelLine(parallelLine)) {
point.setLocation(parallelLine.getParallelValue(), parallelLine.getEndPosition());
}
return point;
}
@Override
public Point getExtendedLineEndPoint(AbstractFormParallelLine parallelLine) {
Point point = new Point();
if (isBeforeParallelLine(parallelLine)) {
point.setLocation(parallelLine.getParallelValue(), getStartPosition());
} else if (isBehindParallelLine(parallelLine)) {
point.setLocation(parallelLine.getParallelValue(), getEndPosition());
}
return point;
}
}
Loading…
Cancel
Save