org.datafx.flow.wysiwyg.netview.NetViewSkin Maven / Gradle / Ivy
The newest version!
package org.datafx.flow.wysiwyg.netview;
import javafx.collections.ListChangeListener;
import javafx.collections.WeakListChangeListener;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.control.SkinBase;
import java.util.ArrayList;
import java.util.List;
public class NetViewSkin extends SkinBase> {
private final ListChangeListener netViewItemsListener = (c) -> getSkinnable().requestLayout();
private final WeakListChangeListener weakNetViewItemsListener =
new WeakListChangeListener(netViewItemsListener);
private final ListChangeListener> netViewConnectionsListener = (c) -> getSkinnable().requestLayout();
private final WeakListChangeListener> weakNetViewConnectionsListener =
new WeakListChangeListener>(netViewConnectionsListener);
protected NetViewSkin(NetView control) {
super(control);
updateNetViewItems();
updateNetViewConnections();
getSkinnable().itemsProperty().addListener((observable, oldValue, newValue) -> updateNetViewItems());
getSkinnable().connectionsProperty().addListener((observable, oldValue, newValue) -> updateNetViewConnections());
}
public void updateNetViewConnections() {
if (getSkinnable().getConnections() != null) {
getSkinnable().getConnections().removeListener(weakNetViewConnectionsListener);
}
if (getSkinnable().getConnections() != null) {
getSkinnable().getConnections().addListener(weakNetViewConnectionsListener);
}
getSkinnable().requestLayout();
}
public void updateNetViewItems() {
if (getSkinnable().getItems() != null) {
getSkinnable().getItems().removeListener(weakNetViewItemsListener);
}
if (getSkinnable().getItems() != null) {
getSkinnable().getItems().addListener(weakNetViewItemsListener);
}
getSkinnable().requestLayout();
}
@Override
protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) {
if(getSkinnable().getItems() == null) {
getChildren().clear();
return;
}
updateCells();
updateConnections();
if (getSkinnable().getItems() != null) {
for(Node child : getChildren()) {
if(child instanceof NetCell) {
NetCell cell = (NetCell) child;
Point2D point = getSkinnable().getNetViewPositionHandler().getPosition(cell.getItem());
double prefWidth = cell.prefWidth(-1);
double prefHeigth = cell.prefHeight(-1);
cell.relocate(point.getX() - prefWidth / 2, point.getY() - prefHeigth / 2);
cell.resize(prefWidth, prefHeigth);
}
}
for(Node child : getChildren()) {
if(child instanceof NetConnectionView) {
NetConnectionView connectionView = (NetConnectionView) child;
NetCell startCell = null;
NetCell endCell = null;
for(Node child2 : getChildren()) {
if(child2 instanceof NetCell) {
NetCell cell = (NetCell) child2;
if(cell.getItem().equals(connectionView.getConnection().getStartItem())) {
startCell = cell;
}
if(cell.getItem().equals(connectionView.getConnection().getEndItem())) {
endCell = cell;
}
}
}
connectionView.layout(startCell, endCell);
}
}
}
}
private void updateCells() {
List toRemove = new ArrayList<>();
List usedItems = new ArrayList<>();
for(Node child : getChildren()) {
if(child instanceof NetCell) {
T item = ((NetCell) child).getItem();
if(getSkinnable().getItems().contains(item)) {
usedItems.add(item);
} else {
toRemove.add(child);
}
}
}
for(Node child : toRemove) {
getChildren().remove(child);
}
for(T item : getSkinnable().getItems()) {
if(!usedItems.contains(item)) {
NetCell cell = getSkinnable().getCellFactory().call(getSkinnable());
cell.setNetView(getSkinnable());
cell.setItem(item);
getChildren().add(cell);
}
}
}
private void updateConnections() {
if(getSkinnable().getConnections() == null) {
return;
}
List toRemove = new ArrayList<>();
List> usedConnections = new ArrayList<>();
for(Node child : getChildren()) {
if(child instanceof NetConnectionView) {
NetConnection connection = ((NetConnectionView) child).getConnection();
if(getSkinnable().getConnections().contains(connection)) {
usedConnections.add(connection);
} else {
toRemove.add(child);
}
}
}
for(Node child : toRemove) {
getChildren().remove(child);
}
for(NetConnection connection : getSkinnable().getConnections()) {
if(!usedConnections.contains(connection)) {
NetConnectionView connectionView = getSkinnable().getConnectionFactory().call(getSkinnable());
connectionView.setNetView(getSkinnable());
connectionView.setConnection(connection);
getChildren().add(connectionView);
}
}
}
}