/
NormalizeConnectedOnDragHandler.java
158 lines (138 loc) · 4.38 KB
/
NormalizeConnectedOnDragHandler.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
/*******************************************************************************
* Copyright (c) 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 and implementation
*
*******************************************************************************/
package org.eclipse.gef.mvc.fx.handlers;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.gef.geometry.planar.Dimension;
import org.eclipse.gef.mvc.fx.models.SelectionModel;
import org.eclipse.gef.mvc.fx.parts.IContentPart;
import org.eclipse.gef.mvc.fx.parts.IVisualPart;
import org.eclipse.gef.mvc.fx.policies.BendConnectionPolicy;
import javafx.scene.Node;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
/**
* Triggers a normalization of the control points of all content parts that
* support {@link BendConnectionPolicy} and are anchored to the host of this
* policy.
*
* @author mwienand
*
*/
public class NormalizeConnectedOnDragHandler extends AbstractHandler
implements IOnDragHandler {
private Set<IVisualPart<? extends Node>> targetParts;
private boolean invalidGesture = false;
@Override
public void abortDrag() {
if (invalidGesture) {
return;
}
for (IVisualPart<? extends Node> part : targetParts) {
rollback(part.getAdapter(BendConnectionPolicy.class));
restoreRefreshVisuals(part);
}
targetParts = null;
}
/**
* Determines the target parts for this policy.
*
* @return The {@link IVisualPart} that should be considered as target
* parts.
*/
protected Set<IVisualPart<? extends Node>> determineTargetParts() {
Set<IVisualPart<? extends Node>> targetParts = Collections
.newSetFromMap(
new IdentityHashMap<IVisualPart<? extends Node>, Boolean>());
for (IVisualPart<? extends Node> anchored : getHost()
.getAnchoredsUnmodifiable()) {
if (anchored instanceof IContentPart) {
BendConnectionPolicy bendConnectionPolicy = anchored
.getAdapter(BendConnectionPolicy.class);
if (bendConnectionPolicy != null
&& !targetParts.contains(anchored)) {
targetParts.add(anchored);
}
}
}
// filter out selected
SelectionModel selectionModel = getHost().getRoot().getViewer()
.getAdapter(SelectionModel.class);
Iterator<IVisualPart<? extends Node>> it = targetParts.iterator();
while (it.hasNext()) {
IVisualPart<? extends Node> part = it.next();
if (part instanceof IContentPart && selectionModel
.isSelected((IContentPart<? extends Node>) part)) {
it.remove();
}
}
return targetParts;
}
@Override
public void drag(MouseEvent e, Dimension delta) {
if (invalidGesture) {
return;
}
for (IVisualPart<? extends Node> part : targetParts) {
part.getAdapter(BendConnectionPolicy.class).normalize();
}
}
@Override
public void endDrag(MouseEvent e, Dimension delta) {
if (invalidGesture) {
return;
}
for (IVisualPart<? extends Node> part : targetParts) {
commit(part.getAdapter(BendConnectionPolicy.class));
restoreRefreshVisuals(part);
}
targetParts = null;
}
@Override
public void hideIndicationCursor() {
}
/**
* Returns <code>true</code> if the given {@link MouseEvent} should trigger
* normalization. Otherwise returns <code>false</code>. Per default always
* returns <code>true</code> if there are target parts.
*
* @param event
* The {@link MouseEvent} in question.
* @return <code>true</code> to indicate that the given {@link MouseEvent}
* should trigger normalization, otherwise <code>false</code>.
*/
protected boolean isNormalize(MouseEvent event) {
return !targetParts.isEmpty();
}
@Override
public boolean showIndicationCursor(KeyEvent event) {
return false;
}
@Override
public boolean showIndicationCursor(MouseEvent event) {
return false;
}
@Override
public void startDrag(MouseEvent e) {
targetParts = determineTargetParts();
invalidGesture = !isNormalize(e);
if (invalidGesture) {
return;
}
for (IVisualPart<? extends Node> part : targetParts) {
storeAndDisableRefreshVisuals(part);
init(part.getAdapter(BendConnectionPolicy.class));
}
}
}