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.aptana.editor.php.internal.ui.preferences.PHPLibrariesPreferencePage.java

License:Open Source License

@Override
protected Control createContents(Composite parent) {
    Composite body = new Composite(parent, SWT.NONE);

    body.setLayout(new GridLayout(1, false));
    Label label = new Label(body, SWT.NONE | SWT.WRAP);
    label.setText(Messages.PHPLibrariesPreferencePage_librariesTitle);
    final Map<URL, Image> images = new HashMap<URL, Image>();
    Composite tableAndButton = new Composite(body, SWT.NONE);
    tableAndButton.setLayout(new GridLayout(2, false));
    newCheckList = CheckboxTableViewer.newCheckList(tableAndButton, SWT.BORDER);
    newCheckList.setContentProvider(new ArrayContentProvider());
    newCheckList.setInput(LibraryManager.getInstance().getAllLibraries());
    Composite buttons = new Composite(tableAndButton, SWT.NONE);
    buttons.setLayout(new GridLayout(1, false));
    newCheckList.setComparator(new ViewerComparator());
    newCheckList.setLabelProvider(new LibraryLabelProvider(images));
    GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.minimumHeight = 400;//from  www  .  j a  v  a 2 s  . c o m
    newCheckList.getControl().setLayoutData(layoutData);
    body.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            for (Image m : images.values()) {
                m.dispose();
            }
        }

    });
    layoutData = new GridData();
    layoutData.heightHint = 400;
    body.setLayoutData(layoutData);
    for (IPHPLibrary l : LibraryManager.getInstance().getAllLibraries()) {
        newCheckList.setChecked(l, l.isTurnedOn());
    }

    buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));
    Button add = new Button(buttons, SWT.PUSH);
    add.setText(Messages.PHPLibrariesPreferencePage_newUserLibrary);
    add.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    add.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {

        }

        public void widgetSelected(SelectionEvent e) {
            PHPLibraryDialog libraryDialog = new PHPLibraryDialog(Display.getCurrent().getActiveShell(), null,
                    getContent());
            if (libraryDialog.open() == Dialog.OK) {
                UserLibrary result = libraryDialog.getResult();
                newCheckList.add(result);
                newCheckList.setChecked(result, true);
            }
        }

    });
    final Button edit = new Button(buttons, SWT.PUSH);
    edit.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
            // empty
        }

        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection ss = (IStructuredSelection) newCheckList.getSelection();
            UserLibrary firstElement = (UserLibrary) ss.getFirstElement();
            PHPLibraryDialog libraryDialog = new PHPLibraryDialog(Display.getCurrent().getActiveShell(),
                    firstElement, getContent());
            if (libraryDialog.open() == Dialog.OK) {
                newCheckList.remove(firstElement);
                newCheckList.add(libraryDialog.getResult());
            }
        }

    });
    edit.setText(Messages.PHPLibrariesPreferencePage_editLibrary);
    edit.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    final Button remove = new Button(buttons, SWT.PUSH);
    remove.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {

        }

        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection ss = (IStructuredSelection) newCheckList.getSelection();
            for (Object o : ss.toArray()) {
                newCheckList.remove(o);
            }
        }

    });
    remove.setText(Messages.PHPLibrariesPreferencePage_removeLibrary);
    tableAndButton.setLayoutData(new GridData(GridData.FILL_BOTH));
    remove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    newCheckList.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection ss = (IStructuredSelection) event.getSelection();
            if (ss.isEmpty() || ss.getFirstElement() instanceof PHPLibrary) {
                edit.setEnabled(false);
                remove.setEnabled(false);
                return;
            }
            edit.setEnabled(true);
            remove.setEnabled(true);
        }

    });
    Button selectAll = new Button(buttons, SWT.PUSH);
    selectAll.setText(Messages.LibrariesPage_selectAll);
    selectAll.addSelectionListener(new SelectAction(true));
    Button deselectAll = new Button(buttons, SWT.PUSH);
    deselectAll.setText(Messages.LibrariesPage_deselectAll);
    selectAll.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    deselectAll.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    deselectAll.addSelectionListener(new SelectAction(false));
    edit.setEnabled(false);
    remove.setEnabled(false);
    return body;
}

From source file:com.aptana.editor.php.internal.ui.preferences.PHPLibraryDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    Composite body = new Composite(parent, SWT.NONE);
    body.setLayout(new GridLayout(2, false));
    body.setLayoutData(new GridData(GridData.FILL_BOTH));
    Label label = new Label(body, SWT.NONE);
    label.setText(Messages.PHPLibraryDialog_libraryName);
    nameText = new Text(body, SWT.BORDER);
    nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    if (library != null) {
        nameText.setText(library.getName());
    }/*  w  w  w  .  j a  v  a  2 s .  c om*/
    nameText.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            validate();
        }

    });
    Group pComp = new Group(body, SWT.NONE);
    pComp.setText(Messages.PHPLibraryDialog_libraryContent);
    pComp.setLayout(new GridLayout(2, false));
    GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.horizontalSpan = 2;
    layoutData.minimumHeight = 200;
    pComp.setLayoutData(layoutData);
    viewer = new TableViewer(pComp, SWT.BORDER);
    viewer.setComparator(new ViewerComparator());
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(new LabelProvider() {

        public String getText(Object element) {
            return element.toString();

        }

        public Image getImage(Object element) {
            return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
        }
    });
    Composite buttons = new Composite(pComp, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.marginHeight = 0;
    if (library != null) {
        viewer.setInput(library.getDirectories().toArray());
    } else {
        viewer.setInput(new String[0]); // $codepro.audit.disable reusableImmutables
    }
    buttons.setLayout(layout);
    Button add = new Button(buttons, SWT.NONE);
    add.setText(Messages.PHPLibraryDialog_addFolder);
    add.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {

        }

        public void widgetSelected(SelectionEvent e) {
            DirectoryDialog directoryDialog = new DirectoryDialog(Display.getCurrent().getActiveShell());
            directoryDialog.setText(Messages.PHPLibraryDialog_selectFolder);
            String open = directoryDialog.open();
            viewer.add(open);
        }

    });
    add.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button remove = new Button(buttons, SWT.NONE);
    remove.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {

        }

        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            for (Object o : selection.toArray()) {
                viewer.remove(o);
            }
        }

    });
    remove.setText(Messages.PHPLibraryDialog_removeSelected);
    remove.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));
    viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
    Control createDialogArea = super.createDialogArea(parent);
    setMessage(Messages.PHPLibraryDialog_libraryConfigureMessage);
    setTitleImage(titleImage);
    setTitle(Messages.PHPLibraryDialog_libraryConfigureTitle);
    buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    return createDialogArea;
}

From source file:com.aptana.explorer.ExplorerContextContributor.java

License:Open Source License

private void addSelectedFiles(CommandContext context) {
    final IStructuredSelection[] structuredSelection = new IStructuredSelection[1];

    // First try to get the current selection from evaluation context, so we get selection from active view...
    PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

        public void run() {
            // Grab the active project given the context. Check active view or editor, then grab project
            // from it, falling back to App Explorer's active project.
            IEvaluationService evaluationService = (IEvaluationService) PlatformUI.getWorkbench()
                    .getService(IEvaluationService.class);
            if (evaluationService != null) {
                IEvaluationContext currentState = evaluationService.getCurrentState();
                Object variable = currentState.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
                if (variable instanceof IStructuredSelection) {
                    structuredSelection[0] = (IStructuredSelection) variable;
                } else {
                    // checks the active editor
                    variable = currentState.getVariable(ISources.ACTIVE_EDITOR_NAME);
                    if (variable instanceof IEditorPart) {
                        IEditorInput editorInput = ((IEditorPart) variable).getEditorInput();
                        if (editorInput instanceof IFileEditorInput) {
                            structuredSelection[0] = new StructuredSelection(
                                    ((IFileEditorInput) editorInput).getFile());
                        }//w  w w .j  av  a  2 s.  com
                    }
                }
            }
        }
    });

    // We failed to get selection from active view, may have been an editor active, fall back to selection in App
    // Explorer.
    if (structuredSelection[0] == null) {
        CommonNavigator nav = getAppExplorer();
        if (nav == null) {
            return;
        }
        ISelection sel = nav.getCommonViewer().getSelection();
        if (sel instanceof IStructuredSelection) {
            structuredSelection[0] = (IStructuredSelection) sel;
        }
    }

    if (structuredSelection[0] == null) {
        return;
    }

    StringBuilder builder = new StringBuilder();
    IStructuredSelection struct = structuredSelection[0];
    for (Object selected : struct.toArray()) {
        // TODO Should we handle IAdaptables that can be adapted to IResources?
        if (selected instanceof IResource) {
            IPath location = ((IResource) selected).getLocation();
            if (location != null) {
                builder.append("'").append(location.toOSString()).append("' "); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
    }

    if (builder.length() > 0) {
        builder.deleteCharAt(builder.length() - 1);
        context.put(TM_SELECTED_FILES, builder.toString());
    }
}

From source file:com.aptana.git.ui.internal.actions.ShowInHistoryHandler.java

License:Open Source License

@SuppressWarnings("unchecked")
private IResource getResource(IEvaluationContext evContext) {
    Object input = evContext.getVariable(ISources.SHOW_IN_INPUT);
    if (input instanceof IFileEditorInput) {
        IFileEditorInput fei = (IFileEditorInput) input;
        return fei.getFile();
    }//from   w  ww .ja  va 2  s .com

    input = evContext.getDefaultVariable();
    if (input instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) input;
        Object[] selectedFiles = selection.toArray();
        for (Object selected : selectedFiles) {
            if (selected instanceof IResource) {
                return (IResource) selected;
            } else if (selected instanceof IAdaptable) {
                return (IResource) ((IAdaptable) selected).getAdapter(IResource.class);
            }
        }
    } else if (input instanceof Collection) {
        Collection<Object> selectedFiles = (Collection<Object>) input;
        for (Object selected : selectedFiles) {
            if (selected instanceof IResource) {
                return (IResource) selected;
            } else if (selected instanceof IAdaptable) {
                return (IResource) ((IAdaptable) selected).getAdapter(IResource.class);
            }

        }
    }
    return null;
}

From source file:com.aptana.ide.core.ui.actions.ActionDelegate.java

License:Open Source License

/**
 * Returns a valid array of selections/*from w  w  w. ja  v a2  s  . com*/
 * 
 * @param selection
 * @return Object[]
 */
protected Object[] getValidSelection(ISelection selection) {
    if (selection instanceof IStructuredSelection == false) {
        return null;
    }

    IStructuredSelection structuredSelection = ((IStructuredSelection) selection);

    if (structuredSelection.size() == 0) {
        return new Object[0];
    } else {
        return structuredSelection.toArray();
    }
}

From source file:com.aptana.ide.debug.internal.ui.preferences.JSDetailFormattersPreferencePage.java

License:Open Source License

private void removeTypes() {
    Object[] list = formatters.toArray();
    IStructuredSelection selection = (IStructuredSelection) listViewer.getSelection();
    Object first = selection.getFirstElement();
    int index = -1;
    for (int i = 0; i < list.length; i++) {
        if (list[i].equals(first)) {
            index = i;//from  w  w  w.  j  a  v a  2  s  .c  o  m
            break;
        }
    }

    removeDetailFormatters(selection.toArray());

    list = formatters.toArray();
    if (index > list.length - 1) {
        index = list.length - 1;
    }
    if (index >= 0) {
        listViewer.setSelection(new StructuredSelection(list[index]));
    }
}

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  w  w w.ja  v  a  2 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.installer.actions.InstallFeatureAction.java

License:Open Source License

/**
 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
 *      org.eclipse.jface.viewers.ISelection)
 *//*  ww  w .  ja  va2  s.c  o m*/
public void selectionChanged(IAction action, ISelection selection) {
    this.selectedFeatures = null;
    if (selection != null && !selection.isEmpty()) {
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection sel = (IStructuredSelection) selection;
            Object[] elements = sel.toArray();
            List<Plugin> features = new ArrayList<Plugin>();
            for (Object element : elements) {
                if (element instanceof Plugin) {
                    Plugin ref = (Plugin) element;
                    URL url = ref.getURL();
                    String protocol = url.getProtocol();
                    if (!protocol.equals("file") && //$NON-NLS-1$
                            !FeatureUtil.isInstalled(ref.getId())) {
                        features.add(ref);
                    }

                }
            }
            selectedFeatures = features.toArray(new Plugin[features.size()]);
        }
    }
}

From source file:com.aptana.ide.search.AptanaFileSearchPage.java

License:Open Source License

private void doAdd(IMenuManager mgr) {
    IStructuredSelection selection = (IStructuredSelection) getViewer().getSelection();
    if (!selection.isEmpty()) {
        ReplaceAction replaceSelection = new ReplaceAction(getSite().getShell(), (FileSearchResult) getInput(),
                selection.toArray(), true);
        replaceSelection.setText(SearchMessages.ReplaceAction_label_selected);
        mgr.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceSelection);
    }/*from   w  ww  .  ja v  a2 s. c  o  m*/
    ReplaceAction replaceAll = new ReplaceAction(getSite().getShell(), (FileSearchResult) getInput(), null,
            true);
    replaceAll.setText(SearchMessages.ReplaceAction_label_all);
    mgr.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAll);
}

From source file:com.aptana.ide.search.ui.filesystem.AptanaFileSystemSearchPage.java

License:Open Source License

/**
 * @see org.eclipse.search.internal.ui.text.FileSearchPage#fillContextMenu(org.eclipse.jface.action.IMenuManager)
 *///ww w .  j  a  v a  2  s . co  m
protected void fillContextMenu(IMenuManager mgr) {
    super.fillContextMenu(mgr);

    // should be here if our layout is active
    IStructuredSelection selection = (IStructuredSelection) getViewer().getSelection();
    if (!selection.isEmpty()) {
        FileSystemReplaceAction replaceSelection = new FileSystemReplaceAction(getSite().getShell(),
                (FileSystemSearchResult) getInput(), selection.toArray(), true);
        replaceSelection.setText(SearchMessages.ReplaceAction_label_selected);
        mgr.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceSelection);
    }
    FileSystemReplaceAction replaceAll = new FileSystemReplaceAction(getSite().getShell(),
            (FileSystemSearchResult) getInput(), null, true);
    replaceAll.setText(SearchMessages.ReplaceAction_label_all);
    mgr.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAll);

    if (this.layout == AptanaFileSystemSearchPage.LAYOUT_MATCHES) {
        Separator find = (Separator) mgr.find(IContextMenuConstants.GROUP_VIEWER_SETUP);
        IContributionItem[] items = find.getParent().getItems();
        int indexOf = Arrays.asList(items).indexOf(find);
        MenuManager contributionItem = (MenuManager) items[indexOf + 1];
        IContributionItem[] items2 = contributionItem.getItems();
        ActionContributionItem it0 = (ActionContributionItem) items2[0];
        ActionContributionItem it1 = (ActionContributionItem) items2[1];
        it0.getAction().setChecked(fOrder == AptanaFileSystemSearchPage.SHOW_LABEL_PATH);
        it1.getAction().setChecked(fOrder == AptanaFileSystemSearchPage.SHOW_PATH_LABEL);
    }
}