/
system-edge-creation-tool.ts
227 lines (203 loc) · 8 KB
/
system-edge-creation-tool.ts
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
/********************************************************************************
* Copyright (c) 2024 CrossBreeze.
********************************************************************************/
import {
Action,
Connectable,
CreateEdgeOperation,
CursorCSS,
Disposable,
DragAwareMouseListener,
EdgeCreationTool,
EnableDefaultToolsAction,
FeedbackEdgeEndMovingMouseListener,
GEdge,
GLSPActionDispatcher,
GModelElement,
HoverFeedbackAction,
ITypeHintProvider,
ModifyCSSFeedbackAction,
Point,
TriggerEdgeCreationAction,
cursorFeedbackAction,
findParentByFeature,
isConnectable,
isCtrlOrCmd
} from '@eclipse-glsp/client';
import {
DrawFeedbackEdgeAction,
RemoveFeedbackEdgeAction
} from '@eclipse-glsp/client/lib/features/tools/edge-creation/dangling-edge-feedback';
import { injectable } from '@theia/core/shared/inversify';
const CSS_EDGE_CREATION = 'edge-creation';
@injectable()
export class SystemEdgeCreationTool extends EdgeCreationTool {
override doEnable(): void {
const mouseMovingFeedback = new FeedbackEdgeEndMovingMouseListener(this.anchorRegistry, this.feedbackDispatcher);
const listener = new SystemEdgeCreationToolMouseListener(this.triggerAction, this.actionDispatcher, this.typeHintProvider, this);
this.toDisposeOnDisable.push(
listener,
mouseMovingFeedback,
this.mouseTool.registerListener(listener),
this.mouseTool.registerListener(mouseMovingFeedback),
this.registerFeedback([], this, [
RemoveFeedbackEdgeAction.create(),
cursorFeedbackAction(),
ModifyCSSFeedbackAction.create({ remove: [CSS_EDGE_CREATION] })
])
);
}
}
export interface ConnectionContext {
element?: GModelElement & Connectable;
canConnect?: boolean;
}
export interface DragConnectionContext {
element: GModelElement & Connectable;
dragStart: Point;
}
export class SystemEdgeCreationToolMouseListener extends DragAwareMouseListener implements Disposable {
protected source?: string;
protected target?: string;
protected proxyEdge: GEdge;
protected dragContext?: DragConnectionContext;
protected mouseMoveFeedback?: Disposable;
protected sourceFeedback?: Disposable;
constructor(
protected triggerAction: TriggerEdgeCreationAction,
protected actionDispatcher: GLSPActionDispatcher,
protected typeHintProvider: ITypeHintProvider,
protected tool: EdgeCreationTool
) {
super();
this.proxyEdge = new GEdge();
this.proxyEdge.type = triggerAction.elementTypeId;
}
protected isSourceSelected(): boolean {
return this.source !== undefined;
}
protected isTargetSelected(): boolean {
return this.target !== undefined;
}
override mouseDown(target: GModelElement, event: MouseEvent): Action[] {
const result = super.mouseDown(target, event);
if (event.button === 0 && !this.isSourceSelected()) {
// update the current target
const context = this.calculateContext(target, event);
if (context.element && context.canConnect) {
this.dragContext = { element: context.element, dragStart: { x: event.clientX, y: event.clientY } };
}
}
return result;
}
override mouseMove(target: GModelElement, event: MouseEvent): Action[] {
const result = super.mouseMove(target, event);
if (this.isMouseDrag && this.dragContext && !this.isSourceSelected()) {
const dragDistance = Point.maxDistance(this.dragContext.dragStart, { x: event.clientX, y: event.clientY });
if (dragDistance > 3) {
// assign source if possible
this.source = this.dragContext.element.id;
this.tool.registerFeedback([
DrawFeedbackEdgeAction.create({ elementTypeId: this.triggerAction.elementTypeId, sourceId: this.source })
]);
this.dragContext = undefined;
}
}
this.updateFeedback(target, event);
return result;
}
override draggingMouseUp(target: GModelElement, event: MouseEvent): Action[] {
const result = super.draggingMouseUp(target, event);
if (this.isSourceSelected()) {
const context = this.calculateContext(target, event);
if (context.element && context.canConnect) {
this.target = context.element.id;
result.push(
CreateEdgeOperation.create({
elementTypeId: this.triggerAction.elementTypeId,
sourceElementId: this.source!,
targetElementId: this.target,
args: this.triggerAction.args
})
);
if (!isCtrlOrCmd(event)) {
result.push(EnableDefaultToolsAction.create());
}
}
}
this.reinitialize();
return result;
}
override nonDraggingMouseUp(_element: GModelElement, event: MouseEvent): Action[] {
this.reinitialize();
return [EnableDefaultToolsAction.create()];
}
protected canConnect(element: GModelElement | undefined, role: 'source' | 'target'): boolean {
return (
!!element &&
!!isConnectable(element) &&
element.canConnect(this.proxyEdge, role) &&
(role !== 'target' || this.source !== element?.id)
);
}
protected updateFeedback(target: GModelElement, event: MouseEvent): void {
const context = this.calculateContext(target, event);
// source element feedback
if (this.isSourceSelected()) {
this.sourceFeedback = this.tool.registerFeedback(
[HoverFeedbackAction.create({ mouseoverElement: this.source!, mouseIsOver: true })],
this.proxyEdge,
[HoverFeedbackAction.create({ mouseoverElement: this.source!, mouseIsOver: false })]
);
}
// cursor feedback
if (!context.element || context.element?.id === this.source) {
// by default we want to use the edge creation CSS when the tool is active
this.registerFeedback(
[ModifyCSSFeedbackAction.create({ add: [CSS_EDGE_CREATION] })],
[ModifyCSSFeedbackAction.create({ remove: [CSS_EDGE_CREATION] })]
);
return;
}
if (!context.canConnect) {
this.registerFeedback([cursorFeedbackAction(CursorCSS.OPERATION_NOT_ALLOWED)], [cursorFeedbackAction()]);
return;
}
const cursorCss = this.isSourceSelected() ? CursorCSS.EDGE_CREATION_TARGET : CursorCSS.EDGE_CREATION_SOURCE;
this.registerFeedback(
[cursorFeedbackAction(cursorCss), HoverFeedbackAction.create({ mouseoverElement: context.element.id, mouseIsOver: true })],
[cursorFeedbackAction(), HoverFeedbackAction.create({ mouseoverElement: context.element.id, mouseIsOver: false })]
);
}
protected registerFeedback(feedbackActions: Action[], cleanupActions?: Action[]): Disposable {
this.mouseMoveFeedback?.dispose();
this.mouseMoveFeedback = this.tool.registerFeedback(feedbackActions, this, cleanupActions);
return this.mouseMoveFeedback;
}
protected calculateContext(target: GModelElement, event: MouseEvent, previousContext?: ConnectionContext): ConnectionContext {
const context: ConnectionContext = {};
context.element = findParentByFeature(target, isConnectable);
if (previousContext && previousContext.element === context.element) {
return previousContext;
}
if (!this.isSourceSelected()) {
context.canConnect = this.canConnect(context.element, 'source');
} else if (!this.isTargetSelected()) {
context.canConnect = this.canConnect(context.element, 'target');
} else {
context.canConnect = false;
}
return context;
}
protected reinitialize(): void {
this.source = undefined;
this.target = undefined;
this.tool.registerFeedback([RemoveFeedbackEdgeAction.create()]);
this.dragContext = undefined;
this.mouseMoveFeedback?.dispose();
this.sourceFeedback?.dispose();
}
dispose(): void {
this.reinitialize();
}
}