Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e2ebe9dbc209a328ee07ffa3167806060f05d03 (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
/*****************************************************************************
 * Copyright (c) 2010 ATOS ORIGIN.
 *
 * 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:
 *  Tristan Faure (ATOS ORIGIN INTEGRATION) tristan.faure@atosorigin.com - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.resourceloading;

import java.util.List;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.services.controlmode.history.HistoryModel;
import org.eclipse.papyrus.infra.services.controlmode.history.utils.HistoryUtils;
import org.eclipse.papyrus.infra.services.controlmode.mm.history.ControledResource;

/**
 * This manager navigates in the history to access to controlled resources
 *
 * @author tfaure
 *
 */
public class HistoryRoutingManager {

	private ControledResourceAdapter adapter;

	private final IProxyManager proxyManager;

	public HistoryRoutingManager(IProxyManager proxyManager) {
		this.proxyManager = proxyManager;
	}

	/**
	 * Returns the eobject navigating through the history
	 *
	 * @param modelSet
	 * @param resourceURI
	 * @param fragment
	 * @return
	 */
	public EObject getEObject(ModelSet modelSet, String resourceURI, String fragment) {
		adapter = HistoryRoutingUtils.getControledResourceAdapter(modelSet);
		EObject result = null;
		HistoryModel historyModel = HistoryUtils.getHistoryModel(modelSet);
		if (historyModel != null) {
			List<ControledResource> res = adapter.getControledResource(resourceURI);
			if (res != null) {
				for (ControledResource controled : res) {
					for (ControledResource child : controled.getChildren()) {
						URI locationURI = URI.createURI(child.getResourceURL());
						URI toGet = locationURI.resolve(HistoryUtils.getURIFullPath(child.eResource().getURI()));
						Resource resource = modelSet.getResource(toGet, proxyManager.loadResource(locationURI));
						if (resource != null) {
							result = resource.getEObject(fragment);
							if (result != null) {
								break;
							} else {
								result = getEObject(modelSet, child.getResourceURL(), fragment);
								if (result != null) {
									break;
								}
							}
						}
					}
				}
			}
		}
		return result;
	}

	/**
	 * Unload the routing manager dropping the ControledResourceAdapter
	 */
	public void unload() {
		if (adapter != null) {
			Notifier resSet = adapter.getTarget();
			resSet.eAdapters().remove(adapter);
			adapter = null;
		}
	}

}

Back to the top