Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2f10f5170d1b40b1ba11f7cd834abb9df493daec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2015 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sun Volland<sun.volland@free.fr>
 *******************************************************************************/
package org.eclipse.fx.ui.workbench.renderers.fx.services;

import org.eclipse.fx.ui.workbench.renderers.base.services.MaximizationTransitionService;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.util.Duration;

/**
 *
 * Maximization transition performing growing/shrinking of the maximized
 * component until maximize/original size is reached.
 *
 * @since 2.2.0
 */
public class ProgressiveMaximizationTransitionService implements MaximizationTransitionService<Pane, Region> {

	@Override
	public void maximize(Pane commonRoot, Pane greyPane, Pane containerPane, Region node, Runnable finished) {

		Bounds bounds = getBoundsInParent(commonRoot, node);

		Pane pane = new Pane();
		pane.getChildren().add(node);
		node.setTranslateX(bounds.getMinX());
		node.setTranslateY(bounds.getMinY());
		node.setPrefWidth(node.getWidth());
		node.setPrefHeight(node.getHeight());
		containerPane.getChildren().add(pane);

		Timeline timeline = new Timeline();
		timeline.setCycleCount(1);

		double duration = 300;
		Interpolator interpolator = Interpolator.EASE_BOTH;

		KeyValue kvOpacity = new KeyValue(greyPane.opacityProperty(), Double.valueOf(1.0), interpolator);
		KeyFrame kfOpacity = new KeyFrame(Duration.millis(duration), kvOpacity);
		timeline.getKeyFrames().add(kfOpacity);

		KeyValue kv = new KeyValue(node.translateXProperty(), Double.valueOf(0.0), interpolator);
		KeyFrame kf = new KeyFrame(Duration.millis(duration), kv);
		timeline.getKeyFrames().add(kf);

		KeyValue kvY = new KeyValue(node.translateYProperty(), Double.valueOf(0.0), interpolator);
		KeyFrame kfY = new KeyFrame(Duration.millis(duration), kvY);
		timeline.getKeyFrames().add(kfY);

		KeyValue kvYWidth = new KeyValue(node.prefWidthProperty(), Double.valueOf(commonRoot.getWidth()), interpolator);
		KeyFrame kfYWidth = new KeyFrame(Duration.millis(duration), kvYWidth);
		timeline.getKeyFrames().add(kfYWidth);

		KeyValue kvYHeight = new KeyValue(node.prefHeightProperty(), Double.valueOf(commonRoot.getHeight()), interpolator);
		KeyFrame kfYHeight = new KeyFrame(Duration.millis(duration), kvYHeight);
		timeline.getKeyFrames().add(kfYHeight);

		timeline.setOnFinished(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				node.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
				node.setTranslateX(0);
				node.setTranslateY(0);
				finished.run();
			}
		});

		timeline.play();
	}

	@Override
	public void restore(Pane commonRoot, Pane greyPane, Pane containerPane, Pane nodeContainer, Region node, Runnable finished) {

		Bounds bounds = getBoundsInParent(commonRoot, nodeContainer);

		Pane pane = new Pane();
		pane.getChildren().add(node);
		node.setPrefSize(node.getWidth(), node.getHeight());
		containerPane.getChildren().add(pane);

		Timeline timeline = new Timeline();
		timeline.setCycleCount(1);

		double duration = 300;
		Interpolator interpolator = Interpolator.EASE_BOTH;

		KeyValue kvOpacity = new KeyValue(greyPane.opacityProperty(), Double.valueOf(0.0), interpolator);
		final KeyFrame kfOpacity = new KeyFrame(Duration.millis(duration), kvOpacity);
		timeline.getKeyFrames().add(kfOpacity);

		KeyValue kv = new KeyValue(node.translateXProperty(), Double.valueOf(bounds.getMinX()), interpolator);
		KeyFrame kf = new KeyFrame(Duration.millis(duration), kv);
		timeline.getKeyFrames().add(kf);
		KeyValue kvY = new KeyValue(node.translateYProperty(), Double.valueOf(bounds.getMinY()), interpolator);
		KeyFrame kfY = new KeyFrame(Duration.millis(duration), kvY);
		timeline.getKeyFrames().add(kfY);

		KeyValue kvYWidth = new KeyValue(node.prefWidthProperty(), Double.valueOf(bounds.getWidth()), interpolator);
		KeyFrame kfYWidth = new KeyFrame(Duration.millis(duration), kvYWidth);
		timeline.getKeyFrames().add(kfYWidth);

		KeyValue kvYHeight = new KeyValue(node.prefHeightProperty(), Double.valueOf(bounds.getHeight()), interpolator);
		KeyFrame kfYHeight = new KeyFrame(Duration.millis(duration), kvYHeight);
		timeline.getKeyFrames().add(kfYHeight);

		timeline.setOnFinished(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				node.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
				node.setTranslateX(0);
				node.setTranslateY(0);
				finished.run();
			}
		});

		timeline.play();
	}

	/**
	 * Utility to get a child's bound relative to an ancestor.
	 *
	 * @param parentNode
	 *            ancestor to get child bounds for
	 * @param childNode
	 *            child
	 * @return bounds of child relative to ancestor
	 */
	private static Bounds getBoundsInParent(Node parentNode, Node childNode) {
		Node child = childNode;
		Bounds bounds = child.getLayoutBounds();
		while (child != null && child != parentNode) {
			bounds = child.localToParent(bounds);
			child = child.getParent();
		}
		return bounds;
	}
}

Back to the top