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.rcpcompany.uibindings.debug.internals.views.CurrentSelectionView.java

License:Open Source License

protected void updateSelection(IWorkbenchPart part, ISelection selection) {
    // LogUtils.debug(this, "part=" + part + "\n" + selection);
    if (part == this)
        return;//from  w  ww.ja  v a2  s  . com
    myCurrentPartValue.setValue(part.toString());

    try {
        myEObjectList.clear();
        myObjectList.clear();

        if (!(selection instanceof IStructuredSelection))
            return;

        final IStructuredSelection ss = (IStructuredSelection) selection;
        for (final Object o : ss.toArray()) {
            if (o instanceof EObject) {
                myEObjectList.add(o);
            } else {
                myObjectList.add(o);
            }
        }
    } finally {
        myObjectViewer.refresh();
    }
}

From source file:com.rcpcompany.uibindings.extests.viewerBindings.ViewerItemMoveTest.java

License:Open Source License

/**
 * Checks the operation of the move command with focus on the resulting selection and focus
 */// www  . jav  a2s.co  m
@Test
public void testOp() {
    try {
        final ICommandService cs = (ICommandService) myView.getSite().getService(ICommandService.class);
        final IHandlerService hs = (IHandlerService) myView.getSite().getService(IHandlerService.class);

        final ParameterizedCommand command = cs.deserialize(myCommandId);
        assertTrue(what + " is defined", command.getCommand().isDefined());

        final Country country = myShop.getCountries().get(myRow);
        // LogUtils.debug(country, "" + country.getName());

        postMouse(myTableViewer.getTable(), myColumn + myViewerBinding.getFirstTableColumnOffset(), myRow);
        yield();

        assertTrue(what + " is not handled", command.getCommand().isHandled());
        assertTrue(what + " is not defined", command.getCommand().isDefined());

        ViewerCell viewerCell = myTableViewer.getColumnViewerEditor().getFocusCell();
        assertEquals(what + " index wrong", myColumn + myViewerBinding.getFirstTableColumnOffset(),
                viewerCell.getColumnIndex());
        assertEquals(what + " element wrong", country, viewerCell.getElement());
        assertEquals(what + " item wrong", myTableViewer.getTable().getItem(myRow),
                viewerCell.getViewerRow().getItem());
        assertEquals(what + " row element", country, viewerCell.getViewerRow().getElement());

        assertEquals(4, myShop.getCountries().size());
        hs.executeCommand(command, null);
        assertEquals(4, myShop.getCountries().size());

        // Seq of items
        final List<Country> myResultList = new ArrayList<Country>();
        for (int i = 0; i < mySeq.length(); i++) {
            final String s = mySeq.substring(i, i + 1);
            for (final Country c : myShop.getCountries()) {
                if (c.getName().equals(s)) {
                    myResultList.add(c);
                    break;
                }
            }
        }
        assertArrayEquals(myResultList.toArray(), myShop.getCountries().toArray());

        // Selection
        final TableItem[] tableItems = myTable.getSelection();
        assertEquals(1, tableItems.length);
        assertEquals(country, tableItems[0].getData());

        // ISelection
        final ISelection selection = myTableViewer.getSelection();
        assertTrue(selection instanceof IStructuredSelection);
        final IStructuredSelection ss = (IStructuredSelection) selection;
        assertEquals(1, ss.toArray().length);
        assertEquals(country, ss.getFirstElement());

        // Viewer cell

        // Focus cell
        viewerCell = myTableViewer.getColumnViewerEditor().getFocusCell();
        assertEquals(myColumn + myViewerBinding.getFirstTableColumnOffset(), viewerCell.getColumnIndex());
        assertEquals(country, viewerCell.getElement());
        assertEquals(myTableViewer.getTable().getItem(myNewRow), viewerCell.getViewerRow().getItem());
        assertEquals(country, viewerCell.getViewerRow().getElement());
    } catch (final Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerPart.java

License:Open Source License

public void refresh(IStructuredSelection selection) {
    Object[] selectedElements = selection.toArray();
    for (int i = 0; i < selectedElements.length; i++) {
        fViewer.refresh(selectedElements[i]);
    }//from  w w w. ja  v  a  2s .c om
}

From source file:com.rsomeara.eclipse.repository.workingsets.handlers.AssignHandler.java

License:Open Source License

/**
 * Filters the selection to project elements
 * //  w  ww.ja v  a 2  s .c o m
 * @param selection
 *            The selection made in the view
 * @return A collection of the selected elements which are projects
 */
private Collection<IProject> getSelectedProjects(IStructuredSelection selection) {
    Collection<IProject> projects = new ArrayList<>();

    if (selection != null) {
        for (Object selected : selection.toArray()) {
            if (selected instanceof IProject) {
                projects.add((IProject) selected);
            } else if (selected instanceof IJavaProject) {
                projects.add(((IJavaProject) selected).getProject());
            }
        }
    }

    return projects;
}

From source file:com.safi.workshop.sqlexplorer.connections.actions.DeleteAction.java

License:Open Source License

@Override
public void run() {
    SafiNavigator nav = SafiWorkshopEditorUtil.getSafiNavigator();
    IStructuredSelection viewerSelection = nav.getViewerSelection();
    if (viewerSelection.isEmpty())
        return;//from  w w w.  ja  va  2s .co  m

    boolean okToDelete = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "Delete Resource",
            "Are you sure you  want to delete the selected resource?");

    if (!okToDelete)
        return;

    List<IResource> resources = new ArrayList<IResource>();
    List<Object> dbResources = new ArrayList<Object>();
    List<ServerResource> serverResources = new ArrayList<ServerResource>();
    for (Object selected : viewerSelection.toArray()) {
        if (selected instanceof ManagedDriver) {

            MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Delete Driver rejected",
                    "You can not delete Driver Resource.");
            return;

        }
        if (selected instanceof DBResource || selected instanceof User || selected instanceof Alias
        /* || selected instanceof ManagedDriver */) {
            dbResources.add(selected);
        } else if (selected instanceof IResource)
            resources.add((IResource) selected);
        else if (selected instanceof ServerResource)
            serverResources.add((ServerResource) selected);
    }

    if (!resources.isEmpty()) {
        if (SafiServerPlugin.getDefault().isConnected()) {
            boolean foundPersisted = false;
            for (IResource res : resources) {
                try {
                    int id = SafletPersistenceManager.getInstance().getResourceId(res);
                    if (id >= 0) {
                        foundPersisted = true;
                        break;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (foundPersisted)
                MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Delete Local Only",
                        "This operation will delete only local copies of any selected Saflets or SafiProjects.  "
                                + " If you wish to delete them from the production SafiServer open the Delete Saflet dialog from the main toolbar.");
        }
        com.safi.workshop.navigator.DeleteResourceAction ac = new com.safi.workshop.navigator.DeleteResourceAction();
        ac.selectionChanged(viewerSelection);
        ac.run();
    }
    if (!dbResources.isEmpty()) {
        int deletePref = 1;
        com.safi.db.server.config.User user = SafiServerPlugin.getDefault().getCurrentUser();
        boolean isConnected = SafiServerPlugin.getDefault().isConnected();

        if (isConnected
                && !EntitlementUtils.isUserEntitled(user, EntitlementUtils.ENTIT_PUBLISH_DB_RESOURCES)) {
            MessageDialog.openError(SafiWorkshopEditorUtil.getActiveShell(), "Not Entitled",
                    "You do not have sufficient privileges to carry out this operation.");
            return;
        }
        removeDBResourceChildren(dbResources);

        // if (SafiServerPlugin.getDefault().isConnected())
        // deleteFromServer =
        // MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),
        // "Delete From Server?",
        // "Do you want to delete database resource(s) from the production SafiServer?");
        for (Object o : dbResources) {
            if (o instanceof Alias) {
                Alias alias = (Alias) o;
                DBConnection conn = alias.getConnection();
                List<Query> queries = new ArrayList<Query>(conn.getQueries());

                alias.getConnection().setLastModified(new Date());
                if (deletePref != 2 && deletePref != 3 && isConnected && alias.getConnection().getId() != -1) {
                    MessageDialog dialog = new MessageDialog(SafiWorkshopEditorUtil.getActiveShell(),
                            "Delete From Server?", null,
                            "Do you want to delete database connection " + alias.getName()
                                    + " from the production SafiServer?",
                            SWT.ICON_QUESTION,
                            new String[] { "YES", "NO", "YES TO ALL", "NO TO ALL", "CANCEL" }, 1);
                    deletePref = dialog.open();
                    if (deletePref == 4)
                        return;
                }
                try {
                    alias.remove(deletePref == 0 || deletePref == 2, true);
                } catch (Exception e) {
                    MessageDialog.openError(SafiWorkshopEditorUtil.getActiveShell(), "Delete Failed",
                            "Couldn't delete alias " + alias.getName() + ": " + e.getLocalizedMessage());
                    AsteriskDiagramEditorPlugin.getInstance().logError("Couldn't delete alias", e);
                }
                for (Query query : queries) {
                    SafiWorkshopEditorUtil.closeSQLEditors(query);
                }
            } else if (o instanceof Query) {
                Query query = (Query) o;
                if (deletePref != 2 && deletePref != 3 && isConnected && query.getId() != -1) {
                    MessageDialog dialog = new MessageDialog(SafiWorkshopEditorUtil.getActiveShell(),
                            "Delete From Server?", null,
                            "Do you want to delete database query " + query.getName()
                                    + " from the production SafiServer?",
                            SWT.ICON_QUESTION,
                            new String[] { "YES", "NO", "YES TO ALL", "NO TO ALL", "CANCEL" }, 1);
                    deletePref = dialog.open();
                    if (deletePref == 4)
                        return;
                }
                if (deletePref == 0 || deletePref == 2) {

                    try {
                        SQLExplorerPlugin.getDefault().deleteDBResource(query);
                    } catch (Exception e) {
                        MessageDialog.openError(SafiWorkshopEditorUtil.getActiveShell(), "Delete Failed",
                                "Couldn't delete query " + query.getName() + ": " + e.getLocalizedMessage());
                        AsteriskDiagramEditorPlugin.getInstance()
                                .logError("Couldn't delete query " + query.getName(), e);
                    }
                }
                query.getConnection().setLastModified(new Date());
                query.getConnection().getQueries().remove(query);
                SafiWorkshopEditorUtil.closeSQLEditors(query);
            } else if (o instanceof ManagedDriver) {
                ManagedDriver driver = (ManagedDriver) o;
                if (driver.getDriver() != null && !driver.getDriver().isDefault()) {
                    if (deletePref != 2 && deletePref != 3 && isConnected && driver.getDriver().getId() != -1) {
                        MessageDialog dialog = new MessageDialog(SafiWorkshopEditorUtil.getActiveShell(),
                                "Delete From Server?", null,
                                "Do you want to delete database driver " + driver.getDriver().getName()
                                        + " from the production SafiServer?",
                                SWT.ICON_QUESTION,
                                new String[] { "YES", "NO", "YES TO ALL", "NO TO ALL", "CANCEL" }, 1);
                        deletePref = dialog.open();
                        if (deletePref == 4)
                            return;
                    }
                    if (deletePref == 0 || deletePref == 1) {
                        try {
                            SQLExplorerPlugin.getDefault().deleteDBResource(driver.getDriver());
                        } catch (Exception e) {
                            MessageDialog.openError(SafiWorkshopEditorUtil.getActiveShell(), "Delete Failed",
                                    "Couldn't delete query " + driver.getDriver().getName() + ": "
                                            + e.getLocalizedMessage());
                            AsteriskDiagramEditorPlugin.getInstance()
                                    .logError("Couldn't delete query " + driver.getDriver().getName(), e);
                        }
                    }
                    SQLExplorerPlugin.getDefault().getDriverModel().removeDriver(driver);
                }
            }
        }
        SQLExplorerPlugin.getDefault().saveDBResources(false);
    }

    if (!serverResources.isEmpty()) {
        com.safi.db.server.config.User user = SafiServerPlugin.getDefault().getCurrentUser();

        List<ServerResource> deleted = new ArrayList<ServerResource>();
        SafiServer production = null;
        try {
            production = SafiServerPlugin.getDefault().getSafiServer(true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (production != null) {
            removeChildren(serverResources);
            boolean checkedTelEntit = false;
            boolean checkedUsers = false;
            for (ServerResource r : serverResources) {
                if (r instanceof com.safi.db.server.config.User) {
                    if (!checkedUsers) {
                        if (!EntitlementUtils.isUserEntitled(user, EntitlementUtils.ENTIT_MANAGE_USERS)) {
                            MessageDialog.openError(SafiWorkshopEditorUtil.getActiveShell(), "Not Entitled",
                                    "You do not have sufficient privileges to carry out this operation.");
                            return;
                        } else
                            checkedUsers = true;
                    }

                    if (r.getId() == SafiServerPlugin.getDefault().getCurrentUser().getId()) {
                        MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
                                "Can't Delete Self", "The currently logged in user cannot be deleted.");
                        continue;
                    } else {
                        production.getUsers().remove(r);
                        deleted.add(r);
                    }
                }

            }
        } else {
            MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Can't Delete",
                    "An error has occurred.  No SafiServer instance could be found.");
            return;
        }
        deleteServerResources(production, deleted);

    }

    if (dbResources.isEmpty() && serverResources.isEmpty())
        nav.refresh();
    else {
        try {
            SafiServerPlugin.getDefault().updateServerResources(new NullProgressMonitor());
        } catch (Exception e) {
            e.printStackTrace();
            MessageDialog.openError(SafiWorkshopEditorUtil.getActiveShell(), "Database Error",
                    "Couldn't refresh from production SafiServer: " + e.getLocalizedMessage());
            return;
        }
        // nav.modelChanged(SafiServerPlugin.getDefault().isConnected());
    }

    // if (!viewerSelection.isEmpty() && viewerSelection.getFirstElement()
    // instanceof
    // IResource){
    // DeleteResourceAction action = new
    // DeleteResourceAction(Display.getCurrent().getActiveShell());
    // action.selectionChanged(viewerSelection);
    // action.run();
    // return;
    // }
    //    
    //
    //    
    // boolean deleteFromServer = false;
    // if (SafiServerPlugin.getDefault().hasProductionServerInfo())
    // deleteFromServer =
    // MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),
    // "Delete From Server?",
    // "Do you want to delete from the production SafiServer?");
    // boolean needsToSave = false;
    //    
    // Set<User> selectedUsers = nav.getSelectedUsers(false);
    // Date now = new Date();
    // for (User user : selectedUsers) {
    // user.getAlias().removeUser(user);
    // user.getAlias().getConnection().setLastModified(now);
    // }
    // if (!selectedUsers.isEmpty()) {
    // needsToSave = true;
    // }
    // Set<Alias> selectedAliases = nav.getSelectedAliases(false);
    // for (Alias alias : selectedAliases) {
    // alias.getConnection().setLastModified(now);
    // try {
    // alias.remove(deleteFromServer, true);
    // } catch (Exception e) {
    // MessageDialog.openError(AsteriskDiagramEditorUtil.getActiveShell(),
    // "Delete Failed", "Couldn't delete alias " + alias.getName() + ": "
    // + e.getLocalizedMessage());
    // AsteriskDiagramEditorPlugin.getInstance().logError("Couldn't delete alias",
    // e);
    // return;
    // }
    // }
    // if (!selectedAliases.isEmpty())
    // needsToSave = true;
    // Set<Query> selectedQueries = nav.getSelectedQueries(false);
    // for (Query query : selectedQueries) {
    // if (deleteFromServer) {
    // query.getConnection().setLastModified(now);
    // try {
    // SQLExplorerPlugin.getDefault().deleteDBResource(query);
    // } catch (Exception e) {
    // MessageDialog.openError(AsteriskDiagramEditorUtil.getActiveShell(),
    // "Delete Failed", "Couldn't delete query " + query.getName() + ": "
    // + e.getLocalizedMessage());
    // AsteriskDiagramEditorPlugin.getInstance().logError(
    // "Couldn't delete query " + query.getName(), e);
    // }
    // }
    // query.getConnection().getQueries().remove(query);
    // }
    // if (!selectedQueries.isEmpty())
    // needsToSave = true;
    //
    // Set<ManagedDriver> selectedDrivers = nav.getSelectedDrivers(false);
    // if (selectedDrivers != null && !selectedDrivers.isEmpty()) {
    // for (ManagedDriver driver : selectedDrivers) {
    // if (driver.getDriver() != null && !driver.getDriver().isDefault()) {
    // if (deleteFromServer) {
    // try {
    // SQLExplorerPlugin.getDefault().deleteDBResource(driver.getDriver());
    // } catch (Exception e) {
    // MessageDialog.openError(AsteriskDiagramEditorUtil.getActiveShell(),
    // "Delete Failed", "Couldn't delete query "
    // + driver.getDriver().getName() + ": " + e.getLocalizedMessage());
    // AsteriskDiagramEditorPlugin.getInstance().logError(
    // "Couldn't delete query " + driver.getDriver().getName(), e);
    // }
    // }
    // SQLExplorerPlugin.getDefault().getDriverModel().removeDriver(driver);
    // needsToSave = true;
    // }
    // }
    // }
    //
    // if (needsToSave) {
    // SQLExplorerPlugin.getDefault().saveDBResources(false);
    // nav.modelChanged();
    // return;
    // }
    // nav.refresh();
}

From source file:com.safi.workshop.sqlexplorer.connections.actions.DeleteAction.java

License:Open Source License

/**
 * Only show action when there is at least 1 alias selected
 * //from  w w w.j  av a2 s .com
 * @see com.safi.workshop.sqlexplorer.connections.actions.AbstractConnectionTreeAction#isAvailable()
 */
@Override
public boolean isAvailable() {
    SafiNavigator nav = SafiWorkshopEditorUtil.getSafiNavigator();
    IStructuredSelection viewerSelection = nav.getViewerSelection();
    if (viewerSelection == null || viewerSelection.isEmpty())
        return false;

    for (Object o : viewerSelection.toArray()) {
        if (!(o instanceof Alias || o instanceof ManagedDriver || o instanceof ServerResource
                || o instanceof DBResource || o instanceof IResource))
            return false;

    }
    // if (getView() == null)
    // return false;
    // Set<Alias> aliases = getView().getSelectedAliases(false);
    // Set<User> users = getView().getSelectedUsers(false);
    // Set<Query> queries = getView().getSelectedQueries(false);
    // if (aliases.isEmpty() && users.isEmpty() && queries.isEmpty())
    // return false;
    // for (User user : users)
    // if (user.getAlias().hasNoUserName())
    // return false;
    return true;
}

From source file:com.sap.dirigible.ide.editor.text.command.TextEditorHandler.java

License:Open Source License

private void execute(IWorkbenchPage page, IStructuredSelection selection) {
    Throwable error = null;/*from w ww.  ja v  a2 s .  c  om*/
    for (Object element : selection.toArray()) {
        if (element instanceof IFile) {
            try {
                execute(page, (IFile) element);
            } catch (PartInitException ex) {
                if (error == null) {
                    error = ex;
                }
            }

        }
    }
    if (error != null) {
        logger.error(COULD_NOT_OPEN_EDITOR_FOR_SOME_OR_ALL_OF_THE_SELECTED_ITEMS, error);
        MessageDialog.openError(null, Messages.TextEditorHandler_OPERATION_ERROR,
                COULD_NOT_OPEN_EDITOR_FOR_SOME_OR_ALL_OF_THE_SELECTED_ITEMS);
    }

}

From source file:com.sap.dirigible.ide.jgit.utils.CommandHandlerUtils.java

License:Open Source License

public static IProject[] getProjects(ISelection selection, Logger logger) {
    if (!(selection instanceof IStructuredSelection)) {
        logger.error(UNKNOWN_SELECTION_TYPE);
        return new IProject[0];
    }//from   ww  w . j a  v a  2s  .  c  o  m
    final List<IProject> result = new ArrayList<IProject>();
    final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
    for (Object element : structuredSelection.toArray()) {
        if (element instanceof IProject) {
            result.add((IProject) element);
        }
    }
    return result.toArray(new IProject[0]);
}

From source file:com.sap.dirigible.ide.publish.PublishManager.java

License:Open Source License

public static IProject[] getProjects(ISelection selection) {
    if ((selection == null) || !(selection instanceof IStructuredSelection)) {
        logger.error(UNKNOWN_SELECTION_TYPE);
        return new IProject[0];
    }//from  w  ww  .  ja  v a 2 s.c  om
    final Set<IProject> result = new HashSet<IProject>();
    final IStructuredSelection structuredSelection = (IStructuredSelection) selection;

    IProject project = null;
    for (Object element : structuredSelection.toArray()) {
        project = getProject(element);
        if (project != null) {
            result.add(project);
        }
    }
    return result.toArray(new IProject[0]);
}

From source file:com.sap.dirigible.ide.publish.PublishManager.java

License:Open Source License

public static IFile[] getFiles(ISelection selection) {
    if (!(selection instanceof IStructuredSelection)) {
        logger.error(UNKNOWN_SELECTION_TYPE);
        return new IFile[0];
    }//  ww  w. j a va2  s .c o m
    final Set<IFile> result = new HashSet<IFile>();
    final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
    for (Object element : structuredSelection.toArray()) {
        if (element instanceof IFile) {
            result.add((IFile) element);
        }
    }
    return result.toArray(new IFile[0]);
}