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

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

Introduction

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

Prototype

public int size();

Source Link

Document

Returns the number of elements selected in this selection.

Usage

From source file:com.google.dart.tools.internal.search.ui.SearchMatchPage.java

License:Open Source License

/**
 * Opens selected {@link ElementItem} in the editor.
 *//*from w w  w .j av a2s.co m*/
private void openSelectedElement(ISelection selection) {
    // need IStructuredSelection
    if (!(selection instanceof IStructuredSelection)) {
        return;
    }
    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
    // only single element
    if (structuredSelection.size() != 1) {
        return;
    }
    Object firstElement = structuredSelection.getFirstElement();
    // line item
    if (firstElement instanceof LineItem) {
        LineItem lineItem = (LineItem) firstElement;
        Position position = lineItem.positions.get(0).position;
        openPositionInElement(lineItem.item.element, position);
    }
    // element item
    if (firstElement instanceof ElementItem) {
        ElementItem item = (ElementItem) firstElement;
        // use ResultCursor to find first occurrence in the requested subtree
        itemCursor = new ItemCursor(item);
        boolean found = itemCursor.next();
        if (!found) {
            return;
        }
        Element element = itemCursor.item.element;
        Position position = itemCursor.getPosition();
        // open position
        openPositionInElement(element, position);
    }
}

From source file:com.google.dart.tools.internal.search.ui.SearchResultPage_NEW.java

License:Open Source License

/**
 * Opens selected {@link ElementItem} in the editor.
 *//* w w w.  j a  v  a2 s . c  o m*/
private void openSelectedElement(ISelection selection) {
    // need IStructuredSelection
    if (!(selection instanceof IStructuredSelection)) {
        return;
    }
    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
    // only single element
    if (structuredSelection.size() != 1) {
        return;
    }
    Object firstElement = structuredSelection.getFirstElement();
    // line item
    if (firstElement instanceof LineItem) {
        LineItem lineItem = (LineItem) firstElement;
        Position position = lineItem.positions.get(0).position;
        openPosition(lineItem.filePath, position);
    }
    // element item
    if (firstElement instanceof ElementItem) {
        ElementItem item = (ElementItem) firstElement;
        // use ResultCursor to find first occurrence in the requested subtree
        itemCursor = new ItemCursor(item);
        boolean found = itemCursor.next();
        if (!found) {
            return;
        }
        String filePath = itemCursor.getLine().filePath;
        Position position = itemCursor.getPosition();
        // open position
        openPosition(filePath, position);
    }
}

From source file:com.google.dart.tools.search.internal.ui.SearchMatchPage.java

License:Open Source License

/**
 * Opens selected {@link ResultItem} in the editor.
 *//*ww  w .j  a  v a2s.c  om*/
private void openSelectedElement(ISelection selection) {
    // need IStructuredSelection
    if (!(selection instanceof IStructuredSelection)) {
        return;
    }
    IStructuredSelection structuredSelection = (IStructuredSelection) selection;
    // only single element
    if (structuredSelection.size() != 1) {
        return;
    }
    Object firstElement = structuredSelection.getFirstElement();
    // prepare ResultItem
    if (!(firstElement instanceof ResultItem)) {
        return;
    }
    ResultItem item = (ResultItem) firstElement;
    // use ResultCursor to find first occurrence in the requested subtree
    itemCursor = new ResultCursor(item);
    boolean found = itemCursor.next();
    if (!found) {
        return;
    }
    Element element = itemCursor.item.element;
    Position position = itemCursor.getPosition();
    // show Element and Position
    try {
        IEditorPart editor = DartUI.openInEditor(context, element, true);
        revealInEditor(editor, position);
    } catch (Throwable e) {
        ExceptionHandler.handle(e, "Search", "Exception during open.");
    }
}

From source file:com.google.dart.tools.search.ui.text.AbstractTextSearchViewPage.java

License:Open Source License

private Object getFirstSelectedElement() {
    IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
    if (selection.size() > 0) {
        return selection.getFirstElement();
    }//  w w w . j  av  a  2s . c  om
    return null;
}

From source file:com.google.dart.tools.ui.actions.AbstractDartSelectionAction.java

License:Open Source License

/**
 * @return the {@link IFile} corresponding to the given {@link IStructuredSelection}. May be
 *         {@code null}./*from  w  w  w  .  ja  v a2  s .c  o m*/
 */
protected static IFile getSelectionContextFile(IStructuredSelection selection) {
    if (selection.size() == 1) {
        Object object = selection.getFirstElement();
        if (object instanceof LightNodeElement) {
            return ((LightNodeElement) object).getContextFile();
        }
    }
    return null;
}

From source file:com.google.dart.tools.ui.actions.AbstractDartSelectionAction.java

License:Open Source License

/**
 * @return the only {@link Element} in the given {@link IStructuredSelection}. May be {@code null}
 *         ./*from www . jav  a  2  s  .  c o  m*/
 */
protected static Element getSelectionElement(IStructuredSelection selection) {
    if (selection.size() == 1) {
        Object object = selection.getFirstElement();
        if (object instanceof Element) {
            return (Element) object;
        }
        if (object instanceof LightNodeElement) {
            return ((LightNodeElement) object).getElement();
        }
    }
    return null;
}

From source file:com.google.dart.tools.ui.actions.CopyFilePathAction.java

License:Open Source License

/**
 * On each selection change event, call {@link #setEnabled(boolean)} iff a single element is
 * selected that is a {@link java.io.File}.
 *//*from   w ww.  j  ava 2s .  co  m*/
@Override
public void selectionChanged(SelectionChangedEvent event) {
    // if the selection is a structured selection (aka, from the Files view)
    if (event.getSelection() instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        // if there is one element selected
        if (selection.size() == 1) {
            Object firstElt = selection.getFirstElement();
            // if that element is a java.io.File element
            if (firstElt instanceof IResource) {
                setEnabled(true);
                return;
            }
        }
    }
    setEnabled(false);
}

From source file:com.google.dart.tools.ui.actions.FindAction.java

License:Open Source License

DartElement getDartElement(IStructuredSelection selection, boolean silent) {
    if (selection.size() == 1) {
        Object firstElement = selection.getFirstElement();
        DartElement elem = null;//w  ww .  j  a  va  2s.  c o  m
        if (firstElement instanceof DartElement) {
            elem = (DartElement) firstElement;
        } else if (firstElement instanceof IAdaptable) {
            elem = (DartElement) ((IAdaptable) firstElement).getAdapter(DartElement.class);
        }
        if (elem != null) {
            return getTypeIfPossible(elem, silent);
        }

    }
    return null;
}

From source file:com.google.dart.tools.ui.actions.OpenEditorActionGroup.java

License:Open Source License

private void addOpenWithMenu(IMenuManager menu) {
    ISelection selection = getContext().getSelection();
    if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
        return;/*from w  ww . ja  v a2 s . co m*/
    }
    IStructuredSelection ss = (IStructuredSelection) selection;
    if (ss.size() != 1) {
        return;
    }

    Object o = ss.getFirstElement();
    if (!(o instanceof IAdaptable)) {
        return;
    }

    IAdaptable element = (IAdaptable) o;
    Object resource = element.getAdapter(IResource.class);
    if (!(resource instanceof IFile)) {
        return;
    }

    // Create a menu.
    IMenuManager submenu = new MenuManager(ActionMessages.OpenWithMenu_label);
    submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));

    // Add the submenu.
    menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
}

From source file:com.google.dart.tools.ui.actions.OpenNewFolderWizardAction.java

License:Open Source License

private void updateEnablement(ISelection selection) {
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        if (structuredSelection.size() == 1) {
            if (structuredSelection.getFirstElement() instanceof IResource) {
                IResource element = (IResource) structuredSelection.getFirstElement();
                if (element.getType() == IResource.FOLDER || element.getType() == IResource.PROJECT) {
                    setEnabled(true);/*w w  w .j a va 2s  .c o m*/
                    return;
                }
            }
        }
    }
    setEnabled(false);
}