KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > JavaSetupParticipant


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso;
5
6 import org.eclipse.core.filebuffers.FileBuffers;
7 import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
8 import org.eclipse.core.filebuffers.IFileBuffer;
9 import org.eclipse.core.filebuffers.IFileBufferListener;
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.jdt.core.ICompilationUnit;
13 import org.eclipse.jdt.core.JavaCore;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.ui.PlatformUI;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 /**
21  * Hackey way to see that a Java module is being loaded into an editor.
22  * We might need to inspect it at load time.
23  *
24  * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant
25  * @see TcPlugin.inspect
26  */

27
28 public class JavaSetupParticipant implements IDocumentSetupParticipant {
29   private static JavaSetupParticipant m_setupParticipant;
30   private ArrayList JavaDoc<IFile> m_fileList;
31   
32   public JavaSetupParticipant() {
33     m_setupParticipant = this;
34     m_fileList = new ArrayList JavaDoc<IFile>();
35     
36     FileBuffers.getTextFileBufferManager().addFileBufferListener(
37       new IFileBufferListener() {
38         public void bufferCreated(IFileBuffer buffer) {
39           IFile file = fileForBuffer(buffer);
40             
41           if(file != null && shouldInspect(file)) {
42             m_fileList.add(file);
43             PlatformUI.getWorkbench().getDisplay().asyncExec(new Inspector(file));
44           }
45         }
46                                                                  
47         public void bufferDisposed(IFileBuffer buffer) {
48           IFile file = fileForBuffer(buffer);
49           
50           if(file != null) {
51             m_fileList.remove(file);
52           }
53         }
54
55         public void underlyingFileMoved(IFileBuffer buffer, IPath path) {
56           IFile file;
57           Iterator JavaDoc iter = m_fileList.iterator();
58
59           while(iter.hasNext()) {
60             file = (IFile)iter.next();
61             
62             if(!file.exists()) {
63               iter.remove();
64             }
65           }
66
67           m_fileList.add(FileBuffers.getWorkspaceFileAtLocation(path));
68         }
69         
70         public void underlyingFileDeleted(IFileBuffer buffer) {
71           IFile file = fileForBuffer(buffer);
72           
73           if(file != null) {
74             m_fileList.remove(file);
75           }
76         }
77         
78         public void bufferContentAboutToBeReplaced(IFileBuffer buffer) {/**/}
79         public void bufferContentReplaced(IFileBuffer buffer) {/**/}
80         public void stateChanging(IFileBuffer buffer) {/**/}
81         public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {/**/}
82         public void stateValidationChanged(IFileBuffer buffer, boolean isStateValidated) {/**/}
83         public void stateChangeFailed(IFileBuffer buffer) {/**/}
84       });
85   }
86   
87   private IFile fileForBuffer(IFileBuffer buffer) {
88     return FileBuffers.getWorkspaceFileAtLocation(buffer.getLocation());
89   }
90     
91   private boolean shouldInspect(IFile file) {
92     IPath path = file.getLocation();
93     String JavaDoc extension = path.getFileExtension();
94     
95     return extension.equals("java") &&
96       TcPlugin.getDefault().hasTerracottaNature(file.getProject());
97   }
98   
99   public void setup(IDocument document) {/**/}
100   
101   class Inspector implements Runnable JavaDoc {
102     private IFile m_file;
103     
104     Inspector(IFile file) {
105       m_file = file;
106     }
107     
108     public void run() {
109       if(m_file.exists()) {
110         ICompilationUnit cu = JavaCore.createCompilationUnitFrom(m_file);
111         
112         if(cu != null) {
113           TcPlugin.getDefault().inspect(cu);
114         }
115       }
116     }
117   }
118   
119   protected void inspectAllBuffers() {
120     IFile file;
121     Iterator JavaDoc iter = m_fileList.iterator();
122     
123     while(iter.hasNext()) {
124       file = (IFile)iter.next();
125       
126       if(file.exists()) {
127         PlatformUI.getWorkbench().getDisplay().asyncExec(new Inspector(file));
128       }
129       else {
130         iter.remove();
131       }
132     }
133   }
134   
135   /**
136    * Inspect each file associated with a Java buffer that is open.
137    * This is called by ConfigurationEditor after the user had manually modified
138    * the config (XML) document. This causes the graphical adornments to be synchronized
139    * with the configuration.
140    */

141   public static void inspectAll() {
142     if(m_setupParticipant != null) {
143       m_setupParticipant.inspectAllBuffers();
144     }
145   }
146 }
147
Popular Tags