[356088] Corrupted diagram
diff --git a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/feature/CreateJPAEntityFeature.java b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/feature/CreateJPAEntityFeature.java
index 8150e71..227f853 100644
--- a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/feature/CreateJPAEntityFeature.java
+++ b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/feature/CreateJPAEntityFeature.java
@@ -15,17 +15,12 @@
  *******************************************************************************/
 package org.eclipse.jpt.jpadiagrameditor.ui.internal.feature;
 
-import java.io.IOException;
-import java.util.Enumeration;
 import java.util.List;
 import java.util.ListIterator;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Status;
@@ -35,11 +30,6 @@
 import org.eclipse.graphiti.mm.pictograms.Diagram;
 import org.eclipse.graphiti.mm.pictograms.Shape;
 import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IJavaElement;
-import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.JavaCore;
-import org.eclipse.jdt.core.search.SearchEngine;
-import org.eclipse.jdt.internal.core.search.JavaSearchScope;
 import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jpt.jpa.core.JpaProject;
@@ -58,10 +48,8 @@
 import org.eclipse.ui.IWorkbenchSite;
 
 
-@SuppressWarnings({ "restriction" })
 public class CreateJPAEntityFeature extends AbstractCreateFeature {
 	
-	private String PERSISTENCE_PROVIDER_LIBRARY_STRING = "javax/persistence/"; //$NON-NLS-1$
 	private IPreferenceStore jpaPreferenceStore = JPADiagramEditorPlugin.getDefault().getPreferenceStore();
 	private boolean isMappedSuperclassChild;
 	private String mappedSuperclassName;
@@ -120,7 +108,7 @@
 														 JPADiagramPropertyPage.getDefaultPackage(jpaProject.getProject()), 
 														 getFeatureProvider()); 
 
-		if(!checkIsSetPersistenceProviderLibrary(jpaProject)){
+		if(!JPAEditorUtil.checkIsSetPersistenceProviderLibrary(jpaProject)){
 			Shell shell = JPADiagramEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
 			IStatus status = new Status(IStatus.ERROR, JPADiagramEditor.ID, JPAEditorMessages.CreateJPAEntityFeature_createEntityErrorStatusMsg);
 			ErrorDialog.openError(shell, JPAEditorMessages.CreateJPAEntityFeature_createEntityErrorMsgTitle, 
@@ -182,42 +170,6 @@
         
 	private JpaProject getTargetJPAProject() {
 		return getFeatureProvider().getMoinIntegrationUtil().getProjectByDiagram(getDiagram());
-	}
-	
-	private boolean isPersistenceProviderLibraryInClasspath(String classPathEntry) {
-		try {
-			JarFile jar = new JarFile(classPathEntry);
-			Enumeration<JarEntry> entries = jar.entries();
-
-			while (entries.hasMoreElements()) {
-				JarEntry entry = entries.nextElement();
-				if (entry.getName().equals(PERSISTENCE_PROVIDER_LIBRARY_STRING)) {
-					return true;
-				}
-			}
-
-		} catch (IOException e) {
-			JPADiagramEditorPlugin.logError(e); 	     					
-		}
-		return false;
-	}
-	
-	private boolean checkIsSetPersistenceProviderLibrary(JpaProject jpaProject) {
-		IJavaProject javaProject = JavaCore.create(jpaProject.getProject());
-		IJavaElement[] elements = new IJavaElement[] { javaProject };
-		JavaSearchScope scope = (JavaSearchScope) SearchEngine.createJavaSearchScope(elements);
-		boolean isAdded = false;
-
-		IPath[] paths = scope.enclosingProjectsAndJars();
-		for (int i = 1; i < paths.length; i++) {
-			IPath path = paths[i];
-			if (isPersistenceProviderLibraryInClasspath(path.toString())) {
-				isAdded = true;
-				break;
-			}
-		}
-		return isAdded;
-	}
-		
+	}		
 	
 }
\ No newline at end of file
diff --git a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/modelintegration/util/ModelIntegrationUtil.java b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/modelintegration/util/ModelIntegrationUtil.java
index 5cbc93b..c38d3ba 100644
--- a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/modelintegration/util/ModelIntegrationUtil.java
+++ b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/modelintegration/util/ModelIntegrationUtil.java
@@ -18,9 +18,11 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.ref.WeakReference;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.Properties;
+import java.util.WeakHashMap;
 
 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.filesystem.IFileInfo;
@@ -70,6 +72,7 @@
 	public static final String DEFAULT_RES_FOLDER = "src";		//$NON-NLS-1$
 
 	private static boolean xmiExists = false;
+	private static WeakHashMap<IProject, WeakReference<Diagram>> projectToDiagram = new  WeakHashMap<IProject, WeakReference<Diagram>>();
 	
 	public static IPath createDiagramPath(PersistenceUnit persistenceUnit) throws CoreException {
 	    IProject project = persistenceUnit.getJpaProject().getProject();
@@ -209,11 +212,11 @@
 		});
 		
 		if (!resource.isLoaded())
-			return createNewDiagram(defaultTransEditDomain, resourceSet, resource, diagramName, grid, snap);
+			return createNewDiagram(project, defaultTransEditDomain, resourceSet, resource, diagramName, grid, snap);
 		
 		EList<EObject> objs = resource.getContents();
 		if (objs == null) 
-			return createNewDiagram(defaultTransEditDomain, resourceSet, resource, diagramName, grid, snap);
+			return createNewDiagram(project, defaultTransEditDomain, resourceSet, resource, diagramName, grid, snap);
 		Iterator<EObject> it = objs.iterator();
 		while (it.hasNext()) {
 			EObject obj = it.next();
@@ -224,10 +227,11 @@
 			defaultTransEditDomain.getCommandStack().flush();
 			return diagram;
 		}
-		return createNewDiagram(defaultTransEditDomain, resourceSet, resource, diagramName, grid, snap);
+		return createNewDiagram(project, defaultTransEditDomain, resourceSet, resource, diagramName, grid, snap);
 	}
 	
-	private static Diagram createNewDiagram(TransactionalEditingDomain editingDomain,
+	private static Diagram createNewDiagram(final IProject project,
+											TransactionalEditingDomain editingDomain,
 											ResourceSet resourceSet,
 											final Resource resource,
 											final String diagramName,
@@ -238,6 +242,7 @@
 			@Override
 			protected void doExecute() {
 				Diagram diagram = Graphiti.getPeService().createDiagram(JPA_DIAGRAM_TYPE, diagramName, grid, snap);
+				projectToDiagram.put(project, new WeakReference<Diagram>(diagram));
 				wrp.setObject(diagram);
 				resource.getContents().add(diagram);
 				try {
@@ -336,29 +341,36 @@
 				.addFileExtension(
 						ModelIntegrationUtil.DIAGRAM_XML_FILE_EXTENSION)).getFullPath();
 	}
+	
+	synchronized public static void putProjectToDiagram(IProject project, Diagram d) {
+		projectToDiagram.put(project, new WeakReference<Diagram>(d));
+	}
 
-	public static Diagram getDiagramByProject(IProject project) {
+	synchronized public static Diagram getDiagramByProject(IProject project) {
 		if (project == null)
 			return null;
 		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 		IWorkbenchPage workbenchPage = null; 
 		try {
 			workbenchPage = workbenchWindow.getActivePage();
+			IEditorReference[] editorRefs = workbenchPage.getEditorReferences();
+			for (IEditorReference editorRef : editorRefs) {
+				if(!JPADiagramEditorPlugin.PLUGIN_ID.equals(editorRef.getId())) 
+					continue;
+				JPADiagramEditor editor = (JPADiagramEditor)editorRef.getEditor(false);
+				if (editor == null)
+					continue;
+				JPAEditorDiagramTypeProvider diagramProvider = editor.getDiagramTypeProvider();  
+				Diagram d = diagramProvider.getDiagram();
+				if (d.getName().equals(project.getName()))
+					return d;
+			}			
 		} catch (NullPointerException e) {
-			return null;
+			// ignore
 		}
-		IEditorReference[] editorRefs = workbenchPage.getEditorReferences();
-		for (IEditorReference editorRef : editorRefs) {
-			if(!JPADiagramEditorPlugin.PLUGIN_ID.equals(editorRef.getId())) 
-				continue;
-			JPADiagramEditor editor = (JPADiagramEditor)editorRef.getEditor(false);
-			if (editor == null)
-				continue;
-			JPAEditorDiagramTypeProvider diagramProvider = editor.getDiagramTypeProvider();  
-			Diagram d = diagramProvider.getDiagram();
-			if (d.getName().equals(project.getName()))
-				return d;
-		}
+		WeakReference<Diagram> ref = projectToDiagram.get(project);
+		if (ref != null)
+			return ref.get();
 		return null;
 	}
 
diff --git a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/provider/JPAEditorDiagramTypeProvider.java b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/provider/JPAEditorDiagramTypeProvider.java
index 440fddc..714cd27 100644
--- a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/provider/JPAEditorDiagramTypeProvider.java
+++ b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/provider/JPAEditorDiagramTypeProvider.java
@@ -160,10 +160,11 @@
 		});    	
     }
     
-	private boolean openPersistedDiagram(boolean hasToAdd) {		
-		final Diagram diagram = getDiagram();
+	private boolean openPersistedDiagram(boolean hasToAdd) {	
 		final JpaProject proj = getTargetJPAProject();
 		IProject project = proj.getProject();
+		final Diagram diagram = getDiagram();
+		ModelIntegrationUtil.putProjectToDiagram(project, diagram);
 		PersistenceUnit pu = JpaArtifactFactory.instance().getPersistenceUnit(proj);
 		String diagramName = pu.getName();
 	    IPath path = ModelIntegrationUtil.getDiagramsFolderPath(project).append(diagramName).addFileExtension(ModelIntegrationUtil.DIAGRAM_FILE_EXTENSION);
diff --git a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/EntitiesCoordinatesXML.java b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/EntitiesCoordinatesXML.java
index 63d5858..db7b402 100644
--- a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/EntitiesCoordinatesXML.java
+++ b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/EntitiesCoordinatesXML.java
@@ -160,11 +160,10 @@
 	}
 
 	synchronized public void load(Hashtable<String, SizePosition> marks){
-		
 		if(rootElement.getChildNodes().getLength()<=1){
 			if(ModelIntegrationUtil.xmiExists()){
 				store();
-				clean();
+				save();
 			}
 		} 
 		
@@ -205,7 +204,7 @@
 		parentElement.appendChild(document.createTextNode(position));
 	}
 	
-	synchronized public void close() {
+	public void save() {
 		OutputStream os = null;
 		try {
 			TransformerFactory transformerFactory = TransformerFactory.newInstance();
@@ -221,8 +220,11 @@
 			} catch (IOException e) {
 				// ignore
 			}
-		}
-		
+		}		
+	}
+	
+	synchronized public void close() {
+		save();
 		clean();
 	}
 	
diff --git a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/JPAEditorUtil.java b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/JPAEditorUtil.java
index ea6266b..3dc6702 100644
--- a/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/JPAEditorUtil.java
+++ b/jpa_diagram_editor/plugins/org.eclipse.jpt.jpadiagrameditor.ui/src/org/eclipse/jpt/jpadiagrameditor/ui/internal/util/JPAEditorUtil.java
@@ -17,9 +17,13 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -28,11 +32,16 @@
 import java.util.Locale;
 import java.util.Properties;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.transaction.RecordingCommand;
@@ -55,12 +64,15 @@
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IImportDeclaration;
+import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.IPackageFragment;
 import org.eclipse.jdt.core.IPackageFragmentRoot;
 import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.search.SearchEngine;
+import org.eclipse.jdt.internal.core.search.JavaSearchScope;
 import org.eclipse.jdt.ui.actions.FormatAllAction;
 import org.eclipse.jdt.ui.actions.OrganizeImportsAction;
 import org.eclipse.jface.preference.IPreferenceStore;
@@ -98,10 +110,12 @@
 import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
 
 
+@SuppressWarnings("restriction")
 public class JPAEditorUtil {
 	
 
 	private static IPeServiceUtil peUtil = null;
+	private final static String PERSISTENCE_PROVIDER_LIBRARY_STRING = "javax/persistence/"; //$NON-NLS-1$
 		
 	public static String capitalizeFirstLetter(String s) {
 		if (s.length() == 0) return s;
@@ -1403,4 +1417,58 @@
 		return JPAEditorConstants.PRIMITIVE_TO_WRAPPER.get(primitive);
 	}
 	
+	static private File getFile(IPath classPathEntry) {
+		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+		IFile f = root.getFile(classPathEntry);
+		if ((f != null) && f.exists()) {
+			URI uri = f.getLocationURI();
+			if (uri == null) 
+				return new File(classPathEntry.toOSString());
+			File file = new File(uri);
+			return file;
+		} else {
+			return new File(classPathEntry.toOSString()); 	
+		}
+
+	}
+	
+	static public boolean isPersistenceProviderLibraryInClasspath(IPath classPathEntry) {
+		try {
+			File f = getFile(classPathEntry);
+			if ((f == null) || !f.exists()) 
+				return false;
+			JarFile jar = new JarFile(f);
+			Enumeration<JarEntry> entries = jar.entries();
+
+			while (entries.hasMoreElements()) {
+				JarEntry entry = entries.nextElement();
+				if (entry.getName().equals(PERSISTENCE_PROVIDER_LIBRARY_STRING)) {
+					return true;
+				}
+			}
+
+		} catch (IOException e) {
+			JPADiagramEditorPlugin.logError(e); 	     					
+		}
+		return false;
+	}
+		
+	static public boolean checkIsSetPersistenceProviderLibrary(JpaProject jpaProject) {
+		IJavaProject javaProject = JavaCore.create(jpaProject.getProject());
+		IJavaElement[] elements = new IJavaElement[] { javaProject };
+		JavaSearchScope scope = (JavaSearchScope) SearchEngine.createJavaSearchScope(elements);
+		boolean isAdded = false;
+
+		IPath[] paths = scope.enclosingProjectsAndJars();
+		for (int i = 1; i < paths.length; i++) {
+			IPath path = paths[i];
+			if (isPersistenceProviderLibraryInClasspath(path)) {
+				isAdded = true;
+				break;
+			}
+		}
+		return isAdded;
+	}
+	
+	
 }
\ No newline at end of file