Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: a75b8652df9eff116f956ff865ba8c6b73293707 (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
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Apr 1, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.wst.common.internal.emf.utilities;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;

/**
 * @author DABERG
 * 
 * To change the template for this generated type comment go to Window>Preferences>Java>Code
 * Generation>Code and Comments
 */
public class IsLoadingProxyAdapter extends AdapterImpl {
	private Resource resource;
	private Adapter targetAdapter;
	private EObject targetObject;

	public IsLoadingProxyAdapter(Adapter aTargetAdapter, EObject aTargetObject) {
		targetAdapter = aTargetAdapter;
		targetObject = aTargetObject;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 */
	public void notifyChanged(Notification msg) {
		if (resource != null) {
			//listen for the remove of the loading adapter
			if (msg.getFeatureID(null) == Resource.RESOURCE__IS_LOADED && msg.getEventType() == Notification.SET) {
				removeProxySupport();
				reset();
			}
		} else if (cacheResource()) {
			targetAdapter.notifyChanged(msg);
			reset();
		}
	}

	/**
	 * Cache the resource variable and return true if we removed the proxy support.
	 */
	private boolean cacheResource() {
		if (getTarget() != null) {
			EObject eObj = (EObject) getTarget();
			resource = eObj.eResource();
			if (resource != null) {
				eObj.eAdapters().remove(this);
				if (ExtendedEcoreUtil.isLoading(resource))
					resource.eAdapters().add(this);
				else {
					targetObject.eAdapters().add(targetAdapter);
					return true;
				}
			}
		}
		return false;
	}

	/**
	 *  
	 */
	private void removeProxySupport() {
		getTarget().eAdapters().remove(this);
		targetObject.eAdapters().add(targetAdapter);
	}

	private void reset() {
		resource = null;
		targetAdapter = null;
		targetObject = null;
	}

}

Back to the top