KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IResourceChangeEvent;
15 import org.eclipse.core.resources.IResourceChangeListener;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.NullProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jdt.core.JavaCore;
21 import org.eclipse.pde.core.plugin.ModelEntry;
22 import org.eclipse.pde.internal.core.FeatureModelManager;
23 import org.eclipse.pde.internal.core.IFeatureModelDelta;
24 import org.eclipse.pde.internal.core.IFeatureModelListener;
25 import org.eclipse.pde.internal.core.IPluginModelListener;
26 import org.eclipse.pde.internal.core.PDECore;
27 import org.eclipse.pde.internal.core.PluginModelDelta;
28 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
29
30 /**
31  * Revalidates workspace features, on change in plug-ins or features
32  */

33 public class FeatureRebuilder implements IFeatureModelListener, IPluginModelListener, IResourceChangeListener {
34
35     private boolean fTouchFeatures;
36
37     public void start() {
38         PDECore.getDefault().getFeatureModelManager().addFeatureModelListener(this);
39         PDECore.getDefault().getModelManager().addPluginModelListener(this);
40         JavaCore.addPreProcessingResourceChangedListener(this, IResourceChangeEvent.PRE_BUILD);
41     }
42
43     public void stop() {
44         PDECore.getDefault().getFeatureModelManager().removeFeatureModelListener(this);
45         PDECore.getDefault().getModelManager().removePluginModelListener(this);
46         JavaCore.removePreProcessingResourceChangedListener(this);
47     }
48
49     public void modelsChanged(IFeatureModelDelta delta) {
50         if ((IFeatureModelDelta.ADDED & delta.getKind()) != 0
51                 || (IFeatureModelDelta.REMOVED & delta.getKind()) != 0)
52             fTouchFeatures = true;
53     }
54
55     public void modelsChanged(PluginModelDelta delta) {
56         if ((PluginModelDelta.ADDED & delta.getKind()) != 0
57                 || (PluginModelDelta.REMOVED & delta.getKind()) != 0) {
58             fTouchFeatures = true;
59         } else {
60             // listen for changes in checked/unchecked state
61
// of plug-ins on the Target Platform preference page.
62
// Only first entry will do, since workspace/target batch changes
63
// typically do not mix.
64
ModelEntry[] changed = delta.getChangedEntries();
65             if (changed.length > 0) {
66                 if (!changed[0].hasWorkspaceModels())
67                     touchFeatures();
68             }
69         }
70     }
71
72     public void resourceChanged(IResourceChangeEvent event) {
73         if (event.getType() == IResourceChangeEvent.PRE_BUILD && fTouchFeatures) {
74             touchFeatures();
75         }
76     }
77     
78     private void touchFeatures() {
79         FeatureModelManager manager = PDECore.getDefault().getFeatureModelManager();
80         IFeatureModel[] workspaceFeatures = manager.getWorkspaceModels();
81         if (workspaceFeatures.length > 0) {
82             IProgressMonitor monitor = new NullProgressMonitor();
83             monitor.beginTask("", workspaceFeatures.length); //$NON-NLS-1$
84
for (int i = 0; i < workspaceFeatures.length; i++) {
85                 try {
86                     IResource resource = workspaceFeatures[i].getUnderlyingResource();
87                     if (resource != null) {
88                         resource.touch(new SubProgressMonitor(monitor, 1));
89                     } else {
90                         monitor.worked(1);
91                     }
92                 } catch (CoreException e) {
93                 }
94             }
95         }
96         fTouchFeatures = false;
97     }
98
99 }
100
Popular Tags