/**java
* @Title: DynamicLine.javaide
* @Package com.main.oildepot.ui.componentui
* @Description: TODOthis
* Copyright: Copyright (c) 2011 .net
* Company:成都康賽電子科大信息技術有限責任公司component
* orm
* @author Comsys-Administratorip
* @date 2016年8月18日 下午5:26:50get
* @version V1.0input
*/
package com.main.oildepot.ui;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import com.main.oildepot.OilDepot;
import com.main.oildepot.ui.component.BindCircle;
import com.main.oildepot.ui.component.DragContainer;
import com.main.oildepot.ui.component.OilLine;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.DoubleBinding;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Circle;
/**
* @Description: TODO
*
* @author 重慶灰鯨科技
*
* @date 2016年8月18日 下午5:26:50
*
*
*/
public class OilLinePaint extends DragContainer {
/**
*
* @Fields serialVersionUID : TODO(用一句話描述這個變量表示什麼)
*
*/
private ComponentData componentData = new ComponentData();
private OilLine oilLine;
private BindCircle startCircle;
private BindCircle endCircle;
private Map<Object, BoundData> boundMap = new HashMap<>();
/**
*
*
*
* 建立一個新的實例 DynamicLine.
*
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
*
*/
public OilLinePaint() {
initComponent();
initComponentAction();
}
private void initComponent() {
oilLine = new OilLine(componentData);
startCircle = new BindCircle(4, Color.WHITE);
startCircle.setStroke(Color.WHITE);
endCircle = new BindCircle(4, Color.WHITE);
endCircle.setStroke(Color.BLUE);
startCircle.setCenterX(0);
endCircle.setCenterX(55);
oilLine.startXProperty().bind(startCircle.centerXProperty());
oilLine.startYProperty().bind(startCircle.centerYProperty());
oilLine.endXProperty().bind(endCircle.centerXProperty());
oilLine.endYProperty().bind(endCircle.centerYProperty());
this.getChildren().add(oilLine);
this.getChildren().add(startCircle);
this.getChildren().add(endCircle);
}
private void initComponentAction() {
this.setOnDragDetected(null);
oilLine.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
startCircle.centerXProperty().unbind();
startCircle.centerYProperty().unbind();
endCircle.centerXProperty().unbind();
endCircle.centerYProperty().unbind();
Dragboard dragboard = startDragAndDrop(getTransferMode());
Map<DataFormat, Object> map = new HashMap<DataFormat, Object>();
componentData.setOffsetX(event.getX());
componentData.setOffsetY(event.getY());
map.put(ComponentData.DRAG_FORMAT, generateDragContent());
dragboard.setContent(map);
// dragboard.setDragView(exportNodeImg(),
// Math.abs(event.getX()), event.getY());
// dragboard.setDragView(exportNodeImg(), event.getX(),
// event.getY());
}
});
startCircle.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
startCircle.startFullDrag();
}
});
startCircle.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (!startCircle.centerXProperty().isBound()) {
startCircle.setCenterX(event.getX());
}
if (!startCircle.centerYProperty().isBound()) {
startCircle.setCenterY(event.getY());
}
event.consume();
}
});
endCircle.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
endCircle.startFullDrag();
endCircle.toFront();
}
});
endCircle.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (!endCircle.centerXProperty().isBound()) {
endCircle.setCenterX(event.getX());
}
if (!endCircle.centerYProperty().isBound()) {
endCircle.setCenterY(event.getY());
}
event.consume();
}
});
endCircle.setOnMouseDragEntered(new EventHandler<MouseDragEvent>() {
@Override
public void handle(MouseDragEvent event) {
if (event.getGestureSource() != oilLine && event.getGestureSource() != startCircle
&& event.getGestureSource() != endCircle) {
if (event.getGestureSource() instanceof Circle) {
Circle gestureCircle = (Circle) event.getGestureSource();
// System.out.println(event.getX());
// System.out.println(event.getY());
BoundData boundData = new BoundData();
boundData.setBoundSource(gestureCircle);
double endCenterOffsetX = endCircle.getCenterX() - gestureCircle.getCenterX();
double endCenterOffsetY = endCircle.getCenterY() - gestureCircle.getCenterY();
// 矯正偏移量
endCenterOffsetX = endCenterOffsetX + (event.getX() - endCircle.getCenterX());
//
endCenterOffsetY = endCenterOffsetY + (event.getY() - endCircle.getCenterY());
boundData.setOffsetX(endCenterOffsetX);
boundData.setOffsetY(endCenterOffsetY);
boundMap.put(gestureCircle, boundData);
DoubleBinding endCenterXBinding = Bindings.createDoubleBinding(new Callable<Double>() {
@Override
public Double call() throws Exception {
return endCircle.getCenterX() - boundMap.get(gestureCircle).getOffsetX();
}
}, endCircle.centerXProperty());
DoubleBinding endCenterYBinding = Bindings.createDoubleBinding(new Callable<Double>() {
@Override
public Double call() throws Exception {
return endCircle.getCenterY() - boundMap.get(gestureCircle).getOffsetY();
}
}, endCircle.centerYProperty());
gestureCircle.centerXProperty().bind(endCenterXBinding);
gestureCircle.centerYProperty().bind(endCenterYBinding);
endCircle.setFill(Color.GRAY);
}
}
event.consume();
}
});
}
/*
*
* <p>Title: generateDragContent</p>
*
* <p>Description: </p>
*
* @return
*
* @see com.main.oildepot.ui.component.DragContainer#generateDragContent()
*
*/
@Override
public ComponentData generateDragContent() {
return componentData;
}
@Override
public TransferMode getTransferMode() {
return TransferMode.MOVE;
}
public BindCircle getStartCircle() {
return startCircle;
}
public void setStartCircle(BindCircle startCircle) {
this.startCircle = startCircle;
}
public BindCircle getEndCircle() {
return endCircle;
}
public void setEndCircle(BindCircle endCircle) {
this.endCircle = endCircle;
}
@Override
public void dragDetected() {
}
public ComponentData getComponentData() {
return componentData;
}
public void setComponentData(ComponentData componentData) {
this.componentData = componentData;
}
}