/
NodeHidingBehavior.java
155 lines (136 loc) · 5.25 KB
/
NodeHidingBehavior.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
/*******************************************************************************
* 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:
* Matthias Wienand (itemis AG) - initial API & implementation
*
*******************************************************************************/
package org.eclipse.gef4.zest.fx.behaviors;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.gef4.mvc.behaviors.BehaviorUtils;
import org.eclipse.gef4.mvc.parts.IVisualPart;
import org.eclipse.gef4.zest.fx.models.HidingModel;
import org.eclipse.gef4.zest.fx.parts.HiddenNeighborsFeedbackPart;
import org.eclipse.gef4.zest.fx.parts.NodePart;
import javafx.collections.SetChangeListener;
import javafx.scene.Node;
/**
* The {@link NodeHidingBehavior} is specific to {@link NodePart}. It
* registers listeners on the {@link HidingModel} upon activation. When the
* {@link HidingModel} changes, the hidden status of the {@link NodePart}
* is determined. If the hidden status of the part changed, either
* {@link #hide()} or {@link #show()} will be called, respectively, to hide/show
* the part. Additionally, a {@link HiddenNeighborsFeedbackPart} is managed by
* this {@link NodeHidingBehavior}. The {@link HiddenNeighborsFeedbackPart}
* shows the number of hidden neighbors of the {@link NodePart}.
*
* @author mwienand
*
*/
// Only applicable for NodePart (see #getHost())
public class NodeHidingBehavior extends AbstractHidingBehavior {
private IVisualPart<Node, ? extends Node> hiddenNeighborsFeedbackPart;
private boolean containsAny(Set<org.eclipse.gef4.graph.Node> hidden, Set<org.eclipse.gef4.graph.Node> neighbors) {
boolean containsAny = false;
for (org.eclipse.gef4.graph.Node n : neighbors) {
if (hidden.contains(n)) {
containsAny = true;
break;
}
}
return containsAny;
}
/**
* Creates the {@link HiddenNeighborsFeedbackPart} that shows the hidden
* neighbors of the {@link NodePart} on which this
* {@link NodeHidingBehavior} is installed.
*/
protected void createHiddenNeighborsFeedbackPart() {
if (hiddenNeighborsFeedbackPart == null) {
// TODO: delegate to factory, ensure adaptable context is updated
// before
hiddenNeighborsFeedbackPart = new HiddenNeighborsFeedbackPart();
}
BehaviorUtils.<Node> addAnchoreds(getHost().getRoot(), Collections.singletonList(getHost()),
Collections.singletonList(hiddenNeighborsFeedbackPart));
}
@Override
protected boolean determineHiddenStatus() {
return getHidingModel().isHidden(getHost().getContent());
}
@Override
protected void doActivate() {
super.doActivate();
// create hidden neighbors part if it is already associated with our
// host
if (getHidingModel().hasHiddenNeighbors(getHost())) {
createHiddenNeighborsFeedbackPart();
}
}
@Override
protected void doDeactivate() {
// remove hidden neighbors part if it is currently associated with our
// host
if (getHidingModel().hasHiddenNeighbors(getHost())) {
removeHiddenNeighborsFeedbackPart();
}
super.doDeactivate();
}
/**
* Returns the {@link HiddenNeighborsFeedbackPart} that is managed by this
* {@link NodeHidingBehavior}.
*
* @return The {@link HiddenNeighborsFeedbackPart} that is managed by this
* {@link NodeHidingBehavior}.
*/
protected IVisualPart<Node, ? extends Node> getHiddenNeighborsFeedbackPart() {
return hiddenNeighborsFeedbackPart;
}
@Override
public NodePart getHost() {
return (NodePart) super.getHost();
}
@Override
protected void onHidingModelChange(SetChangeListener.Change<? extends org.eclipse.gef4.graph.Node> change) {
super.onHidingModelChange(change);
Set<org.eclipse.gef4.graph.Node> newHidden = new HashSet<>(change.getSet());
Set<org.eclipse.gef4.graph.Node> oldHidden = new HashSet<>(change.getSet());
oldHidden.remove(change.getElementAdded());
oldHidden.add(change.getElementRemoved());
// check if we have to show/hide/update the pruned neighbors part
org.eclipse.gef4.graph.Node content = getHost().getContent();
Set<org.eclipse.gef4.graph.Node> neighbors = content.getLocalNeighbors();
if (!containsAny(oldHidden, neighbors) && containsAny(newHidden, neighbors)) {
createHiddenNeighborsFeedbackPart();
} else if (containsAny(oldHidden, neighbors) && !containsAny(newHidden, neighbors)) {
removeHiddenNeighborsFeedbackPart();
} else {
// TODO: only necessary when neighbors change
if (hiddenNeighborsFeedbackPart != null) {
updateHiddenNeighborsFeedbackPart();
}
}
}
/**
* Removes the {@link HiddenNeighborsFeedbackPart} that is managed by this
* {@link NodeHidingBehavior}.
*/
protected void removeHiddenNeighborsFeedbackPart() {
BehaviorUtils.<Node> removeAnchoreds(getHost().getRoot(), Collections.singletonList(getHost()),
Collections.singletonList(hiddenNeighborsFeedbackPart));
}
/**
* Refreshes the {@link HiddenNeighborsFeedbackPart} that is managed by this
* {@link NodeHidingBehavior}.
*/
protected void updateHiddenNeighborsFeedbackPart() {
hiddenNeighborsFeedbackPart.refreshVisual();
}
}