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.android.ide.eclipse.adt.AdtUtils.java

License:Open Source License

/**
 * Returns the Android project(s) that are selected or active, if any. This
 * considers the selection, the active editor, etc.
 *
 * @param selection the current selection
 * @return a list of projects, possibly empty (but never null)
 *//* w w  w.j  a  va 2 s .c o m*/
@NonNull
public static List<IProject> getSelectedProjects(@Nullable ISelection selection) {
    List<IProject> projects = new ArrayList<IProject>();

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        // get the unique selected item.
        Iterator<?> iterator = structuredSelection.iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();

            // First look up the resource (since some adaptables
            // provide an IResource but not an IProject, and we can
            // always go from IResource to IProject)
            IResource resource = null;
            if (element instanceof IResource) { // may include IProject
                resource = (IResource) element;
            } else if (element instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) element;
                Object adapter = adaptable.getAdapter(IResource.class);
                resource = (IResource) adapter;
            }

            // get the project object from it.
            IProject project = null;
            if (resource != null) {
                project = resource.getProject();
            } else if (element instanceof IAdaptable) {
                project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
            }

            if (project != null && !projects.contains(project)) {
                projects.add(project);
            }
        }
    }

    if (projects.isEmpty()) {
        // Try to look at the active editor instead
        IFile file = AdtUtils.getActiveFile();
        if (file != null) {
            projects.add(file.getProject());
        }
    }

    if (projects.isEmpty()) {
        // If we didn't find a default project based on the selection, check how many
        // open Android projects we can find in the current workspace. If there's only
        // one, we'll just select it by default.
        IJavaProject[] open = AdtUtils.getOpenAndroidProjects();
        for (IJavaProject project : open) {
            projects.add(project.getProject());
        }
        return projects;
    } else {
        // Make sure all the projects are Android projects
        List<IProject> androidProjects = new ArrayList<IProject>(projects.size());
        for (IProject project : projects) {
            if (BaseProjectHelper.isAndroidProject(project)) {
                androidProjects.add(project);
            }
        }
        return androidProjects;
    }
}

From source file:com.appnativa.studio.properties.PropertySheetViewer.java

License:Open Source License

/**
 * Reset the selected properties to their default values.
 *///from w  w  w .  j av  a 2 s .c o m
public void resetProperties() {
    // Determine the selection
    IStructuredSelection selection = (IStructuredSelection) getSelection();
    // Iterate over entries and reset them
    Iterator itr = selection.iterator();

    while (itr.hasNext()) {
        ((IPropertySheetEntry) itr.next()).resetPropertyValue();
    }
}

From source file:com.aptana.ide.debug.internal.ui.actions.WatchAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
 *      org.eclipse.jface.viewers.ISelection)
 *//*from w w w.  j a va 2 s. com*/
public void selectionChanged(IAction action, ISelection selection) {
    fSelection = null;
    if (!action.isEnabled()) {
        return;
    }
    int enabled = 0;
    int size = -1;
    if (selection instanceof IStructuredSelection) {
        fSelection = selection;
        IStructuredSelection sSelection = (IStructuredSelection) selection;
        size = sSelection.size();
        IExpressionManager manager = DebugPlugin.getDefault().getExpressionManager();
        Iterator iterator = sSelection.iterator();
        while (iterator.hasNext()) {
            IVariable variable = (IVariable) iterator.next();
            if (manager.hasWatchExpressionDelegate(variable.getModelIdentifier())) {
                enabled++;
            } else {
                break;
            }
        }
        if (enabled != size) {
            action.setEnabled(false);
        }
    } else if (selection instanceof ITextSelection) {
        fSelection = selection;
        ITextSelection tSelection = (ITextSelection) selection;
        if (tSelection.getLength() == 0) {
            action.setEnabled(false);
        }
    }
}

From source file:com.aptana.ide.editors.views.profiles.ProfilesView.java

License:Open Source License

private void addDragDrop() {
    final DropTarget labeldt = new DropTarget(infoLabel, DND.DROP_MOVE);

    labeldt.setTransfer(new Transfer[] { FileTransfer.getInstance() });
    labeldt.addDropListener(new DropTargetAdapter() {
        public void drop(DropTargetEvent event) {
            handleDrop(event);/*from   www . j  av a2 s .c om*/
        }
    });

    DragSource ds = new DragSource(viewer.getControl(), DND.DROP_COPY | DND.DROP_MOVE);
    ds.setTransfer(new Transfer[] { FileTransfer.getInstance() });
    ds.addDragListener(new DragSourceAdapter() {
        @SuppressWarnings("unchecked")
        public void dragStart(DragSourceEvent event) {

            super.dragStart(event);

            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            for (Iterator iter = selection.iterator(); iter.hasNext();) {
                Object element = iter.next();
                if (element instanceof Profile) {
                    Profile p = (Profile) element;
                    if (p.getURIs().length == 0) {
                        event.doit = false;
                        return;
                    }
                }
            }
        }

        public void dragSetData(DragSourceEvent event) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();

            if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
                Object[] items = selection.toArray();
                ArrayList<String> al = new ArrayList<String>();

                for (int i = 0; i < items.length; i++) {
                    if (items[i] instanceof ProfileURI) {
                        al.add(CoreUIUtils.getPathFromURI(((ProfileURI) items[i]).getURI()));
                    } else if (items[i] instanceof Profile) {
                        Profile p = (Profile) items[i];
                        for (int j = 0; j < p.getURIs().length; j++) {
                            ProfileURI object = p.getURIs()[j];
                            al.add(CoreUIUtils.getPathFromURI(object.getURI()));
                        }
                    }
                }

                event.data = al.toArray(new String[0]);
            }
        }
    });

    DropTarget dt = new DropTarget(viewer.getControl(), DND.DROP_MOVE);
    dt.setTransfer(new Transfer[] { FileTransfer.getInstance() });
    dt.addDropListener(new DropTargetAdapter() {
        public void drop(DropTargetEvent event) {
            handleDrop(event);
        }
    });
}

From source file:com.aptana.ide.search.epl.ResourceTransferDragAdapter.java

License:Open Source License

private List convertSelection() {
    ISelection s = fProvider.getSelection();
    if (!(s instanceof IStructuredSelection))
        return Collections.EMPTY_LIST;
    IStructuredSelection selection = (IStructuredSelection) s;
    List result = new ArrayList(selection.size());
    for (Iterator iter = selection.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (element instanceof IResource) {
            result.add(element);//  w  w w  .ja va 2 s  . c o m
        }
    }
    return result;
}

From source file:com.aptana.ide.ui.io.navigator.resources.FileDropAdapterAssistant.java

License:Open Source License

@SuppressWarnings("rawtypes")
private static IAdaptable[] getSelectedSourceFiles(IStructuredSelection selection) {
    List<IAdaptable> selectedFiles = new ArrayList<IAdaptable>();

    Iterator iter = selection.iterator();
    Object object;//  w w w.ja va 2  s.co  m
    IFileStore fileStore;
    IAdaptable adaptable;
    while (iter.hasNext()) {
        object = iter.next();
        if (object instanceof IAdaptable) {
            adaptable = (IAdaptable) object;
            fileStore = Utils.getFileStore(adaptable);
            if (fileStore != null) {
                // valid selection
                selectedFiles.add(adaptable);
            }
        }
    }

    return selectedFiles.toArray(new IAdaptable[selectedFiles.size()]);
}

From source file:com.aptana.index.core.ui.views.IndexView.java

License:Open Source License

/**
 * setInputFromSelection/*  w w w.j a  v a2  s . c o  m*/
 * 
 * @param selection
 */
private void setInputFromSelection(ISelection selection) {
    IStructuredSelection ss = (IStructuredSelection) selection;

    if (ss != null && treeViewer != null && !treeViewer.getTree().isDisposed()) {
        Iterator<?> items = ss.iterator();

        while (items.hasNext()) {
            Object item = items.next();

            if (item instanceof IResource) {
                IProject project = ((IResource) item).getProject();

                if (project.isOpen()) {
                    treeViewer.setInput(project);
                } else {
                    treeViewer.setInput(null);
                }

                break;
            }
        }
    }
}

From source file:com.aptana.js.debug.ui.internal.actions.WatchAction.java

License:Open Source License

@SuppressWarnings("rawtypes")
public void selectionChanged(IAction action, ISelection selection) {
    fSelection = null;//from   w w  w .  j ava  2 s  . c  o m
    if (!action.isEnabled()) {
        return;
    }
    int enabled = 0;
    int size = -1;
    if (selection instanceof IStructuredSelection) {
        fSelection = selection;
        IStructuredSelection sSelection = (IStructuredSelection) selection;
        size = sSelection.size();
        IExpressionManager manager = DebugPlugin.getDefault().getExpressionManager();
        IVariable variable;
        for (Iterator iterator = sSelection.iterator(); iterator.hasNext();) {
            variable = (IVariable) iterator.next();
            if (manager.hasWatchExpressionDelegate(variable.getModelIdentifier())) {
                enabled++;
            } else {
                break;
            }
        }
        if (enabled != size) {
            action.setEnabled(false);
        }
    } else if (selection instanceof ITextSelection) {
        fSelection = selection;
        ITextSelection tSelection = (ITextSelection) selection;
        if (tSelection.getLength() == 0) {
            action.setEnabled(false);
        }
    }
}

From source file:com.arc.cdt.debug.seecode.internal.ui.action.AbstractDebugActionDelegate.java

License:Open Source License

/**
 * Runs this action in the UI thread./*from   w  w  w  . ja  v a  2  s.co m*/
 */
private void runInForeground(final IStructuredSelection selection) {
    final MultiStatus status = new MultiStatus(CDebugUIPlugin.getUniqueIdentifier(),
            DebugException.REQUEST_FAILED, getStatusMessage(), null);
    BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            Iterator<Object> selectionIter = selection.iterator();
            while (selectionIter.hasNext()) {
                Object element = selectionIter.next();
                try {
                    doAction(element);
                } catch (DebugException e) {
                    status.merge(e.getStatus());
                }
            }
        }
    });
    reportErrors(status);
}

From source file:com.arc.cdt.debug.seecode.internal.ui.action.AbstractDebugActionDelegate.java

License:Open Source License

/**
 * Return whether the action should be enabled or not based on the given selection.
 *///from  w  ww  .  j  a  va 2s .co m
@SuppressWarnings("unchecked")
protected boolean getEnableStateForSelection(IStructuredSelection selection) {
    if (selection.size() == 0) {
        return false;
    }
    Iterator<Object> itr = selection.iterator();
    while (itr.hasNext()) {
        Object element = itr.next();
        if (!isEnabledFor(element)) {
            return false;
        }
    }
    return true;
}