Example usage for org.eclipse.jface.viewers IStructuredSelection iterator

List of usage examples for org.eclipse.jface.viewers IStructuredSelection iterator

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers IStructuredSelection iterator.

Prototype

@Override
public Iterator iterator();

Source Link

Document

Returns an iterator over the elements of this selection.

Usage

From source file:com.foosbar.mailsnag.views.MessagesView.java

License:Open Source License

/**
 * Gets an iterator containing elements selected in the Mail View
 * // w  w  w .j  a v  a  2  s . c  o m
 * @return iterator with selected items
 */
@SuppressWarnings("unchecked")
private Iterator<Object> getSelectedMessagesIterator() {
    IStructuredSelection iss = (IStructuredSelection) getViewer().getSelection();
    return iss.iterator();
}

From source file:com.github.hs2501.eclipse.util.showfile.ShowFileInHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchSite site = HandlerUtil.getActiveSite(event);
    if (site == null) {
        return null;
    }// w w  w.ja v a 2 s  . c o  m

    // We will support multi-selection
    IStructuredSelection selection = getStructuredSelection(event);
    Iterator<?> iterator = selection.iterator();
    while (iterator.hasNext()) {
        Object elem = iterator.next();
        IResource res = null;
        // Most of the time the selected item will not be IResource but
        // adaptable to IResource for instance IJavaElement.
        if (elem instanceof IAdaptable) {
            res = (IResource) ((IAdaptable) elem).getAdapter(IResource.class);
        } else if (elem instanceof IResource) {
            res = (IResource) elem;
        }

        // TODO: If we can't find the corresponding resource
        // then we should disable the command on the selection.
        if (res == null) {
            return null;
        }

        String path = mapResourceToFilePath(res);

        // We will get null value in the path, if we couldn't find 
        if (path == null) {
            // TODO: Localize the string
            String msg = MessageFormat
                    .format("Unable to find the corresponding file for the selected resource: {0}", res);//$NON-NLS-1$
            IStatus status = new Status(IStatus.WARNING, ShowFilePlugin.PLUGIN_ID, msg);
            ShowFilePlugin.getDefault().getLog().log(status);
        } else {
            try {
                // Create OS specific command which we will execute.
                String cmdToExecute = getRuntimeCommand(path);
                Runtime.getRuntime().exec(cmdToExecute);
            } catch (Exception e) {
                // TODO: Localize the string
                String msg = MessageFormat.format("Error in opening the file resource: {0}", res);//$NON-NLS-1$
                IStatus status = new Status(IStatus.ERROR, ShowFilePlugin.PLUGIN_ID, msg, e);
                ShowFilePlugin.getDefault().getLog().log(status);
            }
        }
    }
    return null;
}

From source file:com.github.parzonka.ccms.handler.BatchProcessingHandler.java

License:Open Source License

/**
 * Converts the given structured selection into an array of Java elements.
 * structured selection is not of type <code>IJavaElement</code> it is not
 * included into the list./* ww  w  .ja va2 s  .  co m*/
 *
 * @param selection
 *            the selection
 * @return the Java element contained in the selection
 */
private static IJavaElement[] getJavaElements(IStructuredSelection selection) {

    List<IJavaElement> result = new ArrayList<IJavaElement>();
    Iterator<?> iter = selection.iterator();
    while (iter.hasNext()) {
        Object element = iter.next();
        if (element instanceof IJavaElement)
            result.add((IJavaElement) element);
    }
    return result.toArray(new IJavaElement[result.size()]);
}

From source file:com.github.sdbg.debug.ui.internal.util.LaunchUtils.java

License:Open Source License

public static IResource getSelectedResource(IWorkbenchWindow window) {
    IWorkbenchPage page = window.getActivePage();

    if (page == null) {
        return null;
    }/*ww  w .  j ava 2  s  .c  o  m*/

    IWorkbenchPart part = page.getActivePart();

    if (part instanceof IEditorPart) {
        IEditorPart epart = (IEditorPart) part;

        return (IResource) epart.getEditorInput().getAdapter(IResource.class);
    } else if (part != null) {
        IWorkbenchPartSite site = part.getSite();

        if (site != null) {
            ISelectionProvider provider = site.getSelectionProvider();

            if (provider != null) {
                ISelection selection = provider.getSelection();

                if (selection instanceof IStructuredSelection) {
                    IStructuredSelection ss = (IStructuredSelection) selection;

                    if (!ss.isEmpty()) {
                        Iterator<?> iterator = ss.iterator();

                        while (iterator.hasNext()) {
                            Object next = iterator.next();

                            //&&&                
                            //                if (next instanceof DartElement) {
                            //                  next = ((DartElement) next).getResource();
                            //                }
                            //
                            if (next instanceof IResource) {
                                return (IResource) next;
                            } else if (next != null) {
                                IResource resource = (IResource) Platform.getAdapterManager().getAdapter(next,
                                        IResource.class);

                                if (resource != null) {
                                    return resource;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (page.getActiveEditor() != null) {
        return (IResource) page.getActiveEditor().getEditorInput().getAdapter(IResource.class);
    }

    return null;
}

From source file:com.google.code.t4eclipse.tools.action.ShowSelection.java

License:Open Source License

/**
 * The action has been activated. The argument of the method represents the
 * 'real' action sitting in the workbench UI.
 * /* www.  j a v a2 s .c om*/
 * @see IWorkbenchWindowActionDelegate#run
 */
@SuppressWarnings("rawtypes")
public void run(IAction action) {
    IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();

    ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
    if (selectionProvider == null) {
        System.out.println("no selection provide or this part!");
        return;
    }

    System.out.println("the selection provide is:" + selectionProvider.getClass().getName());

    ISelection selection = selectionProvider.getSelection();

    if (selection instanceof IStructuredSelection) {
        System.out.println("structure selection:" + selection.getClass().getName() + " :" + selection);
        IStructuredSelection sstruction = (IStructuredSelection) selection;
        System.out.println("first element: class" + sstruction.getFirstElement().getClass().getName()
                + ":  content:" + sstruction.getFirstElement());

        Object[] allObject = sstruction.toArray();
        System.out.println(" object size:" + allObject.length);
        Iterator iterator = sstruction.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().getClass().getName());
        }
    } else {
        System.out.println("commmon  selection:" + selection.getClass().getName());

        System.out.println("selection" + selection);
    }

    if (selection instanceof TreeSelection) {
        System.out.println("TreeSelection:");
        TreeSelection selection3 = (TreeSelection) selection;
        TreePath[] path = selection3.getPaths();
        for (TreePath p : path) {
            System.out.println(" tree path--------------- :" + p);
            int count = p.getSegmentCount();
            for (int i = 0; i < count; i++) {
                Object segment = p.getSegment(i);
                System.out.println(" segament " + i + " :" + segment.getClass().getName() + " " + segment);
            }
        }
    }
}

From source file:com.google.dart.tools.debug.ui.internal.util.LaunchUtils.java

License:Open Source License

public static IResource getSelectedResource(IWorkbenchWindow window) {
    IWorkbenchPage page = window.getActivePage();

    if (page == null) {
        return null;
    }/*from  w ww.ja v a2  s.  co  m*/

    IWorkbenchPart part = page.getActivePart();

    if (part instanceof IEditorPart) {
        IEditorPart epart = (IEditorPart) part;

        return (IResource) epart.getEditorInput().getAdapter(IResource.class);
    } else if (part != null) {
        IWorkbenchPartSite site = part.getSite();

        if (site != null) {
            ISelectionProvider provider = site.getSelectionProvider();

            if (provider != null) {
                ISelection selection = provider.getSelection();

                if (selection instanceof IStructuredSelection) {
                    IStructuredSelection ss = (IStructuredSelection) selection;

                    if (!ss.isEmpty()) {
                        Iterator<?> iterator = ss.iterator();

                        while (iterator.hasNext()) {
                            Object next = iterator.next();

                            if (next instanceof DartElement) {
                                next = ((DartElement) next).getResource();
                            }

                            if (next instanceof IResource) {
                                return (IResource) next;
                            } else if (next != null) {
                                IResource resource = (IResource) Platform.getAdapterManager().getAdapter(next,
                                        IResource.class);

                                if (resource != null) {
                                    return resource;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (page.getActiveEditor() != null) {
        return (IResource) page.getActiveEditor().getEditorInput().getAdapter(IResource.class);
    }

    return null;
}

From source file:com.google.dart.tools.search2.internal.ui.SearchHistorySelectionDialog.java

License:Open Source License

@Override
protected void buttonPressed(int buttonId) {
    if (buttonId == REMOVE_ID) {
        IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
        Iterator<?> searchResults = selection.iterator();
        while (searchResults.hasNext()) {
            Object curr = searchResults.next();
            fRemovedEntries.add(curr);//  ww w.j a  v  a  2  s  .c  om
            fInput.remove(curr);
            fViewer.remove(curr);
        }
        if (fViewer.getSelection().isEmpty() && !fInput.isEmpty()) {
            fViewer.setSelection(new StructuredSelection(fInput.get(0)));
        }
        return;
    }
    super.buttonPressed(buttonId);
}

From source file:com.google.dart.tools.ui.actions.CleanFoldersAction.java

License:Open Source License

private boolean allSelectedAreProjects(IStructuredSelection selection) {
    for (Iterator<?> iterator = selection.iterator(); iterator.hasNext();) {
        Object selectedElement = iterator.next();
        if (!(selectedElement instanceof IProject)) {
            return false;
        }//from  ww w. j av a 2  s. c  o m
    }
    return true;
}

From source file:com.google.dart.tools.ui.actions.OpenAction.java

License:Open Source License

private boolean checkEnabled(IStructuredSelection selection) {
    if (selection.isEmpty()) {
        return false;
    }/* w ww .ja  v a 2s.  co  m*/
    for (Iterator<?> iter = selection.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (element instanceof SourceReference) {
            continue;
        }
        if (element instanceof IFile) {
            continue;
        }
        if (DartModelUtil.isOpenableStorage(element)) {
            continue;
        }
        if (element instanceof HTMLFile) {
            continue;
        }
        return false;
    }
    return true;
}

From source file:com.google.dart.tools.ui.callhierarchy.CallHierarchyUI.java

License:Open Source License

/**
 * Converts an ISelection (containing MethodWrapper instances) to an ISelection with the
 * MethodWrapper's replaced by their corresponding TypeMembers. If the selection contains elements
 * which are not MethodWrapper instances or not already TypeMember instances they are discarded.
 * /*from  w w w. j ava 2  s . c o m*/
 * @param selection The selection to convert.
 * @return An ISelection containing TypeMember's in place of MethodWrapper instances.
 */
static ISelection convertSelection(ISelection selection) {
    if (selection.isEmpty()) {
        return selection;
    }

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        List<DartElement> elements = new ArrayList<DartElement>();
        for (Iterator<?> iter = structuredSelection.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (element instanceof MethodWrapper) {
                DartElement member = ((MethodWrapper) element).getMember();
                if (member != null) {
                    elements.add(member);
                }
            } else if (element instanceof DartElement) {
                elements.add((DartElement) element);
            } else if (element instanceof CallLocation) {
                DartElement member = ((CallLocation) element).getMember();
                elements.add(member);
            }
        }
        return new StructuredSelection(elements);
    }
    return StructuredSelection.EMPTY;
}