KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > JavaElementChangeListener


1 /*******************************************************************************
2  * Copyright (c) 2005, 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;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.jdt.core.ElementChangedEvent;
23 import org.eclipse.jdt.core.IClasspathEntry;
24 import org.eclipse.jdt.core.IElementChangedListener;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IJavaElementDelta;
27 import org.eclipse.jdt.core.IJavaModel;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.IPackageFragment;
30 import org.eclipse.jdt.core.IPackageFragmentRoot;
31 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.core.JavaModelException;
33 import org.eclipse.pde.core.plugin.IPluginModelBase;
34 import org.eclipse.pde.core.plugin.PluginRegistry;
35
36
37 public class JavaElementChangeListener implements IElementChangedListener {
38     
39     private static final String JavaDoc FILENAME = "clean-cache.properties"; //$NON-NLS-1$
40

41     private Properties JavaDoc fTable = new Properties JavaDoc();
42
43     public void start() {
44         JavaCore.addElementChangedListener(this, ElementChangedEvent.POST_CHANGE);
45         load();
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.jdt.core.IElementChangedListener#elementChanged(org.eclipse.jdt.core.ElementChangedEvent)
50      */

51     public void elementChanged(ElementChangedEvent event) {
52         handleDelta(event.getDelta());
53     }
54     
55     public void shutdown() {
56         JavaCore.removeElementChangedListener(this);
57         save();
58     }
59     
60     private void handleDelta(IJavaElementDelta delta) {
61         IJavaElement element = delta.getElement();
62         
63         if (element instanceof IJavaModel) {
64             handleChildDeltas(delta);
65         } else if (element instanceof IJavaProject) {
66             if (isInterestingProject((IJavaProject)element)) {
67                 if (delta.getKind() == IJavaElementDelta.CHANGED) {
68                     handleChildDeltas(delta);
69                 } else if (delta.getKind() == IJavaElementDelta.ADDED) {
70                     updateTable(element);
71                 }
72             }
73         } else if (element instanceof IPackageFragmentRoot) {
74             handleChildDeltas(delta);
75         }
76     }
77     
78     private void handleChildDeltas(IJavaElementDelta delta) {
79         IJavaElementDelta[] deltas = delta.getAffectedChildren();
80         for (int i = 0; i < deltas.length; i++) {
81             if (ignoreDelta(deltas[i]))
82                 continue;
83             if (isInterestingDelta(deltas[i])) {
84                 updateTable(deltas[i].getElement());
85                 break;
86             }
87             handleDelta(deltas[i]);
88         }
89     }
90     
91     private boolean isInterestingDelta(IJavaElementDelta delta) {
92         int kind = delta.getKind();
93         boolean interestingKind = kind == IJavaElementDelta.ADDED
94                 || kind == IJavaElementDelta.REMOVED;
95         
96         IJavaElement element = delta.getElement();
97         boolean interestingElement = element instanceof IPackageFragment
98                 || element instanceof IPackageFragmentRoot;
99
100         if (interestingElement && interestingKind)
101             return true;
102         
103         if (kind == IJavaElementDelta.CHANGED && element instanceof IPackageFragmentRoot) {
104             IPackageFragmentRoot root = (IPackageFragmentRoot)element;
105             return root.isArchive();
106         }
107         return false;
108     }
109     
110     private boolean ignoreDelta(IJavaElementDelta delta) {
111         try {
112             IJavaElement element = delta.getElement();
113             if (element instanceof IPackageFragmentRoot) {
114                 IPackageFragmentRoot root = (IPackageFragmentRoot)element;
115                 IClasspathEntry entry = root.getRawClasspathEntry();
116                 if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
117                     return true;
118             }
119         } catch (JavaModelException e) {
120         }
121         return false;
122     }
123     
124     private boolean isInterestingProject(IJavaProject jProject) {
125         IProject project = jProject.getProject();
126         return WorkspaceModelManager.isPluginProject(project)
127                 && !WorkspaceModelManager.isBinaryProject(project)
128                 && !project.exists(ICoreConstants.MANIFEST_PATH);
129     }
130     
131     private void updateTable(IJavaElement element) {
132         IJavaProject jProject = (IJavaProject)element.getAncestor(IJavaElement.JAVA_PROJECT);
133         if (jProject != null) {
134             IProject project = jProject.getProject();
135             IPluginModelBase model = PluginRegistry.findModel(project);
136             if (model != null) {
137                 String JavaDoc id = model.getPluginBase().getId();
138                 if (id != null)
139                     fTable.put(id, Long.toString(System.currentTimeMillis()));
140             }
141         }
142     }
143     
144     private void save() {
145         // start by cleaning up extraneous keys.
146
Enumeration JavaDoc keys = fTable.keys();
147         while(keys.hasMoreElements()) {
148             String JavaDoc id = keys.nextElement().toString();
149             IPluginModelBase model = PluginRegistry.findModel(id);
150             if (model == null || model.getUnderlyingResource() == null)
151                 fTable.remove(id);
152         }
153         
154         FileOutputStream JavaDoc stream = null;
155         try {
156             stream = new FileOutputStream JavaDoc(new File JavaDoc(getDirectory(), FILENAME));
157             fTable.store(stream, "Cached timestamps"); //$NON-NLS-1$
158
stream.flush();
159         } catch (IOException JavaDoc e) {
160             PDECore.logException(e);
161         } finally {
162             try {
163                 if (stream != null)
164                     stream.close();
165             } catch (IOException JavaDoc e1) {
166             }
167         }
168     }
169     
170     private File JavaDoc getDirectory() {
171         IPath path = PDECore.getDefault().getStateLocation().append(".cache"); //$NON-NLS-1$
172
File JavaDoc directory = new File JavaDoc(path.toOSString());
173         if (!directory.exists() || !directory.isDirectory())
174             directory.mkdirs();
175         return directory;
176     }
177     
178     private void load() {
179         FileInputStream JavaDoc is = null;
180         try {
181             File JavaDoc file = new File JavaDoc(getDirectory(), FILENAME);
182             if (file.exists() && file.isFile()) {
183                 is = new FileInputStream JavaDoc(file);
184                 fTable.load(is);
185             }
186         } catch (IOException JavaDoc e) {
187         } finally {
188             try {
189                 if (is != null) {
190                     is.close();
191                 }
192             } catch (IOException JavaDoc e1) {
193             }
194         }
195     }
196     
197     public void synchronizeManifests(File JavaDoc cacheDirectory) {
198         Enumeration JavaDoc keys = fTable.keys();
199         while (keys.hasMoreElements()) {
200             String JavaDoc id = keys.nextElement().toString();
201             IPluginModelBase model = PluginRegistry.findModel(id);
202             if (model != null) {
203                 File JavaDoc file = new File JavaDoc(cacheDirectory, id + "_" + model.getPluginBase().getVersion() + ".MF"); //$NON-NLS-1$ //$NON-NLS-2$
204
if (file.exists() && file.isFile() && file.lastModified() < Long.parseLong(fTable.get(id).toString()))
205                     file.delete();
206             }
207         }
208     }
209
210 }
211
Popular Tags