001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso;
005:
006: import org.eclipse.core.filebuffers.FileBuffers;
007: import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
008: import org.eclipse.core.filebuffers.IFileBuffer;
009: import org.eclipse.core.filebuffers.IFileBufferListener;
010: import org.eclipse.core.resources.IFile;
011: import org.eclipse.core.runtime.IPath;
012: import org.eclipse.jdt.core.ICompilationUnit;
013: import org.eclipse.jdt.core.JavaCore;
014: import org.eclipse.jface.text.IDocument;
015: import org.eclipse.ui.PlatformUI;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019:
020: /**
021: * Hackey way to see that a Java module is being loaded into an editor.
022: * We might need to inspect it at load time.
023: *
024: * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant
025: * @see TcPlugin.inspect
026: */
027:
028: public class JavaSetupParticipant implements IDocumentSetupParticipant {
029: private static JavaSetupParticipant m_setupParticipant;
030: private ArrayList<IFile> m_fileList;
031:
032: public JavaSetupParticipant() {
033: m_setupParticipant = this ;
034: m_fileList = new ArrayList<IFile>();
035:
036: FileBuffers.getTextFileBufferManager().addFileBufferListener(
037: new IFileBufferListener() {
038: public void bufferCreated(IFileBuffer buffer) {
039: IFile file = fileForBuffer(buffer);
040:
041: if (file != null && shouldInspect(file)) {
042: m_fileList.add(file);
043: PlatformUI.getWorkbench().getDisplay()
044: .asyncExec(new Inspector(file));
045: }
046: }
047:
048: public void bufferDisposed(IFileBuffer buffer) {
049: IFile file = fileForBuffer(buffer);
050:
051: if (file != null) {
052: m_fileList.remove(file);
053: }
054: }
055:
056: public void underlyingFileMoved(IFileBuffer buffer,
057: IPath path) {
058: IFile file;
059: Iterator iter = m_fileList.iterator();
060:
061: while (iter.hasNext()) {
062: file = (IFile) iter.next();
063:
064: if (!file.exists()) {
065: iter.remove();
066: }
067: }
068:
069: m_fileList.add(FileBuffers
070: .getWorkspaceFileAtLocation(path));
071: }
072:
073: public void underlyingFileDeleted(IFileBuffer buffer) {
074: IFile file = fileForBuffer(buffer);
075:
076: if (file != null) {
077: m_fileList.remove(file);
078: }
079: }
080:
081: public void bufferContentAboutToBeReplaced(
082: IFileBuffer buffer) {/**/
083: }
084:
085: public void bufferContentReplaced(IFileBuffer buffer) {/**/
086: }
087:
088: public void stateChanging(IFileBuffer buffer) {/**/
089: }
090:
091: public void dirtyStateChanged(IFileBuffer buffer,
092: boolean isDirty) {/**/
093: }
094:
095: public void stateValidationChanged(
096: IFileBuffer buffer, boolean isStateValidated) {/**/
097: }
098:
099: public void stateChangeFailed(IFileBuffer buffer) {/**/
100: }
101: });
102: }
103:
104: private IFile fileForBuffer(IFileBuffer buffer) {
105: return FileBuffers.getWorkspaceFileAtLocation(buffer
106: .getLocation());
107: }
108:
109: private boolean shouldInspect(IFile file) {
110: IPath path = file.getLocation();
111: String extension = path.getFileExtension();
112:
113: return extension.equals("java")
114: && TcPlugin.getDefault().hasTerracottaNature(
115: file.getProject());
116: }
117:
118: public void setup(IDocument document) {/**/
119: }
120:
121: static class Inspector implements Runnable {
122: private IFile m_file;
123:
124: Inspector(IFile file) {
125: m_file = file;
126: }
127:
128: public void run() {
129: if (m_file.exists()) {
130: ICompilationUnit cu = JavaCore
131: .createCompilationUnitFrom(m_file);
132:
133: if (cu != null) {
134: TcPlugin.getDefault().inspect(cu);
135: }
136: }
137: }
138: }
139:
140: protected void inspectAllBuffers() {
141: IFile file;
142: Iterator iter = m_fileList.iterator();
143:
144: while (iter.hasNext()) {
145: file = (IFile) iter.next();
146:
147: if (file.exists()) {
148: (new Inspector(file)).run();
149: } else {
150: iter.remove();
151: }
152: }
153: }
154:
155: /**
156: * Inspect each file associated with a Java buffer that is open.
157: * This is called by ConfigurationEditor after the user had manually modified
158: * the config (XML) document. This causes the graphical adornments to be synchronized
159: * with the configuration.
160: */
161: public static void inspectAll() {
162: if (m_setupParticipant != null) {
163: m_setupParticipant.inspectAllBuffers();
164: }
165: }
166: }
|