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.microsoft.tfs.client.common.ui.controls.wit.WorkItemList.java

License:Open Source License

private void addContextMenu() {
    final MenuManager menuMgr = new MenuManager("#PopUp"); //$NON-NLS-1$
    final IAction detailsAction = new Action() {
        @Override//from w  ww.  j  a  v  a2s. c o m
        public void run() {
            final IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
            workItemDoubleClicked((WorkItemCheckinInfo) selection.getFirstElement());
        }
    };
    detailsAction.setText(Messages.getString("WorkItemList.DetailsActionText")); //$NON-NLS-1$
    detailsAction.setEnabled(false);
    menuMgr.add(detailsAction);

    tableViewer.getControl().setMenu(menuMgr.createContextMenu(tableViewer.getControl()));

    tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(final SelectionChangedEvent event) {
            final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            final boolean selectedItem = selection.size() > 0;
            detailsAction.setEnabled(selectedItem);
        }
    });
}

From source file:com.microsoft.tfs.client.common.ui.dialogs.vc.candidates.DeleteFromDiskAction.java

License:Open Source License

@Override
protected boolean computeEnablement(final IStructuredSelection selection) {
    if (selection.size() == 0) {
        return false;
    }//  w  ww  .  j  a v a  2  s.  c  o m

    final ChangeItem[] changes = (ChangeItem[]) SelectionUtils.selectionToArray(getSelection(),
            ChangeItem.class);

    // Ensure all are adds
    boolean nonAdd = false;
    for (final ChangeItem change : changes) {
        if (!change.getChangeType().contains(ChangeType.ADD)) {
            nonAdd = true;
            break;
        }
    }

    return !nonAdd;
}

From source file:com.microsoft.tfs.client.common.ui.dialogs.vc.candidates.ViewLocalFolderAction.java

License:Open Source License

@Override
protected boolean computeEnablement(final IStructuredSelection selection) {
    return selection.size() == 1;
}

From source file:com.microsoft.tfs.client.common.ui.editors.HistoryEditor.java

License:Open Source License

private String getStatusLineMessage(final IStructuredSelection selection) {
    if (selection.size() == 1) {
        final Changeset changeset = (Changeset) selection.getFirstElement();
        final int id = changeset.getChangesetID();
        final int count = changeset.getChanges() != null ? changeset.getChanges().length : 0;
        final String owner = changeset.getOwnerDisplayName();
        final String date = dateFormat.format(changeset.getDate().getTime());

        if (count == 1) {
            final String messageFormat = Messages.getString("HistoryEditor.SingleSelectOneChangeStatusFormat"); //$NON-NLS-1$
            return MessageFormat.format(messageFormat, Integer.toString(id), owner, date);

        } else {// w  ww . j a v a  2 s. co  m
            final String messageFormat = Messages
                    .getString("HistoryEditor.SingleSelectMultiChangeStatusFormat"); //$NON-NLS-1$
            return MessageFormat.format(messageFormat, Integer.toString(id), count, owner, date);
        }
    }

    if (selection.size() > 1) {
        final String messageFormat = Messages.getString("HistoryEditor.MultiSelectStatusFormat"); //$NON-NLS-1$
        return MessageFormat.format(messageFormat, selection.size());
    }

    return null;
}

From source file:com.microsoft.tfs.client.common.ui.framework.helper.SelectionUtils.java

License:Open Source License

/**
 * Obtains the size of the specified {@link ISelection}:
 * <ul>/*from w w  w  . j av  a  2 s .  c o m*/
 * <li>If the selection is <code>null</code>, <code>0</code> is returned
 * </li>
 * <li>If the selection is not an {@link IStructuredSelection}, an
 * {@link IllegalArgumentException} is thrown</li>
 * <li>Otherwise, the size of the structured selection is returned</li>
 * </ul>
 *
 * @param selection
 *        the {@link ISelection} to get the size for (can be
 *        <code>null</code> but if non-<code>null</code> must be an
 *        {@link IStructuredSelection})
 * @throws IllegalArgumentException
 *         if the specified {@link ISelection} is not <code>null</code> and
 *         is not an {@link IStructuredSelection}
 * @return the size of the structured selection
 */
public static final int getSelectionSize(final ISelection selection) {
    final IStructuredSelection ss = getStructuredSelection(selection);
    return (ss == null ? 0 : ss.size());
}

From source file:com.microsoft.tfs.client.common.ui.framework.helper.SelectionUtils.java

License:Open Source License

/**
 * <p>//from  ww w . jav a  2  s  .c o m
 * Returns the elements contained in the selection as an array. The runtime
 * type of the array is specified by <code>targetType</code>. If
 * <code>adapt</code> is <code>true</code>, each element in the selection is
 * adapted to the specified target type. The array is computed in the
 * following way:
 * <ul>
 * <li>If the selection is <code>null</code>, an empty array (of the
 * specified target type) is returned</li>
 * <li>If the selection is not an {@link IStructuredSelection}, an
 * {@link IllegalArgumentException} is thrown</li>
 * <li>Otherwise, the selection's elements are optionally adapted and
 * converted to an array (of the specified target type) and returned</li>
 * </ul>
 * </p>
 *
 * <p>
 * You must not specify a primitive type as the <code>targetType</code>.
 * </p>
 *
 * @param selection
 *        the {@link ISelection} to get the elements for (can be
 *        <code>null</code> but if non-<code>null</code> must be an
 *        {@link IStructuredSelection})
 * @throws IllegalArgumentException
 *         if the specified {@link ISelection} is not <code>null</code> and
 *         is not an {@link IStructuredSelection}, or if the specified
 *         <code>targetType</code> is a primitive type, or if
 *         <code>adapt</code> is <code>true</code> and any of the selection
 *         elements could not be adapted to the target type
 * @throws ArrayStoreException
 *         if any of the (possibly adapted) selection elements are not
 *         compatible with the specified <code>targetType</code>
 * @return the (possibly adapted) selection elements as an array of the
 *         target type (never <code>null</code>)
 */
public static final Object[] selectionToArray(final ISelection selection, final Class targetType,
        final boolean adapt) {
    Check.notNull(targetType, "targetType"); //$NON-NLS-1$

    final IStructuredSelection structuredSelection = getStructuredSelection(selection);

    if (targetType.isPrimitive()) {
        throw new IllegalArgumentException("You must specify a non-primitive target type"); //$NON-NLS-1$
    }

    final Object[] result = (Object[]) Array.newInstance(targetType,
            (structuredSelection == null ? 0 : structuredSelection.size()));

    if (structuredSelection == null) {
        return result;
    }

    int ix = 0;
    for (final Iterator it = structuredSelection.iterator(); it.hasNext();) {
        final Object sourceElement = it.next();
        Object targetElement = sourceElement;

        if (adapt && sourceElement != null) {
            targetElement = Adapters.getAdapter(sourceElement, targetType);

            if (targetElement == null) {
                final String messageFormat = "adaptSelectionToArray({0}): could not adapt [{1}]"; //$NON-NLS-1$
                final String message = MessageFormat.format(messageFormat, targetType.getName(),
                        sourceElement.getClass().getName());
                throw new IllegalArgumentException(message);
            }
        }

        result[ix++] = targetElement;
    }

    return result;
}

From source file:com.microsoft.tfs.client.common.ui.framework.table.TableControl.java

License:Open Source License

/**
 * Called when a {@link CheckStateChangedEvent} occurs on the
 * {@link CheckboxTableViewer} backing this {@link TableControl}.
 *
 * @param event/*from   w w w .  j a  v  a 2s  .c om*/
 *        the {@link CheckStateChangedEvent}
 */
protected void onCheckStateChanged(final CheckStateChangedEvent event) {
    final boolean checked = event.getChecked();

    if (isChecksAffectSelection()) {
        /*
         * propagate checked state to all elements in the current selection
         */
        viewer.getTable().setRedraw(false);
        final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
        if (selection.size() == viewer.getTable().getItemCount()) {
            ((CheckboxTableViewer) viewer).setAllChecked(checked);
        } else {
            /*
             * Make sure the element being checked is part of the selection.
             * Otherwise, we ignore the selected elements. Avoids the case
             * where you have some elements selected, and check a
             * non-selected element. The checkstate of the selected elements
             * should NOT change in this case.
             */
            boolean checkedElementInSelection = false;

            for (final Iterator it = selection.iterator(); it.hasNext();) {
                if (it.next().equals(event.getElement())) {
                    checkedElementInSelection = true;
                    break;
                }
            }

            if (checkedElementInSelection) {
                for (final Iterator it = selection.iterator(); it.hasNext();) {
                    ((CheckboxTableViewer) viewer).setChecked(it.next(), checked);
                }
            }
        }
        viewer.getTable().setRedraw(true);
    }

    computeCheckedElements(true);

    final Object element = event.getElement();
    if (!hideElementFromCollections(element)) {
        notifyCheckStateListeners(element, event.getChecked());
    }
}

From source file:com.microsoft.tfs.client.common.ui.framework.validation.SelectionProviderValidator.java

License:Open Source License

@Override
protected IValidity computeValidity(final ISelection selection) {
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        final int size = structuredSelection.size();
        return numericConstraint.passes(size) ? Validity.VALID : Validity.invalid(errorMessage);
    }//from   w w w.  j av a  2  s  .  c  o m

    return Validity.invalid(errorMessage);
}

From source file:com.microsoft.tfs.client.common.ui.teambuild.teamexplorer.actions.TeamExplorerSingleBuildDefinitionAction.java

License:Open Source License

@Override
protected void onSelectionChanged(final IAction action, final ISelection selection) {
    super.onSelectionChanged(action, selection);
    if (action.isEnabled()) {
        if (action.isEnabled() && selection instanceof IStructuredSelection) {
            final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
            if (structuredSelection.size() != 1) {
                action.setEnabled(false);
                return;
            }// w ww . j  ava2  s. com

            final Object obj = structuredSelection.getFirstElement();
            if (obj instanceof IBuildDefinition) {
                selectedDefinition = (IBuildDefinition) obj;
                return;
            } else if (obj instanceof BuildDetail) {
                final BuildDetail buildDetail = (BuildDetail) obj;
                if (buildDetail.getBuildDefinition() != null) {
                    selectedDefinition = buildDetail.getBuildDefinition();
                    return;
                }
            } else if (obj instanceof QueuedBuild) {
                final QueuedBuild queuedBuild = (QueuedBuild) obj;
                if (queuedBuild.getBuildDefinition() != null) {
                    selectedDefinition = queuedBuild.getBuildDefinition();
                    return;
                }
            } else if (obj instanceof BuildFavoriteItem) {
                final BuildFavoriteItem favorite = (BuildFavoriteItem) obj;
                if (favorite.getBuildDefinitionType() == DefinitionType.XAML) {
                    selectedDefinition = (IBuildDefinition) favorite.getBuildDefinition();
                    return;
                }
            }

            selectedDefinition = null;
            action.setEnabled(false);
            return;
        }
    }
}

From source file:com.microsoft.tfs.client.common.ui.vcexplorer.versioncontrol.VersionControlEditor.java

License:Open Source License

private void handleFolderSelection(final ISelection selection) {
    if (!(selection instanceof IStructuredSelection)) {
        return;//  w w w  . j av a  2s .  co  m
    }

    CodeMarkerDispatch.dispatch(CODEMARKER_FILE_REFRESH_START);

    final IStructuredSelection iss = (IStructuredSelection) selection;
    final TFSRepository repository = getRepository();

    if (iss.size() != 1) {
        fileControl.clear();
    } else if (repository != null) {
        final TFSFolder folder = (TFSFolder) iss.getFirstElement();
        fileControl.refresh(folder);
        try {
            fileControl.setSelectedServerPath(repository, folder.getFullPath());
        } catch (final ServerPathFormatException e) {
            final String messageFormat = "Server path incorrect: {0}"; //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, folder.getFullPath());
            log.error(message, e);
        }
    }

    CodeMarkerDispatch.dispatch(CODEMARKER_FILE_REFRESH_COMPLETE);
}