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

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

Introduction

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

Prototype

public Object[] toArray();

Source Link

Document

Returns the elements in this selection as an array.

Usage

From source file:com.centurylink.mdw.plugin.designer.wizards.ExportProjectWizard.java

License:Apache License

public void init(IWorkbench workbench, IStructuredSelection selection) {
    setDefaultPageImageDescriptor(MdwPlugin.getImageDescriptor("icons/mdw_wiz.png"));
    projectsToExport = new ArrayList<WorkflowProject>();
    if (selection != null) {
        for (Object element : selection.toArray()) {
            if (element instanceof WorkflowProject && ((WorkflowProject) element).isRemote())
                projectsToExport.add((WorkflowProject) element);
        }/*from w  ww .  jav  a2s.  co m*/
    }
}

From source file:com.codenvy.eclipse.ui.team.AbstractProjectHandler.java

License:Open Source License

@Override
public final Object execute(ExecutionEvent event) throws ExecutionException {
    final Set<IProject> projects = new HashSet<>();
    final ISelection selection = HandlerUtil.getCurrentSelection(event);

    if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
        final IStructuredSelection structuredSelection = (IStructuredSelection) selection;

        for (Object oneObject : structuredSelection.toArray()) {
            final IResource oneResource = getResource(oneObject);
            if (oneResource != null) {
                projects.add(oneResource.getProject());
            }//from   w  w w . java2s . co  m
        }
    }

    // if resource are selected the active editor input is null
    final IEditorInput activeEditorInput = HandlerUtil.getActiveEditorInput(event);
    if (activeEditorInput != null) {
        final IResource editorResource = getResource(activeEditorInput);

        if (editorResource != null) {
            projects.add(editorResource.getProject());
        }
    }

    return execute(projects, event);
}

From source file:com.dadabeatnik.codemap.ui.editors.EntryTableViewerDragDropHandler.java

License:Open Source License

private void doMoveOperation(DropTargetEvent event) {
    // Dragged Entries
    IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();
    Object[] dragged = selection.toArray();

    // Find Drop Target
    Object dropTarget = getDropTarget(event);

    List<EntryModel> entries = ((CodeMapModel) fEditor.getAdapter(CodeMapModel.class)).getEntries();

    int newPosition;

    // Dropped on an entry
    if (dropTarget instanceof EntryModel) {
        newPosition = entries.indexOf(dropTarget);
    }//  w  w  w .j  a  v  a2  s .  c  om
    // Dropped in clear space
    else {
        newPosition = entries.size() - 1;
    }

    for (Object draggedEntry : dragged) {
        int oldPosition = entries.indexOf(draggedEntry);
        entries.remove(draggedEntry);
        entries.add(newPosition, (EntryModel) draggedEntry);
        if (newPosition < oldPosition) {
            newPosition++;
        }
    }

    fViewer.refresh();
    fEditor.setDirty(true);
}

From source file:com.dadabeatnik.codemap.ui.editors.FileTreeViewerDragDropHandler.java

License:Open Source License

private void doDropOperation(DropTargetEvent event) {
    if (!(LocalSelectionTransfer.getTransfer().getSelection() instanceof IStructuredSelection)) {
        return;//from  w w w .java 2  s.c om
    }

    IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();

    for (Object element : selection.toArray()) {
        FileModel fileModel = null;

        if (element instanceof IFile) {
            fileModel = new FileModel((IFile) element);
        }

        else if (element instanceof ICompilationUnit) {
            IFile iFile = (IFile) ((ICompilationUnit) element).getResource();
            fileModel = new FileModel(iFile);
        }

        if (fileModel != null && fEntryModel != null && fEntryModel.addReferencedFile(fileModel)) {
            fEditor.setDirty(true);
        }
    }

    fViewer.refresh();
}

From source file:com.drgarbage.controlflowgraphfactory.actions.CopyAction.java

License:Apache License

/**
 * Sets the selected EditPart and refreshes the enabled state of this action.
 * //from w  w  w  .  j  a va2 s.  co  m
 * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
 */
public void selectionChanged(SelectionChangedEvent event) {
    ISelection s = event.getSelection();
    if (!(s instanceof IStructuredSelection))
        return;
    IStructuredSelection selection = (IStructuredSelection) s;
    template = null;
    if (selection != null) {

        Object objs[] = selection.toArray();
        List<EditPart> elements = new ArrayList<EditPart>();
        for (Object o : objs) {
            if (o instanceof EditPart) {
                Object model = ((EditPart) o).getModel();
                if (model != null && !(model instanceof ControlFlowGraphDiagram)) {
                    elements.add((EditPart) o);
                }
            }
        }
        if (elements.size() != 0) {
            template = elements;
        }

    }
    refresh();
}

From source file:com.gcsf.pcm.gui.views.ContactsSearchBar.java

License:Open Source License

private void createRefreshJob() {
    fRefreshJob = new WorkbenchJob("") {//$NON-NLS-1$
        @Override/*from ww w . j a v  a 2 s  .co m*/
        public IStatus runInUIThread(IProgressMonitor monitor) {

            /* Tree Disposed */
            if (fViewer.getControl().isDisposed())
                return Status.CANCEL_STATUS;

            /* Get the Filter Pattern */
            String text = fFilterText != null ? fFilterText.getText() : null;
            if (text == null)
                return Status.OK_STATUS;

            /* Check if the Initial Text was set */
            boolean initial = fInitialText != null && fInitialText.equals(text);

            try {
                fViewer.getControl().getParent().setRedraw(false);

                /* Remember Expanded Elements if not yet done */
                if (fExpandedElements == null)
                    fExpandedElements = fViewer.getExpandedElements();

                /* Remember Selected Elements if present */
                IStructuredSelection sel = (IStructuredSelection) fViewer.getSelection();
                if (!sel.isEmpty())
                    fSelectedElements = sel.toArray();

                /* Refresh Tree */
                BusyIndicator.showWhile(getDisplay(), new Runnable() {
                    public void run() {
                        fViewer.refresh(false);
                    }
                });

                /* Restore Expanded Elements and Selection when Filter is disabled */
                if (text.length() == 0) {

                    /* Restore Expansion */
                    fViewer.collapseAll();
                    for (Object element : fExpandedElements) {
                        fViewer.setExpandedState(element, true);
                    }

                    /* Restore Selection */
                    if (fSelectedElements != null)
                        fViewer.setSelection(new StructuredSelection(fSelectedElements), true);

                    /* Clear Fields */
                    fExpandedElements = null;
                    fSelectedElements = null;
                }

                /*
                 * Expand elements one at a time. After each is expanded, check to see
                 * if the filter text has been modified. If it has, then cancel the
                 * refresh job so the user doesn't have to endure expansion of all the
                 * nodes.
                 */
                if (text.length() > 0 && !initial) {
                    IStructuredContentProvider provider = (IStructuredContentProvider) fViewer
                            .getContentProvider();
                    Object[] elements = provider.getElements(fViewer.getInput());
                    for (Object element : elements) {
                        if (monitor.isCanceled())
                            return Status.CANCEL_STATUS;

                        fViewer.expandToLevel(element, AbstractTreeViewer.ALL_LEVELS);
                    }

                    /* Make Sure to show the First Item */
                    TreeItem[] items = fViewer.getTree().getItems();
                    if (items.length > 0)
                        fViewer.getTree().showItem(items[0]);

                    /* Enable Toolbar to allow resetting the Filter */
                    setToolBarVisible(true);
                }

                /* Disable Toolbar - No Filter is currently activated */
                else {
                    setToolBarVisible(false);
                }
            }

            /* Done updating the tree - set redraw back to true */
            finally {
                fViewer.getControl().getParent().setRedraw(true);
            }

            return Status.OK_STATUS;
        }
    };
    fRefreshJob.setSystem(true);

    /* Cancel the Job once the Tree got disposed */
    fViewer.getControl().addDisposeListener(new DisposeListener() {
        public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
            fRefreshJob.cancel();
        };
    });
}

From source file:com.genuitec.eclipse.gerrit.tools.internal.utils.commands.TagAndPushHandler.java

License:Open Source License

@Override
protected Object internalExecute(ExecutionEvent event) throws Exception {
    IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
    final Shell shell = HandlerUtil.getActiveShell(event);

    List<Repository> repositories = new ArrayList<Repository>();

    //acquire the list of repositories
    for (Object o : selection.toArray()) {
        RepositoryNode node = (RepositoryNode) o;
        repositories.add(node.getRepository());
        assert node.getRepository() != null;
    }/*from   w w w.j  a  v  a 2 s  .  c  om*/

    //configure tagging operation
    TagAndPushDialog tagAndPushConfigDialog = new TagAndPushDialog(shell, repositories);
    if (tagAndPushConfigDialog.open() != IDialogConstants.OK_ID) {
        return null;
    }
    //perform tagging operation
    try {
        ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(shell);
        final TagAndPushOperation op = new TagAndPushOperation(progressDialog.getShell(), repositories,
                tagAndPushConfigDialog.getSettings());
        progressDialog.run(true, true, op);
        shell.getDisplay().asyncExec(new Runnable() {

            public void run() {
                if (op.getResult().getSeverity() >= IStatus.WARNING) {
                    Policy.getStatusHandler().show(op.getResult(), "Results of the operation");
                } else {
                    MessageDialog.openInformation(shell, "Create and Push Tag",
                            "Repositories has been successfully tagged.");
                }
            }
        });
    } catch (InterruptedException e) {
        //ignore
    } catch (Exception e) {
        GerritToolsPlugin.getDefault().getLog()
                .log(new Status(IStatus.ERROR, GerritToolsPlugin.PLUGIN_ID, e.getLocalizedMessage(), e));
        MessageDialog.openError(shell, "Create and Push Tag", e.getLocalizedMessage());
    }

    return null;
}

From source file:com.github.fengtan.sophie.dialogs.EditListValueDialog.java

License:Open Source License

@Override
protected Control createDialogArea(final Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    composite.setLayout(new RowLayout());

    // Create list widget.
    listViewer = new ListViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.BORDER);
    for (Object value : defaultValue) {
        listViewer.add(Objects.toString(value, StringUtils.EMPTY));
    }/*  w ww .j  av  a2  s.  c om*/

    // Create add/remove buttons.
    Composite buttonsComposite = new Composite(composite, SWT.NULL);
    buttonsComposite.setLayout(new FillLayout(SWT.VERTICAL));

    Button buttonAdd = new Button(buttonsComposite, SWT.PUSH);
    buttonAdd.setText("Add");
    buttonAdd.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            InputDialog input = new InputDialog(parent.getShell(), "Add value", "Add value:", null, null);
            input.open();
            if (input.getReturnCode() == IDialogConstants.OK_ID) {
                listViewer.add(input.getValue());
            }
        }
    });

    Button buttonRemove = new Button(buttonsComposite, SWT.PUSH);
    buttonRemove.setText("Remove");
    buttonRemove.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) listViewer.getSelection();
            if (!selection.isEmpty()) {
                listViewer.remove(selection.toArray());
            }
        }
    });

    return composite;
}

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.
 * /*from w  w w.ja  v  a  2s  .  c o  m*/
 * @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.code.t4eclipse.tools.utility.SelectionUtility.java

License:Open Source License

public static String getSelectionInfo(IWorkbenchPart part) {
    String s = "\n\n";

    ISelectionProvider provider = part.getSite().getSelectionProvider();
    if (provider == null) {
        s += "NO Selection Provider in this active part:\n";

        return s;
    }/*from  ww  w  . jav  a  2s  . com*/

    s += "Selection  Provider:\n";
    s += provider.getClass().getName() + "\n\n";
    ISelection sel = provider.getSelection();
    if (sel == null) {
        s += "Selection is null\n";
        return s;
    }
    s += "Selection:\n";
    s += "class:" + sel.getClass().getName() + "\n";
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) sel;
        s += "\n";
        s += "instanceof IStructuredSelection\n";
        s += "--first element--:\n";
        Object firstElement = ssel.getFirstElement();
        if (firstElement == null) {
            s += "  null\n";
        } else {
            s += "  class:\n    " + firstElement.getClass().getName() + "\n";
            s += "  tostr:\n";
            s += "    " + firstElement.toString() + "\n";
        }

        s += "  size:" + ssel.size() + "\n";
        if (ssel.size() > 1)
            s += "  all elements class:\n";
        Object[] eles = ssel.toArray();
        for (int i = 0; i < eles.length; i++) {
            s += "  [" + i + "]:" + eles[i].getClass().getName() + "\n";
        }

    }

    s += "\n\nselection toString():\n";
    s += sel.toString() + "\n";
    return s;
}