Kaan Burak Sener
Kaan Burak Sener

Reputation: 994

Setting ScrollPane to the Desired Position

I am developing a project in JavaFX 2.0.

The scenario starts when a user creates a UML model like below:

enter image description here

When the user clicks the source code synchronisation button, the application displays the related source codes in Bubbles around the UML model like following:

enter image description here

However, as you might realize that the bubble, which is created on the top is not fully fit the pane. The desired view of the pane should be like this:

enter image description here

Even if I know that I should use HValue and VValue properties to accomplish this. I couldn't understand how should I set these new value of ScrollPane.

The size of the ScrollPane is 8000 x 8000 and I am creating an invisible rectangle which covers all the UML model and the bubbles by calling rescaleView() function. So according to the values of this rectangle, I am rescaling the ScrollPane but I couldn't move the viewport centre again. Here is the function for rescaling:

private void rescaleView() {
    double MARGIN = 5.0;

    List<BubbleView> bubbleViews = new ArrayList<>(diagramController.getAllBubbleViews());

    double xMin = bubbleViews.get(0).getX();
    double yMin = bubbleViews.get(0).getY();
    double xMax = bubbleViews.get(0).getX() + bubbleViews.get(0).getWidth();
    double yMax = bubbleViews.get(0).getY() + bubbleViews.get(0).getHeight();

    if(bubbleViews.size() > 0) {
        for(int i = 1; i < bubbleViews.size(); i++) {
            if((bubbleViews.get(i).getX() + bubbleViews.get(i).getWidth()) > xMax) {
                xMax = bubbleViews.get(i).getX() + bubbleViews.get(i).getWidth();
            }

            if(bubbleViews.get(i).getX() < xMin) {
                xMin = bubbleViews.get(i).getX();
            }

            if((bubbleViews.get(i).getY() + bubbleViews.get(i).getHeight()) > yMax) {
                yMax = bubbleViews.get(i).getY() + bubbleViews.get(i).getHeight();
            }

            if(bubbleViews.get(i).getY() < yMin) {
                yMin = bubbleViews.get(i).getY();
            }
        }

        double toBeScaled = Math.min((Math.max(diagramController.getScrollPane().getWidth(), diagramController.getScrollPane().getHeight()) / Math.max((xMax - xMin), (yMax - yMin))),(Math.min(diagramController.getScrollPane().getWidth(), diagramController.getScrollPane().getHeight()) / Math.min((xMax - xMin), (yMax - yMin)))) * 100;
        diagramController.getGraphController().zoomPane(toBeScaled - MARGIN);
        diagramController.getGraphController().center(xMin, yMin);
    }
}

but the center(x,y) function below doesn't make the viewport center:

public void center(double x, double y) {
    ScrollPane scrollPane = diagramController.getScrollPane();

    double xScroll = x / 8000; //8000 is the size of aDrawPane set in view.classDiagramView.fxml
    double yScroll = y / 8000;

    scrollPane.setHvalue(xScroll);
    scrollPane.setVvalue(yScroll);
}

How should I set these setHValue and setVValue?

Thanks in advance.

Upvotes: 1

Views: 106

Answers (1)

monolith52
monolith52

Reputation: 884

I'm not sure it works or not.

diagramController.getGraphController().center((xMax + xMin) / 2, (yMax + yMin) / 2);

double xScroll = (x - scrollPane.getWidth() / 2) / (8000 - scrollPane.getWidth());
double yScroll = (y - scrollPane.getHeight() / 2) / (8000 - scrollPane.getHeight());

And toBeScaled seems to have undesirable case such as vertically long scrollPane with wide contents. Ignore the code below if it was misunderstanding.

final double scrollPaneWidth = diagramController.getScrollPane().getWidth();
final double scrollPaneHeight = diagramController.getScrollPane().getHeight();
final double contentsWidth = xMax - xMin;
final double contentsHeight = yMax - yMin;
double toBeScaled = Math.min(scrollPaneHeight / contentsHeight, scrollPaneWidth / contentsWidth) * 100;

Upvotes: 2

Related Questions