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
|
/*****************************************************************************
* Copyright (c) 2015 Christian W. Damus 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:
* Christian W. Damus - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.sync;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.utils.AdapterUtils;
import org.eclipse.papyrus.infra.gmfdiag.common.Activator;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.GMFUnsafe;
import org.eclipse.papyrus.infra.sync.SyncBucket;
import org.eclipse.papyrus.infra.sync.SyncRegistry;
import org.eclipse.papyrus.infra.sync.service.AbstractSyncTrigger;
import org.eclipse.papyrus.infra.sync.service.ISyncAction;
import org.eclipse.papyrus.infra.sync.service.ISyncService;
/**
* Trigger to engage synchronization on edit parts that have the master/slave synchronization styles set.
*/
public abstract class EditPartSyncTrigger<M extends EObject, T extends EditPart, X> extends AbstractSyncTrigger {
private final Class<? extends SyncRegistry<M, T, X>> registryType;
public EditPartSyncTrigger(Class<? extends SyncRegistry<M, T, X>> registryType) {
super();
this.registryType = registryType;
}
@Override
public ISyncAction trigger(ISyncService syncService, Object object) {
ISyncAction result = null;
if (object instanceof EditPart) {
EditPart editPart = (EditPart) object;
Object model = editPart.getModel();
if (model instanceof View) {
SyncKind kind = SyncStyles.getSyncKind((View) model);
result = trigger(kind);
}
}
return result;
}
protected ISyncAction trigger(final SyncKind syncKind) {
return new ISyncAction() {
@Override
public IStatus perform(final ISyncService syncService, Object object) {
final IStatus[] result = { Status.OK_STATUS };
final EditPart editPart = (EditPart) object;
final TransactionalEditingDomain domain = TransactionUtil.getEditingDomain((View) editPart.getModel());
try {
GMFUnsafe.write(domain, new Runnable() {
@Override
public void run() {
result[0] = doTrigger(syncService, editPart, syncKind);
}
});
} catch (InterruptedException e) {
result[0] = new Status(IStatus.ERROR, Activator.ID, "Synchronization trigger was interrupted", e);
} catch (RollbackException e) {
result[0] = e.getStatus();
}
return result[0];
}
};
}
protected IStatus doTrigger(ISyncService syncService, EditPart editPart, SyncKind syncKind) {
IStatus result = Status.OK_STATUS;
SyncRegistry<M, T, X> registry = null;
M model = null;
T backend = null;
try {
registry = syncService.getSyncRegistry(registryType);
model = AdapterUtils.adapt(editPart, registry.getModelType(), null);
backend = registry.getBackendType().cast(editPart);
} catch (Exception e) {
result = new Status(IStatus.ERROR, Activator.ID, "Failed to access synchronization registry", e);
}
if ((registry != null) && (model != null)) {
switch (syncKind) {
case MASTER:
SyncBucket<M, T, X> bucket = registry.getBucket(model);
if (bucket == null) {
bucket = createSyncBucket(model, backend);
registry.register(bucket);
}
break;
case SLAVE:
registry.synchronize(backend);
break;
default:
result = new Status(IStatus.ERROR, Activator.ID, "Unsupported synchronization kind: " + syncKind, null);
break;
}
}
return result;
}
protected abstract SyncBucket<M, T, X> createSyncBucket(M model, T editPart);
}
|