Skip to content

Commit fc549b0

Browse files
committed
Simplify and refactor code.
1 parent 04ce869 commit fc549b0

1 file changed

Lines changed: 8 additions & 26 deletions

File tree

src/main/java/com/brunomnsilva/smartgraph/graphview/SmartGraphVertexNode.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import javafx.beans.property.DoubleProperty;
2929
import javafx.beans.property.ReadOnlyDoubleProperty;
3030
import javafx.beans.property.SimpleDoubleProperty;
31-
import javafx.geometry.Bounds;
3231
import javafx.geometry.Point2D;
3332
import javafx.scene.Cursor;
3433
import javafx.scene.Group;
@@ -56,7 +55,7 @@
5655
*/
5756
public class SmartGraphVertexNode<T> extends Group implements SmartGraphVertex<T>, SmartLabelledNode {
5857

59-
public static final int ATTACHED_LABEL_OFFSET = 5;
58+
public static final int ATTACHED_LABEL_VERTICAL_OFFSET = 5;
6059

6160
/** The underlying vertex in the graph model represented by this visual node. */
6261
private final Vertex<T> underlyingVertex;
@@ -109,9 +108,6 @@ public class SmartGraphVertexNode<T> extends Group implements SmartGraphVertex<T
109108
/** Reference to the parent panel managing this node. */
110109
private final SmartGraphPanel<T, ?> parent;
111110

112-
/** Bounding box covering the shape and its optional label. */
113-
private Bounds boundingBox;
114-
115111
/**
116112
* Constructor which sets the instance attributes.
117113
*
@@ -155,7 +151,6 @@ public SmartGraphVertexNode(SmartGraphPanel<T, ?> parent, Vertex<T> v, double x,
155151
enableDrag();
156152
}
157153

158-
updateBoundingBox();
159154
propagateHoverEffectToAttachments();
160155
}
161156

@@ -240,8 +235,6 @@ public double getRadius() {
240235
public void setRadius(double radius) {
241236
if (Double.compare(getRadius(), radius) != 0) {
242237
this.radius.set(radius);
243-
244-
updateBoundingBox();
245238
}
246239
}
247240

@@ -442,9 +435,7 @@ public void attachLabel(SmartLabel label) {
442435
label.xProperty().bind(centerXProperty().subtract(Bindings.divide( label.layoutWidthProperty(), 2.0)));
443436

444437
// Put below the vertex, by the specified offset
445-
label.yProperty().bind(centerYProperty().add(Bindings.add( shapeProxy.radiusProperty(), ATTACHED_LABEL_OFFSET)));
446-
447-
updateBoundingBox();
438+
label.yProperty().bind(centerYProperty().add(Bindings.add( shapeProxy.radiusProperty(), ATTACHED_LABEL_VERTICAL_OFFSET)));
448439
}
449440

450441
@Override
@@ -485,6 +476,7 @@ public SmartStylableNode getStylableLabel() {
485476
private boolean hasLabel() {
486477
return attachedLabel != null;
487478
}
479+
488480
/**
489481
* Make a node movable by dragging it around with the mouse primary button.
490482
*/
@@ -556,7 +548,9 @@ private void enableDrag() {
556548
*/
557549
private double boundVertexNodeXPositioning(double xCoord, double minCoordValue, double maxCoordValue) {
558550
// The shape and (possibly attached) label are centered, so its bounds are equals for each side
559-
double lengthToSide = this.boundingBox.getWidth() / 2;
551+
double lengthToSide = Math.max(
552+
getRadius(),
553+
(attachedLabel != null ? attachedLabel.layoutWidthProperty().get()/2 : 0));
560554

561555
if (xCoord < minCoordValue + lengthToSide) {
562556
return minCoordValue + lengthToSide;
@@ -571,7 +565,8 @@ private double boundVertexNodeYPositioning(double yCoord, double minCoordValue,
571565
// The length to the top from the center point is the radius of the surrogate shape
572566
// The length to the bottom from the center point is the radius of the surrogate shape, plus the label offset and height
573567
double lengthToTop = getRadius();
574-
double lengthToBottom = getRadius() + (attachedLabel != null ? attachedLabel.layoutHeightProperty().get() : 0);
568+
double lengthToBottom = getRadius() +
569+
(attachedLabel != null ? ATTACHED_LABEL_VERTICAL_OFFSET + attachedLabel.layoutHeightProperty().get() : 0);
575570

576571
if (yCoord < minCoordValue + lengthToTop) {
577572
return minCoordValue + lengthToTop;
@@ -582,19 +577,6 @@ private double boundVertexNodeYPositioning(double yCoord, double minCoordValue,
582577
}
583578
}
584579

585-
/**
586-
* Updates the bounding box that encloses this node (including its label).
587-
*/
588-
private void updateBoundingBox() {
589-
if(shapeProxy == null || shapeProxy.getShape() == null) return;
590-
591-
this.boundingBox = this.shapeProxy.getShape().getLayoutBounds();
592-
593-
if(hasLabel()) {
594-
this.boundingBox = UtilitiesJavaFX.union(this.boundingBox, attachedLabel.getLayoutBounds());
595-
}
596-
}
597-
598580
/*
599581
* (re)Bind properties of the exposed properties and the underlying shape.
600582
*/

0 commit comments

Comments
 (0)