/* * @see StructuredViewer#handleInvalidSelection(ISelection, ISelection) */ protected void handleInvalidSelection(ISelection invalidSelection, ISelection newSelection) { // on change of input, try to keep selected methods stable by selecting a method with the same // signature: See #5466 List oldSelections = SelectionUtil.toList(invalidSelection); List newSelections = SelectionUtil.toList(newSelection); if (!oldSelections.isEmpty()) { ArrayList newSelectionElements = new ArrayList(newSelections); try { Object[] currElements = getFilteredChildren(getInput()); for (int i = 0; i < oldSelections.size(); i++) { Object curr = oldSelections.get(i); if (curr instanceof IMethod && !newSelections.contains(curr)) { IMethod method = (IMethod) curr; if (method.exists()) { IMethod similar = findSimilarMethod(method, currElements); if (similar != null) { newSelectionElements.add(similar); } } } } if (!newSelectionElements.isEmpty()) { newSelection = new StructuredSelection(newSelectionElements); } else if (currElements.length > 0) { newSelection = new StructuredSelection(currElements[0]); } } catch (ModelException e) { DLTKUIPlugin.log(e); } } setSelection(newSelection); updateSelection(newSelection); }
/** * Finds and returns the Script element selected in the given part. * * @param part the workbench part for which to find the selected Script element * @param selection the selection * @return the selected Script element */ protected Object findSelectedModelElement(IWorkbenchPart part, ISelection selection) { try { if (isValidWorkbenchPart(part) && selection instanceof ITextSelection) { final IEditorPart editor = (IEditorPart) part; IModelElement[] elements = TextSelectionConverter.codeResolve(editor, (ITextSelection) selection); if (elements != null && elements.length > 0) { return elements.length == 1 ? elements[0] : (Object) new ModelElementArray(elements); } } else if (selection instanceof IStructuredSelection) { Object element = SelectionUtil.getSingleElement(selection); return findModelElement(element); } } catch (ModelException e) { DLTKUIPlugin.log(e); } return null; }