diff options
author | Kai Maetzel | 2003-08-13 22:00:57 +0000 |
---|---|---|
committer | Kai Maetzel | 2003-08-13 22:00:57 +0000 |
commit | a97c4d6e893127000d7d123f58015b32b145acc8 (patch) | |
tree | 5e50214498439327422c02a6ac92c081fdaf840e | |
parent | d6ebdd824cc7f4543f987dcf5c3919ac83ea04de (diff) | |
download | eclipse.platform.text-a97c4d6e893127000d7d123f58015b32b145acc8.tar.gz eclipse.platform.text-a97c4d6e893127000d7d123f58015b32b145acc8.tar.xz eclipse.platform.text-a97c4d6e893127000d7d123f58015b32b145acc8.zip |
enabling file buffers
11 files changed, 818 insertions, 147 deletions
diff --git a/org.eclipse.ui.editors/.classpath b/org.eclipse.ui.editors/.classpath index dc73243240d..144b19a40f0 100644 --- a/org.eclipse.ui.editors/.classpath +++ b/org.eclipse.ui.editors/.classpath @@ -12,6 +12,7 @@ <classpathentry kind="src" path="/org.eclipse.ui.workbench"/> <classpathentry kind="src" path="/org.eclipse.ui.views"/> <classpathentry kind="src" path="/org.eclipse.ui.workbench.texteditor"/> + <classpathentry kind="src" path="/org.eclipse.core.filebuffers"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="output" path="bin"/> </classpath> diff --git a/org.eclipse.ui.editors/.project b/org.eclipse.ui.editors/.project index dbddebe826d..2bd93caa223 100644 --- a/org.eclipse.ui.editors/.project +++ b/org.eclipse.ui.editors/.project @@ -4,6 +4,7 @@ <comment></comment> <projects> <project>org.eclipse.core.boot</project> + <project>org.eclipse.core.filebuffers</project> <project>org.eclipse.core.resources</project> <project>org.eclipse.core.runtime</project> <project>org.eclipse.jface</project> diff --git a/org.eclipse.ui.editors/plugin.xml b/org.eclipse.ui.editors/plugin.xml index 1fe36c0b9f3..7f257de6386 100644 --- a/org.eclipse.ui.editors/plugin.xml +++ b/org.eclipse.ui.editors/plugin.xml @@ -21,6 +21,7 @@ <import plugin="org.eclipse.jface.text"/> <import plugin="org.eclipse.core.resources"/> <import plugin="org.eclipse.update.core"/> + <import plugin="org.eclipse.core.filebuffers"/> </requires> <!-- quick diff reference provider extension point --> @@ -107,4 +108,13 @@ id="org.eclipse.ui.preferencePages.TextEditor"> </page> </extension> + + <extension point="org.eclipse.ui.documentProviders"> + <provider + class="org.eclipse.ui.editors.text.TextFileDocumentProvider" + inputTypes="org.eclipse.ui.IStorageEditorInput" + id="org.eclipse.ui.editors.text.TextFileDocumentProvider"> + </provider> + </extension> + </plugin> diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextFileDocumentProvider.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextFileDocumentProvider.java new file mode 100644 index 00000000000..2e9c1b987c8 --- /dev/null +++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/TextFileDocumentProvider.java @@ -0,0 +1,634 @@ +/********************************************************************** +Copyright (c) 2000, 2003 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + IBM Corporation - Initial implementation +**********************************************************************/ +package org.eclipse.ui.editors.text; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.IFileBuffer; +import org.eclipse.core.filebuffers.IFileBufferListener; +import org.eclipse.core.filebuffers.IFileBufferManager; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; + +import org.eclipse.jface.text.Assert; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.IAnnotationModel; + +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.FileEditorInput; +import org.eclipse.ui.texteditor.IDocumentProvider; +import org.eclipse.ui.texteditor.IDocumentProviderExtension; +import org.eclipse.ui.texteditor.IDocumentProviderExtension2; +import org.eclipse.ui.texteditor.IDocumentProviderExtension3; +import org.eclipse.ui.texteditor.IElementStateListener; +import org.eclipse.ui.texteditor.IElementStateListenerExtension; +import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel; + +/** + * @since 3.0 + */ +public class TextFileDocumentProvider implements IDocumentProvider, IDocumentProviderExtension, IDocumentProviderExtension2, IDocumentProviderExtension3, IStorageDocumentProvider { + + + static protected interface IStorageDocumentProvider2 extends IDocumentProvider, IDocumentProviderExtension, IDocumentProviderExtension2, IDocumentProviderExtension3, IStorageDocumentProvider { + } + + static protected class NullProvider implements IStorageDocumentProvider2 { + public void connect(Object element) throws CoreException {} + public void disconnect(Object element) {} + public IDocument getDocument(Object element) { return null; } + public void resetDocument(Object element) throws CoreException {} + public void saveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {} + public long getModificationStamp(Object element) { return 0; } + public long getSynchronizationStamp(Object element) { return 0; } + public boolean isDeleted(Object element) { return true; } + public boolean mustSaveDocument(Object element) { return false; } + public boolean canSaveDocument(Object element) { return false; } + public IAnnotationModel getAnnotationModel(Object element) { return null; } + public void aboutToChange(Object element) {} + public void changed(Object element) {} + public void addElementStateListener(IElementStateListener listener) {} + public void removeElementStateListener(IElementStateListener listener) {} + public boolean isReadOnly(Object element) { return true; } + public boolean isModifiable(Object element) { return false; } + public void validateState(Object element, Object computationContext) throws CoreException {} + public boolean isStateValidated(Object element) { return true; } + public void updateStateCache(Object element) throws CoreException {} + public void setCanSaveDocument(Object element) {} + public IStatus getStatus(Object element) { return null; } + public void synchronize(Object element) throws CoreException {} + public void setProgressMonitor(IProgressMonitor progressMonitor) {} + public IProgressMonitor getProgressMonitor() { return null; } + public boolean isSynchronized(Object element) { return true; } + public String getDefaultEncoding() { return null; } + public String getEncoding(Object element) { return null; } + public void setEncoding(Object element, String encoding) {} + } + + static protected class FileInfo { + public Object fElement; + public int fCount; + public ITextFileBuffer fTextFileBuffer; + public IAnnotationModel fModel; + } + + protected class FileBufferListener implements IFileBufferListener { + + public FileBufferListener() { + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#bufferContentAboutToBeReplaced(org.eclipse.core.buffer.text.IBufferedFile) + */ + public void bufferContentAboutToBeReplaced(IFileBuffer file) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + IElementStateListener l= (IElementStateListener) e.next(); + l.elementContentAboutToBeReplaced(getElement(file)); + } + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#bufferContentReplaced(org.eclipse.core.buffer.text.IBufferedFile) + */ + public void bufferContentReplaced(IFileBuffer file) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + IElementStateListener l= (IElementStateListener) e.next(); + l.elementContentReplaced(getElement(file)); + } + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#stateChanging(org.eclipse.core.buffer.text.IBufferedFile) + */ + public void stateChanging(IFileBuffer file) { + Object element= getElement(file); + fireElementStateChanging(element); + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#dirtyStateChanged(org.eclipse.core.buffer.text.IBufferedFile, boolean) + */ + public void dirtyStateChanged(IFileBuffer file, boolean isDirty) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + IElementStateListener l= (IElementStateListener) e.next(); + l.elementDirtyStateChanged(getElement(file), isDirty); + } + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#stateValidationChanged(org.eclipse.core.buffer.text.IBufferedFile, boolean) + */ + public void stateValidationChanged(IFileBuffer file, boolean isStateValidated) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + Object l= e.next(); + if (l instanceof IElementStateListenerExtension) { + IElementStateListenerExtension x= (IElementStateListenerExtension) l; + x.elementStateValidationChanged(getElement(file), isStateValidated); + } + } + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#underlyingFileMoved(org.eclipse.core.buffer.text.IBufferedFile, org.eclipse.core.resources.IFile) + */ + public void underlyingFileMoved(IFileBuffer file, IFile target) { + IEditorInput input= target == null ? null : new FileEditorInput(target); + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + IElementStateListener l= (IElementStateListener) e.next(); + l.elementMoved(getElement(file), input); + } + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#underlyingFileDeleted(org.eclipse.core.buffer.text.IBufferedFile) + */ + public void underlyingFileDeleted(IFileBuffer file) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + IElementStateListener l= (IElementStateListener) e.next(); + l.elementDeleted(getElement(file)); + } + } + + /* + * @see org.eclipse.core.buffer.text.IBufferedFileListener#stateChangeFailed(org.eclipse.core.buffer.text.IBufferedFile) + */ + public void stateChangeFailed(IFileBuffer file) { + Object element= getElement(file); + fireElementStateChangeFailed(element); + } + } + + + + /** The parent document provider */ + private IStorageDocumentProvider2 fParentProvider; + /** Element information of all connected elements */ + private Map fFileInfoMap= new HashMap(); + /** The list of element state listeners */ + private List fElementStateListeners= new ArrayList(); + /** The buffered file listener */ + private IFileBufferListener fBufferedFileListener= new FileBufferListener(); + /** The progress monitor */ + private IProgressMonitor fProgressMonitor; + + + public TextFileDocumentProvider() { + fParentProvider= new NullProvider(); + IFileBufferManager manager= FileBuffers.getTextFileBufferManager(); + manager.addFileBufferListener(fBufferedFileListener); + } + + public void setParentDocumentProvider(IDocumentProvider parentProvider) { + fParentProvider= (IStorageDocumentProvider2) parentProvider; + if (fParentProvider == null) + fParentProvider= new NullProvider(); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#connect(java.lang.Object) + */ + public void connect(Object element) throws CoreException { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info == null) { + + info= createFileInfo(element); + if (info == null) { + fParentProvider.connect(element); + return; + } + + info.fElement= element; + fFileInfoMap.put(element, info); + } + ++ info.fCount; + } + + protected FileInfo createEmptyFileInfo() { + return new FileInfo(); + } + + protected FileInfo createFileInfo(Object element) throws CoreException { + if (element instanceof IFileEditorInput) { + IFileEditorInput input= (IFileEditorInput) element; + + ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); + manager.connect(input.getFile(), getProgressMonitor()); + ITextFileBuffer f= manager.getTextFileBuffer(input.getFile()); + + FileInfo info= createEmptyFileInfo(); + info.fTextFileBuffer= f; + info.fModel= createAnnotationModel(input.getFile()); + return info; + } + return null; + } + + protected IAnnotationModel createAnnotationModel(IFile file) { + return new ResourceMarkerAnnotationModel(file); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#disconnect(java.lang.Object) + */ + public void disconnect(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + + if (info == null) { + fParentProvider.disconnect(element); + return; + } + + if (info.fCount == 1) { + + fFileInfoMap.remove(element); + disposeFileInfo(element, info); + + } else + -- info.fCount; + } + + protected void disposeFileInfo(Object element, FileInfo info) { + IFileBufferManager manager= FileBuffers.getTextFileBufferManager(); + try { + manager.disconnect(info.fTextFileBuffer.getUnderlyingFile(), getProgressMonitor()); + } catch (CoreException x) { + handleCoreException(x, "FileDocumentProvider.disposeElementInfo"); + } + } + + protected Object getElement(IFileBuffer file) { + Iterator e= fFileInfoMap.keySet().iterator(); + while (e.hasNext()) { + Object key= e.next(); + FileInfo info= (FileInfo) fFileInfoMap.get(key); + if (info != null && file == info.fTextFileBuffer) + return info.fElement; + } + return null; + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#getDocument(java.lang.Object) + */ + public IDocument getDocument(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.getDocument(); + return fParentProvider.getDocument(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#resetDocument(java.lang.Object) + */ + public void resetDocument(Object element) throws CoreException { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + info.fTextFileBuffer.revert(getProgressMonitor()); + else + fParentProvider.resetDocument(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#saveDocument(org.eclipse.core.runtime.IProgressMonitor, java.lang.Object, org.eclipse.jface.text.IDocument, boolean) + */ + public void saveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + info.fTextFileBuffer.commit(monitor, overwrite); + else + fParentProvider.saveDocument(monitor, element, document, overwrite); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#getModificationStamp(java.lang.Object) + */ + public long getModificationStamp(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) { + File file= getSystemFile(info); + if (file != null) + return file.lastModified(); + return info.fTextFileBuffer.getUnderlyingFile().getModificationStamp(); + } + return fParentProvider.getModificationStamp(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#getSynchronizationStamp(java.lang.Object) + */ + public long getSynchronizationStamp(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return 0; + return fParentProvider.getSynchronizationStamp(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#isDeleted(java.lang.Object) + */ + public boolean isDeleted(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) { + File file= getSystemFile(info); + return file == null ? true : !file.exists(); + } + return fParentProvider.isDeleted(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#mustSaveDocument(java.lang.Object) + */ + public boolean mustSaveDocument(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return (info.fCount == 1) && info.fTextFileBuffer.isDirty(); + return fParentProvider.mustSaveDocument(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#canSaveDocument(java.lang.Object) + */ + public boolean canSaveDocument(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.isDirty(); + return fParentProvider.canSaveDocument(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#getAnnotationModel(java.lang.Object) + */ + public IAnnotationModel getAnnotationModel(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fModel; + return fParentProvider.getAnnotationModel(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#aboutToChange(java.lang.Object) + */ + public void aboutToChange(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info == null) + fParentProvider.aboutToChange(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#changed(java.lang.Object) + */ + public void changed(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info == null) + fParentProvider.changed(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#addElementStateListener(org.eclipse.ui.texteditor.IElementStateListener) + */ + public void addElementStateListener(IElementStateListener listener) { + Assert.isNotNull(listener); + if (!fElementStateListeners.contains(listener)) + fElementStateListeners.add(listener); + fParentProvider.addElementStateListener(listener); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProvider#removeElementStateListener(org.eclipse.ui.texteditor.IElementStateListener) + */ + public void removeElementStateListener(IElementStateListener listener) { + Assert.isNotNull(listener); + fElementStateListeners.remove(listener); + fParentProvider.removeElementStateListener(listener); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#isReadOnly(java.lang.Object) + */ + public boolean isReadOnly(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return isSystemFileReadOnly(info); + return fParentProvider.isReadOnly(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#isModifiable(java.lang.Object) + */ + public boolean isModifiable(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.isStateValidated() ? !isSystemFileReadOnly(info) : true; + return fParentProvider.isModifiable(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#validateState(java.lang.Object, java.lang.Object) + */ + public void validateState(Object element, Object computationContext) throws CoreException { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + info.fTextFileBuffer.validateState(getProgressMonitor(), computationContext); + else + fParentProvider.validateState(element, computationContext); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#isStateValidated(java.lang.Object) + */ + public boolean isStateValidated(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.isStateValidated(); + return fParentProvider.isStateValidated(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#updateStateCache(java.lang.Object) + */ + public void updateStateCache(Object element) throws CoreException { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info == null) + fParentProvider.updateStateCache(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#setCanSaveDocument(java.lang.Object) + */ + public void setCanSaveDocument(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info == null) + fParentProvider.setCanSaveDocument(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#getStatus(java.lang.Object) + */ + public IStatus getStatus(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.getStatus(); + return fParentProvider.getStatus(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#synchronize(java.lang.Object) + */ + public void synchronize(Object element) throws CoreException { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + info.fTextFileBuffer.revert(getProgressMonitor()); + else + fParentProvider.synchronize(element); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension2#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) + */ + public void setProgressMonitor(IProgressMonitor progressMonitor) { + fProgressMonitor= progressMonitor; + fParentProvider.setProgressMonitor(progressMonitor); + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension2#getProgressMonitor() + */ + public IProgressMonitor getProgressMonitor() { + return fProgressMonitor; + } + + /* + * @see org.eclipse.ui.texteditor.IDocumentProviderExtension3#isSynchronized(java.lang.Object) + */ + public boolean isSynchronized(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.getUnderlyingFile().isSynchronized(IResource.DEPTH_ZERO); + return fParentProvider.isSynchronized(element); + } + + /* + * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#getDefaultEncoding() + */ + public String getDefaultEncoding() { + return FileBuffers.getTextFileBufferManager().getDefaultEncoding(); + } + + /* + * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#getEncoding(java.lang.Object) + */ + public String getEncoding(Object element) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + return info.fTextFileBuffer.getEncoding(); + return fParentProvider.getEncoding(element); + } + + /* + * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#setEncoding(java.lang.Object, java.lang.String) + */ + public void setEncoding(Object element, String encoding) { + FileInfo info= (FileInfo) fFileInfoMap.get(element); + if (info != null) + info.fTextFileBuffer.setEncoding(encoding); + else + fParentProvider.setEncoding(element, encoding); + } + + /** + * Defines the standard procedure to handle <code>CoreExceptions</code>. Exceptions + * are written to the plug-in log. + * + * @param exception the exception to be logged + * @param message the message to be logged + */ + protected void handleCoreException(CoreException exception, String message) { + ILog log= Platform.getPlugin(PlatformUI.PLUGIN_ID).getLog(); + IStatus status= message != null ? new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, exception) : exception.getStatus(); + log.log(status); + } + + protected File getSystemFile(FileInfo info) { + IFile file= info.fTextFileBuffer.getUnderlyingFile(); + IPath path= file.getLocation(); + return path == null ? null : path.toFile(); + } + + protected boolean isSystemFileReadOnly(FileInfo info) { + File file= getSystemFile(info); + return file == null ? true : !file.canWrite(); + } + + protected FileInfo getFileInfo(Object element) { + return (FileInfo) fFileInfoMap.get(element); + } + + protected Iterator getConnectedElementsIterator() { + return fFileInfoMap.keySet().iterator(); + } + + protected Iterator getFileInfosIterator() { + return fFileInfoMap.values().iterator(); + } + + protected void fireElementStateChanging(Object element) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + Object l= e.next(); + if (l instanceof IElementStateListenerExtension) { + IElementStateListenerExtension x= (IElementStateListenerExtension) l; + x.elementStateChanging(element); + } + } + } + + protected void fireElementStateChangeFailed(Object element) { + List list= new ArrayList(fElementStateListeners); + Iterator e= list.iterator(); + while (e.hasNext()) { + Object l= e.next(); + if (l instanceof IElementStateListenerExtension) { + IElementStateListenerExtension x= (IElementStateListenerExtension) l; + x.elementStateChangeFailed(element); + } + } + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaDocumentProvider.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaDocumentProvider.java deleted file mode 100644 index 8ff64a983b9..00000000000 --- a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaDocumentProvider.java +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2003 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.ui.examples.javaeditor; - - -import org.eclipse.core.runtime.CoreException; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IDocumentPartitioner; -import org.eclipse.jface.text.rules.DefaultPartitioner; -import org.eclipse.ui.editors.text.FileDocumentProvider; -import org.eclipse.ui.examples.javaeditor.java.JavaPartitionScanner; - -/** - * The JavaDocumentProvider provides the IDocuments used by java editors. - */ - -public class JavaDocumentProvider extends FileDocumentProvider { - - private final static String[] TYPES= new String[] { JavaPartitionScanner.JAVA_DOC, JavaPartitionScanner.JAVA_MULTILINE_COMMENT }; - - private static JavaPartitionScanner fgScanner= null; - - public JavaDocumentProvider() { - super(); - } - - /* (non-Javadoc) - * Method declared on AbstractDocumentProvider - */ - protected IDocument createDocument(Object element) throws CoreException { - IDocument document= super.createDocument(element); - if (document != null) { - IDocumentPartitioner partitioner= createJavaPartitioner(); - document.setDocumentPartitioner(partitioner); - partitioner.connect(document); - } - return document; - } - - /** - * Return a partitioner for .java files. - */ - private IDocumentPartitioner createJavaPartitioner() { - return new DefaultPartitioner(getJavaPartitionScanner(), TYPES); - } - - /** - * Return a scanner for creating java partitions. - */ - private JavaPartitionScanner getJavaPartitionScanner() { - if (fgScanner == null) - fgScanner= new JavaPartitionScanner(); - return fgScanner; - } -} diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaDocumentSetupParticipant.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaDocumentSetupParticipant.java new file mode 100644 index 00000000000..45d4bf08a53 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaDocumentSetupParticipant.java @@ -0,0 +1,41 @@ +/********************************************************************** +Copyright (c) 2000, 2003 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + IBM Corporation - Initial implementation +**********************************************************************/ +package org.eclipse.ui.examples.javaeditor; + +import org.eclipse.core.filebuffers.IDocumentSetupParticipant; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentExtension3; +import org.eclipse.jface.text.IDocumentPartitioner; +import org.eclipse.jface.text.rules.DefaultPartitioner; + +/** + * + */ +public class JavaDocumentSetupParticipant implements IDocumentSetupParticipant { + + /** + */ + public JavaDocumentSetupParticipant() { + } + + /* + * @see org.eclipse.core.filebuffers.IDocumentSetupParticipant#setup(org.eclipse.jface.text.IDocument) + */ + public void setup(IDocument document) { + if (document instanceof IDocumentExtension3) { + IDocumentExtension3 extension3= (IDocumentExtension3) document; + IDocumentPartitioner partitioner= new DefaultPartitioner(JavaEditorExamplePlugin.getDefault().getJavaPartitionScanner(), JavaPartitionScanner.JAVA_PARTITION_TYPES); + extension3.setDocumentPartitioner(JavaEditorExamplePlugin.JAVA_PARTITIONING, partitioner); + partitioner.connect(document); + } + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditor.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditor.java index 7b59f2abfb2..9c4f4d3f4b0 100644 --- a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditor.java +++ b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditor.java @@ -62,7 +62,6 @@ public class JavaEditor extends TextEditor { * disposal actions required by the java editor. */ public void dispose() { - JavaEditorEnvironment.disconnect(this); if (fOutlinePage != null) fOutlinePage.setInput(null); super.dispose(); @@ -140,7 +139,6 @@ public class JavaEditor extends TextEditor { */ protected void initializeEditor() { super.initializeEditor(); - JavaEditorEnvironment.connect(this); setSourceViewerConfiguration(new JavaSourceViewerConfiguration()); setEditorContextMenuId("#JavaEditorContext"); //$NON-NLS-1$ setRulerContextMenuId("#JavaRulerContext"); //$NON-NLS-1$ diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditorEnvironment.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditorEnvironment.java deleted file mode 100644 index 736699144d1..00000000000 --- a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditorEnvironment.java +++ /dev/null @@ -1,73 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2003 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.ui.examples.javaeditor; - - -import org.eclipse.jface.text.rules.RuleBasedScanner; -import org.eclipse.ui.examples.javaeditor.java.JavaCodeScanner; -import org.eclipse.ui.examples.javaeditor.javadoc.JavaDocScanner; -import org.eclipse.ui.examples.javaeditor.util.JavaColorProvider; - -/** The JavaEditorEnvironment maintains singletons used by the java editor - * examples. - */ -public class JavaEditorEnvironment { - - private static JavaColorProvider fgColorProvider; - private static JavaCodeScanner fgCodeScanner; - private static JavaDocScanner fgDocScanner; - - private static int fgRefCount= 0; - - /** - * A connection has occured - initialize the receiver if it is the first activation. - */ - public static void connect(Object client) { - if (++fgRefCount == 1) { - fgColorProvider= new JavaColorProvider(); - fgCodeScanner= new JavaCodeScanner(fgColorProvider); - fgDocScanner= new JavaDocScanner(fgColorProvider); - } - } - - /** - * A disconnection has occured - clear the receiver if it is the last deactivation. - */ - public static void disconnect(Object client) { - if (--fgRefCount == 0) { - fgCodeScanner= null; - fgDocScanner= null; - fgColorProvider.dispose(); - fgColorProvider= null; - } - } - - /** - * Returns the singleton scanner. - */ - public static RuleBasedScanner getJavaCodeScanner() { - return fgCodeScanner; - } - - /** - * Returns the singleton color provider. - */ - public static JavaColorProvider getJavaColorProvider() { - return fgColorProvider; - } - - /** - * Returns the singleton document scanner. - */ - public static RuleBasedScanner getJavaDocScanner() { - return fgDocScanner; - } -} diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditorExamplePlugin.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditorExamplePlugin.java new file mode 100644 index 00000000000..8eb40a03807 --- /dev/null +++ b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaEditorExamplePlugin.java @@ -0,0 +1,99 @@ +/********************************************************************** +Copyright (c) 2000, 2003 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + IBM Corporation - Initial implementation +**********************************************************************/ +package org.eclipse.ui.examples.javaeditor; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPluginDescriptor; + +import org.eclipse.jface.text.rules.RuleBasedScanner; + +import org.eclipse.ui.examples.javaeditor.java.JavaCodeScanner; +import org.eclipse.ui.examples.javaeditor.javadoc.JavaDocScanner; +import org.eclipse.ui.examples.javaeditor.util.JavaColorProvider; +import org.eclipse.ui.plugin.AbstractUIPlugin; + +/** + * The example java editor plugin class. + * + * @since 3.0 + */ +public class JavaEditorExamplePlugin extends AbstractUIPlugin { + + public final static String JAVA_PARTITIONING= "__java_example_partitioning"; //$NON-NLS-1$ + + private static JavaEditorExamplePlugin fgInstance; + private JavaPartitionScanner fPartitionScanner; + private JavaColorProvider fColorProvider; + private JavaCodeScanner fCodeScanner; + private JavaDocScanner fDocScanner; + + /** + * Creates a new plugin instance. + * + * @param descriptor + */ + public JavaEditorExamplePlugin(IPluginDescriptor descriptor) { + super(descriptor); + fgInstance= this; + } + + /** + * Returns the default plugin instance. + * + * @return the default plugin instance + */ + public static JavaEditorExamplePlugin getDefault() { + return fgInstance; + } + + /* + * @see org.eclipse.ui.plugin.AbstractUIPlugin#shutdown() + */ + public void shutdown() throws CoreException { + super.shutdown(); + } + + /** + * Return a scanner for creating java partitions. + */ + public JavaPartitionScanner getJavaPartitionScanner() { + if (fPartitionScanner == null) + fPartitionScanner= new JavaPartitionScanner(); + return fPartitionScanner; + } + + /** + * Returns the singleton scanner. + */ + public RuleBasedScanner getJavaCodeScanner() { + if (fCodeScanner == null) + fCodeScanner= new JavaCodeScanner(getJavaColorProvider()); + return fCodeScanner; + } + + /** + * Returns the singleton color provider. + */ + public JavaColorProvider getJavaColorProvider() { + if (fColorProvider == null) + fColorProvider= new JavaColorProvider(); + return fColorProvider; + } + + /** + * Returns the singleton document scanner. + */ + public RuleBasedScanner getJavaDocScanner() { + if (fDocScanner == null) + fDocScanner= new JavaDocScanner(fColorProvider); + return fDocScanner; + } +} diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/java/JavaPartitionScanner.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaPartitionScanner.java index 4bb57624c6e..54bb2edc298 100644 --- a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/java/JavaPartitionScanner.java +++ b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaPartitionScanner.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package org.eclipse.ui.examples.javaeditor.java; +package org.eclipse.ui.examples.javaeditor; import java.util.ArrayList; @@ -23,6 +23,7 @@ public class JavaPartitionScanner extends RuleBasedPartitionScanner { public final static String JAVA_MULTILINE_COMMENT= "__java_multiline_comment"; //$NON-NLS-1$ public final static String JAVA_DOC= "__java_javadoc"; //$NON-NLS-1$ + public final static String[] JAVA_PARTITION_TYPES= new String[] { JAVA_MULTILINE_COMMENT, JAVA_DOC }; /** * Detector for empty comments. diff --git a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaSourceViewerConfiguration.java b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaSourceViewerConfiguration.java index 017e2a479b8..5df2a8737d3 100644 --- a/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaSourceViewerConfiguration.java +++ b/org.eclipse.ui.examples.javaeditor/Eclipse Java Editor Example/org/eclipse/ui/examples/javaeditor/JavaSourceViewerConfiguration.java @@ -11,15 +11,28 @@ package org.eclipse.ui.examples.javaeditor; -import org.eclipse.jface.text.*; +import org.eclipse.swt.graphics.RGB; + +import org.eclipse.jface.text.DefaultAutoIndentStrategy; +import org.eclipse.jface.text.IAutoIndentStrategy; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextDoubleClickStrategy; +import org.eclipse.jface.text.ITextHover; +import org.eclipse.jface.text.TextAttribute; import org.eclipse.jface.text.contentassist.ContentAssistant; import org.eclipse.jface.text.contentassist.IContentAssistant; import org.eclipse.jface.text.presentation.IPresentationReconciler; import org.eclipse.jface.text.presentation.PresentationReconciler; -import org.eclipse.jface.text.rules.*; -import org.eclipse.jface.text.source.*; -import org.eclipse.swt.graphics.RGB; -import org.eclipse.ui.examples.javaeditor.java.*; +import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; +import org.eclipse.jface.text.rules.DefaultDamagerRepairer; +import org.eclipse.jface.text.rules.Token; +import org.eclipse.jface.text.source.IAnnotationHover; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.SourceViewerConfiguration; + +import org.eclipse.ui.examples.javaeditor.java.JavaAutoIndentStrategy; +import org.eclipse.ui.examples.javaeditor.java.JavaCompletionProcessor; +import org.eclipse.ui.examples.javaeditor.java.JavaDoubleClickSelector; import org.eclipse.ui.examples.javaeditor.javadoc.JavaDocCompletionProcessor; import org.eclipse.ui.examples.javaeditor.util.JavaColorProvider; @@ -59,6 +72,13 @@ public class JavaSourceViewerConfiguration extends SourceViewerConfiguration { return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? new JavaAutoIndentStrategy() : new DefaultAutoIndentStrategy()); } + /* + * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer) + */ + public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) { + return JavaEditorExamplePlugin.JAVA_PARTITIONING; + } + /* (non-Javadoc) * Method declared on SourceViewerConfiguration */ @@ -72,6 +92,7 @@ public class JavaSourceViewerConfiguration extends SourceViewerConfiguration { public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { ContentAssistant assistant= new ContentAssistant(); + assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); assistant.setContentAssistProcessor(new JavaCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE); assistant.setContentAssistProcessor(new JavaDocCompletionProcessor(), JavaPartitionScanner.JAVA_DOC); @@ -79,7 +100,7 @@ public class JavaSourceViewerConfiguration extends SourceViewerConfiguration { assistant.setAutoActivationDelay(500); assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY); assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE); - assistant.setContextInformationPopupBackground(JavaEditorEnvironment.getJavaColorProvider().getColor(new RGB(150, 150, 0))); + assistant.setContextInformationPopupBackground(JavaEditorExamplePlugin.getDefault().getJavaColorProvider().getColor(new RGB(150, 150, 0))); return assistant; } @@ -110,10 +131,11 @@ public class JavaSourceViewerConfiguration extends SourceViewerConfiguration { */ public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { - JavaColorProvider provider= JavaEditorEnvironment.getJavaColorProvider(); + JavaColorProvider provider= JavaEditorExamplePlugin.getDefault().getJavaColorProvider(); PresentationReconciler reconciler= new PresentationReconciler(); + reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); - DefaultDamagerRepairer dr= new DefaultDamagerRepairer(JavaEditorEnvironment.getJavaCodeScanner()); + DefaultDamagerRepairer dr= new DefaultDamagerRepairer(JavaEditorExamplePlugin.getDefault().getJavaCodeScanner()); reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); |