Fixed: 1760857 - Avoid refreshing the preview when phpeditor got focus.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / DocumentAdapter.java
index 006503e..6c0bdff 100644 (file)
@@ -11,7 +11,6 @@
 
 package net.sourceforge.phpeclipse.phpeditor;
 
-
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -43,126 +42,178 @@ import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IDocumentListener;
 import org.eclipse.swt.widgets.Display;
 
-
-
 /**
- * Adapts <code>IDocument</code> to <code>IBuffer</code>. Uses the
- * same algorithm as the text widget to determine the buffer's line delimiter.
- * All text inserted into the buffer is converted to this line delimiter.
- * This class is <code>public</code> for test purposes only.
+ * Adapts <code>IDocument</code> to <code>IBuffer</code>. Uses the same
+ * algorithm as the text widget to determine the buffer's line delimiter. All
+ * text inserted into the buffer is converted to this line delimiter. This class
+ * is <code>public</code> for test purposes only.
  */
 public class DocumentAdapter implements IBuffer, IDocumentListener {
 
-               /**
-                * Internal implementation of a NULL instanceof IBuffer.
-                */
-               static private class NullBuffer implements IBuffer {
-                       public void addBufferChangedListener(IBufferChangedListener listener) {}
-                       public void append(char[] text) {}
-                       public void append(String text) {}
-                       public void close() {}
-                       public char getChar(int position) { return 0; }
-                       public char[] getCharacters() { return null; }
-                       public String getContents() { return null; }
-                       public int getLength() { return 0; }
-                       public IOpenable getOwner() { return null; }
-                       public String getText(int offset, int length) { return null; }
-                       public IResource getUnderlyingResource() { return null; }
-                       public boolean hasUnsavedChanges() { return false; }
-                       public boolean isClosed() { return false; }
-                       public boolean isReadOnly() { return true; }
-                       public void removeBufferChangedListener(IBufferChangedListener listener) {}
-                       public void replace(int position, int length, char[] text) {}
-                       public void replace(int position, int length, String text) {}
-                       public void save(IProgressMonitor progress, boolean force) throws JavaModelException {}
-                       public void setContents(char[] contents) {}
-                       public void setContents(String contents) {}
-               }
-
-
-               /** NULL implementing <code>IBuffer</code> */
-               public final static IBuffer NULL= new NullBuffer();
-
-
-               /**
-                *  Executes a document set content call in the ui thread.
-                */
-               protected class DocumentSetCommand implements Runnable {
-
-                       private String fContents;
-
-                       public void run() {
-                               fDocument.set(fContents);
-                       }
+       /**
+        * Internal implementation of a NULL instanceof IBuffer.
+        */
+       static private class NullBuffer implements IBuffer {
+               public void addBufferChangedListener(IBufferChangedListener listener) {
+               }
 
-                       public void set(String contents) {
-                               fContents= contents;
-                               Display.getDefault().syncExec(this);
-                       }
+               public void append(char[] text) {
                }
 
-               /**
-                * Executes a document replace call in the ui thread.
-                */
-               protected class DocumentReplaceCommand implements Runnable {
+               public void append(String text) {
+               }
 
-                       private int fOffset;
-                       private int fLength;
-                       private String fText;
+               public void close() {
+               }
 
-                       public void run() {
-                               try {
-                                       fDocument.replace(fOffset, fLength, fText);
-                               } catch (BadLocationException x) {
-                                       // ignore
-                               }
-                       }
+               public char getChar(int position) {
+                       return 0;
+               }
 
-                       public void replace(int offset, int length, String text) {
-                               fOffset= offset;
-                               fLength= length;
-                               fText= text;
-                               Display.getDefault().syncExec(this);
+               public char[] getCharacters() {
+                       return null;
+               }
+
+               public String getContents() {
+                       return null;
+               }
+
+               public int getLength() {
+                       return 0;
+               }
+
+               public IOpenable getOwner() {
+                       return null;
+               }
+
+               public String getText(int offset, int length) {
+                       return null;
+               }
+
+               public IResource getUnderlyingResource() {
+                       return null;
+               }
+
+               public boolean hasUnsavedChanges() {
+                       return false;
+               }
+
+               public boolean isClosed() {
+                       return false;
+               }
+
+               public boolean isReadOnly() {
+                       return true;
+               }
+
+               public void removeBufferChangedListener(IBufferChangedListener listener) {
+               }
+
+               public void replace(int position, int length, char[] text) {
+               }
+
+               public void replace(int position, int length, String text) {
+               }
+
+               public void save(IProgressMonitor progress, boolean force)
+                               throws JavaModelException {
+               }
+
+               public void setContents(char[] contents) {
+               }
+
+               public void setContents(String contents) {
+               }
+       }
+
+       /** NULL implementing <code>IBuffer</code> */
+       public final static IBuffer NULL = new NullBuffer();
+
+       /**
+        * Executes a document set content call in the ui thread.
+        */
+       protected class DocumentSetCommand implements Runnable {
+
+               private String fContents;
+
+               public void run() {
+                       fDocument.set(fContents);
+               }
+
+               public void set(String contents) {
+                       fContents = contents;
+                       Display.getDefault().syncExec(this);
+               }
+       }
+
+       /**
+        * Executes a document replace call in the ui thread.
+        */
+       protected class DocumentReplaceCommand implements Runnable {
+
+               private int fOffset;
+
+               private int fLength;
+
+               private String fText;
+
+               public void run() {
+                       try {
+                               fDocument.replace(fOffset, fLength, fText);
+                       } catch (BadLocationException x) {
+                               // ignore
                        }
                }
 
-       private static final boolean DEBUG_LINE_DELIMITERS= true;
+               public void replace(int offset, int length, String text) {
+                       fOffset = offset;
+                       fLength = length;
+                       fText = text;
+                       Display.getDefault().syncExec(this);
+               }
+       }
+
+       private static final boolean DEBUG_LINE_DELIMITERS = true;
 
        private IOpenable fOwner;
+
        private IFile fFile;
+
        private ITextFileBuffer fTextFileBuffer;
+
        private IDocument fDocument;
 
-       private DocumentSetCommand fSetCmd= new DocumentSetCommand();
-       private DocumentReplaceCommand fReplaceCmd= new DocumentReplaceCommand();
+       private DocumentSetCommand fSetCmd = new DocumentSetCommand();
+
+       private DocumentReplaceCommand fReplaceCmd = new DocumentReplaceCommand();
 
        private Set fLegalLineDelimiters;
 
-       private List fBufferListeners= new ArrayList(3);
-       private IStatus fStatus;
+       private List fBufferListeners = new ArrayList(3);
 
+       private IStatus fStatus;
 
        /**
         * This method is <code>public</code> for test purposes only.
         */
        public DocumentAdapter(IOpenable owner, IFile file) {
 
-               fOwner= owner;
-               fFile= file;
+               fOwner = owner;
+               fFile = file;
 
                initialize();
        }
 
        private void initialize() {
-               ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
-               IPath location= fFile.getFullPath();
+               ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
+               IPath location = fFile.getFullPath();
                try {
                        manager.connect(location, new NullProgressMonitor());
-                       fTextFileBuffer= manager.getTextFileBuffer(location);
-                       fDocument= fTextFileBuffer.getDocument();
+                       fTextFileBuffer = manager.getTextFileBuffer(location);
+                       fDocument = fTextFileBuffer.getDocument();
                } catch (CoreException x) {
-                       fStatus= x.getStatus();
-                       fDocument= manager.createEmptyDocument(location);
+                       fStatus = x.getStatus();
+                       fDocument = manager.createEmptyDocument(location);
                }
                fDocument.addPrenotifiedDocumentListener(this);
        }
@@ -180,7 +231,7 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
 
        /**
         * Returns the adapted document.
-        *
+        * 
         * @return the adapted document
         */
        public IDocument getDocument() {
@@ -229,18 +280,20 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
                if (isClosed())
                        return;
 
-               IDocument d= fDocument;
-               fDocument= null;
+               IDocument d = fDocument;
+               fDocument = null;
                d.removePrenotifiedDocumentListener(this);
 
                if (fTextFileBuffer != null) {
-                       ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
+                       ITextFileBufferManager manager = FileBuffers
+                                       .getTextFileBufferManager();
                        try {
-                               manager.disconnect(fTextFileBuffer.getLocation(), new NullProgressMonitor());
+                               manager.disconnect(fTextFileBuffer.getLocation(),
+                                               new NullProgressMonitor());
                        } catch (CoreException x) {
                                // ignore
                        }
-                       fTextFileBuffer= null;
+                       fTextFileBuffer = null;
                }
 
                fireBufferChanged(new BufferChangedEvent(this, 0, 0, null));
@@ -259,10 +312,10 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
        }
 
        /*
-        *  @see IBuffer#getCharacters()
+        * @see IBuffer#getCharacters()
         */
        public char[] getCharacters() {
-               String content= getContents();
+               String content = getContents();
                return content == null ? null : content.toCharArray();
        }
 
@@ -323,8 +376,9 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
         * @see IBuffer#isReadOnly()
         */
        public boolean isReadOnly() {
-               IResource resource= getUnderlyingResource();
-               return resource == null ? true : resource.getResourceAttributes().isReadOnly();
+               IResource resource = getUnderlyingResource();
+               return resource == null ? true : resource.getResourceAttributes()
+                               .isReadOnly();
        }
 
        /*
@@ -347,7 +401,8 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
        /*
         * @see IBuffer#save(IProgressMonitor, boolean)
         */
-       public void save(IProgressMonitor progress, boolean force) throws JavaModelException {
+       public void save(IProgressMonitor progress, boolean force)
+                       throws JavaModelException {
                try {
                        if (fTextFileBuffer != null)
                                fTextFileBuffer.commit(progress, force);
@@ -367,7 +422,7 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
         * @see IBuffer#setContents(String)
         */
        public void setContents(String contents) {
-               int oldLength= fDocument.getLength();
+               int oldLength = fDocument.getLength();
 
                if (contents == null) {
 
@@ -386,16 +441,15 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
                }
        }
 
-
        private void validateLineDelimiters(String contents) {
 
                if (fLegalLineDelimiters == null) {
                        // collect all line delimiters in the document
-                       HashSet existingDelimiters= new HashSet();
+                       HashSet existingDelimiters = new HashSet();
 
-                       for (int i= fDocument.getNumberOfLines() - 1; i >= 0; i-- ) {
+                       for (int i = fDocument.getNumberOfLines() - 1; i >= 0; i--) {
                                try {
-                                       String curr= fDocument.getLineDelimiter(i);
+                                       String curr = fDocument.getLineDelimiter(i);
                                        if (curr != null) {
                                                existingDelimiters.add(curr);
                                        }
@@ -406,23 +460,24 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
                        if (existingDelimiters.isEmpty()) {
                                return; // first insertion of a line delimiter: no test
                        }
-                       fLegalLineDelimiters= existingDelimiters;
+                       fLegalLineDelimiters = existingDelimiters;
 
                }
 
-               DefaultLineTracker tracker= new DefaultLineTracker();
+               DefaultLineTracker tracker = new DefaultLineTracker();
                tracker.set(contents);
 
-               int lines= tracker.getNumberOfLines();
+               int lines = tracker.getNumberOfLines();
                if (lines <= 1)
                        return;
 
-               for (int i= 0; i < lines; i++) {
+               for (int i = 0; i < lines; i++) {
                        try {
-                               String curr= tracker.getLineDelimiter(i);
+                               String curr = tracker.getLineDelimiter(i);
                                if (curr != null && !fLegalLineDelimiters.contains(curr)) {
-                                       StringBuffer buf= new StringBuffer("New line delimiter added to new code: "); //$NON-NLS-1$
-                                       for (int k= 0; k < curr.length(); k++) {
+                                       StringBuffer buf = new StringBuffer(
+                                                       "New line delimiter added to new code: "); //$NON-NLS-1$
+                                       for (int k = 0; k < curr.length(); k++) {
                                                buf.append(String.valueOf((int) curr.charAt(k)));
                                        }
                                        PHPeclipsePlugin.log(new Exception(buf.toString()));
@@ -444,12 +499,13 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
         * @see IDocumentListener#documentChanged(DocumentEvent)
         */
        public void documentChanged(DocumentEvent event) {
-               fireBufferChanged(new BufferChangedEvent(this, event.getOffset(), event.getLength(), event.getText()));
+               fireBufferChanged(new BufferChangedEvent(this, event.getOffset(), event
+                               .getLength(), event.getText()));
        }
 
        private void fireBufferChanged(BufferChangedEvent event) {
                if (fBufferListeners != null && fBufferListeners.size() > 0) {
-                       Iterator e= new ArrayList(fBufferListeners).iterator();
+                       Iterator e = new ArrayList(fBufferListeners).iterator();
                        while (e.hasNext())
                                ((IBufferChangedListener) e.next()).bufferChanged(event);
                }