Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBenoit Maggi2014-04-18 15:53:52 +0000
committerCamille Letavernier2014-04-18 16:14:18 +0000
commit3541995e815cf2a111e439315d9cefdfd8318699 (patch)
treeb1b4cbe19ea4423b248c3195cecb1322f20d98aa /tests
parent26ca0dd9ac2a74b06d4ae3fed09a801f7706e1cb (diff)
downloadorg.eclipse.papyrus-3541995e815cf2a111e439315d9cefdfd8318699.tar.gz
org.eclipse.papyrus-3541995e815cf2a111e439315d9cefdfd8318699.tar.xz
org.eclipse.papyrus-3541995e815cf2a111e439315d9cefdfd8318699.zip
Bug 431298 - NPE in ShowHideRelatedContentsHandler
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService() there is a non-null activeWindow. Change-Id: I5b9d471a2b1877bf07e9af46f5a625daf9dc90a5 Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
Diffstat (limited to 'tests')
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/ModelExplorerUtils.java425
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/PackageExplorerUtils.java182
2 files changed, 305 insertions, 302 deletions
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/ModelExplorerUtils.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/ModelExplorerUtils.java
index ad3541dd4e7..d9d8bf6a5f7 100644
--- a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/ModelExplorerUtils.java
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/ModelExplorerUtils.java
@@ -1,211 +1,214 @@
-/*****************************************************************************
- * Copyright (c) 2012, 2014 CEA LIST 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
- * Christian W. Damus (CEA) - bug 386118
- *
- *****************************************************************************/
-package org.eclipse.papyrus.junit.utils;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.Command;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.IHandler;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.transaction.RunnableWithResult;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
-import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
-import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPage;
-import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPageBookView;
-import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.commands.ICommandService;
-import org.eclipse.ui.part.IPage;
-import org.junit.Assert;
-
-/**
- * Useful methods for the ModelExplorer view
- */
-public class ModelExplorerUtils {
-
- /**
- * the ID of the ModelExplorerView
- */
- private static final String ModelExplorerViewId = "org.eclipse.papyrus.views.modelexplorer.modelexplorer"; //$NON-NLS-1$
-
- private ModelExplorerUtils() {
- // to prevent instanciation
- }
-
- /**
- *
- * @return
- * the opened modelexplorer. Warning, it should be better that Papyrus was opened yet
- * @throws PartInitException
- */
- public static ModelExplorerView openModelExplorerView() throws PartInitException {
- IViewPart modelexplorer = null;
- modelexplorer = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ModelExplorerViewId);
- final ModelExplorerPageBookView view = (ModelExplorerPageBookView)modelexplorer;
- final IPage currentPage = view.getCurrentPage();
- final ModelExplorerPage page = (ModelExplorerPage)currentPage;
- final IViewPart viewer = page.getViewer();
- Assert.assertNotNull(viewer);
- viewer.setFocus();
- return (ModelExplorerView)viewer;
- }
-
- /**
- *
- * @param view
- * the modelexplorer to manipulate
- * @param elements
- * the elements to select
- */
- public static void setSelectionInTheModelexplorer(final ModelExplorerView view, List<?> elements) {
- view.revealSemanticElement(elements);
- final List<?> currentSelection = getCurrentSelectionInTheModelExplorer();
- Assert.assertTrue("The current selection is not the wanted selection", elements.containsAll(currentSelection));
- Assert.assertTrue("The current selection is not the wanted selection", currentSelection.containsAll(elements));
- }
-
- /**
- *
- * @return
- * the object selected in the ModelExplorer
- * //TODO : should be moved in the ModelExplorer
- */
- public static List<?> getCurrentSelectionInTheModelExplorer() {
- final IStructuredSelection currentSelection = (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(ModelExplorerViewId);
- final List<Object> selection = new ArrayList<Object>();
- final Iterator<?> iter = currentSelection.iterator();
- while(iter.hasNext()) {
- final Object current = iter.next();
- EObject eObject = EMFHelper.getEObject(current);
- if(eObject != null) {
- selection.add(eObject);
- } else {
- selection.add(current);
- }
-
- }
- return selection;
- }
-
- /**
- *
- * @param view
- * the ModelExplorerView
- * @return
- * the root of the Model
- * //TODO : should be moved in the ModelExplorer
- */
- public static final EObject getRootInModelExplorer(final ModelExplorerView view) {
- view.getCommonViewer().expandToLevel(2);
-
- // store the root of the model
- final Object[] visibleElement = view.getCommonViewer().getVisibleExpandedElements();
- EObject modelRoot = null;
- if(visibleElement.length > 0) {
- modelRoot = EMFHelper.getEObject(visibleElement[0]);
- }
- Assert.assertNotNull(modelRoot);
- while(modelRoot.eContainer() != null) {
- modelRoot = modelRoot.eContainer();
- }
- return modelRoot;
- }
-
- /**
- *
- * @param actionContext
- * the creation context
- * @param wantedResult
- * the wanted result
- */
- public static final void testHandlerStatusInModelExplorer(final ModelExplorerView view, final String commandToTest, final EObject actionContext, boolean wantedResult) {
- setSelectionInTheModelexplorer(view, Collections.singletonList(actionContext));
- ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
- Command cmd = commandService.getCommand(commandToTest);
- IHandler handler = cmd.getHandler();
- if(handler instanceof AbstractHandler) {
- ((AbstractHandler)handler).setEnabled(commandToTest);
- }
- boolean res = handler.isEnabled();
- Assert.assertEquals(wantedResult, res);
- }
-
- /**
- * Execute an editor command creation and returns the current papyrus nested editor (you must verify that it is the correct editor to be sure of
- * the command execution)
- *
- * @param currentPapyrusEditor
- * the current PapyrusEditor
- * @param view
- * the model explorer view
- * @param commandToExecute
- * the command to execute
- * @param actionContext
- * the context used for the commadn (the selected elements)
- * @param bundelID
- * the bundle id
- *
- * @return
- * the current papyrus nested editor (you must verify that it is the correct editor to be sure of
- * the command execution)
- */
- public static final Object executeCreateNestedEditorHandlerInModelExplorer(final IMultiDiagramEditor currentPapyrusEditor, final ModelExplorerView view, final String commandToExecute, final EObject actionContext, final String bundelID) {
- setSelectionInTheModelexplorer(view, Collections.singletonList(actionContext));
- ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
- final Command cmd = commandService.getCommand(commandToExecute);
- final IHandler handler = cmd.getHandler();
- if(handler instanceof AbstractHandler) {
- ((AbstractHandler)handler).setEnabled(commandToExecute);
- }
- final RunnableWithResult<?> runnableWithResult = new RunnableWithResult.Impl<Object>() {
-
- public void run() {
- try {
- handler.execute(new ExecutionEvent(cmd, Collections.emptyMap(), null, null));
- } catch (ExecutionException e) {
- setStatus(new Status(IStatus.ERROR, bundelID, e.getMessage()));
- }
-
- IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
- IEditorPart activeEditor = activePage.getActiveEditor();
- if(currentPapyrusEditor != activeEditor) {
- setStatus(new Status(IStatus.ERROR, bundelID, "The current active editor is not the wanted Papyrus Editor"));
- }
-
- setResult(currentPapyrusEditor.getActiveEditor());
- setStatus(Status.OK_STATUS);
- }
- };
- Display.getDefault().syncExec(runnableWithResult);
- Assert.assertEquals(runnableWithResult.getStatus().getMessage(), IStatus.OK, runnableWithResult.getStatus().getSeverity());
- Object result = runnableWithResult.getResult();
- Assert.assertNotNull(result);
- return result;
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2012, 2014 CEA LIST 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
+ * Christian W. Damus (CEA) - bug 386118
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.junit.utils;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.IHandler;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.RunnableWithResult;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
+import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
+import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPage;
+import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPageBookView;
+import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.part.IPage;
+import org.junit.Assert;
+
+/**
+ * Useful methods for the ModelExplorer view
+ */
+public class ModelExplorerUtils {
+
+ /**
+ * the ID of the ModelExplorerView
+ */
+ private static final String ModelExplorerViewId = "org.eclipse.papyrus.views.modelexplorer.modelexplorer"; //$NON-NLS-1$
+
+ private ModelExplorerUtils() {
+ // to prevent instanciation
+ }
+
+ /**
+ *
+ * @return
+ * the opened modelexplorer. Warning, it should be better that Papyrus was opened yet
+ * @throws PartInitException
+ */
+ public static ModelExplorerView openModelExplorerView() throws PartInitException {
+ IViewPart modelexplorer = null;
+ modelexplorer = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ModelExplorerViewId);
+ final ModelExplorerPageBookView view = (ModelExplorerPageBookView)modelexplorer;
+ final IPage currentPage = view.getCurrentPage();
+ final ModelExplorerPage page = (ModelExplorerPage)currentPage;
+ final IViewPart viewer = page.getViewer();
+ Assert.assertNotNull(viewer);
+ viewer.setFocus();
+ return (ModelExplorerView)viewer;
+ }
+
+ /**
+ *
+ * @param view
+ * the modelexplorer to manipulate
+ * @param elements
+ * the elements to select
+ */
+ public static void setSelectionInTheModelexplorer(final ModelExplorerView view, List<?> elements) {
+ view.revealSemanticElement(elements);
+ final List<?> currentSelection = getCurrentSelectionInTheModelExplorer();
+ Assert.assertTrue("The current selection is not the wanted selection", elements.containsAll(currentSelection)); //$NON-NLS-1$
+ Assert.assertTrue("The current selection is not the wanted selection", currentSelection.containsAll(elements)); //$NON-NLS-1$
+ }
+
+ /**
+ *
+ * @return
+ * the object selected in the ModelExplorer
+ * //TODO : should be moved in the ModelExplorer
+ */
+ public static List<?> getCurrentSelectionInTheModelExplorer() {
+ final List<Object> selection = new ArrayList<Object>();
+ IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (activeWorkbenchWindow!=null){
+ final IStructuredSelection currentSelection = (IStructuredSelection)activeWorkbenchWindow.getSelectionService().getSelection(ModelExplorerViewId);
+ final Iterator<?> iter = currentSelection.iterator();
+ while(iter.hasNext()) {
+ final Object current = iter.next();
+ EObject eObject = EMFHelper.getEObject(current);
+ if(eObject != null) {
+ selection.add(eObject);
+ } else {
+ selection.add(current);
+ }
+ }
+ }
+ return selection;
+ }
+
+ /**
+ *
+ * @param view
+ * the ModelExplorerView
+ * @return
+ * the root of the Model
+ * //TODO : should be moved in the ModelExplorer
+ */
+ public static final EObject getRootInModelExplorer(final ModelExplorerView view) {
+ view.getCommonViewer().expandToLevel(2);
+
+ // store the root of the model
+ final Object[] visibleElement = view.getCommonViewer().getVisibleExpandedElements();
+ EObject modelRoot = null;
+ if(visibleElement.length > 0) {
+ modelRoot = EMFHelper.getEObject(visibleElement[0]);
+ }
+ Assert.assertNotNull(modelRoot);
+ while(modelRoot.eContainer() != null) {
+ modelRoot = modelRoot.eContainer();
+ }
+ return modelRoot;
+ }
+
+ /**
+ *
+ * @param actionContext
+ * the creation context
+ * @param wantedResult
+ * the wanted result
+ */
+ public static final void testHandlerStatusInModelExplorer(final ModelExplorerView view, final String commandToTest, final EObject actionContext, boolean wantedResult) {
+ setSelectionInTheModelexplorer(view, Collections.singletonList(actionContext));
+ ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+ Command cmd = commandService.getCommand(commandToTest);
+ IHandler handler = cmd.getHandler();
+ if(handler instanceof AbstractHandler) {
+ ((AbstractHandler)handler).setEnabled(commandToTest);
+ }
+ boolean res = handler.isEnabled();
+ Assert.assertEquals(wantedResult, res);
+ }
+
+ /**
+ * Execute an editor command creation and returns the current papyrus nested editor (you must verify that it is the correct editor to be sure of
+ * the command execution)
+ *
+ * @param currentPapyrusEditor
+ * the current PapyrusEditor
+ * @param view
+ * the model explorer view
+ * @param commandToExecute
+ * the command to execute
+ * @param actionContext
+ * the context used for the commadn (the selected elements)
+ * @param bundelID
+ * the bundle id
+ *
+ * @return
+ * the current papyrus nested editor (you must verify that it is the correct editor to be sure of
+ * the command execution)
+ */
+ public static final Object executeCreateNestedEditorHandlerInModelExplorer(final IMultiDiagramEditor currentPapyrusEditor, final ModelExplorerView view, final String commandToExecute, final EObject actionContext, final String bundelID) {
+ setSelectionInTheModelexplorer(view, Collections.singletonList(actionContext));
+ ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+ final Command cmd = commandService.getCommand(commandToExecute);
+ final IHandler handler = cmd.getHandler();
+ if(handler instanceof AbstractHandler) {
+ ((AbstractHandler)handler).setEnabled(commandToExecute);
+ }
+ final RunnableWithResult<?> runnableWithResult = new RunnableWithResult.Impl<Object>() {
+
+ public void run() {
+ try {
+ handler.execute(new ExecutionEvent(cmd, Collections.emptyMap(), null, null));
+ } catch (ExecutionException e) {
+ setStatus(new Status(IStatus.ERROR, bundelID, e.getMessage()));
+ }
+
+ IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ IEditorPart activeEditor = activePage.getActiveEditor();
+ if(currentPapyrusEditor != activeEditor) {
+ setStatus(new Status(IStatus.ERROR, bundelID, "The current active editor is not the wanted Papyrus Editor")); //$NON-NLS-1$
+ }
+
+ setResult(currentPapyrusEditor.getActiveEditor());
+ setStatus(Status.OK_STATUS);
+ }
+ };
+ Display.getDefault().syncExec(runnableWithResult);
+ Assert.assertEquals(runnableWithResult.getStatus().getMessage(), IStatus.OK, runnableWithResult.getStatus().getSeverity());
+ Object result = runnableWithResult.getResult();
+ Assert.assertNotNull(result);
+ return result;
+ }
+}
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/PackageExplorerUtils.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/PackageExplorerUtils.java
index d6ef585d17e..cb3e3f1b8c6 100644
--- a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/PackageExplorerUtils.java
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/PackageExplorerUtils.java
@@ -1,91 +1,91 @@
-/*****************************************************************************
- * Copyright (c) 2012 CEA LIST.
- *
- *
- * 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:
- * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.junit.utils;
-
-import java.util.List;
-
-import org.eclipse.jdt.ui.IPackagesViewPart;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
-import org.junit.Assert;
-
-/**
- *
- * Utils Methods for the Package Explorer
- *
- */
-public class PackageExplorerUtils {
-
- /** ID of the Package Explorer View */
- private static final String PACKAGE_EXPLORER_VIEW_ID = "org.eclipse.jdt.ui.PackageExplorer";
-
- /**
- * This methods opens the PackageExplorerView, and give it the focus
- *
- * @throws PartInitException
- */
- public static final IPackagesViewPart openPackageExplorerView() throws PartInitException {
- final IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
- IPackagesViewPart pack = null;
- IViewPart packageExplorer = activeWorkbenchWindow.getActivePage().showView(PACKAGE_EXPLORER_VIEW_ID);
- pack = (IPackagesViewPart)packageExplorer;
- Assert.assertNotNull(pack);
- pack.setFocus();
- return pack;
-
- }
-
- /**
- * Set the selection in the PackageExplorer
- *
- * @param packageExplorer
- * the package explorer
- * @param newSelection
- * the new selection
- */
- public static final void setSelectionInPackageExplorerView(final IPackagesViewPart packageExplorer, final IStructuredSelection newSelection) {
- packageExplorer.getTreeViewer().expandAll();
- packageExplorer.getTreeViewer().setSelection(newSelection);
- //we verify that the current selection is correct in the PackageExplorer
- IStructuredSelection currentSelection = (IStructuredSelection)packageExplorer.getTreeViewer().getSelection();
- Assert.assertEquals("Package Explorer: The current selection is not the same as the wanted selection", currentSelection.toList(), newSelection.toList());
-
- //we verify that the current selection is correct using the selection service
- currentSelection = getCurrentSelectionInPackageExplorerView();
- Assert.assertEquals("Package Explorer: The SelectionService doesn't return the wanted selection", currentSelection.toList(), newSelection.toList());
- }
-
- /**
- *
- * @return
- * the current selection in the PackageExplorer
- */
- public static final IStructuredSelection getCurrentSelectionInPackageExplorerView() {
- return (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(PACKAGE_EXPLORER_VIEW_ID);
- }
-
- /**
- *
- * @return
- * the current selection in the PackageExplorer as List
- */
- public static final List<?> getCurrentSelectionAsListInPackageExplorerView() {
- final IStructuredSelection selection = (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(PACKAGE_EXPLORER_VIEW_ID);
- final List<?> list = selection.toList();
- return list;
- }
-}
+/*****************************************************************************
+ * Copyright (c) 2012 CEA LIST.
+ *
+ *
+ * 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:
+ * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.junit.utils;
+
+import java.util.List;
+
+import org.eclipse.jdt.ui.IPackagesViewPart;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.junit.Assert;
+
+/**
+ *
+ * Utils Methods for the Package Explorer
+ *
+ */
+public class PackageExplorerUtils {
+
+ /** ID of the Package Explorer View */
+ private static final String PACKAGE_EXPLORER_VIEW_ID = "org.eclipse.jdt.ui.PackageExplorer"; //$NON-NLS-1$
+
+ /**
+ * This methods opens the PackageExplorerView, and give it the focus
+ *
+ * @throws PartInitException
+ */
+ public static final IPackagesViewPart openPackageExplorerView() throws PartInitException {
+ final IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ IPackagesViewPart pack = null;
+ IViewPart packageExplorer = activeWorkbenchWindow.getActivePage().showView(PACKAGE_EXPLORER_VIEW_ID);
+ pack = (IPackagesViewPart)packageExplorer;
+ Assert.assertNotNull(pack);
+ pack.setFocus();
+ return pack;
+
+ }
+
+ /**
+ * Set the selection in the PackageExplorer
+ *
+ * @param packageExplorer
+ * the package explorer
+ * @param newSelection
+ * the new selection
+ */
+ public static final void setSelectionInPackageExplorerView(final IPackagesViewPart packageExplorer, final IStructuredSelection newSelection) {
+ packageExplorer.getTreeViewer().expandAll();
+ packageExplorer.getTreeViewer().setSelection(newSelection);
+ //we verify that the current selection is correct in the PackageExplorer
+ IStructuredSelection currentSelection = (IStructuredSelection)packageExplorer.getTreeViewer().getSelection();
+ Assert.assertEquals("Package Explorer: The current selection is not the same as the wanted selection", currentSelection.toList(), newSelection.toList()); //$NON-NLS-1$
+
+ //we verify that the current selection is correct using the selection service
+ currentSelection = getCurrentSelectionInPackageExplorerView();
+ Assert.assertEquals("Package Explorer: The SelectionService doesn't return the wanted selection", currentSelection.toList(), newSelection.toList()); //$NON-NLS-1$
+ }
+
+ /**
+ *
+ * @return
+ * the current selection in the PackageExplorer
+ */
+ public static final IStructuredSelection getCurrentSelectionInPackageExplorerView() {
+ return (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(PACKAGE_EXPLORER_VIEW_ID);
+ }
+
+ /**
+ *
+ * @return
+ * the current selection in the PackageExplorer as List
+ */
+ public static final List<?> getCurrentSelectionAsListInPackageExplorerView() {
+ final IStructuredSelection selection = (IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(PACKAGE_EXPLORER_VIEW_ID);
+ final List<?> list = selection.toList();
+ return list;
+ }
+}

Back to the top