From 6a02e74534f20edfbdc34b4e26917eec31dabdda Mon Sep 17 00:00:00 2001 From: Leo Dos Santos Date: Wed, 3 Oct 2012 18:45:36 -0700 Subject: [PATCH] STS-2927: re-enabled Open Bean Definition action --- .../actions/OpenBeanDefinitionAction.java | 146 ++++++++++++++++++ .../actions/OpenContextFileAction.java | 99 ------------ .../livegraph/views/LiveBeansGraphView.java | 27 ++-- 3 files changed, 161 insertions(+), 111 deletions(-) create mode 100644 plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenBeanDefinitionAction.java delete mode 100644 plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenContextFileAction.java diff --git a/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenBeanDefinitionAction.java b/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenBeanDefinitionAction.java new file mode 100644 index 0000000000..c8a1080b56 --- /dev/null +++ b/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenBeanDefinitionAction.java @@ -0,0 +1,146 @@ +/******************************************************************************* + * Copyright (c) 2012 VMware, Inc. + * 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 + * + * Contributors: + * VMware, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.eclipse.beans.ui.livegraph.actions; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.IType; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.actions.BaseSelectionListenerAction; +import org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUiPlugin; +import org.springframework.ide.eclipse.beans.ui.livegraph.model.LiveBean; +import org.springsource.ide.eclipse.commons.core.JdtUtils; +import org.springsource.ide.eclipse.commons.core.SpringCoreUtils; +import org.springsource.ide.eclipse.commons.core.StatusHandler; +import org.springsource.ide.eclipse.commons.ui.SpringUIUtils; + +/** + * @author Leo Dos Santos + */ +public class OpenBeanDefinitionAction extends BaseSelectionListenerAction { + + public OpenBeanDefinitionAction() { + super("Open Bean Definition File"); + } + + @Override + public void run() { + IStructuredSelection selection = getStructuredSelection(); + List elements = selection.toList(); + String appName = null; + final List contexts = new ArrayList(); + for (Object obj : elements) { + if (obj instanceof LiveBean) { + LiveBean bean = (LiveBean) obj; + appName = bean.getApplicationName(); + final String appContext = bean.getResource(); + if (appContext != null && appContext.trim().length() > 0 && !appContext.equalsIgnoreCase("null")) { + String resourceStr = null; + + // extract the resource path out of the descriptive text + int indexStart = appContext.indexOf("["); + int indexEnd = appContext.indexOf("]"); + if (indexStart > -1 && indexEnd > -1 && indexStart < indexEnd) { + resourceStr = appContext.substring(indexStart + 1, indexEnd); + } + + if (resourceStr != null) { + if (resourceStr.endsWith(".xml")) { + // Strip the path until we can map it properly to a + // project resource. For new we're going to traverse + // the project structure to open XML files + if (resourceStr.contains(File.separator)) { + int pathSeparator = resourceStr.lastIndexOf(File.separator); + resourceStr = resourceStr.substring(pathSeparator + 1); + contexts.add(resourceStr); + } + } + else if (resourceStr.endsWith(".class")) { + // Strip the path until we can map it properly to a + // project resource. For now if the .class file name + // matches the bean type, open the bean type. + if (resourceStr.contains(File.separator)) { + int pathSeparator = resourceStr.lastIndexOf(File.separator); + String className = resourceStr.substring(pathSeparator + 1, + resourceStr.lastIndexOf(".class")); + String beanType = bean.getBeanType(); + if (beanType != null && beanType.endsWith(className) && appName != null) { + try { + IProject project = SpringCoreUtils.createProject(appName, null, + new NullProgressMonitor()); + IType type = JdtUtils.getJavaType(project, beanType); + SpringUIUtils.openInEditor(type); + } + catch (CoreException e) { + StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID, + "An error occurred while attempting to open a class file.", e)); + } + } + } + } + } + } + } + } + + if (appName != null) { + // find the XML files in the workspace and open them + try { + IProject project = SpringCoreUtils.createProject(appName, null, new NullProgressMonitor()); + project.accept(new IResourceVisitor() { + public boolean visit(final IResource resource) throws CoreException { + if (resource instanceof IFile) { + for (String appContext : contexts) { + if (appContext.equals(resource.getName().trim())) { + SpringUIUtils.openInEditor((IFile) resource, 0); + } + } + return false; + } + return true; + } + }); + } + catch (CoreException e) { + StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID, + "An error occurred while attempting to open an application context file.", e)); + } + } + } + + @Override + protected boolean updateSelection(IStructuredSelection selection) { + if (!selection.isEmpty()) { + List elements = selection.toList(); + for (Object obj : elements) { + if (obj instanceof LiveBean) { + LiveBean bean = (LiveBean) obj; + String appContext = bean.getResource(); + if (appContext != null && appContext.trim().length() > 0 && !appContext.equalsIgnoreCase("null")) { + return true; + } + } + } + } + return false; + } + +} diff --git a/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenContextFileAction.java b/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenContextFileAction.java deleted file mode 100644 index 2db8608feb..0000000000 --- a/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/actions/OpenContextFileAction.java +++ /dev/null @@ -1,99 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012 VMware, Inc. - * 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 - * - * Contributors: - * VMware, Inc. - initial API and implementation - *******************************************************************************/ -package org.springframework.ide.eclipse.beans.ui.livegraph.actions; - -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.IResourceVisitor; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Status; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.ui.actions.BaseSelectionListenerAction; -import org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUiPlugin; -import org.springframework.ide.eclipse.beans.ui.livegraph.model.LiveBean; -import org.springsource.ide.eclipse.commons.core.SpringCoreUtils; -import org.springsource.ide.eclipse.commons.core.StatusHandler; -import org.springsource.ide.eclipse.commons.ui.SpringUIUtils; - -/** - * @author Leo Dos Santos - */ -public class OpenContextFileAction extends BaseSelectionListenerAction { - - public OpenContextFileAction() { - super("Open Context File"); - } - - @Override - public void run() { - IStructuredSelection selection = getStructuredSelection(); - List elements = selection.toList(); - final List contexts = new ArrayList(); - for (Object obj : elements) { - if (obj instanceof LiveBean) { - LiveBean bean = (LiveBean) obj; - final String appContext = bean.getResource(); - if (appContext != null && appContext.trim().length() > 0) { - contexts.add(appContext); - } - } - } - - // find the app contexts in the workspace and open them - try { - // need a project mapper in place of hard-coded sample - IProject project = SpringCoreUtils.createProject("org.springframework.samples.petclinic", null, - new NullProgressMonitor()); - project.accept(new IResourceVisitor() { - public boolean visit(final IResource resource) throws CoreException { - if (resource instanceof IFile) { - for (String appContext : contexts) { - if (appContext.equals(resource.getName().trim())) { - SpringUIUtils.openInEditor((IFile) resource, 0); - } - } - return false; - } - return true; - } - }); - } - catch (CoreException e) { - StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID, - "An error occurred while attempting to open an application context file.", e)); - } - - } - - @Override - protected boolean updateSelection(IStructuredSelection selection) { - if (!selection.isEmpty()) { - List elements = selection.toList(); - for (Object obj : elements) { - if (obj instanceof LiveBean) { - LiveBean bean = (LiveBean) obj; - String appContext = bean.getResource(); - if (appContext != null && appContext.trim().length() > 0) { - return true; - } - } - } - } - return false; - } - -} diff --git a/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/views/LiveBeansGraphView.java b/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/views/LiveBeansGraphView.java index d625db77a1..2ee8d5ec78 100644 --- a/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/views/LiveBeansGraphView.java +++ b/plugins/org.springframework.ide.eclipse.beans.ui.livegraph/src/org/springframework/ide/eclipse/beans/ui/livegraph/views/LiveBeansGraphView.java @@ -15,6 +15,8 @@ import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; +import org.eclipse.jface.viewers.DoubleClickEvent; +import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Menu; @@ -30,6 +32,7 @@ import org.eclipse.zest.layouts.algorithms.HorizontalShift; import org.springframework.ide.eclipse.beans.ui.livegraph.actions.ConnectToApplicationAction; import org.springframework.ide.eclipse.beans.ui.livegraph.actions.OpenBeanClassAction; +import org.springframework.ide.eclipse.beans.ui.livegraph.actions.OpenBeanDefinitionAction; import org.springframework.ide.eclipse.beans.ui.livegraph.model.LiveBeansModel; /** @@ -45,7 +48,7 @@ public class LiveBeansGraphView extends ViewPart { private BaseSelectionListenerAction openBeanClassAction; - // private BaseSelectionListenerAction openContextAction; + private BaseSelectionListenerAction openBeanDefAction; private Action connectApplicationAction; @@ -66,20 +69,20 @@ public void createPartControl(Composite parent) { hookPullDownMenu(); hookContextMenu(); - // viewer.addDoubleClickListener(new IDoubleClickListener() { - // public void doubleClick(DoubleClickEvent event) { - // if (openContextAction != null && openContextAction.isEnabled()) { - // openContextAction.run(); - // } - // } - // }); + viewer.addDoubleClickListener(new IDoubleClickListener() { + public void doubleClick(DoubleClickEvent event) { + if (openBeanDefAction != null && openBeanDefAction.isEnabled()) { + openBeanDefAction.run(); + } + } + }); } @Override public void dispose() { if (viewer != null) { viewer.removeSelectionChangedListener(openBeanClassAction); - // viewer.removeSelectionChangedListener(openContextAction); + viewer.removeSelectionChangedListener(openBeanDefAction); } super.dispose(); } @@ -87,7 +90,7 @@ public void dispose() { private void fillContextMenu(IMenuManager menuManager) { menuManager.add(new Separator()); menuManager.add(openBeanClassAction); - // menuManager.add(openContextAction); + menuManager.add(openBeanDefAction); } private void fillPullDownMenu(IMenuManager menuManager) { @@ -119,8 +122,8 @@ private void hookPullDownMenu() { private void makeActions() { openBeanClassAction = new OpenBeanClassAction(); viewer.addSelectionChangedListener(openBeanClassAction); - // openContextAction = new OpenContextFileAction(); - // viewer.addSelectionChangedListener(openContextAction); + openBeanDefAction = new OpenBeanDefinitionAction(); + viewer.addSelectionChangedListener(openBeanDefAction); connectApplicationAction = new ConnectToApplicationAction(this); }