KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > builders > FeatureRebuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.builders;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IncrementalProjectBuilder;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.core.runtime.Preferences;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.SubProgressMonitor;
24 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
25 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
26 import org.eclipse.core.runtime.jobs.Job;
27 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
28 import org.eclipse.pde.internal.PDE;
29 import org.eclipse.pde.internal.PDEMessages;
30 import org.eclipse.pde.internal.core.IFeatureModelDelta;
31 import org.eclipse.pde.internal.core.IFeatureModelListener;
32 import org.eclipse.pde.internal.core.IPluginModelListener;
33 import org.eclipse.pde.internal.core.PDECore;
34 import org.eclipse.pde.internal.core.PluginModelDelta;
35 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
36
37 /**
38  * Revalidates workspace features, on change in plug-ins or features
39  */

40 public class FeatureRebuilder implements IFeatureModelListener,
41         IPluginModelListener, Preferences.IPropertyChangeListener {
42     private boolean fAutoBuilding;
43
44     private boolean fLastJobPending = false;
45
46     private Preferences fResourcesPreferences;
47
48     public FeatureRebuilder() {
49         super();
50         fResourcesPreferences = ResourcesPlugin.getPlugin()
51                 .getPluginPreferences();
52         fAutoBuilding = fResourcesPreferences
53                 .getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING);
54     }
55
56     public void start() {
57         PDECore.getDefault().getFeatureModelManager().addFeatureModelListener(
58                 this);
59         PDECore.getDefault().getModelManager().addPluginModelListener(this);
60         fResourcesPreferences.addPropertyChangeListener(this);
61
62     }
63
64     public void stop() {
65         fResourcesPreferences.removePropertyChangeListener(this);
66         PDECore.getDefault().getFeatureModelManager()
67                 .removeFeatureModelListener(this);
68         PDECore.getDefault().getModelManager().removePluginModelListener(this);
69     }
70
71     public void modelsChanged(IFeatureModelDelta delta) {
72         if ((IFeatureModelDelta.ADDED & delta.getKind()) != 0
73                 || (IFeatureModelDelta.REMOVED & delta.getKind()) != 0)
74             buildWorkspaceFeatures();
75     }
76
77     public void modelsChanged(PluginModelDelta delta) {
78         if ((PluginModelDelta.ADDED & delta.getKind()) != 0
79                 || (PluginModelDelta.REMOVED & delta.getKind()) != 0)
80             buildWorkspaceFeatures();
81     }
82
83     public void propertyChange(PropertyChangeEvent event) {
84         if (!event.getProperty().equals(ResourcesPlugin.PREF_AUTO_BUILDING))
85             return;
86         // get the new value of auto-build directly from the preferences
87
boolean wasAutoBuilding = fAutoBuilding;
88         fAutoBuilding = fResourcesPreferences
89                 .getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING);
90         // force a build if autobuild has been turned on
91
if (!wasAutoBuilding && fAutoBuilding) {
92             buildWorkspaceFeatures();
93         }
94     }
95
96     private synchronized void buildWorkspaceFeatures() {
97         if (fLastJobPending) {
98             // previously scheduled job will do all features
99
return;
100         }
101         if (!fAutoBuilding) {
102             return;
103         }
104         Job buildJob = new Job(PDEMessages.FeatureConsistencyTrigger_JobName) { //$NON-NLS-1$
105
/*
106              * (non-Javadoc)
107              *
108              * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
109              */

110             public boolean belongsTo(Object JavaDoc family) {
111                 return ResourcesPlugin.FAMILY_AUTO_BUILD == family;
112             }
113
114             //$NON-NLS-1$
115
/*
116              * (non-Javadoc)
117              *
118              * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
119              */

120             protected IStatus run(IProgressMonitor monitor) {
121                 if (!fAutoBuilding) {
122                     return Status.OK_STATUS;
123                 }
124                 try {
125                     IFeatureModel[] workspaceFeatures = PDECore.getDefault()
126                             .getWorkspaceModelManager().getFeatureModels();
127                     monitor.beginTask("", workspaceFeatures.length); //$NON-NLS-1$
128
for (int i = 0; i < workspaceFeatures.length; i++) {
129                         IResource res = workspaceFeatures[i]
130                                 .getUnderlyingResource();
131                         if (res == null) {
132                             monitor.worked(1);
133                             continue;
134                         }
135                         IProject projectToBuild = res.getProject();
136                         if (!projectToBuild.isOpen()) {
137                             monitor.worked(1);
138                             continue;
139                         }
140                         if (monitor.isCanceled()) {
141                             throw new OperationCanceledException();
142                         }
143                         try {
144                             if (projectToBuild.hasNature(PDE.FEATURE_NATURE)) {
145                                 projectToBuild.build(
146                                         IncrementalProjectBuilder.FULL_BUILD,
147                                         PDE.FEATURE_BUILDER_ID, null,
148                                         new SubProgressMonitor(monitor, 1));
149                             } else {
150                                 monitor.worked(1);
151                             }
152                         } catch (CoreException e) {
153                         }
154
155                     }
156                 } catch (OperationCanceledException e) {
157                     return Status.CANCEL_STATUS;
158                 } finally {
159                     monitor.done();
160                 }
161                 return Status.OK_STATUS;
162             }
163         };
164         buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory()
165                 .buildRule());
166         fLastJobPending = true;
167         buildJob.addJobChangeListener(new JobChangeAdapter() {
168             public void aboutToRun(IJobChangeEvent event) {
169                 super.aboutToRun(event);
170                 fLastJobPending = false;
171                 // don't listen to "done", other another job might have been
172
// scheduled by then
173
event.getJob().removeJobChangeListener(this);
174             }
175
176             public void done(IJobChangeEvent event) {
177                 super.done(event);
178                 // aboutToRun was not called, the job did not run
179
fLastJobPending = false;
180             }
181
182         });
183         buildJob.schedule(200);
184     }
185
186 }
187
Popular Tags