1
/*******************************************************************************
2
* Copyright (c) 2007, 2010 Intel Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Intel Corporation - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.managedbuilder.core;
13
import java.util.Arrays;
14
import java.util.HashSet;
17
import org.eclipse.cdt.core.CCProjectNature;
18
import org.eclipse.cdt.core.CProjectNature;
19
import org.eclipse.cdt.core.settings.model.util.ResourceChangeHandlerBase;
20
import org.eclipse.cdt.managedbuilder.internal.core.ManagedBuildInfo;
21
import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
22
import org.eclipse.cdt.managedbuilder.internal.dataprovider.ConfigurationDataProvider;
23
import org.eclipse.core.resources.IProject;
24
import org.eclipse.core.resources.IProjectDescription;
25
import org.eclipse.core.resources.IResource;
26
import org.eclipse.core.resources.IResourceChangeEvent;
27
import org.eclipse.core.resources.IResourceDelta;
28
import org.eclipse.core.resources.IResourceDeltaVisitor;
29
import org.eclipse.core.resources.IWorkspace;
30
import org.eclipse.core.resources.IWorkspaceRoot;
31
import org.eclipse.core.resources.ResourcesPlugin;
32
import org.eclipse.core.runtime.CoreException;
33
import org.eclipse.core.runtime.IProgressMonitor;
34
import org.eclipse.core.runtime.IStatus;
35
import org.eclipse.core.runtime.Status;
36
import org.eclipse.core.runtime.jobs.Job;
39
* @noextend This class is not intended to be subclassed by clients.
40
* @noinstantiate This class is not intended to be instantiated by clients.
42
class ResourceChangeHandler2 extends ResourceChangeHandlerBase{
43
private class RcMoveHandler implements IResourceMoveHandler {
48
public void handleProjectClose(IProject project) {
51
ManagedBuildManager.setLoaddedBuildInfo(project, null);
52
} catch (CoreException e) {
56
public boolean handleResourceMove(IResource fromRc, IResource toRc) {
57
switch(fromRc.getType()){
58
case IResource.PROJECT:
59
IProject fromProject = fromRc.getProject();
60
IProject toProject = toRc.getProject();
61
ManagedBuildInfo info = (ManagedBuildInfo)ManagedBuildManager.getBuildInfo(fromProject, false);
63
info.updateOwner(toRc);
64
ManagedBuildManager.updateLoaddedInfo(fromProject, toProject, info);
70
public boolean handleResourceRemove(IResource rc) {
72
case IResource.PROJECT:
73
IProject project = rc.getProject();
76
ManagedBuildManager.setLoaddedBuildInfo(project, null);
77
} catch (CoreException e) {
85
protected IResourceMoveHandler createResourceMoveHandler(
86
IResourceChangeEvent event) {
87
return new RcMoveHandler();
90
public void sendClose(IProject project){
91
sendClose(ManagedBuildManager.getBuildInfo(project,false));
94
private void sendClose(IManagedBuildInfo info){
96
IManagedProject managedProj = info.getManagedProject();
97
if (managedProj != null) {
98
IConfiguration cfgs[] = managedProj.getConfigurations();
100
for(int i = 0; i < cfgs.length; i++)
101
ManagedBuildManager.performValueHandlerEvent(cfgs[i], IManagedOptionValueHandler.EVENT_CLOSE, true);
106
private static class Visitor implements IResourceDeltaVisitor {
107
private Set<IProject> fProjSet;
109
Visitor(Set<IProject> projSet){
113
public boolean visit(IResourceDelta delta) throws CoreException {
114
IResource rc = delta.getResource();
115
switch (rc.getType()) {
118
case IResource.PROJECT:
119
int flags = delta.getFlags();
120
if((flags & IResourceDelta.DESCRIPTION) == IResourceDelta.DESCRIPTION){
121
IProject project = rc.getProject();
122
IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
123
if(info != null && info.isValid() && info.getManagedProject() != null){
124
IProjectDescription eDes = project.getDescription();
125
IConfiguration[] cfgs = info.getManagedProject().getConfigurations();
126
String natureIds[] = eDes.getNatureIds();
127
for(int i = 0; i < cfgs.length; i++){
128
String cachedIds[] = ConfigurationDataProvider.getNaturesIdsUsedOnCache(cfgs[i]);
129
if(checkNaturesNeedUpdate(cachedIds, natureIds)){
131
fProjSet = new HashSet<IProject>();
133
fProjSet.add(project);
145
Set<IProject> getProjSet(){
151
private static boolean checkNaturesNeedUpdate(String[] oldIds, String[] newIds){
155
Set<String> oldSet = new HashSet<String>(Arrays.asList(oldIds));
156
Set<String> oldSetCopy = new HashSet<String>(oldSet);
157
Set<String> newSet = new HashSet<String>(Arrays.asList(newIds));
158
oldSet.removeAll(newSet);
159
newSet.removeAll(oldSetCopy);
160
if(oldSet.contains(CProjectNature.C_NATURE_ID)
161
|| oldSet.contains(CCProjectNature.CC_NATURE_ID)
162
|| newSet.contains(CProjectNature.C_NATURE_ID)
163
|| newSet.contains(CCProjectNature.CC_NATURE_ID))
170
public void resourceChanged(IResourceChangeEvent event) {
171
super.resourceChanged(event);
173
switch(event.getType()){
174
case IResourceChangeEvent.POST_CHANGE:
175
IResourceDelta delta = event.getDelta();
177
Visitor visitor = new Visitor(null);
179
delta.accept(visitor);
180
} catch (CoreException e) {
181
ManagedBuilderCorePlugin.log(e);
183
postProcess(visitor.getProjSet());
188
private void postProcess(final Set<IProject> projSet){
189
if(projSet == null || projSet.size() == 0)
192
IWorkspace wsp = ResourcesPlugin.getWorkspace();
193
IWorkspaceRoot root = wsp.getRoot();
195
Job job = new Job(ManagedMakeMessages.getString("ResourceChangeHandler2.0")){ //$NON-NLS-1$
198
protected IStatus run(IProgressMonitor monitor) {
199
for (IProject project : projSet) {
201
ManagedBuildManager.updateCoreSettings(project);
202
} catch (CoreException e) {
203
ManagedBuilderCorePlugin.log(e);
206
return Status.OK_STATUS;