Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1600741da7210a9d4889f17d8fadc2cfe986e77 (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
/*******************************************************************************
 *  Copyright (c) 2011  Oracle. 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: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.jaxb.core.internal.resource.jaxbindex;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IFile;
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.core.runtime.content.IContentType;
import org.eclipse.jpt.common.core.JptResourceModel;
import org.eclipse.jpt.common.utility.internal.iterables.SnapshotCloneIterable;
import org.eclipse.jpt.jaxb.core.JaxbProject;
import org.eclipse.jpt.jaxb.core.JaxbResourceModelProvider;
import org.eclipse.jpt.jaxb.core.JptJaxbCorePlugin;
import org.eclipse.jpt.jaxb.core.resource.jaxbindex.JaxbIndexResource;


public class JaxbIndexResourceModelProvider
		implements JaxbResourceModelProvider {
	
	// singleton
	private static final JaxbIndexResourceModelProvider INSTANCE = new JaxbIndexResourceModelProvider();
	
	/**
	 * Return the singleton.
	 */
	public static JaxbIndexResourceModelProvider instance() {
		return INSTANCE;
	}
	
	
	private final Map<IFile, JaxbIndexResourceImpl> models = new HashMap<IFile, JaxbIndexResourceImpl>();
	
	private final IResourceChangeListener resourceChangeListener;
	
	
	/**
	 * Enforce singleton usage
	 */
	private JaxbIndexResourceModelProvider() {
		super();
		this.resourceChangeListener = buildResourceChangeListener();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this.resourceChangeListener);
	}
	
	
	protected IResourceChangeListener buildResourceChangeListener() {
		return new IResourceChangeListener() {	
			public void resourceChanged(IResourceChangeEvent event) {
				handleResourceChangeEvent(event);
			}
		};
	}
	
	public IContentType getContentType() {
		return JptJaxbCorePlugin.JAXB_INDEX_CONTENT_TYPE;
	}
	
	public JptResourceModel buildResourceModel(JaxbProject jaxbProject, IFile file) {
		return buildResourceModel(file);
	}
	
	public JaxbIndexResource buildResourceModel(IFile file) {
		JaxbIndexResourceImpl resource = this.models.get(file);
		if (resource != null) {
			return resource;
		}
		resource = new JaxbIndexResourceImpl(file);
		this.models.put(file, resource);
		return resource;
	}
	
	protected void handleResourceChangeEvent(IResourceChangeEvent event) {
		switch (event.getType()) {
			case IResourceChangeEvent.PRE_CLOSE :
			case IResourceChangeEvent.PRE_DELETE :
				removeProjectResources((IProject) event.getResource());
				return;
			
			case IResourceChangeEvent.POST_CHANGE :
				updateResources(event.getDelta());
		}
	}
	
	protected void removeProjectResources(IProject removedProject) {
		for (IFile file : new SnapshotCloneIterable<IFile>(this.models.keySet())) {
			if (file.getProject().equals(removedProject)) {
				removeResource(file);
			}
		}
	}
	
	protected void updateResources(IResourceDelta delta) {
		try {
			delta.accept(new ResourceDeltaVisitor());
		}
		catch (CoreException ce) {
			// shouldn't happen
			JptJaxbCorePlugin.log(ce);
		}
	}
	
	protected void removeResource(IFile file) {
		this.models.remove(file);
	}
	
	protected void updateResource(IFile file) {
		JaxbIndexResourceImpl resource = this.models.get(file);
		if (resource != null) {
			resource.update();
		}
	}
	
	
	protected class ResourceDeltaVisitor
			implements IResourceDeltaVisitor {
		
		public boolean visit(IResourceDelta delta) {
			IResource res = delta.getResource();
			switch (res.getType()) {
				case IResource.ROOT :
					return true;  // visit children
				case IResource.PROJECT :
					return true;  // visit children
				case IResource.FOLDER :
					return true;  // visit children
				case IResource.FILE :
					fileChanged((IFile) res, delta.getKind());
					return false;  // no children
				default :
					return false;  // no children (probably shouldn't get here...)
			}
		}
		
		protected void fileChanged(IFile file, int deltaKind) {
			if (deltaKind == IResourceDelta.REMOVED) {
				JaxbIndexResourceModelProvider.this.removeResource(file);
			}
			else if (deltaKind == IResourceDelta.CHANGED) {
				JaxbIndexResourceModelProvider.this.updateResource(file);
			}
		}
	}
}

Back to the top