Skip to content
Snippets Groups Projects
Commit f9dedd8f authored by Simon Barner's avatar Simon Barner
Browse files

selectAll(): make private and return empty list instead of null

* Return empty list instead of null on unexpected input (null or wrong
  selection type)
* Fixes NPE in setSelection()

Issue-Ref: 3545
Issue-Url: https://af3-developer.fortiss.org/issues/3545



Signed-off-by: default avatarSimon Barner <barner@fortiss.org>
parent 18f0422e
No related branches found
No related tags found
1 merge request!28selectAll(): make private and return empty list instead of null
...@@ -7,7 +7,7 @@ EObjectSelectionUtils.java 128cf8f96c6b9478171dff3deda662d5934f5f44 GREEN ...@@ -7,7 +7,7 @@ EObjectSelectionUtils.java 128cf8f96c6b9478171dff3deda662d5934f5f44 GREEN
KernelUIUtils.java 46d3279ef3523b104f89a6c526109f72d36f72f2 GREEN KernelUIUtils.java 46d3279ef3523b104f89a6c526109f72d36f72f2 GREEN
ObservableUtils.java 34abfd1dfaf9c0acbb31caf1f525e7b39416c116 GREEN ObservableUtils.java 34abfd1dfaf9c0acbb31caf1f525e7b39416c116 GREEN
PropertiesConstantUtils.java 59b1a1e4d594bb98db3aa396f2ff6474ba405920 GREEN PropertiesConstantUtils.java 59b1a1e4d594bb98db3aa396f2ff6474ba405920 GREEN
SelectionUtils.java 3d20f87eaaee04173686ef62b09ca6971702cd00 GREEN SelectionUtils.java 33aec7cccccb28e5568140cf8e5443ce0f9f59f7 YELLOW
TutorialUIServiceUtils.java 093a8a3549c6952d44ea508e66691434b17a95b5 GREEN TutorialUIServiceUtils.java 093a8a3549c6952d44ea508e66691434b17a95b5 GREEN
UndoRedoImpl.java f218500875bda0ef52f4cc2ccdf452825e6751f7 GREEN UndoRedoImpl.java f218500875bda0ef52f4cc2ccdf452825e6751f7 GREEN
WidgetsFactory.java 5be121cc81e93731f4d0ab11e7707417fa950c2c GREEN WidgetsFactory.java 5be121cc81e93731f4d0ab11e7707417fa950c2c GREEN
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
+--------------------------------------------------------------------------*/ +--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.util; package org.fortiss.tooling.kernel.ui.util;
import static java.util.Collections.emptyList;
import static org.conqat.ide.commons.ui.ui.WorkbenchUtils.getActiveEditor; import static org.conqat.ide.commons.ui.ui.WorkbenchUtils.getActiveEditor;
import static org.fortiss.tooling.common.util.LambdaUtils.filterByType;
import java.util.ArrayList; import java.util.Collection;
import java.util.List; import java.util.List;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
...@@ -90,29 +92,24 @@ public class SelectionUtils { ...@@ -90,29 +92,24 @@ public class SelectionUtils {
} }
/** /**
* Returns a list of all the selected elements that have the given type or * Returns a {@link Collection} of all the selected elements that have the given type (possibly
* null if selection is null or is not an IStructuredSelection. * an empty list, in particular if {@code selection} is {@code null} or is not an
* {@link IStructuredSelection}.
* *
* @param selection * @param selection
* the current selection * the current selection
* @param type * @param type
* the type of elements to be returned * the type of elements to be returned
* @return the list of elements of the given type * @return the {@link Collection} of elements of the given type
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> List<T> selectAll(ISelection selection, Class<T> type) { private static <T> Collection<T> selectAll(ISelection selection, Class<T> type) {
if(selection == null || !(selection instanceof IStructuredSelection)) { if(selection == null || !(selection instanceof IStructuredSelection)) {
return null; return emptyList();
} }
IStructuredSelection structuredSelection = (IStructuredSelection)selection; IStructuredSelection structuredSelection = (IStructuredSelection)selection;
List<T> elements = new ArrayList<T>(); return filterByType(structuredSelection.toList(), type);
for(Object element : structuredSelection.toList()) {
if(type.isInstance(element)) {
elements.add((T)element);
}
}
return elements;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment