final DoubleProperty frameWidthProperty = getSkinnable().frameWidthProperty();
final DoubleProperty scopeLineWidthProperty = getSkinnable().scopeLineWidthProperty();
final BooleanProperty scopeLinesVisibleProperty = getSkinnable().scopeLinesVisibleProperty();
// Adding listener to control "radiusProperty" to add the value to "localRadius".
getSkinnable().radiusProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
localRadius.set(getSkinnable().getRadius());
}
});
localRadius.set(getSkinnable().getRadius());
// Adding listener to control "scaleFactorProperty" to add the value to "localScaleFactor".
getSkinnable().scaleFactorProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
localScaleFactor.set(getSkinnable().getScaleFactor());
}
});
localScaleFactor.set(getSkinnable().getScaleFactor());
snapView = new ImageView();
callBack = new Callback<SnapshotResult, Void>() {
@Override
public Void call(SnapshotResult result) {
return null;
}
};
final Scale scale = new Scale();
scale.xProperty().bind(localScaleFactor);
scale.yProperty().bind(localScaleFactor);
param = new SnapshotParameters();
param.setDepthBuffer(true);
param.setTransform(scale);
final StackPane mainContent = new StackPane();
final Circle frame = new Circle();
frame.getStyleClass().add("magnifier-frame");
frame.radiusProperty().bind(localRadius.add(frameWidthProperty));
final Circle cClip = new Circle();
cClip.radiusProperty().bind(localRadius);
cClip.translateXProperty().bind(localRadius);
cClip.translateYProperty().bind(localRadius);
viewer = new Viewer(localRadius, localRadius);
viewer.setClip(cClip);
final Line vL = new Line();
vL.setStartX(0);
vL.setStartY(0);
vL.setEndX(0);
vL.getStyleClass().add("magnifier-vLine");
vL.strokeWidthProperty().bind(scopeLineWidthProperty);
vL.visibleProperty().bind(scopeLinesVisibleProperty);
vL.endYProperty().bind(localRadius.multiply(2));
final Line hL = new Line();
hL.setStartX(0);
hL.setStartY(0);
hL.setEndY(0);
hL.getStyleClass().add("magnifier-hLine");
hL.strokeWidthProperty().bind(scopeLineWidthProperty);
hL.visibleProperty().bind(scopeLinesVisibleProperty);
hL.endXProperty().bind(localRadius.multiply(2));
// Adding all parts in a container.
mainContent.getChildren().addAll(frame, viewer, vL, hL);
final Popup popUp = new Popup();
popUp.getContent().add(mainContent);
final EventHandler<MouseEvent> enteredEvent = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
popUp.show(getSkinnable(), e.getScreenX() - shift, e.getScreenY() - shift);
takeSnap(e.getX(), e.getY());
}
};
final EventHandler<MouseEvent> exitedEvent = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
popUp.hide();
}
};
final EventHandler<MouseEvent> movedEvent = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
final double r = localRadius.get();
final double s = localScaleFactor.get();
if (e.getSceneX() > (scene.getWidth() - (2 * r))) {
popUp.setX(e.getScreenX() - (2 * r) - shift);
} else {
popUp.setX(e.getScreenX() - shift);
}
if (e.getSceneY() > (scene.getHeight() - (2 * r))) {
popUp.setY(e.getScreenY() - (2 * r) - shift);
} else {
popUp.setY(e.getScreenY() - shift);
}
prevX.set(e.getX());
prevY.set(e.getY());
shiftViewerContent(prevX.get(), prevY.get(), r, s);
}
};
// Adding mask implementation. The below mask is responsible to not access the contents when magnifier is shown.
final StackPane mask = new StackPane();
// Adding listener to activateProperty.
getSkinnable().activeProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
final Magnifier skinnable = getSkinnable();
if (skinnable.isActive()) {
skinnable.addEventFilter(MouseEvent.MOUSE_ENTERED, enteredEvent);
skinnable.addEventFilter(MouseEvent.MOUSE_EXITED, exitedEvent);
skinnable.addEventFilter(MouseEvent.MOUSE_MOVED, movedEvent);
if (!getChildren().contains(mask)) {
getChildren().add(mask);
}
} else {
skinnable.removeEventFilter(MouseEvent.MOUSE_ENTERED, enteredEvent);
skinnable.removeEventFilter(MouseEvent.MOUSE_EXITED, exitedEvent);
skinnable.removeEventFilter(MouseEvent.MOUSE_MOVED, movedEvent);
if (getChildren().contains(mask)) {
getChildren().remove(mask);
}
}
}
});
if (getSkinnable().isActive()) {
final Magnifier skinnable = getSkinnable();
skinnable.addEventFilter(MouseEvent.MOUSE_ENTERED, enteredEvent);
skinnable.addEventFilter(MouseEvent.MOUSE_EXITED, exitedEvent);
skinnable.addEventFilter(MouseEvent.MOUSE_MOVED, movedEvent);
}
// Adding listener to contentProperty.
getSkinnable().contentProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
getChildren().clear();
getChildren().addAll(getSkinnable().getContent(),mask);
}