Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5d3a70a41b0c380996ed569e894a9e41ff887408 (plain) (blame)
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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.core.resource;

import java.io.IOException;

import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewInvalidationEvent;
import org.eclipse.emf.cdo.view.CDOViewSet;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.papyrus.cdo.core.IPapyrusRepository;
import org.eclipse.papyrus.cdo.core.IPapyrusRepositoryManager;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
import org.eclipse.papyrus.infra.services.resourceloading.OnDemandLoadingModelSet;

import com.google.common.collect.Iterables;

/**
 * This is the CDOAwareModelSet type. Enjoy.
 */
public class CDOAwareModelSet extends OnDemandLoadingModelSet {

	private final ThreadLocal<Boolean> inGetResource = new ThreadLocal<Boolean>();

	private final IPapyrusRepositoryManager repositoryManager;

	private IPapyrusRepository repository;

	private IListener invalidationListener;

	public CDOAwareModelSet(IPapyrusRepositoryManager repositoryManager) {
		super();
		setTrackingModification(false);

		this.repositoryManager = repositoryManager;
	}

	@Override
	public EObject getEObject(URI uri, boolean loadOnDemand) {
		return CDOUtils.isCDOURI(uri) ? basicGetEObject(uri, loadOnDemand) : super.getEObject(uri, loadOnDemand);
	}

	protected EObject basicGetEObject(URI uri, boolean loadOnDemand) {
		Resource resource = getResource(uri.trimFragment(), loadOnDemand);

		return (resource == null) ? null : resource.getEObject(uri.fragment());
	}

	@Override
	public Resource getResource(URI uri, boolean loadOnDemand) {
		Boolean oldValue = inGetResource.get();
		inGetResource.set(Boolean.TRUE);

		try {
			return super.getResource(uri, loadOnDemand);
		} finally {
			inGetResource.set(oldValue);
		}
	}

	boolean isInGetResource() {
		return inGetResource.get() == Boolean.TRUE;
	}

	@Override
	public Resource createResource(URI uri, String contentType) {
		initTransaction(uri);
		return super.createResource(uri, contentType);
	}

	@Override
	protected void demandLoad(Resource resource) throws IOException {

		if(CDOUtils.isCDOURI(resource.getURI())) {
			// XML options not applicable to CDO resources
			resource.load(null);

			resourceLoadedHook(resource);
		} else {
			super.demandLoad(resource);
		}
	}

	protected void resourceLoadedHook(Resource resource) {
		for(Diagram next : Iterables.filter(resource.getContents(), Diagram.class)) {

			DawnDiagramUpdater.initializeElement(next);
		}
	}

	public CDOView getCDOView() {
		CDOViewSet viewSet = CDOUtil.getViewSet(this);
		CDOView[] views = (viewSet == null) ? null : viewSet.getViews();

		return ((views != null) && (views.length > 0)) ? views[0] : null;
	}

	@Override
	public void createModels(URI newURI) {
		initTransaction(newURI);
		super.createModels(newURI);
	}

	@Override
	public void loadModels(URI uri) throws ModelMultiException {

		initTransaction(uri);
		super.loadModels(uri);
	}

	protected void initTransaction(URI uri) {
		if(getCDOView() == null) {
			// get the repository and start a transaction on it

			if(repository == null) {
				repository = repositoryManager.getRepositoryForURI(uri);
			}

			if(repository != null) {
				repository.createTransaction(this);
				CDOView view = getCDOView();
				if(view != null) {
					view.addListener(getInvalidationListener());
				}
			}
		}
	}

	@Override
	public void unload() {
		if((repository != null) && (getCDOView() != null)) {
			CDOView view = getCDOView();
			if(view != null) {
				view.removeListener(getInvalidationListener());
			}
			invalidationListener = null;

			// dispose the transaction
			repository.close(this);
		}

		repository = null;

		super.unload();
	}

	protected final IListener getInvalidationListener() {
		if(invalidationListener == null) {
			invalidationListener = createInvalidationListener();
		}
		return invalidationListener;
	}

	protected IListener createInvalidationListener() {
		return new IListener() {

			public void notifyEvent(IEvent event) {
				if(event instanceof CDOViewInvalidationEvent) {
					TransactionalEditingDomain domain = getTransactionalEditingDomain();
					if(domain instanceof CDOAwareTransactionalEditingDomain) {
						((CDOAwareTransactionalEditingDomain)domain).fireResourceSetChanged((CDOViewInvalidationEvent)event);
					}
				}
			}
		};
	}
}

Back to the top