[204226] refactor rename does not work as expected after save as
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java
index 3133251..0e7fd4e 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java
@@ -63,6 +63,7 @@
 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
 import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
 import org.eclipse.wst.sse.ui.StructuredTextEditor;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
 import org.eclipse.wst.xsd.ui.internal.adapters.CategoryAdapter;
@@ -152,6 +153,45 @@
       }
     }
   }
+  
+  protected void setInputToGraphicalViewer(IDocument newDocument)
+  {
+    IStructuredModel structuredModel = null;
+    try
+    {
+      structuredModel = StructuredModelManager.getModelManager().getExistingModelForRead(newDocument);
+      
+      if (structuredModel == null)
+        structuredModel = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument)newDocument);
+
+      if ((structuredModel != null) && (structuredModel instanceof IDOMModel))
+      {
+        Document doc = ((IDOMModel) structuredModel).getDocument();
+        if (doc != null)
+        {
+          XSDModelAdapter modelAdapter = XSDModelAdapter.lookupOrCreateModelAdapter(doc);
+          if (modelAdapter != null) // Assert should not be null
+          {
+            modelAdapter.setSchema(xsdSchema);
+            xsdSchema = modelAdapter.resetSchema(doc);
+            model = (IModel) XSDAdapterFactory.getInstance().adapt(xsdSchema);
+          }
+        }
+      }
+    }
+    catch (Exception e)
+    {
+    }
+    finally
+    {
+      if (structuredModel != null)
+      {
+        structuredModel.releaseFromRead();
+      }
+    }
+  }
+  
+ 
   public IModel buildModel()
   {
     try
@@ -171,13 +211,23 @@
       if (doc instanceof IStructuredDocument)
       {
         IStructuredModel model = null;
+        try
+        {
         // TODO: for StorageEditorInputs, should be forRead
-        model = StructuredModelManager.getModelManager().getExistingModelForEdit(doc);
-        if (model == null) {
-          model = StructuredModelManager.getModelManager().getModelForEdit((IStructuredDocument) doc);
+          model = StructuredModelManager.getModelManager().getExistingModelForEdit(doc);
+          if (model == null)
+          {
+            model = StructuredModelManager.getModelManager().getModelForEdit((IStructuredDocument) doc);
+          }
+          document = ((IDOMModel) model).getDocument();
         }
-        structuredModel = model;
-        document = ((IDOMModel)model).getDocument();
+        finally
+        {
+          if (model != null)
+          {
+            model.releaseFromEdit();
+          }
+        }
       }
       Assert.isNotNull(document);
 
@@ -256,18 +306,43 @@
 
   public void dispose()
   {
-    if (structuredModel != null)
+    IStructuredModel structuredModel = null;
+    XSDModelAdapter modelAdapter = null;
+    IDOMDocument doc = null;
+    IDocument idoc = structuredTextEditor.getDocumentProvider().getDocument(getEditorInput());
+    if (idoc != null)
     {
-      structuredModel.releaseFromEdit();
-      structuredModel = null;
+      structuredModel = StructuredModelManager.getModelManager().getExistingModelForRead(idoc);
+      if ((structuredModel != null) && (structuredModel instanceof IDOMModel))
+      {
+        try
+        {
+          if ((structuredModel != null) && (structuredModel instanceof IDOMModel))
+          {
+            doc = ((IDOMModel) structuredModel).getDocument();
+            if (doc != null)
+            {
+              modelAdapter = (XSDModelAdapter) doc.getExistingAdapter(XSDModelAdapter.class);
+              if (modelAdapter != null)
+              {
+                doc.getModel().removeModelStateListener(modelAdapter.getModelReconcileAdapter());
+                doc.removeAdapter(modelAdapter.getModelReconcileAdapter());
+                doc.removeAdapter(modelAdapter);
+                modelAdapter.clear();
+                modelAdapter = null;
+              }
+            }
+          }
+        }
+        finally
+        {
+          structuredModel.releaseFromRead();
+        }
+      }
     }
-    
-    if (schemaNodeAdapter != null)
-    {
-      schemaNodeAdapter.clear();
-      schemaNodeAdapter = null;
-    }
-    
+   
+
+
     if (fOutlinePage != null)
     {
 //      if (fOutlinePage instanceof ConfigurableContentOutlinePage && fOutlineListener != null)
@@ -282,6 +357,7 @@
     getSelectionManager().removeSelectionChangedListener(fXSDSelectionListener);
     XSDEditorPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(xsdPreferenceStoreListener);
     xsdPreferenceStoreListener = null;
+    structuredTextEditor.dispose();
     super.dispose();
   }
 
@@ -1179,4 +1255,60 @@
     XSDEditorPlugin.getPlugin().getPreferenceStore().setValue(DEFAULT_EDITOR_MODE_ID, id);
   }
 
+  /* (non-Javadoc)
+   * @see org.eclipse.ui.part.EditorPart#doSaveAs()
+   */
+  public void doSaveAs()
+  {
+    // When performing a save as, the document changes.   Our model state listeners should listen
+    // to the new document.
+    
+    // First get the current document
+    IDocument currentDocument = getDocument();
+    XSDModelAdapter modelAdapter = null;
+    IDOMDocument doc = null;
+    if (currentDocument != null)
+    {
+      IStructuredModel structuredModel = StructuredModelManager.getModelManager().getExistingModelForRead(currentDocument);
+      if (structuredModel != null)
+      {
+        try
+        {
+          if ((structuredModel != null) && (structuredModel instanceof IDOMModel))
+          {
+            // Get the associated IDOMDocument model
+            doc = ((IDOMModel) structuredModel).getDocument();
+            // and now get our adapter that listens to DOM changes
+            if (doc != null)
+            {
+              modelAdapter = (XSDModelAdapter) doc.getExistingAdapter(XSDModelAdapter.class);
+            }
+          }
+        }
+        finally
+        {
+          structuredModel.releaseFromRead();
+        }
+      }
+    }
+    // perform save as
+    structuredTextEditor.doSaveAs();
+
+    setInput(structuredTextEditor.getEditorInput());
+    setPartName(structuredTextEditor.getEditorInput().getName());
+    
+    getCommandStack().markSaveLocation();
+   
+    // Now do the clean up on the old document
+    if (doc != null)
+    {
+      // remove the adapters
+      doc.getModel().removeModelStateListener(modelAdapter.getModelReconcileAdapter());
+      doc.removeAdapter(modelAdapter.getModelReconcileAdapter());
+      doc.removeAdapter(modelAdapter);
+      modelAdapter.clear();
+      modelAdapter = null;
+    }
+  }
+
 }  
\ No newline at end of file
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/text/XSDModelAdapter.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/text/XSDModelAdapter.java
index 5979978..7aaf8c2 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/text/XSDModelAdapter.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/text/XSDModelAdapter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
+ * Copyright (c) 2004, 2008 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
@@ -146,7 +146,7 @@
       // attach an adapter to keep the XSD model and DOM in sync
       //
       modelReconcileAdapter = new XSDModelReconcileAdapter(document, schema);
-      domNode.getModel().addModelStateListener(modelReconcileAdapter);
+//      domNode.getModel().addModelStateListener(modelReconcileAdapter);
     }
     catch (Exception ex)
     {
@@ -162,7 +162,29 @@
   {     
     return createSchema(element.getOwnerDocument());
   }
-  
+
+  public XSDSchema resetSchema(Document document)
+  {
+    // The document has changed so the schema should be updated as well.
+    try
+    {
+      IDOMNode domNode = (IDOMNode)document;
+      schema.setDocument(document);
+      schema.setElement(document.getDocumentElement());
+      
+      resourceSet = XSDSchemaImpl.createResourceSet();
+      resourceSet.getAdapterFactories().add(new XSDSchemaLocationResolverAdapterFactory());                
+      resourceSet.getResources().add(schema.eResource());
+ 
+      modelReconcileAdapter = new XSDModelReconcileAdapter(document, schema);
+      domNode.getModel().addModelStateListener(modelReconcileAdapter);
+    }
+    catch (Exception ex)
+    {
+    }
+    return schema;
+  }  
+
   public static XSDModelAdapter lookupOrCreateModelAdapter(Document document)
   {
     XSDModelAdapter adapter = null;
@@ -173,13 +195,12 @@
       if (adapter == null)
       {
         adapter = new XSDModelAdapter();
-        notifier.addAdapter(adapter);        
+        notifier.addAdapter(adapter);
       } 
     }   
     return adapter;
   }
   
-  
   public static XSDSchema lookupOrCreateSchema(final Document document)
   {    
     XSDSchema result = null;    
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/ui/internal/adt/editor/CommonMultiPageEditor.java b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/ui/internal/adt/editor/CommonMultiPageEditor.java
index b421e65..7499b3d 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/ui/internal/adt/editor/CommonMultiPageEditor.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/ui/internal/adt/editor/CommonMultiPageEditor.java
@@ -43,6 +43,7 @@
 import org.eclipse.gef.ui.parts.SelectionSynchronizer;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.jface.text.IDocument;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.MouseAdapter;
 import org.eclipse.swt.events.MouseEvent;
@@ -176,7 +177,6 @@
     setInput(structuredTextEditor.getEditorInput());
     setPartName(editor.getTitle());
     getCommandStack().markSaveLocation();
-    
   }
 
   /* (non-Javadoc)
@@ -285,6 +285,31 @@
       }
     }
   }
+  
+  // Should override to set the input to the design viewer for a new document change
+  // ie. when doing a saveAs
+  protected void setInputToGraphicalViewer(IDocument newInput)
+  {
+  }
+
+  protected void setInput(IEditorInput input)
+  {
+    super.setInput(input);
+    if (graphicalViewer != null)
+    {
+      setInputToGraphicalViewer(getDocument());
+    }
+  }
+
+  protected IDocument getDocument()
+  {
+    IDocument document = null;
+    if (structuredTextEditor != null)
+    {
+      document = structuredTextEditor.getDocumentProvider().getDocument(structuredTextEditor.getEditorInput());
+    }
+    return document;
+  }
 
   /**
    * @return
@@ -385,8 +410,6 @@
    */
   public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException
   {
-//    if (!(editorInput instanceof IFileEditorInput))
-//      throw new PartInitException("Invalid Input: Must be IFileEditorInput"); //$NON-NLS-1$
     super.init(site, editorInput);
     
     getCommandStack().addCommandStackListener(this);