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:com.clustercontrol.repository.view.AgentListView.java

License:Open Source License

/**
 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
 *//*  ww  w . ja v a  2  s .  co m*/
@Override
public void createPartControl(Composite parent) {
    super.createPartControl(parent);
    GridLayout layout = new GridLayout(1, true);
    parent.setLayout(layout);
    layout.marginHeight = 0;
    layout.marginWidth = 0;

    composite = new AgentListComposite(parent, SWT.NONE);
    WidgetTestUtil.setTestId(this, null, composite);
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.verticalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    composite.setLayoutData(gridData);

    //??
    createContextMenu();

    // ?
    composite.getTableViewer().addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            //?[]??
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            IViewPart viewPart = page.findView(AgentListView.ID);
            //??
            StructuredSelection selection = (StructuredSelection) event.getSelection();

            if (viewPart != null && selection != null) {
                AgentListView view = (AgentListView) viewPart.getAdapter(AgentListView.class);
                if (view == null) {
                    m_log.info("selection changed: view is null");
                    return;
                }
                //???/???
                view.setEnabledAction(selection.size(), event.getSelection());
            }
        }
    });
    update();
}

From source file:com.crispico.flower.mp.doc.DocumentationController.java

License:Open Source License

/**
 * This method must be called when the documentation controller must take it's information from a new element.
 * //ww w  .ja  va2s  .c o  m
 * <p/> This controller will provide documentation content only if finds a single element in the selection,
 * that single element provides for a documentable element and labeled element which provide {@link IDocumentationProvider}
 * respectively an label provider. 
 * 
 * <p/> The behavior is to first remove the listeners on the old selection by calling {@link #clearSelection()}.
 * After it obtains the documentable element from {@link DocumentationUtils#getDocumentableElement(Object)}
 * and after it obtains the labeled element which is capable or obtaining the label provider. Listeners are added
 * if the selection is valid, and after {@link #processDocumentationContent()} is called for processing
 * and sending to the client the documentation content and {@link #processSelectionTitle()} is called for
 * processing and sending the label and the image to the client. In final {@link #processHasDocumentation()} 
 * method will be called for telling the client that the pages must be switched.
 * @flowerModelElementId _kWZpgom4Ed-IjqfeDOxYSw
 */
public void setSelection(ISelection selection) {

    clearSelection();

    // Take the first element of a structured selection and make sure that only 1 object was selected.
    Object selectedObject = null;
    if (selection instanceof StructuredSelection) {
        StructuredSelection structuredSelection = (StructuredSelection) selection;
        if (structuredSelection.size() == 1) {
            selectedObject = structuredSelection.getFirstElement();
        }
    }

    // For the selected object we see if we can obtain a documentable element it means it provides for documentation 
    Element documentableElement = DocumentationUtils.getDocumentableElement(selectedObject);
    if (documentableElement != null) {
        this.selectedElement = (Element) selectedObject;
        this.selectedDocumentableElement = documentableElement;
        // for the selected object we find the element which can give us a ItemProviderAdapter.
        this.selectedLabeledElement = (Element) findSelectedLabeledObject(selectedObject);
        // obtain an itemProviderAdapter to be used for text and image 
        this.selectedLabelProvider = (ItemProviderAdapter) FlowerEditingDomain
                .getEditingDomainItemProviderFor(selectedLabeledElement);

        // an label item provider could not be found and we are sure that the selection id documentable so throw exception
        if (selectedLabelProvider == null)
            throw new IllegalStateException("Could not obtain a label item provider for selected element");
    }

    if (selectedDocumentableElement != null) {
        //processDocumentationDetails(false, false);

        selectedDocumentableElement.eAdapters().add(this);
        selectedLabelProvider.addListener(this);
    }
    // we need to call the processing of the switching of the pages after then information has been set to the client
    // because otherwise, when changing from no doc to with doc, we would first the see the old documentation flickering
    // to the new documentation.
    processDocumentationDetails(true, true);
}

From source file:com.foglyn.helpers.FBStatusesComboViewer.java

License:Open Source License

public Set<StatusID> getSelectedStatuses() {
    StructuredSelection ssel = (StructuredSelection) getSelection();
    if (ssel.isEmpty()) {
        return null;
    }/*from   w  w w. jav a 2s.  c  o  m*/

    Assert.isTrue(ssel.size() == 1);

    Object obj = ssel.getFirstElement();
    if (HelperConstants.NULL_VALUE.equals(obj)) {
        return null;
    }

    FBStatusItem fbsi = (FBStatusItem) obj;
    return fbsi.getStatuses();
}

From source file:com.mentor.nucleus.bp.internal.tools.process.ChangeSetCreator.java

License:Open Source License

public void run() {
    ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
            .getSelection();// ww  w  .j  a  v a  2 s .  com
    if (selection instanceof StructuredSelection) {
        StructuredSelection structuredSelection = (StructuredSelection) selection;
        if (structuredSelection.size() == 1) {
            if (structuredSelection.getFirstElement() instanceof IFile) {
                IFile implementationNote = (IFile) structuredSelection.getFirstElement();
                if (implementationNote.getFileExtension().equals("int")) {
                    InputStream inStream;
                    try {
                        inStream = implementationNote.getContents();
                        long byteCount = implementationNote.getRawLocation().toFile().length();
                        byte[] byteArray = new byte[(int) byteCount];
                        inStream.read(byteArray);
                        IDocument document = new Document(new String(byteArray));
                        FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
                        IRegion startRegion = frda.find(0, "8. Code Changes", true, true, true, false);
                        int startOffset = startRegion.getOffset() + 17;
                        IRegion endRegion = frda.find(startOffset, "End", true, true, true, false);
                        int endOffset = endRegion.getOffset();
                        String fileList = document.get(startOffset, endOffset - startOffset);
                        fileList = fileList.replaceAll("---------------\r\n", "");
                        fileList = fileList.replaceAll("Branch name:.*\r\n", "");
                        fileList = fileList.replaceAll("\r\n    ", "");
                        fileList = fileList.replaceAll("\r\n", ",");
                        fileList = fileList.replaceAll(">", "");
                        String[] filesString = fileList.split(",");
                        if (filesString.length == 0)
                            return;
                        ArrayList<IFile> list = new ArrayList<IFile>();
                        for (int i = 0; i < filesString.length; i++) {
                            if (!filesString[i].equals("")) {
                                IResource resource = ResourcesPlugin.getWorkspace().getRoot()
                                        .findMember(filesString[i]);
                                if (resource instanceof IFile) {
                                    list.add((IFile) resource);
                                }
                            }
                        }
                        if (list.size() == 0)
                            return;
                        IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
                        String workingSetName = "chgset-" + implementationNote.getName().replaceAll(".int", "");
                        IWorkingSet set = workingSetManager.createWorkingSet(workingSetName,
                                list.toArray(new IFile[list.size()]));
                        IWorkingSet existingSet = workingSetManager.getWorkingSet(workingSetName);
                        if (existingSet != null) {
                            workingSetManager.removeWorkingSet(existingSet);
                        }
                        workingSetManager.addWorkingSet(set);
                        inStream.close();
                    } catch (CoreException e) {
                    } catch (IOException e) {
                    } catch (BadLocationException e) {
                    }
                }
            }
        }
    }
}

From source file:com.redhat.ceylon.eclipse.code.open.FilteredItemsSelectionDialog.java

License:Open Source License

@Override
protected Control createDialogArea(final Composite parent) {
    final Composite dialogArea = (Composite) super.createDialogArea(parent);

    final Composite content = new Composite(dialogArea, SWT.NONE);
    content.setLayoutData(new GridData(GridData.FILL_BOTH));

    GridLayout layout = new GridLayout();
    layout.numColumns = 1;//  w w w  . j  a  va 2s.  com
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    content.setLayout(layout);

    createPatternInput(content);

    final Label listLabel = createLabels(content);

    sash = new SashForm(content, SWT.HORIZONTAL | SWT.SMOOTH);
    sash.setLayoutData(new GridData(GridData.FILL_BOTH));
    Composite composite = new Composite(sash, SWT.NONE);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    composite.setLayout(GridLayoutFactory.fillDefaults().create());
    list = new TableViewer(composite,
            (multi ? SWT.MULTI : SWT.SINGLE) | SWT.BORDER | SWT.V_SCROLL | SWT.VIRTUAL);
    list.getTable().getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            if (e.childID == ACC.CHILDID_SELF) {
                e.result = LegacyActionTools.removeMnemonics(listLabel.getText());
            }
        }
    });
    list.setContentProvider(contentProvider);
    list.setLabelProvider(itemsListLabelProvider);
    list.setInput(new Object[0]);
    list.setItemCount(contentProvider.getNumberOfElements());
    applyDialogFont(list.getTable());
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = list.getTable().getItemHeight() * 15;
    list.getTable().setLayoutData(gd);

    if (enableDocArea()) {
        browser = new DocBrowser(sash, SWT.BORDER);
        browser.addLocationListener(new LocationListener() {
            @Override
            public void changing(LocationEvent event) {
                String location = event.location;
                //necessary for windows environment (fix for blank page)
                //somehow related to this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=129236
                if (!"about:blank".equals(location) && !location.startsWith("http:")) {
                    event.doit = false;
                    handleLink(event.location, browser);
                }
            }

            @Override
            public void changed(LocationEvent event) {
            }
        });
        refreshBrowserContent(browser, null);
        setDocAreaVisible(toggleDocAction == null || toggleDocAction.isChecked());
    }

    createPopupMenu();

    pattern.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            applyFilter();
        }
    });

    pattern.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == SWT.ARROW_DOWN) {
                if (list.getTable().getItemCount() > 0) {
                    list.getTable().setFocus();
                }
            }
        }
    });

    list.addSelectionChangedListener(new ISelectionChangedListener() {
        boolean first = true;

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            StructuredSelection selection = (StructuredSelection) event.getSelection();
            handleSelected(selection);
            //we need to re-layout the status area *only* the 
            //very first time an icon is displayed!
            if (first && selection != null && !selection.isEmpty()) {
                statusArea.getParent().layout();
                first = false;
            }
        }
    });

    list.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            handleDoubleClick();
        }
    });

    list.getTable().addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {

            if (e.keyCode == SWT.DEL) {

                List selectedElements = ((StructuredSelection) list.getSelection()).toList();

                Object item = null;
                boolean isSelectedHistory = true;

                for (Iterator it = selectedElements.iterator(); it.hasNext();) {
                    item = it.next();
                    if (item instanceof ItemsListSeparator || !isHistoryElement(item)) {
                        isSelectedHistory = false;
                        break;
                    }
                }
                if (isSelectedHistory)
                    removeSelectedItems(selectedElements);

            }

            if (e.keyCode == SWT.ARROW_UP && (e.stateMask & SWT.SHIFT) != 0 && (e.stateMask & SWT.CTRL) != 0) {
                StructuredSelection selection = (StructuredSelection) list.getSelection();

                if (selection.size() == 1) {
                    Object element = selection.getFirstElement();
                    if (element.equals(list.getElementAt(0))) {
                        pattern.setFocus();
                    }
                    if (list.getElementAt(
                            list.getTable().getSelectionIndex() - 1) instanceof ItemsListSeparator)
                        list.getTable().setSelection(list.getTable().getSelectionIndex() - 1);
                    list.getTable().notifyListeners(SWT.Selection, new Event());

                }
            }

            if (e.keyCode == SWT.ARROW_DOWN && (e.stateMask & SWT.SHIFT) != 0
                    && (e.stateMask & SWT.CTRL) != 0) {

                if (list.getElementAt(list.getTable().getSelectionIndex() + 1) instanceof ItemsListSeparator)
                    list.getTable().setSelection(list.getTable().getSelectionIndex() + 1);
                list.getTable().notifyListeners(SWT.Selection, new Event());
            }

        }
    });

    createStatusArea(content);

    applyDialogFont(content);

    restoreDialog(getDialogSettings());

    if (initialPatternText != null) {
        pattern.setText(initialPatternText);
    }

    switch (selectionMode) {
    case CARET_BEGINNING:
        pattern.setSelection(0, 0);
        break;
    case FULL_SELECTION:
        pattern.setSelection(0, initialPatternText.length());
        break;
    }

    // apply filter even if pattern is empty (display history)
    applyFilter();

    return dialogArea;
}

From source file:com.redhat.ceylon.eclipse.code.open.FilteredItemsSelectionDialog.java

License:Open Source License

/**
 * Refreshes the details field according to the current selection in the
 * items list.//from www  .j  av  a2  s.  c  o  m
 */
private void refreshDetails() {
    StructuredSelection selection = getSelectedItems();
    switch (selection.size()) {
    case 0:
        if (details != null)
            details.setInput(null);
        if (moreDetails != null)
            moreDetails.setInput(null);
        if (evenMoreDetails != null)
            evenMoreDetails.setInput(null);
        break;
    case 1:
        Object firstElement = selection.getFirstElement();
        if (details != null)
            details.setInput(firstElement);
        if (moreDetails != null)
            moreDetails.setInput(firstElement);
        if (evenMoreDetails != null)
            evenMoreDetails.setInput(firstElement);
        break;
    default:
        if (details != null)
            details.setInput(selection.size() + " items selected");
        if (moreDetails != null)
            moreDetails.setInput(null);
        if (evenMoreDetails != null)
            evenMoreDetails.setInput(null);
        break;
    }
    ((Composite) statusArea.getContent()).layout();
}

From source file:com.redhat.ceylon.eclipse.code.open.FilteredItemsSelectionDialog.java

License:Open Source License

/**
 * Handle selection in the items list by updating labels of selected and
 * unselected items and refresh the details field using the selection.
 * //ww  w  .j a  v  a2  s. co m
 * @param selection
 *            the new selection
 */
protected void handleSelected(StructuredSelection selection) {
    IStatus status = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, IStatus.OK, EMPTY_STRING, null);

    Object[] lastSelection = currentSelection;

    currentSelection = selection.toArray();

    if (selection.size() == 0) {
        status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.ERROR, EMPTY_STRING, null);

        if (lastSelection != null
        /*&& getListSelectionLabelDecorator() != null*/) {
            list.update(lastSelection, null);
        }

        currentSelection = null;

    } else {
        status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.ERROR, EMPTY_STRING, null);

        List items = selection.toList();

        Object item = null;
        IStatus tempStatus = null;

        for (Iterator it = items.iterator(); it.hasNext();) {
            Object o = it.next();

            if (o instanceof ItemsListSeparator) {
                continue;
            }

            item = o;
            tempStatus = validateItem(item);

            if (tempStatus.isOK()) {
                status = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, IStatus.OK, EMPTY_STRING, null);
            } else {
                status = tempStatus;
                // if any selected element is not valid status is set to
                // ERROR
                break;
            }
        }

        if (lastSelection != null
        /*&& getListSelectionLabelDecorator() != null*/) {
            list.update(lastSelection, null);
        }

        //            if (getListSelectionLabelDecorator() != null) {
        list.update(currentSelection, null);
        //            }
    }

    if (enableDocArea()) {
        refreshBrowserContent(browser, currentSelection);
    }

    refreshDetails();
    updateStatus(status);
}

From source file:com.siteview.mde.internal.ui.views.target.StateViewPage.java

License:Open Source License

protected void handleDoubleClick() {
    StructuredSelection selection = (StructuredSelection) fTreeViewer.getSelection();
    if (selection.size() == 1) {
        BundleDescription desc = getBundleDescription(selection.getFirstElement());
        if (desc != null)
            ManifestEditor.openPluginEditor(desc);
    }//from   w w w .  j a va2s  .  c om
}

From source file:com.svcdelivery.liquibase.eclipse.internal.ui.RollbackCommandHandler.java

License:Apache License

@Override
public final Object execute(final ExecutionEvent event) throws ExecutionException {
    selection = HandlerUtil.getCurrentSelection(event);
    shell = HandlerUtil.getActiveShell(event);
    IFile file = null;//from   ww  w. ja v  a2  s . c om
    if (selection instanceof StructuredSelection) {
        final StructuredSelection structured = (StructuredSelection) selection;
        if (structured.size() == 1) {
            file = (IFile) structured.getFirstElement();
        }
    }
    IWizard targetWizard = new RollbackScriptsWizard(file);
    showWizard(targetWizard);
    return null;
}

From source file:com.svcdelivery.liquibase.eclipse.internal.ui.RollbackCommandHandler.java

License:Apache License

@Override
public final void run() {
    ChangeSetTreeItem item = null;//from w  ww  .  j  a va2s.c  o m
    if (selection instanceof StructuredSelection) {
        final StructuredSelection structured = (StructuredSelection) selection;
        if (structured.size() == 1)
            item = (ChangeSetTreeItem) structured.getFirstElement();
    }
    IWizard targetWizard = new RollbackChangeSetWizard(item);
    showWizard(targetWizard);
}