[397904] Memory leaks in Juno when opening and closing XML Editor
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/StructuredTextEditor.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/StructuredTextEditor.java
index f92cbbc..244dfd9 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/StructuredTextEditor.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/StructuredTextEditor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2012 IBM Corporation and others.
+ * Copyright (c) 2001, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -105,6 +105,8 @@
 import org.eclipse.swt.custom.VerifyKeyListener;
 import org.eclipse.swt.dnd.DND;
 import org.eclipse.swt.dnd.DropTarget;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
 import org.eclipse.swt.events.VerifyEvent;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Image;
@@ -1112,7 +1114,7 @@
 	
 	private boolean fSelectionChangedFromGoto = false;
 
-	private final CharacterPairListener fPairInserter = new CharacterPairListener();
+	private CharacterPairListener fPairInserter = new CharacterPairListener();
 
 	/**
 	 * Creates a new Structured Text Editor.
@@ -1736,6 +1738,7 @@
 
 		if (fOutlineHandler != null) {
 			fOutlineHandler.dispose();
+			fOutlineHandler = null;
 		}
 		// dispose of selection history
 		if (fSelectionHistory != null) {
@@ -1756,9 +1759,11 @@
 		// dispose of menus that were being tracked
 		if (fTextContextMenu != null) {
 			fTextContextMenu.dispose();
+			fTextContextMenu = null;
 		}
 		if (fRulerContextMenu != null) {
 			fRulerContextMenu.dispose();
+			fRulerContextMenu = null;
 		}
 		if (fTextContextMenuManager != null) {
 			fTextContextMenuManager.removeMenuListener(getContextMenuListener());
@@ -1779,6 +1784,8 @@
 		// less severe.
 		if (fStructuredModel != null) {
 			fStructuredModel.removeModelStateListener(getInternalModelStateListener());
+			//fStructuredModel.setStructuredDocument(null);
+			fStructuredModel = null;
 		}
 
 		// BUG155335 - if there was no document provider, there was nothing
@@ -1801,7 +1808,9 @@
 			}
 			if (fOutlinePageListener != null) {
 				fOutlinePage.removeSelectionChangedListener(fOutlinePageListener);
+				fOutlinePageListener = null;
 			}
+			fOutlinePage = null;
 		}
 
 		fEditorDisposed = true;
@@ -1817,8 +1826,10 @@
 
 		uninstallSemanticHighlighting();
 
-		if (fPairInserter != null)
+		if (fPairInserter != null) {
 			fPairInserter.dispose();
+			fPairInserter = null;
+		}
 
 
 		setPreferenceStore(null);
@@ -1833,6 +1844,12 @@
 
 		if (fStructuredSelectionProvider != null) {
 			fStructuredSelectionProvider.dispose();
+			fStructuredSelectionProvider = null;
+		}
+		
+		if (fStatusLineLabelProvider != null) {
+			fStatusLineLabelProvider.dispose();
+			fStatusLineLabelProvider = null;
 		}
 
 		setStatusLineMessage(null);
@@ -2083,7 +2100,7 @@
 		}
 		// content outline page
 		else if (IContentOutlinePage.class.equals(required)) {
-			if (fOutlinePage == null || fOutlinePage.getControl() == null || fOutlinePage.getControl().isDisposed()) {
+			if (fOutlinePage == null) {
 				ContentOutlineConfiguration cfg = createContentOutlineConfiguration();
 				if (cfg != null) {
 					ConfigurableContentOutlinePage outlinePage = new ConfigurableContentOutlinePage();
@@ -2637,6 +2654,14 @@
 		fDropAdapter.setTextViewer(textViewer);
 		fDropTarget.setTransfer(fDropAdapter.getTransfers());
 		fDropTarget.addDropListener(fDropAdapter);
+		fDropTarget.addDisposeListener(new DisposeListener() {
+			
+			public void widgetDisposed(DisposeEvent e) {
+				fDropTarget.removeDropListener(fDropAdapter);
+				fDropTarget.removeDisposeListener(this);
+				fDropTarget.dispose();
+			}
+		});
 	}
 
 	/*
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/contentassist/StructuredContentAssistProcessor.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/contentassist/StructuredContentAssistProcessor.java
index 78efc28..27ff497 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/contentassist/StructuredContentAssistProcessor.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/contentassist/StructuredContentAssistProcessor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2011 IBM Corporation and others.
+ * Copyright (c) 2010, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -362,9 +362,12 @@
 		if(this.fViewer != null) {
 			this.fViewer.removeTextInputListener(this.fTextInputListener);
 			this.fViewer = null;
+			this.fTextInputListener = null;
 		}
 		if (this.fAssistant != null) {
 			this.fAssistant.removeCompletionListener(fCompletionListener);
+			this.fCompletionListener = null;
+			this.fAssistant = null;
 		}
 	}
 	
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/StructuredTextViewer.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/StructuredTextViewer.java
index cc42573..b0320f1 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/StructuredTextViewer.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/StructuredTextViewer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2012 IBM Corporation and others.
+ * Copyright (c) 2001, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -723,6 +723,14 @@
 			fRecHighlighter.uninstall();
 			fRecHighlighter = null;
 		}
+		if (fContentAssistant != null) {
+			fContentAssistant.uninstall();
+			if (fContentAssistantFacade != null) {
+				fContentAssistantFacade= null;
+			}
+			fContentAssistantInstalled = false;
+			fContentAssistant = null;
+		}
 		super.handleDispose();
 
 		Logger.trace("Source Editor", "StructuredTextViewer::handleDispose exit"); //$NON-NLS-1$ //$NON-NLS-2$
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/CompoundContentAssistProcessor.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/CompoundContentAssistProcessor.java
index 7e89b43..f76003e 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/CompoundContentAssistProcessor.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/CompoundContentAssistProcessor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 IBM Corporation and others.
+ * Copyright (c) 2005, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -375,6 +375,7 @@
 				((IReleasable) p).release();
 			}
 		}
+		fProcessors.clear();
 	}
 	
 	private static class WrappedContextInformation implements IContextInformation, IContextInformationExtension {
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/StructuredContentAssistant.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/StructuredContentAssistant.java
index 8d124ce..53e9d51 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/StructuredContentAssistant.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/contentassist/StructuredContentAssistant.java
@@ -119,6 +119,7 @@
 			}
 		}
 		fProcessorsReleased = true;
+		this.fReleasableProcessors.clear();
 		super.uninstall();
 	}
 }
\ No newline at end of file
diff --git a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/propertytester/CustomFilterPropertyTester.java b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/propertytester/CustomFilterPropertyTester.java
index e4fa4d7..d1cd362 100644
--- a/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/propertytester/CustomFilterPropertyTester.java
+++ b/bundles/org.eclipse.wst.sse.ui/src/org/eclipse/wst/sse/ui/internal/propertytester/CustomFilterPropertyTester.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2012 IBM Corporation and others.
+ * Copyright (c) 2011, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -24,7 +24,8 @@
 	 */
 	public boolean test(Object receiver, String property, Object[] args,	Object expectedValue) {
 		if (receiver instanceof IEditorPart){
-			return ((IEditorPart) receiver).getAdapter(IContentOutlinePage.class) instanceof ConfigurableContentOutlinePage;
+			IContentOutlinePage outlinePage  = (IContentOutlinePage) ((IEditorPart) receiver).getAdapter(IContentOutlinePage.class);
+			return (outlinePage instanceof ConfigurableContentOutlinePage && outlinePage.getControl() != null && !outlinePage.getControl().isDisposed());
 		}
 		return false;
 	}
diff --git a/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/SourceEditorActionBarContributor.java b/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/SourceEditorActionBarContributor.java
index d70cdd3..b9fa6f5 100644
--- a/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/SourceEditorActionBarContributor.java
+++ b/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/SourceEditorActionBarContributor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2004, 2009 IBM Corporation and others.
+ * Copyright (c) 2004, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -70,14 +70,17 @@
 
 		if (designViewerActionBarContributor != null) {
 			designViewerActionBarContributor.dispose();
+			designViewerActionBarContributor = null;
 		}
 
 		if (sourceViewerActionContributor != null) {
 			sourceViewerActionContributor.dispose();
+			sourceViewerActionContributor = null;
 		}
 
 		if (extendedContributor != null) {
 			extendedContributor.dispose();
+			extendedContributor = null;
 		}
 		
 		multiPageEditor = null;
diff --git a/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLMultiPageEditorPart.java b/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLMultiPageEditorPart.java
index 014eb75..5e8c942 100644
--- a/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLMultiPageEditorPart.java
+++ b/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLMultiPageEditorPart.java
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2004, 2012 IBM Corporation and others. All rights reserved. This
+ * Copyright (c) 2004, 2013 IBM Corporation and others. All rights reserved. This
  * program and the accompanying materials are made available under the terms
  * of the Eclipse Public License v1.0 which accompanies this distribution, and
  * is available at http://www.eclipse.org/legal/epl-v10.html
@@ -74,8 +74,6 @@
 import org.eclipse.ui.part.MultiPageSelectionProvider;
 import org.eclipse.ui.progress.UIJob;
 import org.eclipse.ui.texteditor.IDocumentProvider;
-import org.eclipse.ui.texteditor.ITextEditor;
-import org.eclipse.ui.texteditor.TextSelectionNavigationLocation;
 import org.eclipse.wst.sse.core.StructuredModelManager;
 import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
@@ -491,6 +489,8 @@
 	private MenuManager fMenuManager;
 
 	private boolean fAllocateToolbar = true;
+
+	private TextInputListener fTextInputListener;
 	
 	/**
 	 * StructuredTextMultiPageEditorPart constructor comment.
@@ -520,7 +520,8 @@
 		// Changes to the Text Viewer's document instance should also
 		// force an
 		// input refresh
-		fTextEditor.getTextViewer().addTextInputListener(new TextInputListener());
+		fTextInputListener = new TextInputListener();
+		fTextEditor.getTextViewer().addTextInputListener(fTextInputListener);
 	}
 
 	/**
@@ -861,7 +862,12 @@
 	public void dispose() {
 		Logger.trace("Source Editor", "XMLMultiPageEditorPart::dispose entry"); //$NON-NLS-1$ //$NON-NLS-2$
 
+		if (fTextInputListener != null) {
+			fTextEditor.getTextViewer().removeTextInputListener(fTextInputListener);
+			fTextInputListener = null;
+		}
 		disconnectDesignPage();
+		fDesignViewer = null;
 
 		if (fActivationListener != null) {
 			fActivationListener.dispose();
@@ -874,15 +880,27 @@
 			fMenuManager = null;
 		}
 
-		if ((fTextEditor != null) && (fPropertyListener != null)) {
+		if (fPropertyListener != null) {
 			fTextEditor.removePropertyListener(fPropertyListener);
+			fPropertyListener = null;
 		}
 		
+		if (fEditorManager != null) {
+			fEditorManager.dispose();
+			fEditorManager = null;
+		}
+		if (fToolbarManager != null) {
+			fToolbarManager.dispose();
+			fToolbarManager = null;
+		}
 		// moved to last when added window ... seems like
 		// we'd be in danger of losing some data, like site,
 		// or something.
 		super.dispose();
 
+		fTextEditor = null;
+		fPageInitializer = null;
+		
 		Logger.trace("Source Editor", "StructuredTextMultiPageEditorPart::dispose exit"); //$NON-NLS-1$ //$NON-NLS-2$
 
 	}
@@ -1136,15 +1154,7 @@
 			return new DesignPageNavigationLocation(this, fDesignViewer, false);
 		}
 		// Makes sure that the text editor is returned
-		return new TextSelectionNavigationLocation(fTextEditor, false) {
-			protected IEditorPart getEditorPart() {
-				IEditorPart part = super.getEditorPart();
-				if (part != null) {
-					part = (ITextEditor) part.getAdapter(ITextEditor.class);
-				}
-				return part;
-			}
-		};
+		return fTextEditor.createEmptyNavigationLocation();
 	}
 
 	public INavigationLocation createNavigationLocation() {
@@ -1152,15 +1162,7 @@
 			return new DesignPageNavigationLocation(this, fDesignViewer, true);
 		}
 		// Makes sure that the text editor is returned
-		return new TextSelectionNavigationLocation(fTextEditor, true) {
-			protected IEditorPart getEditorPart() {
-				IEditorPart part = super.getEditorPart();
-				if (part != null) {
-					part = (ITextEditor) part.getAdapter(ITextEditor.class);
-				}
-				return part;
-			}
-		};
+		return fTextEditor.createNavigationLocation();
 	}
 
 	public void saveState(IMemento memento) {
diff --git a/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLTableTreeActionBarContributor.java b/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLTableTreeActionBarContributor.java
index 4af5032..10a5707 100644
--- a/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLTableTreeActionBarContributor.java
+++ b/bundles/org.eclipse.wst.xml.ui/src-multipage/org/eclipse/wst/xml/ui/internal/tabletree/XMLTableTreeActionBarContributor.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2004, 2010 IBM Corporation and others.
+ * Copyright (c) 2004, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -54,6 +54,7 @@
 	protected ViewerExpandCollapseAction collapseAction;
 	protected ViewerExpandCollapseAction xmlMenuExpandAction;
 	protected ViewerExpandCollapseAction xmlMenuCollapseAction;
+	private IActionBars actionBars;
 
 	public XMLTableTreeActionBarContributor() {
 	}
@@ -88,6 +89,7 @@
 	}
 
 	public void init(IActionBars bars) {
+		this.actionBars = bars;
 //		IToolBarManager tbm = bars.getToolBarManager();
 
 /*		IMenuManager xmlMenu = bars.getMenuManager().findMenuUsingPath("org.eclipse.core.runtime.xml.design.xmlmenu"); //$NON-NLS-1$
@@ -184,15 +186,16 @@
 			xmlMenuCollapseAction.setViewer(tableTreeViewer);
 		}
 */
+		ITextEditor textEditor = null;
 		if (editorPart instanceof XMLMultiPageEditorPart) {
 			IWorkbenchPartSite site = editorPart.getSite();
 			if (site instanceof IEditorSite) {
-				ITextEditor textEditor = ((XMLMultiPageEditorPart) editorPart).getTextEditor();
-				IActionBars actionBars = ((IEditorSite) site).getActionBars();
-				actionBars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, getAction(textEditor, ITextEditorActionConstants.UNDO));
-				actionBars.setGlobalActionHandler(ITextEditorActionConstants.REDO, getAction(textEditor, ITextEditorActionConstants.REDO));
+				textEditor = ((XMLMultiPageEditorPart) editorPart).getTextEditor();
 			}
 		}
+		actionBars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, getAction(textEditor, ITextEditorActionConstants.UNDO));
+		actionBars.setGlobalActionHandler(ITextEditorActionConstants.REDO, getAction(textEditor, ITextEditorActionConstants.REDO));
+		
 
 		// TODO... uncomment this and investigate NPE
 		//
@@ -317,5 +320,6 @@
 	 * @see org.eclipse.ui.IEditorActionBarContributor#dispose()
 	 */
 	public void dispose() {
+		setActiveEditor(null);
 	}
 }
diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/DOMObserver.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/DOMObserver.java
index 4b90cfb..1cb7f23 100644
--- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/DOMObserver.java
+++ b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/DOMObserver.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2009 IBM Corporation and others.
+ * Copyright (c) 2001, 2013 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -13,6 +13,9 @@
 package org.eclipse.wst.xml.ui.internal;
 
 
+import java.util.Collection;
+import java.util.Iterator;
+
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -164,6 +167,14 @@
 		if (fDocument != null) {
 			// here we create and init an adapter that will listen to
 			// changes to the document and contained elements
+			Collection adapters = ((INodeNotifier) fDocument).getAdapters();
+			Iterator iterator = adapters.iterator();
+			while (iterator.hasNext()) {
+				INodeAdapter adapter = (INodeAdapter) iterator.next();
+				if (adapter instanceof MyDocumentAdapter) {
+					return;
+				}
+			}
 			MyDocumentAdapter adapter = new MyDocumentAdapter();
 			adapter.connect(fDocument);