/
AbstractDomain.java
448 lines (400 loc) · 13.2 KB
/
AbstractDomain.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*******************************************************************************
* 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.domain;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IOperationHistoryListener;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.commands.operations.OperationHistoryEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.gef4.common.activate.ActivatableSupport;
import org.eclipse.gef4.common.adapt.AdaptableSupport;
import org.eclipse.gef4.common.adapt.AdapterKey;
import org.eclipse.gef4.common.adapt.IAdaptable;
import org.eclipse.gef4.common.adapt.inject.AdaptableScope;
import org.eclipse.gef4.common.adapt.inject.AdaptableScopes;
import org.eclipse.gef4.common.adapt.inject.InjectAdapters;
import org.eclipse.gef4.mvc.operations.AbstractCompositeOperation;
import org.eclipse.gef4.mvc.operations.ForwardUndoCompositeOperation;
import org.eclipse.gef4.mvc.operations.ITransactionalOperation;
import org.eclipse.gef4.mvc.operations.ReverseUndoCompositeOperation;
import org.eclipse.gef4.mvc.tools.ITool;
import org.eclipse.gef4.mvc.viewer.IViewer;
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyMapProperty;
import javafx.collections.ObservableMap;
/**
* The {@link AbstractDomain} can be used as a base class for {@link IDomain}
* implementations.
*
* @author anyssen
* @author mwienand
*
* @param <VR>
* The visual root node of the UI toolkit used, e.g.
* javafx.scene.Node in case of JavaFX.
*/
public abstract class AbstractDomain<VR> implements IDomain<VR> {
private static final int DEFAULT_UNDO_LIMIT = 128;
private static final UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
}
};
private ActivatableSupport acs = new ActivatableSupport(this);
private AdaptableSupport<IDomain<VR>> ads = new AdaptableSupport<IDomain<VR>>(
this);
private IOperationHistory operationHistory;
private IUndoContext undoContext;
private AbstractCompositeOperation transaction;
private Set<ITool<VR>> transactionContext = new HashSet<>();
private IOperationHistoryListener transactionListener = new IOperationHistoryListener() {
@Override
public void historyNotification(OperationHistoryEvent event) {
if (event.getEventType() == OperationHistoryEvent.ABOUT_TO_UNDO) {
if (!transactionContext.isEmpty() && transaction != null) {
if (transaction.getOperations().isEmpty()) {
for (ITool<VR> tool : transactionContext) {
closeExecutionTransaction(tool);
}
} else {
throw new IllegalStateException(
"Cannot perform UNDO while a currently open execution transaction contains operations.");
}
}
}
}
};
/**
* Creates a new {@link AbstractDomain} instance, setting the
* {@link AdaptableScope} for each of its {@link IAdaptable}-compliant types
* (super classes implementing {@link IAdaptable} and super-interfaces
* extending {@link IAdaptable}) to the newly created instance (see
* AdaptableScopes#scopeTo(IAdaptable)).
*/
public AbstractDomain() {
AdaptableScopes.enter(this);
// ensure uncaught exception handler is used
Thread.currentThread()
.setUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER);
}
@Override
public void activate() {
if (!acs.isActive()) {
acs.activate();
}
}
@Override
public ReadOnlyBooleanProperty activeProperty() {
return acs.activeProperty();
}
@Override
public ReadOnlyMapProperty<AdapterKey<?>, Object> adaptersProperty() {
return ads.adaptersProperty();
}
/**
* Applies the undo context to the given operation. May be overwritten by
* clients to filter out operations that should not be undoable in the given
* context.
*
* @param operation
* The {@link ITransactionalOperation} to apply the
* {@link #getUndoContext()} to.
*/
protected void applyUndoContext(ITransactionalOperation operation) {
// if (operation.isContentRelevant()) {
operation.addContext(getUndoContext());
// }
}
@Override
public void closeExecutionTransaction(ITool<VR> tool) {
// if (!transactionContext.contains(tool)) {
// throw new IllegalStateException(
// "No transaction active for tool " + tool + ".");
// }
if (!transactionContext.contains(tool)) {
return; // TODO: is this legal?
}
// remove tool from the transaction context and close the transaction in
// case the tool was the last one
if (transactionContext.size() == 1
&& transactionContext.contains(tool)) {
// Close transaction by adding it to the operation history in case
// it has an effect; all its nested operations have already been
// executed, thus it does not have to be executed again
if (transaction == null) {
throw new IllegalStateException(
"No transaction is currently active, while the transaction context sill contained tool "
+ tool + ".");
}
List<ITransactionalOperation> operations = transaction
.getOperations();
if (!operations.isEmpty()) {
// use the concatenation of the operations' labels as the
// transaction label
StringBuffer label = new StringBuffer();
int operationCount = operations.size();
for (int i = 0; i < operationCount; i++) {
label.append(operations.get(i).getLabel());
if (operations.size() - 1 > i) {
label.append(", ");
}
}
transaction.setLabel(label.toString());
// only add undo context if we have a content related change
applyUndoContext(transaction);
getOperationHistory().add(transaction);
}
transaction = null;
}
transactionContext.remove(tool);
}
/**
* Creates a {@link ForwardUndoCompositeOperation} which is used to store
* the operations within an execution transaction. The operation is opened
* on the {@link #getOperationHistory() operation history}.
*
* @return A new {@link ForwardUndoCompositeOperation} which is configured
* to store the operations within an execution transaction.
*/
protected AbstractCompositeOperation createExecutionTransaction() {
ReverseUndoCompositeOperation transaction = new ReverseUndoCompositeOperation(
Long.toString(System.currentTimeMillis()));
return transaction;
}
@Override
public void deactivate() {
if (acs.isActive()) {
acs.deactivate();
}
}
@Override
public void dispose() {
// dispose transaction related objects
operationHistory.removeOperationHistoryListener(transactionListener);
transactionListener = null;
transactionContext.clear();
transactionContext = null;
transaction = null;
// dispose operation history and undo context
operationHistory.dispose(undoContext, true, true, true);
operationHistory = null;
undoContext = null;
// clear adaptable scope
AdaptableScopes.leave(this);
// dispose adaptable and activatable support
ads.dispose();
ads = null;
acs = null;
}
/**
* @deprecated Use
* {@link #execute(ITransactionalOperation, IProgressMonitor)}
* instead. This method will be removed in GEF 5.0.0.
*/
@Deprecated
@Override
public void execute(ITransactionalOperation operation)
throws ExecutionException {
execute(operation, new NullProgressMonitor());
}
/**
* @since 1.1
*/
@Override
public void execute(ITransactionalOperation operation,
IProgressMonitor monitor) throws ExecutionException {
// reduce composite operations
if (operation instanceof AbstractCompositeOperation) {
operation = ((AbstractCompositeOperation) operation).unwrap(true);
}
// do not execute NoOps
if (operation.isNoOp()) {
return;
}
// check if we can execute operation
if (!operation.canExecute()) {
throw new IllegalArgumentException("Operation cannot be executed.");
}
if (transaction != null) {
// execute operation locally and add it to the current transaction
operation.execute(monitor, null);
transaction.add(operation);
} else {
// execute operation directly on operation history
applyUndoContext(operation);
getOperationHistory().execute(operation, monitor, null);
}
}
@Override
public <T> T getAdapter(AdapterKey<T> key) {
return ads.getAdapter(key);
}
@Override
public <T> T getAdapter(Class<T> classKey) {
return ads.<T> getAdapter(classKey);
}
@Override
public <T> T getAdapter(TypeToken<T> key) {
return ads.getAdapter(key);
}
@Override
public <T> AdapterKey<T> getAdapterKey(T adapter) {
return ads.getAdapterKey(adapter);
}
@Override
public ObservableMap<AdapterKey<?>, Object> getAdapters() {
return ads.getAdapters();
}
@Override
public <T> Map<AdapterKey<? extends T>, T> getAdapters(
Class<? super T> classKey) {
return ads.getAdapters(classKey);
}
@Override
public <T> Map<AdapterKey<? extends T>, T> getAdapters(
TypeToken<? super T> key) {
return ads.getAdapters(key);
}
@Override
public IOperationHistory getOperationHistory() {
return operationHistory;
}
@Override
public Map<AdapterKey<? extends ITool<VR>>, ITool<VR>> getTools() {
return ads.getAdapters(ITool.class);
}
@Override
public IUndoContext getUndoContext() {
return undoContext;
}
@Override
public Map<AdapterKey<? extends IViewer<VR>>, IViewer<VR>> getViewers() {
return ads.getAdapters(IViewer.class);
}
@Override
public boolean isActive() {
return acs.isActive();
}
/**
* Returns <code>true</code> if an execution transaction is currently open.
* Otherwise returns <code>false</code>.
*
* @return <code>true</code> if an execution transaction is currently open,
* otherwise <code>false</code>.
*/
protected boolean isExecutionTransactionOpen() {
return transaction != null;
}
@Override
public boolean isExecutionTransactionOpen(ITool<VR> tool) {
return transactionContext.contains(tool);
}
@Override
public void openExecutionTransaction(ITool<VR> tool) {
// if (transactionContext.contains(tool)) {
// throw new IllegalStateException(
// "A transaction is already active for tool " + tool + ".");
// }
// Create a new transaction in case the tool is the first one to open a
// transaction.
if (transactionContext.contains(tool)) {
return; // TODO: is this legal??
}
transactionContext.add(tool);
if (transactionContext.size() == 1
&& transactionContext.contains(tool)) {
if (transaction != null) {
throw new IllegalStateException(
"A transaction is already active, while this is the first tool within the transaction context.");
}
transaction = createExecutionTransaction();
}
}
@Override
public <T> void setAdapter(T adapter) {
ads.setAdapter(adapter);
}
@Override
public <T> void setAdapter(T adapter, String role) {
ads.setAdapter(adapter, role);
}
@Override
public <T> void setAdapter(TypeToken<T> adapterType, T adapter) {
ads.setAdapter(adapterType, adapter);
}
@InjectAdapters
@Override
public <T> void setAdapter(TypeToken<T> adapterType, T adapter,
String role) {
ads.setAdapter(adapterType, adapter, role);
}
/**
* Sets the {@link IOperationHistory} that is used by this
* {@link AbstractDomain} to the given value. Operation history listeners
* are un-/registered accordingly.
*
* @param operationHistory
* The new {@link IOperationHistory} for this domain.
*/
@Inject
public void setOperationHistory(IOperationHistory operationHistory) {
if (this.operationHistory != null
&& this.operationHistory != operationHistory) {
this.operationHistory
.removeOperationHistoryListener(transactionListener);
}
if (this.operationHistory != operationHistory) {
this.operationHistory = operationHistory;
if (this.operationHistory != null) {
this.operationHistory
.addOperationHistoryListener(transactionListener);
if (undoContext != null) {
this.operationHistory.setLimit(undoContext,
DEFAULT_UNDO_LIMIT);
}
}
}
}
/**
* Sets the {@link IUndoContext} that is used by this {@link AbstractDomain}
* to the given value.
*
* @param undoContext
* The new {@link IUndoContext} for this domain.
*/
@Inject
public void setUndoContext(IUndoContext undoContext) {
this.undoContext = undoContext;
if (operationHistory != null && undoContext != null) {
operationHistory.setLimit(undoContext, DEFAULT_UNDO_LIMIT);
}
}
@Override
public <T> void unsetAdapter(T adapter) {
ads.unsetAdapter(adapter);
}
}