From a19a9b1d97989c7121ec46f421b2501b3439d650 Mon Sep 17 00:00:00 2001
From: Karen Butzke
Date: Mon, 17 Sep 2012 07:57:17 -0400
Subject: Bug 365474 - 'Show In' menu for JPA perspective for java and xml
files should have JPA views
---
jpa/plugins/org.eclipse.jpt.jpa.ui/plugin.xml | 10 ++
.../ui/internal/ViewPartShowInAdapterFactory.java | 194 +++++++++++++++++++++
2 files changed, 204 insertions(+)
create mode 100644 jpa/plugins/org.eclipse.jpt.jpa.ui/src/org/eclipse/jpt/jpa/ui/internal/ViewPartShowInAdapterFactory.java
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.ui/plugin.xml b/jpa/plugins/org.eclipse.jpt.jpa.ui/plugin.xml
index dc9c462b00..fee69f1979 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.ui/plugin.xml
+++ b/jpa/plugins/org.eclipse.jpt.jpa.ui/plugin.xml
@@ -120,6 +120,13 @@
+
+
+
+
+
@@ -1013,6 +1020,9 @@
+
+
+
+ * {@link org.eclipse.ui.part.IShowInTarget}
+ *
+ * See org.eclipse.jpt.jpa.ui/plugin.xml:org.eclipse.core.runtime.adapters
.
+ */
+public class ViewPartShowInAdapterFactory implements IAdapterFactory {
+
+ private static final Class>[] ADAPTER_LIST = new Class[] { IShowInTarget.class};
+
+ public Class>[] getAdapterList() {
+ return ADAPTER_LIST;
+ }
+
+ public Object getAdapter(Object adaptableObject, @SuppressWarnings("rawtypes") Class adapterType) {
+ if (adaptableObject instanceof IViewPart) {
+ return this.getAdapter((IViewPart) adaptableObject, adapterType);
+ }
+ return null;
+ }
+
+ private Object getAdapter(IViewPart viewPart, Class> adapterType) {
+ if (adapterType == IShowInTarget.class) {
+ return new ShowInTargetAdapter(viewPart);
+ }
+ return null;
+ }
+
+ // ********** Show In Target **********
+
+ /**
+ * Adapt an #IViewPart to the #IShowInTarget interface
+ *
+ * This adapter is currently implemented for JPA related views
+ * such as #JpaDetailsView and #JpaStructureView. Users may
+ * need to update the show() methods if they want to reuse
+ * this adapter to support their own views.
+ */
+ /* CU private */ static class ShowInTargetAdapter implements IShowInTarget {
+ private IViewPart view;
+
+ public ShowInTargetAdapter(IViewPart viewPart) {
+ super();
+ view = viewPart;
+ }
+
+ public boolean show(ShowInContext context) {
+ IStructuredSelection selection = getSelection(context);
+ if (selection != null && !selection.isEmpty()) {
+ if (selection.getFirstElement() instanceof ICompilationUnit) {
+ CompilationUnit compilationUnit = (CompilationUnit) selection.getFirstElement();
+ this.show(compilationUnit);
+ }
+ else if (selection.getFirstElement() instanceof IFile) {
+ IFile file = (IFile) selection.getFirstElement();
+ this.show(file);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ private void show(CompilationUnit compilationUnit) {
+ try {
+ this.openInEditor((IFile) compilationUnit.getTypeRoot().getCorrespondingResource());
+ } catch (JavaModelException jme) {
+ JptJpaUiPlugin.instance().logError(jme);
+ }
+ JpaProject jpaProject = (JpaProject) compilationUnit.getJavaProject().getProject().getAdapter(JpaProject.class);
+ PersistenceUnit persistenceUnit = this.getPersistenceUnit(jpaProject);
+ if (persistenceUnit != null) {
+ String typeName = compilationUnit.getTypeRoot().findPrimaryType().getFullyQualifiedName();
+ PersistentType persistentType =this.getPersistenceUnit(jpaProject).getPersistentType(typeName);
+ if (persistentType != null) {
+ this.setSelection(persistentType);
+ }
+ }
+ }
+
+ private void show(IFile file) {
+ this.openInEditor(file);
+ JpaProject jpaProject = (JpaProject) file.getProject().getAdapter(JpaProject.class);
+ IContentType contentType = PlatformTools.getContentType(file);
+ if (contentType != null) {
+ PersistenceUnit persistenceUnit = this.getPersistenceUnit(jpaProject);
+ if (persistenceUnit != null) {
+ if (contentType.isKindOf(ResourceMappingFile.Root.CONTENT_TYPE)) {
+ for (MappingFileRef mappingFileRef : persistenceUnit.getMappingFileRefs()) {
+ if (mappingFileRef.getMappingFile() != null &&
+ mappingFileRef.getMappingFile().getResource().getProjectRelativePath().equals(file.getProjectRelativePath())) {
+ this.setSelection(mappingFileRef.getMappingFile().getRoot());
+ }
+ }
+ } else if (contentType.isKindOf(XmlPersistence.CONTENT_TYPE)) {
+ // use persistence instead of persistence unit in order to set the selection to the root node
+ this.setSelection(this.getPersistence(jpaProject));
+ }
+ }
+ }
+ }
+
+ private IStructuredSelection getSelection(ShowInContext context) {
+ if (context == null) {
+ return StructuredSelection.EMPTY;
+ }
+ ISelection selection = context.getSelection();
+ if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
+ return (IStructuredSelection) selection;
+ }
+ Object input = context.getInput();
+ if (input != null) {
+ return new StructuredSelection(input);
+ }
+ return StructuredSelection.EMPTY;
+ }
+
+ private void openInEditor(IFile file) {
+ IWorkbenchPage page = view.getSite().getPage();
+ try {
+ IDE.openEditor(page, file);
+ } catch (PartInitException ex) {
+ JptJpaUiPlugin.instance().logError(ex);
+ }
+ }
+
+ private PersistenceUnit getPersistenceUnit(JpaProject jpaProject) {
+ Persistence persistence = this.getPersistence(jpaProject);
+ if (persistence == null) {
+ return null;
+ }
+ if (persistence.getPersistenceUnitsSize() == 0) {
+ return null;
+ }
+ return persistence.getPersistenceUnits().iterator().next();
+ }
+
+ private Persistence getPersistence(JpaProject jpaProject) {
+ if (jpaProject == null) {
+ return null;
+ }
+ PersistenceXml persistenceXml = jpaProject.getRootContextNode().getPersistenceXml();
+ if (persistenceXml == null) {
+ return null;
+ }
+ return persistenceXml.getRoot();
+ }
+
+ private void setSelection(JpaStructureNode node) {
+ IWorkbenchPage page = view.getSite().getPage();
+ JpaSelectionManager selectionManager = PlatformTools.getAdapter(page, JpaSelectionManager.class);
+ selectionManager.setSelection(node);
+ }
+ }
+
+}
\ No newline at end of file
--
cgit v1.2.3