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: 3a1d2e792a5c21326cb9b7219a1c36b7da390e61 (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
/*******************************************************************************
 * Copyright (c) 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 Feb 17, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.eclipse.jst.j2ee.navigator.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.SWTException;

/**
 * @author Admin
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EMFRootObjectProvider implements  IResourceChangeListener, IResourceDeltaVisitor{
	private final HashMap emfModelCache = new HashMap();
	private final List listeners = new ArrayList();
	
	public interface IRefreshHandlerListener {
		void onRefresh(Object element);
	}
	
	/**
	 *  
	 */
	public EMFRootObjectProvider() {
		super();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
	}
	
	public Object[] getModels(IProject project){
		try {
		EMFModelManager modelManager = (EMFModelManager) emfModelCache.get(project);
		if (modelManager == null) {
			synchronized (emfModelCache) {
				modelManager= EMFModelManagerFactory.createEMFModelManager(project,this) ;
				emfModelCache.put(project,modelManager);
			}
		}
		return modelManager.getModels();
		} catch (Exception ex) {
			//ex.printStackTrace();
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
	 */
	public void resourceChanged(IResourceChangeEvent event) {
		final IResourceDelta delta = event.getDelta();

		if (delta != null) {
			try {
				delta.accept(EMFRootObjectProvider.this);
			} catch (CoreException e) {
				Logger.getLogger().logError(e);
			} catch (SWTException swte) {
				Logger.getLogger().logError(swte);
			} catch (SWTError swte) {
				Logger.getLogger().logError(swte);
			}
		}
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
	 */
	public boolean visit(IResourceDelta delta) throws CoreException {
		IResource resource = delta.getResource();
		if (resource != null) {
			switch (resource.getType()) {
				case IResource.ROOT :
					return true;
				case IResource.PROJECT :
					boolean projectOpenStateChanged = ((delta.getFlags() & IResourceDelta.OPEN) != 0);
					if (delta.getKind() == IResourceDelta.REMOVED || projectOpenStateChanged) {
						IProject project = (IProject) resource;
						dispose(project);
					}
					return false;
			}
		}
		return false;
	}
	
	private void dispose(IProject project) {
		if (project == null) return;
		EMFModelManager modelManager = (EMFModelManager) emfModelCache.remove(project);
		if (modelManager != null) {
			modelManager.dispose();
			modelManager = null;
		}
	}
	public void dispose() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		EMFModelManager modelManager = null;
		Object[] keys = emfModelCache.keySet().toArray();
		for (int i = 0; i < keys.length; i++) {
			modelManager = (EMFModelManager) emfModelCache.remove(keys[i]);
			if (modelManager != null) {
				modelManager.dispose();
			}
		}
	}
	

	/**
	 * @param aProject
	 */
	public void notifyListeners(IProject aProject) {
		for (int x = 0; x < listeners.size(); ++x) {
			IRefreshHandlerListener refreshHandler = (IRefreshHandlerListener) listeners.get(x);
			refreshHandler.onRefresh(aProject);
		}

	}
	
	public void addRefreshHandlerListener(IRefreshHandlerListener aListener) {
		synchronized (getListeners()) {
			if (aListener != null && !getListeners().contains(aListener))
				getListeners().add(aListener);
		}
	}

	public void removeRefreshHandlerListener(IRefreshHandlerListener aListener) {
		synchronized (listeners) {
			listeners.remove(aListener);
		}
	}

	/**
	 * Insert the method's description here. Creation date: (4/11/2001 4:42:58 PM)
	 * 
	 * @return java.util.List
	 */
	protected java.util.List getListeners() {
		return listeners;
	}
	

}

Back to the top