-
Notifications
You must be signed in to change notification settings - Fork 54
/
FXViewer.java
296 lines (265 loc) · 9.2 KB
/
FXViewer.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************************
* Copyright (c) 2014, 2016 itemis AG 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:
* Alexander Nyßen (itemis AG) - initial API and implementation
*
*******************************************************************************/
package org.eclipse.gef4.mvc.fx.viewer;
import org.eclipse.gef4.fx.nodes.InfiniteCanvas;
import org.eclipse.gef4.mvc.fx.domain.FXDomain;
import org.eclipse.gef4.mvc.parts.IRootPart;
import org.eclipse.gef4.mvc.parts.IVisualPart;
import org.eclipse.gef4.mvc.viewer.AbstractViewer;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Window;
/**
* The {@link FXViewer} is an {@link AbstractViewer} that is parameterized by
* {@link Node}. It manages an {@link InfiniteCanvas} that displays the viewer's
* contents, adds scrollbars when necessary, and renders a background grid.
*
* @author anyssen
*
*/
public class FXViewer extends AbstractViewer<Node> {
/**
* Defines the default CSS styling for the {@link InfiniteCanvas}: no
* background, no border.
*/
public static final String DEFAULT_STYLE = "-fx-background-insets: 0; -fx-padding: 0; -fx-background-color: rgba(0,0,0,0);";
/**
* Defines the CSS styling that is used to highlight a focused viewer.
*/
public static final String FOCUSED_STYLE = "-fx-background-insets: 0; -fx-padding: 0; -fx-background-color: rgba(0,0,0,0); -fx-border-color: #8ec0fc; -fx-border-width: 2;";
/**
* The {@link InfiniteCanvas} that displays the viewer's contents.
*
* @deprecated This field will be made private. Use {@link #getCanvas()}
* instead.
*/
@Deprecated
protected InfiniteCanvas infiniteCanvas;
private boolean isInitialized = false;
private boolean isWindowFocused = false;
private boolean isFocusOwnerFocused = false;
private ReadOnlyBooleanWrapper viewerFocusedProperty = new ReadOnlyBooleanWrapper(
false);
private BooleanBinding viewerFocusedPropertyBinding = new BooleanBinding() {
@Override
protected boolean computeValue() {
return isWindowFocused && isFocusOwnerFocused;
}
};
private ChangeListener<Window> windowObserver = new ChangeListener<Window>() {
@Override
public void changed(ObservableValue<? extends Window> observable,
Window oldValue, Window newValue) {
onWindowChanged(oldValue, newValue);
}
};
private ChangeListener<Boolean> windowFocusedObserver = new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
onWindowFocusedChanged(oldValue, newValue);
}
};
private ChangeListener<Node> focusOwnerObserver = new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> observable,
Node oldValue, Node newValue) {
if (oldValue != newValue) {
onFocusOwnerChanged(oldValue, newValue);
}
}
};
private ChangeListener<Boolean> focusOwnerFocusedObserver = new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
// XXX: If a new focusOwner is set and the old focusOwner was
// focused, it will fire a "focused" change from true to false. Such
// events are disregarded, so that the viewer does not lose its
// focus if it will gain it immediately afterwards (which is handled
// within #focusOwnerChanged()).
if (observable == getCanvas().getScene().getFocusOwner()) {
if (oldValue == null ? newValue != null
: !oldValue.equals(newValue)) {
onFocusOwnerFocusedChanged(oldValue, newValue);
}
}
}
};
/**
* Creates a new {@link FXViewer}.
*/
public FXViewer() {
super();
// add binding to viewer focused property to have its value computed
// based on the values of:
// - window focused
// - focusOwner
// - focusOwner focused
viewerFocusedProperty.bind(viewerFocusedPropertyBinding);
}
/**
* Returns the {@link InfiniteCanvas} that is managed by this
* {@link FXViewer} .
*
* @return The {@link InfiniteCanvas} that is managed by this
* {@link FXViewer} .
*/
public InfiniteCanvas getCanvas() {
if (infiniteCanvas == null) {
IRootPart<Node, ? extends Node> rootPart = getRootPart();
if (rootPart != null) {
infiniteCanvas = new InfiniteCanvas();
infiniteCanvas.setStyle(DEFAULT_STYLE);
// register root visual
infiniteCanvas.getContentGroup().getChildren()
.addAll((Parent) rootPart.getVisual());
// ensure we can properly react to scene and focus owner changes
infiniteCanvas.sceneProperty()
.addListener(new ChangeListener<Scene>() {
@Override
public void changed(
ObservableValue<? extends Scene> observable,
Scene oldValue, Scene newValue) {
onSceneChanged(oldValue, newValue);
}
});
if (infiniteCanvas.getScene() != null) {
onSceneChanged(null, infiniteCanvas.getScene());
}
}
}
return infiniteCanvas;
}
@Override
public FXDomain getDomain() {
return (FXDomain) super.getDomain();
}
/**
* Returns the {@link Scene} in which the {@link InfiniteCanvas} of this
* {@link FXViewer} is displayed.
*
* @return The {@link Scene} in which the {@link InfiniteCanvas} of this
* {@link FXViewer} is displayed.
*/
public Scene getScene() {
return infiniteCanvas.getScene();
}
@Override
public boolean isViewerFocused() {
return viewerFocusedProperty.get();
}
@Override
public boolean isViewerVisual(Node node) {
while (node != null) {
if (node == infiniteCanvas) {
return true;
}
node = node.getParent();
}
return false;
}
private void onFocusOwnerChanged(Node oldFocusOwner, Node newFocusOwner) {
if (oldFocusOwner != null && isViewerVisual(oldFocusOwner)) {
oldFocusOwner.focusedProperty()
.removeListener(focusOwnerFocusedObserver);
}
if (newFocusOwner != null && isViewerVisual(newFocusOwner)) {
newFocusOwner.focusedProperty()
.addListener(focusOwnerFocusedObserver);
// check if viewer is focused
if (Boolean.TRUE.equals(newFocusOwner.focusedProperty().get())) {
isFocusOwnerFocused = true;
viewerFocusedPropertyBinding.invalidate();
}
} else {
// viewer unfocused
isFocusOwnerFocused = false;
viewerFocusedPropertyBinding.invalidate();
}
}
private void onFocusOwnerFocusedChanged(Boolean oldValue,
Boolean newValue) {
isFocusOwnerFocused = Boolean.TRUE.equals(newValue);
viewerFocusedPropertyBinding.invalidate();
}
private void onSceneChanged(Scene oldScene, Scene newScene) {
Window oldWindow = null;
Window newWindow = null;
Node oldFocusOwner = null;
Node newFocusOwner = null;
if (oldScene != null) {
oldWindow = oldScene.windowProperty().get();
oldScene.windowProperty().removeListener(windowObserver);
oldFocusOwner = oldScene.focusOwnerProperty().get();
oldScene.focusOwnerProperty().removeListener(focusOwnerObserver);
}
if (newScene != null) {
newWindow = newScene.windowProperty().get();
newScene.windowProperty().addListener(windowObserver);
newFocusOwner = newScene.focusOwnerProperty().get();
newScene.focusOwnerProperty().addListener(focusOwnerObserver);
}
onWindowChanged(oldWindow, newWindow);
onFocusOwnerChanged(oldFocusOwner, newFocusOwner);
}
private void onWindowChanged(Window oldValue, Window newValue) {
if (oldValue != null) {
oldValue.focusedProperty().removeListener(windowFocusedObserver);
}
if (newValue != null) {
newValue.focusedProperty().addListener(windowFocusedObserver);
// check if window is focused
if (Boolean.TRUE.equals(newValue.focusedProperty().get())) {
isWindowFocused = true;
viewerFocusedPropertyBinding.invalidate();
}
} else {
// window unfocused
isInitialized = false;
isWindowFocused = false;
viewerFocusedPropertyBinding.invalidate();
}
}
private void onWindowFocusedChanged(Boolean oldValue, Boolean newValue) {
isWindowFocused = Boolean.TRUE.equals(newValue);
viewerFocusedPropertyBinding.invalidate();
if (!isInitialized) {
// XXX: When the embedded scene is opened, the viewer needs to
// request focus for the root visual once so that a focus owner is
// set. This could also possibly be done in the FXFocusBehavior, but
// keeping it here we can limit 'knowledge' about the embedded
// window.
getRootPart().getVisual().requestFocus();
isInitialized = true;
}
}
@Override
public void reveal(IVisualPart<Node, ? extends Node> visualPart) {
if (visualPart == null) {
getCanvas().setHorizontalScrollOffset(0);
getCanvas().setVerticalScrollOffset(0);
} else {
getCanvas().reveal(visualPart.getVisual());
}
}
@Override
public ReadOnlyBooleanProperty viewerFocusedProperty() {
return viewerFocusedProperty.getReadOnlyProperty();
}
}