Example usage for org.eclipse.jface.viewers StructuredSelection size

List of usage examples for org.eclipse.jface.viewers StructuredSelection size

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers StructuredSelection size.

Prototype

@Override
    public int size() 

Source Link

Usage

From source file:org.eclipse.ui.internal.intro.universal.CustomizationContentsArea.java

License:Open Source License

private void fillPopupMenu(IMenuManager manager, final TableViewer viewer) {
    StructuredSelection ssel = (StructuredSelection) viewer.getSelection();

    manager.add(new Separator());
    Action addSeparator = new Action(Messages.WelcomeCustomizationPreferencePage_addSeparator) {
        public void run() {
            doAddSeparator(viewer);/*w  w  w .  j a  va2  s . c o m*/
        }
    };

    manager.add(addSeparator);
    manager.add(new Separator());

    if (ssel.size() == 1 && viewer != available) {
        Action upAction = new Action(Messages.WelcomeCustomizationPreferencePage_up) {

            public void run() {
                doMove(viewer, true);
            }
        };
        Action downAction = new Action(Messages.WelcomeCustomizationPreferencePage_down) {

            public void run() {
                doMove(viewer, false);
            }
        };
        BaseData ed = (BaseData) ssel.getFirstElement();
        GroupData gd = (GroupData) viewer.getInput();
        upAction.setEnabled(gd.canMoveUp(ed));
        downAction.setEnabled(gd.canMoveDown(ed));
        manager.add(upAction);
        manager.add(downAction);
    }
    if (ssel.size() > 0) {
        manager.add(new Separator());
        MenuManager menu = new MenuManager(Messages.WelcomeCustomizationPreferencePage_moveTo);
        addMoveToAction(menu, available, viewer, Messages.WelcomeCustomizationPreferencePage_menu_available);
        addMoveToAction(menu, topLeft, viewer, Messages.WelcomeCustomizationPreferencePage_menu_top_left);
        addMoveToAction(menu, topRight, viewer, Messages.WelcomeCustomizationPreferencePage_menu_top_right);
        addMoveToAction(menu, bottomLeft, viewer, Messages.WelcomeCustomizationPreferencePage_menu_bottom_left);
        addMoveToAction(menu, bottomRight, viewer,
                Messages.WelcomeCustomizationPreferencePage_menu_bottom_right);
        manager.add(menu);

        boolean addDeleteSeparator = false;

        for (Iterator iter = ssel.iterator(); iter.hasNext();) {
            Object obj = iter.next();
            if (obj instanceof SeparatorData)
                addDeleteSeparator = true;
            else {
                addDeleteSeparator = false;
                break;
            }
        }
        if (addDeleteSeparator) {
            Action deleteSeparator = new Action(Messages.WelcomeCustomizationPreferencePage_removeSeparator) {
                public void run() {
                    doRemoveSeparators(viewer);
                }
            };
            manager.add(deleteSeparator);
        }
    }
}

From source file:org.eclipse.ui.internal.views.markers.ExtendedMarkersView.java

License:Open Source License

/**
 * Set the selection of the receiver. reveal the item if reveal is true.
 * //from ww w  . ja v a 2  s .c o  m
 * @param structuredSelection
 * @param reveal
 */
void setSelection(StructuredSelection structuredSelection, boolean reveal) {
    List newSelection = new ArrayList(structuredSelection.size());
    for (Iterator i = structuredSelection.iterator(); i.hasNext();) {
        Object next = i.next();
        if (next instanceof IMarker) {
            MarkerItem marker = builder.getMarkers().getMarkerItem((IMarker) next);
            if (marker != null) {
                newSelection.add(marker);
            }
        }
    }
    IStructuredSelection structured = new StructuredSelection(newSelection);
    viewer.setSelection(structured, reveal);
    updateStatusLine(structured);
}

From source file:org.eclipse.ui.tests.navigator.NavigatorTest.java

License:Open Source License

/**
 * Tests that the Navigator is initially populated with
 * the correct elements from the workspace.
 *//*w w w.  j ava 2s  .  c o  m*/
public void testInitialPopulation() throws CoreException, PartInitException {
    createTestFile();
    showNav();

    // test its initial content by setting and getting selection on the file
    ISelectionProvider selProv = navigator.getSite().getSelectionProvider();
    StructuredSelection sel = new StructuredSelection(testFile);
    selProv.setSelection(sel);
    assertEquals(sel.size(), ((IStructuredSelection) selProv.getSelection()).size());
    assertEquals(sel.getFirstElement(), ((IStructuredSelection) selProv.getSelection()).getFirstElement());
}

From source file:org.eclipse.ui.tests.navigator.NavigatorTest.java

License:Open Source License

/**
 * Tests that the Navigator updates properly when a file is added to the workbench.
 *///  w ww .j  av  a2s .c o  m
public void testFileAddition() throws CoreException, PartInitException {
    createTestFolder(); // create the project and folder before the Navigator is shown
    showNav();
    createTestFile(); // create the file after the Navigator is shown

    // test its initial content by setting and getting selection on the file
    ISelectionProvider selProv = navigator.getSite().getSelectionProvider();
    StructuredSelection sel = new StructuredSelection(testFile);
    selProv.setSelection(sel);
    assertEquals(sel.size(), ((IStructuredSelection) selProv.getSelection()).size());
    assertEquals(sel.getFirstElement(), ((IStructuredSelection) selProv.getSelection()).getFirstElement());
}

From source file:org.eclipse.wazaabi.ui.runtime.parts.PropertySheetPage.java

License:Open Source License

public void selectionChanged(IWorkbenchPart part, ISelection selection) {
    Object input = null;//from  w  w w . j  a  v a 2 s.  c om

    // TODO : when the selection has IPropertySource, transform property
    // sources into relevant objects
    if (selection instanceof StructuredSelection) {
        StructuredSelection structuredSelection = (StructuredSelection) selection;
        if (structuredSelection.size() == 1)
            if (getSelectionProcessorCodeDescriptor() != null && getGetObjectMethodDescriptor() != null)
                input = getSelectionProcessorCodeDescriptor().invokeMethod(getGetObjectMethodDescriptor(),
                        new Object[] { structuredSelection.getFirstElement() });
            else
                input = structuredSelection.getFirstElement();
        else {
            List<Object> transformedObjects = new ArrayList<Object>();
            for (Object item : structuredSelection.toArray()) {
                if (getSelectionProcessorCodeDescriptor() != null && getGetObjectMethodDescriptor() != null)
                    input = getSelectionProcessorCodeDescriptor().invokeMethod(getGetObjectMethodDescriptor(),
                            new Object[] { item });
                else
                    input = item;
                transformedObjects.add(input);
            }
            input = transformedObjects;
        }
    }
    if (getSelectedComponent() != null)
        unsetInput(getSelectedComponent());
    updateSelectedComponent(input);
    if (getSelectedComponent() != null)
        setInput(getSelectedComponent(), input);
}

From source file:org.eclipse.wst.xsd.ui.internal.common.properties.sections.SpecificConstraintsWidget.java

License:Open Source License

public void widgetSelected(SelectionEvent e) {
    XSDSimpleTypeDefinition st = input;/*from  ww w.  j ava  2 s. c  o  m*/
    if (e.widget == addButton) {
        List enumList = st.getEnumerationFacets();
        String newName = XSDCommonUIUtils.createUniqueEnumerationValue("value", enumList); //$NON-NLS-1$

        if (kind == ENUMERATION) {
            CompoundCommand compoundCommand = new CompoundCommand();
            XSDSimpleTypeDefinition targetSimpleType = null;
            if (feature != null) {
                XSDSimpleTypeDefinition anonymousSimpleType = XSDCommonUIUtils.getAnonymousSimpleType(feature,
                        input);
                if (anonymousSimpleType == null) {
                    anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                    anonymousSimpleType.setBaseTypeDefinition(input);

                    ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand(
                            Messages._UI_ACTION_CHANGE_PATTERN, feature);
                    changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                    compoundCommand.add(changeToAnonymousCommand);
                    input = anonymousSimpleType;
                }
                targetSimpleType = anonymousSimpleType;
            } else {
                targetSimpleType = input;
            }

            AddEnumerationsCommand command = new AddEnumerationsCommand(Messages._UI_ACTION_ADD_ENUMERATION,
                    targetSimpleType);
            command.setValue(newName);
            compoundCommand.add(command);
            commandStack.execute(compoundCommand);
            setInput(input);
            constraintsTableViewer.refresh();
            int newItemIndex = constraintsTableViewer.getTable().getItemCount() - 1;
            constraintsTableViewer.editElement(constraintsTableViewer.getElementAt(newItemIndex), 0);
        }
    } else if (e.widget == addUsingDialogButton) {
        Display display = Display.getCurrent();
        // if it is null, get the default one
        display = display == null ? Display.getDefault() : display;
        Shell shell = display.getActiveShell();

        if (kind == PATTERN) {
            String initialValue = ""; //$NON-NLS-1$
            RegexWizard wizard = new RegexWizard(initialValue);

            WizardDialog wizardDialog = new WizardDialog(shell, wizard);
            wizardDialog.setBlockOnOpen(true);
            wizardDialog.create();

            int result = wizardDialog.open();

            if (result == Window.OK) {
                String newPattern = wizard.getPattern();
                CompoundCommand compoundCommand = new CompoundCommand();
                XSDSimpleTypeDefinition targetSimpleType = null;
                if (feature != null) {
                    XSDSimpleTypeDefinition anonymousSimpleType = XSDCommonUIUtils
                            .getAnonymousSimpleType(feature, input);
                    if (anonymousSimpleType == null) {
                        anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                        anonymousSimpleType.setBaseTypeDefinition(input);

                        ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand(
                                Messages._UI_ACTION_CHANGE_PATTERN, feature);
                        changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                        compoundCommand.add(changeToAnonymousCommand);
                        input = anonymousSimpleType;
                    }
                    targetSimpleType = anonymousSimpleType;
                } else {
                    targetSimpleType = input;
                }

                UpdateXSDPatternFacetCommand command = new UpdateXSDPatternFacetCommand(
                        Messages._UI_ACTION_ADD_PATTERN, targetSimpleType, UpdateXSDPatternFacetCommand.ADD);
                command.setValue(newPattern);
                setInput(input);
                compoundCommand.add(command);
                commandStack.execute(compoundCommand);
                facetSection.doSetInput();
            }
            constraintsTableViewer.refresh();
        } else {
            EnumerationsDialog dialog = new EnumerationsDialog(shell);
            dialog.setBlockOnOpen(true);
            int result = dialog.open();

            if (result == Window.OK) {
                String text = dialog.getText();
                String delimiter = dialog.getDelimiter();
                StringTokenizer tokenizer = new StringTokenizer(text, delimiter);
                CompoundCommand compoundCommand = new CompoundCommand(Messages._UI_ACTION_ADD_ENUMERATIONS);

                XSDSimpleTypeDefinition targetSimpleType = null;
                if (feature != null) {
                    XSDSimpleTypeDefinition anonymousSimpleType = XSDCommonUIUtils
                            .getAnonymousSimpleType(feature, input);
                    if (anonymousSimpleType == null) {
                        anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                        anonymousSimpleType.setBaseTypeDefinition(input);

                        ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand(
                                "", feature); //$NON-NLS-1$
                        changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                        compoundCommand.add(changeToAnonymousCommand);
                        input = anonymousSimpleType;
                    }
                    targetSimpleType = anonymousSimpleType;
                } else {
                    targetSimpleType = input;
                }

                while (tokenizer.hasMoreTokens()) {
                    String token = tokenizer.nextToken();
                    if (dialog.isPreserveWhitespace() == false) {
                        token = token.trim();
                    }
                    AddEnumerationsCommand command = new AddEnumerationsCommand(
                            Messages._UI_ACTION_ADD_ENUMERATIONS, targetSimpleType);
                    command.setValue(token);
                    compoundCommand.add(command);
                }
                commandStack.execute(compoundCommand);
            }
            //setInput(input);
            facetSection.doSetInput();
            constraintsTableViewer.refresh();
        }
    } else if (e.widget == deleteButton) {
        StructuredSelection selection = (StructuredSelection) constraintsTableViewer.getSelection();
        CompoundCommand compoundCommand = new CompoundCommand();
        if (selection != null) {
            Iterator i = selection.iterator();
            if (selection.size() > 0) {
                compoundCommand.setLabel(Messages._UI_ACTION_DELETE_CONSTRAINTS);
            } else {
                compoundCommand.setLabel(Messages._UI_ACTION_DELETE_PATTERN);
            }
            while (i.hasNext()) {
                Object obj = i.next();
                if (obj != null) {
                    if (obj instanceof XSDPatternFacet) {
                        UpdateXSDPatternFacetCommand command = new UpdateXSDPatternFacetCommand("", input, //$NON-NLS-1$
                                UpdateXSDPatternFacetCommand.DELETE);
                        command.setPatternToEdit((XSDPatternFacet) obj);
                        compoundCommand.add(command);
                    } else if (obj instanceof XSDEnumerationFacet) {
                        XSDEnumerationFacet enumFacet = (XSDEnumerationFacet) obj;
                        DeleteCommand deleteCommand = new DeleteCommand(Messages._UI_ACTION_DELETE_ENUMERATION,
                                enumFacet);
                        compoundCommand.add(deleteCommand);
                    }
                }
            }
            commandStack.execute(compoundCommand);
            constraintsTableViewer.refresh();
        }
    } else if (e.widget == editButton) {
        StructuredSelection selection = (StructuredSelection) constraintsTableViewer.getSelection();
        if (selection != null) {
            Object obj = selection.getFirstElement();
            if (obj instanceof XSDPatternFacet) {
                XSDPatternFacet pattern = (XSDPatternFacet) obj;
                String initialValue = pattern.getLexicalValue();
                if (initialValue == null) {
                    initialValue = ""; //$NON-NLS-1$
                }

                Shell shell = Display.getCurrent().getActiveShell();

                RegexWizard wizard = new RegexWizard(initialValue);

                WizardDialog wizardDialog = new WizardDialog(shell, wizard);
                wizardDialog.setBlockOnOpen(true);
                wizardDialog.create();

                int result = wizardDialog.open();

                if (result == Window.OK) {
                    String newPattern = wizard.getPattern();
                    pattern.setLexicalValue(newPattern);
                    constraintsTableViewer.refresh();
                }
            }
        }
    }

    setButtonStates(this.kind);
}

From source file:org.fornax.cartridges.sculptor.framework.richclient.table.ColumnChooserDlg.java

License:Apache License

private void doCreateViewerComposite(Composite composite) {
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    GridLayout compositeLayout = new GridLayout();
    compositeLayout.numColumns = 3;//from   w w  w.  ja v a 2 s .  c  o m
    composite.setLayout(compositeLayout);

    Label displayColumnsLabel = new Label(composite, SWT.NONE);
    displayColumnsLabel.setText(Messages.ColumnChooserDlg_displayColumns);
    new Label(composite, SWT.NONE);

    Label availableColumnsLabel = new Label(composite, SWT.NONE);
    availableColumnsLabel.setText(Messages.ColumnChooserDlg_availableColumns);

    displayListViewer = new ListViewer(composite, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL);
    displayListViewer.setContentProvider(new ArrayContentProvider());
    displayListViewer.setInput(tableViewer.getDefaultColumns().toArray());
    displayListViewer.setLabelProvider(new ColumnLabelProvider());
    displayList = displayListViewer.getList();
    displayList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Composite buttonComposite = new Composite(composite, SWT.NONE);
    buttonComposite.setLayout(new GridLayout());

    addButton = new Button(buttonComposite, SWT.NONE);
    addButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    addButton.setText(Messages.ColumnChooserDlg_add);
    addButton.setEnabled(false);

    removeButton = new Button(buttonComposite, SWT.NONE);
    GridData removeButtonGd = new GridData(SWT.LEFT, SWT.TOP, false, false);
    removeButton.setLayoutData(removeButtonGd);
    removeButton.setText(Messages.ColumnChooserDlg_remove);
    removeButton.setEnabled(false);

    upButton = new Button(buttonComposite, SWT.NONE);
    GridData upButtonGd = new GridData(SWT.FILL, SWT.CENTER, false, false);
    upButtonGd.verticalIndent = 20;
    upButton.setLayoutData(upButtonGd);
    upButton.setText(Messages.ColumnChooserDlg_up);
    upButton.setEnabled(false);

    downButton = new Button(buttonComposite, SWT.NONE);
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    downButton.setText(Messages.ColumnChooserDlg_down);
    downButton.setEnabled(false);

    availableListViewer = new ListViewer(composite, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL);
    availableListViewer.setContentProvider(new ArrayContentProvider());
    java.util.List<AbstractViewerColumn> availableColumns = new ArrayList<AbstractViewerColumn>();
    availableColumns.addAll(tableViewer.getAllColumns());
    Collections.sort(availableColumns, new Comparator<AbstractViewerColumn>() {
        public int compare(AbstractViewerColumn col1, AbstractViewerColumn col2) {
            return col1.getText().compareTo(col2.getText());
        }
    });
    availableListViewer.setInput(availableColumns.toArray());
    availableListViewer.setLabelProvider(new ColumnLabelProvider());
    availableList = availableListViewer.getList();
    availableList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    displayListViewer.addSelectionChangedListener(new UpdateButtons());
    availableListViewer.addSelectionChangedListener(new UpdateButtons());

    ViewerFilter filter = new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            Object input = displayListViewer.getInput();
            if (input instanceof Object[]) {
                return !Arrays.asList((Object[]) input).contains(element);
            }

            return true;
        }
    };
    availableListViewer.setFilters(new ViewerFilter[] { filter });

    addButton.addSelectionListener(new SelectionAdapter() {
        @SuppressWarnings("unchecked")
        @Override
        public void widgetSelected(SelectionEvent event) {
            StructuredSelection selection = (StructuredSelection) availableListViewer.getSelection();
            Object[] items = (Object[]) displayListViewer.getInput();
            java.util.List<Object> listItems = new ArrayList<Object>();
            listItems.addAll(Arrays.asList(items));
            listItems.addAll(selection.toList());
            displayListViewer.setInput(listItems.toArray());
            displayListViewer.refresh();
            availableListViewer.refresh();
            updateButtons();
        }
    });

    removeButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            StructuredSelection selection = (StructuredSelection) displayListViewer.getSelection();
            Object[] items = (Object[]) displayListViewer.getInput();
            java.util.List<Object> listItems = new ArrayList<Object>();
            listItems.addAll(Arrays.asList(items));
            listItems.removeAll(selection.toList());
            displayListViewer.setInput(listItems.toArray());
            displayListViewer.refresh();
            availableListViewer.refresh();
            updateButtons();
        }
    });

    upButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            StructuredSelection selection = (StructuredSelection) displayListViewer.getSelection();
            Object[] items = (Object[]) displayListViewer.getInput();
            java.util.List<Object> listItems = new ArrayList<Object>();
            listItems.addAll(Arrays.asList(items));
            for (Object each : selection.toList()) {
                int index = listItems.indexOf(each);
                listItems.remove(each);
                listItems.add(index - 1, each);
            }
            displayListViewer.setInput(listItems.toArray());
            displayListViewer.refresh();
            displayListViewer.setSelection(selection);
            displayListViewer.reveal(selection.getFirstElement());
            updateButtons();
        }
    });

    downButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            StructuredSelection selection = (StructuredSelection) displayListViewer.getSelection();
            Object[] items = (Object[]) displayListViewer.getInput();
            java.util.List<Object> listItems = new ArrayList<Object>();
            listItems.addAll(Arrays.asList(items));
            for (int i = selection.size() - 1; i > -1; i--) {
                Object each = selection.toArray()[i];
                int index = listItems.indexOf(each);
                listItems.remove(each);
                listItems.add(index + 1, each);
            }
            displayListViewer.setInput(listItems.toArray());
            displayListViewer.refresh();
            displayListViewer.setSelection(selection);
            displayListViewer.reveal(selection.getFirstElement());
            updateButtons();
        }
    });

    availableListViewer.refresh();
}

From source file:org.grails.ide.eclipse.editor.gsp.actions.GoToGSPPopDialog.java

License:Open Source License

protected void remmberItemAndClose(ISelection sel) {
    if (!sel.isEmpty() && sel instanceof StructuredSelection) {
        StructuredSelection ss = (StructuredSelection) sel;
        if (ss.size() == 1) {
            result = (IFile) ss.getFirstElement();
        }//from  ww w  .jav a  2  s  .c  o m
    }
    close();
}

From source file:org.jboss.tools.common.editor.form.SampleTreeForm.java

License:Open Source License

public boolean doGlobalAction(String actionId) {
    String actionPath = (String) actionMapping.get(actionId);
    if (actionPath == null)
        return false;
    Control c = tree.getViewer() == null ? null : tree.getViewer().getControl();
    if (c == null || !c.isFocusControl())
        return false;
    ISelection selection = tree.getSelectionProvider().getSelection();
    if (selection == null || selection.isEmpty() || !(selection instanceof StructuredSelection))
        return false;
    StructuredSelection ss = (StructuredSelection) selection;
    if (!(ss.getFirstElement() instanceof XModelObject))
        return true;
    XModelObject object = (XModelObject) ss.getFirstElement();
    XModelObject[] os = null;// w w w .  ja v a  2  s  . c  o m
    if (ss.size() > 1) {
        os = new XModelObject[ss.size()];
        Iterator it = ss.iterator();
        for (int i = 0; i < os.length; i++)
            os[i] = (XModelObject) it.next();
    }
    XAction action = XActionInvoker.getAction(actionPath, object);
    if (action == null)
        return true;
    if (os == null) {
        if (!action.isEnabled(object))
            return true;
        XActionInvoker.invoke(actionPath, object, new Properties());
    } else {
        if (!action.isEnabled(object, os))
            return true;
        XActionInvoker.invoke(actionPath, object, os, new Properties());
    }
    return true;
}

From source file:org.jboss.tools.common.propertieseditor.PropertiesEditor.java

License:Open Source License

private void doXActionCopy() {
    if (xtable == null || xtable.getTable() == null || xtable.getTable().isDisposed()
            || !xtable.getTable().isFocusControl()) {
        return;/*from w w  w .  ja  va  2 s .  c  o  m*/
    }
    ISelection selection = getSelectionProvider().getSelection();
    if (selection == null || selection.isEmpty() || !(selection instanceof StructuredSelection))
        return;
    StructuredSelection ss = (StructuredSelection) selection;
    if (!(ss.getFirstElement() instanceof XModelObject))
        return;
    XModelObject object = (XModelObject) ss.getFirstElement();
    XModelObject[] os = null;
    if (ss.size() > 1) {
        os = new XModelObject[ss.size()];
        Iterator it = ss.iterator();
        for (int i = 0; i < os.length; i++)
            os[i] = (XModelObject) it.next();
    }
    invokeXAction(XAction.COPY, object, os);
}