/** * Returns a valid instance selection for the current selection of the tree viewer. Returns <code> * null</code> if there is none. * * @return a valid instance selection for the current selection of the tree viewer or <code>null * </code> */ private InstanceSelection getValidSelection() { ISelection viewerSelection = treeViewer.getSelection(); if (!(viewerSelection instanceof ITreeSelection) || viewerSelection.isEmpty()) return null; ITreeSelection treeSelection = (ITreeSelection) treeViewer.getSelection(); TreePath firstPath = treeSelection.getPaths()[0]; // XXX use all paths // instead of first // only? InstanceValidationMessage firstMessage; Iterator<InstanceValidationMessage> restIter; if (firstPath.getLastSegment() instanceof InstanceValidationMessage) { firstMessage = (InstanceValidationMessage) firstPath.getLastSegment(); restIter = Iterators.emptyIterator(); } else { Collection<InstanceValidationMessage> messages = contentProvider.getMessages(treeSelection.getPaths()[0]); if (messages.isEmpty()) return null; // shouldn't happen, but doesn't really matter restIter = messages.iterator(); firstMessage = restIter.next(); } InstanceService is = PlatformUI.getWorkbench().getService(InstanceService.class); // check first message for valid instance reference if (firstMessage.getInstanceReference() == null || is.getInstance(firstMessage.getInstanceReference()) == null) return null; Set<InstanceReference> references = new HashSet<InstanceReference>(); references.add(firstMessage.getInstanceReference()); while (restIter.hasNext()) references.add(restIter.next().getInstanceReference()); return new DefaultInstanceSelection(references.toArray()); }
/** * Changes the state of the TransformationView according to the currently selected shape * * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent event) */ @Override public void selectionChanged(SelectionChangedEvent event) { // Get the TransformationView if it is open TransformationView transformationView = (TransformationView) workbenchPage.findView(TransformationView.ID); // Return if not if (transformationView == null) { return; } // Get the tree paths ITreeSelection selection = (ITreeSelection) event.getSelection(); TreePath[] paths = selection.getPaths(); // Remove the "selected" value from previously selected shapes for (IShape shape : selectedShapes) { shape.removeProperty("selected"); } selectedShapes.clear(); // Set the "selected" value to true for newly selected shapes for (TreePath path : paths) { Object selectedObject = path.getLastSegment(); // Only perform the action for selected IShapes // (rather than GeometryComponents or null) if (selectedObject instanceof IShape) { IShape selectedShape = (IShape) selectedObject; selectedShape.setProperty("selected", "true"); selectedShapes.add(selectedShape); } } // Set the TransformationView shape to null if nothing is selected // or there are multiple selections if (paths.length != 1) { transformationView.setShape(null); return; } Object selectedObject = paths[0].getLastSegment(); // Determine if the shape of the TransformationView should be set if (selectedObject instanceof IShape) { transformationView.setShape((IShape) selectedObject); } else { transformationView.setShape(null); } }
private void addCompareActionOrNot(IMenuManager manager) { if (fViewer.getSelection() instanceof ITreeSelection) { ITreeSelection treeSelection = (ITreeSelection) fViewer.getSelection(); if (treeSelection.getPaths().length == 2) { manager.add(fCompareAction); } } }
@SuppressWarnings("static-access") public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchPage page = Workbench.getWorkbenchPage(); if (page == null) { return null; } IPerspectiveDescriptor perspective = page.getPerspective(); if (perspective != null & perspective.getLabel().equals("PyDev")) { Global.FileList.clear(); ISelection selection = page.getSelection(); // Console.println(selection.toString()); if (selection instanceof ITreeSelection) { ITreeSelection treeSelection = (ITreeSelection) selection; TreePath[] paths = treeSelection.getPaths(); if (paths.length == 1) { if (paths[0] != null) { Object object = paths[0].getLastSegment(); if (object instanceof IAdaptable) { IAdaptable adaptable = (IAdaptable) object; IResource resource = (IResource) adaptable.getAdapter(IResource.class); if (resource != null) { if (resource.getType() == resource.FILE) { OpenExplorerFile(resource.getLocation().toOSString()); } else if (resource.getType() == resource.PROJECT || resource.getType() == resource.FOLDER) { OpenExplorerPath(resource.getLocation().toOSString()); } } } } } } } return null; }