KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > builders > PluginRebuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.builders;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IResourceChangeEvent;
20 import org.eclipse.core.resources.IResourceChangeListener;
21 import org.eclipse.core.resources.IWorkspaceRoot;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.osgi.service.resolver.BundleDelta;
26 import org.eclipse.osgi.service.resolver.State;
27 import org.eclipse.osgi.service.resolver.StateDelta;
28 import org.eclipse.pde.core.plugin.IPluginModelBase;
29 import org.eclipse.pde.core.plugin.PluginRegistry;
30 import org.eclipse.pde.internal.core.IStateDeltaListener;
31 import org.eclipse.pde.internal.core.PDECore;
32 import org.eclipse.pde.internal.core.WorkspaceModelManager;
33
34 public class PluginRebuilder implements IStateDeltaListener, IResourceChangeListener {
35     
36     private Set JavaDoc fProjectNames = new HashSet JavaDoc();
37     
38     private boolean fTouchWorkspace = false;
39
40     public void start() {
41         PDECore.getDefault().getModelManager().addStateDeltaListener(this);
42         JavaCore.addPreProcessingResourceChangedListener(this, IResourceChangeEvent.PRE_BUILD);
43     }
44
45     public void stop() {
46         PDECore.getDefault().getModelManager().removeStateDeltaListener(this);
47         JavaCore.removePreProcessingResourceChangedListener(this);
48     }
49
50     public void resourceChanged(IResourceChangeEvent event) {
51         if (event.getType() == IResourceChangeEvent.PRE_BUILD) {
52             IWorkspaceRoot root = PDECore.getWorkspace().getRoot();
53             if (fTouchWorkspace) {
54                 IProject[] projects = root.getProjects();
55                 for (int i = 0; i < projects.length; i++) {
56                     touchProject(projects[i]);
57                 }
58             } else {
59                 Iterator JavaDoc iter = fProjectNames.iterator();
60                 while (iter.hasNext()) {
61                     touchProject(root.getProject((String JavaDoc)iter.next()));
62                 }
63             }
64             fTouchWorkspace = false;
65             fProjectNames.clear();
66         }
67     }
68     
69     private void touchProject(IProject project) {
70         if (WorkspaceModelManager.isPluginProject(project)
71              && !WorkspaceModelManager.isBinaryProject(project)) {
72             try {
73                 // set session property on project
74
// to be read and reset in ManifestConsistencyChecker
75
project.setSessionProperty(PDECore.TOUCH_PROJECT, Boolean.TRUE);
76                 // touch project so that ManifestConsistencyChecker#build(..) gets invoked
77
project.touch(new NullProgressMonitor());
78             } catch (CoreException e) {
79                 PDECore.log(e);
80             }
81         }
82     }
83     
84     public void stateChanged(State newState) {
85         fTouchWorkspace = true;
86         fProjectNames.clear();
87     }
88
89     public void stateResolved(StateDelta delta) {
90         if (delta == null) {
91             // if delta is null, then target has changed
92
// prepare all projects for "touching"
93
fTouchWorkspace = true;
94             fProjectNames.clear();
95         } else {
96             BundleDelta[] deltas = delta.getChanges();
97             for (int i = 0; i < deltas.length; i++) {
98                 // only interested in workspace plug-ins that are affected by delta
99
// but not those who have caused it.
100
int type = deltas[i].getType();
101                 if ((type & BundleDelta.UPDATED) == BundleDelta.UPDATED
102                         || (type & BundleDelta.ADDED) == BundleDelta.ADDED
103                         || (type & BundleDelta.REMOVED) == BundleDelta.REMOVED)
104                     continue;
105             
106                 IPluginModelBase model = PluginRegistry.findModel(deltas[i].getBundle());
107                 IResource resource = model == null ? null : model.getUnderlyingResource();
108                 if (resource != null)
109                     fProjectNames.add(resource.getProject().getName());
110             }
111         }
112     }
113     
114 }
115
Popular Tags