KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > generatedfile > WorkingCopyCleanupListener


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * mkaufman@bea.com - initial API and implementation
10  *******************************************************************************/

11
12
13 package org.eclipse.jdt.apt.core.internal.generatedfile;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jdt.apt.core.internal.AptPlugin;
17 import org.eclipse.jdt.core.ElementChangedEvent;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IElementChangedListener;
20 import org.eclipse.jdt.core.IJavaElementDelta;
21 import org.eclipse.jdt.core.IJavaProject;
22
23 /**
24  * Used by the GeneratedFileManager in order to clean up working copies after a build
25  */

26 public class WorkingCopyCleanupListener implements IElementChangedListener
27 {
28     public void elementChanged(ElementChangedEvent event)
29     {
30         Object JavaDoc o = event.getSource();
31         if ( o instanceof IJavaElementDelta )
32         {
33             IJavaElementDelta delta = (IJavaElementDelta) o;
34             processElementDelta( delta );
35
36         }
37     }
38     
39     private void processElementDelta( IJavaElementDelta delta )
40     {
41         IJavaElementDelta[] deltas = delta.getAffectedChildren();
42         if ( deltas != null && deltas.length > 0 )
43         {
44             for( int i = 0; i<deltas.length; i++ )
45                 processElementDelta( deltas[i] );
46         }
47         if ( delta.getElement() instanceof ICompilationUnit )
48         {
49             //
50
// handle case where a working copy is discarded (e.g., an editor is closed). If an editor
51
// is not open, then the compilation unit's isWorkingCopy() will return false.
52
//
53

54             ICompilationUnit cu = (ICompilationUnit) delta.getElement();
55
56             boolean workingCopyDiscarded =
57                 cu.getOwner() == null ? !cu.isWorkingCopy() : !cu.exists();
58             
59             if ( workingCopyDiscarded )
60             {
61                 IJavaProject jp = cu.getJavaProject();
62                 GeneratedFileManager gfm = AptPlugin.getAptProject(jp).getGeneratedFileManager();
63                 try {
64                     gfm.workingCopyDiscarded( cu );
65                 } catch (CoreException e) {
66                     AptPlugin.log(e, "Failure processing delta: " + delta); //$NON-NLS-1$
67
}
68             }
69         }
70     }
71 }
72
Popular Tags