diff options
author | sdimov | 2011-08-29 15:17:03 +0000 |
---|---|---|
committer | sdimov | 2011-08-29 15:17:03 +0000 |
commit | aa4ee7fa1dca7f1e5106df8bb93f1838c4c614ea (patch) | |
tree | 9f8f1b7880d90d8b6a74e6787cbcb18c5c3c0f6c /jpa_diagram_editor | |
parent | 6ac817299fb60507e53e0757325acb7a3d672889 (diff) | |
download | webtools.dali-aa4ee7fa1dca7f1e5106df8bb93f1838c4c614ea.tar.gz webtools.dali-aa4ee7fa1dca7f1e5106df8bb93f1838c4c614ea.tar.xz webtools.dali-aa4ee7fa1dca7f1e5106df8bb93f1838c4c614ea.zip |
[356088] Corrupted diagram
Diffstat (limited to 'jpa_diagram_editor')
5 files changed, 115 insertions, 82 deletions
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 af13b1d949..284219f0f5 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.features.impl.AbstractCreateFeature; 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.IEditorPart; 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; @@ -100,7 +88,8 @@ public class CreateJPAEntityFeature extends AbstractCreateFeature { return context.getTargetContainer() instanceof Diagram; } - public Object[] create(ICreateContext context) { + @SuppressWarnings("restriction") + public Object[] create(ICreateContext context) { List<Shape> shapes = this.getFeatureProvider().getDiagramTypeProvider().getDiagram().getChildren(); IProject targetProject = null; JpaProject jpaProject = null; @@ -120,7 +109,7 @@ public class CreateJPAEntityFeature extends AbstractCreateFeature { 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, @@ -184,42 +173,6 @@ public class CreateJPAEntityFeature extends AbstractCreateFeature { 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 5cbc93b8cc..c38d3ba0fb 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 @@ package org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.util; 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 class ModelIntegrationUtil { 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 @@ public class ModelIntegrationUtil { }); 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 @@ public class ModelIntegrationUtil { 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 @@ public class ModelIntegrationUtil { @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 @@ public class ModelIntegrationUtil { .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; - } - 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; + // ignore } + 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 9b17022ec4..d89265d328 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 @@ -163,10 +163,11 @@ public class JPAEditorDiagramTypeProvider extends AbstractDiagramTypeProvider { }); } - 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 63d5858e8e..db7b402e6f 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 @@ public class EntitiesCoordinatesXML { } synchronized public void load(Hashtable<String, SizePosition> marks){ - if(rootElement.getChildNodes().getLength()<=1){ if(ModelIntegrationUtil.xmiExists()){ store(); - clean(); + save(); } } @@ -205,7 +204,7 @@ public class EntitiesCoordinatesXML { parentElement.appendChild(document.createTextNode(position)); } - synchronized public void close() { + public void save() { OutputStream os = null; try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); @@ -221,8 +220,11 @@ public class EntitiesCoordinatesXML { } 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 1b246cc723..0429aefeb7 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 @@ package org.eclipse.jpt.jpadiagrameditor.ui.internal.util; 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,10 +32,16 @@ import java.util.ListIterator; 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; @@ -54,12 +64,15 @@ import org.eclipse.graphiti.services.Graphiti; 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; @@ -97,10 +110,12 @@ import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion; 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; @@ -188,8 +203,7 @@ public class JPAEditorUtil { return at.getTypeName(); } - @SuppressWarnings("restriction") - public static List<String> getAttributeTypeTypeNames(JavaResourceAttribute at) { + public static List<String> getAttributeTypeTypeNames(JavaResourceAttribute at) { ListIterator<String> tt = at.getTypeTypeArgumentNames().iterator(); if ((tt == null) || !tt.hasNext()) return null; @@ -204,8 +218,7 @@ public class JPAEditorUtil { } - @SuppressWarnings("restriction") - public static String getAttributeTypeNameWithGenerics(JavaResourceAttribute at) { + public static String getAttributeTypeNameWithGenerics(JavaResourceAttribute at) { StringBuilder res = new StringBuilder(getAttributeTypeName(at)); ListIterator<String> it = at.getTypeTypeArgumentNames().iterator(); if ((it != null) && it.hasNext()) { @@ -1338,7 +1351,6 @@ public class JPAEditorUtil { return file; } - @SuppressWarnings("restriction") static private HashSet<String> getEntityNames(JpaProject jpaProject) { HashSet<String> names = new HashSet<String>(); ListIterator<PersistenceUnit> lit = jpaProject.getRootContextNode().getPersistenceXml(). @@ -1350,7 +1362,6 @@ public class JPAEditorUtil { return names; } - @SuppressWarnings("restriction") static private HashSet<String> getEntitySimpleNames(JpaProject jpaProject) { HashSet<String> names = new HashSet<String>(); ListIterator<PersistenceUnit> lit = jpaProject.getRootContextNode().getPersistenceXml(). @@ -1403,4 +1414,58 @@ public class JPAEditorUtil { 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 |