diff options
author | John Arthorne | 2010-06-04 20:22:29 +0000 |
---|---|---|
committer | John Arthorne | 2010-06-04 20:22:29 +0000 |
commit | 639e096b225d407d69cd2d120e6725a6b3269450 (patch) | |
tree | 7ecd092c7a9a5d1710f26db7a47922c533841d13 /bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/SelectionAdapterFactory.java | |
parent | 94f4993a9537704739f0aff85a0a6815ec668534 (diff) | |
download | eclipse.platform.ui-639e096b225d407d69cd2d120e6725a6b3269450.tar.gz eclipse.platform.ui-639e096b225d407d69cd2d120e6725a6b3269450.tar.xz eclipse.platform.ui-639e096b225d407d69cd2d120e6725a6b3269450.zip |
Bug 315463 - API package name cleanup
Diffstat (limited to 'bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/SelectionAdapterFactory.java')
-rw-r--r-- | bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/SelectionAdapterFactory.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/SelectionAdapterFactory.java b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/SelectionAdapterFactory.java new file mode 100644 index 00000000000..2cb3178cf0c --- /dev/null +++ b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/SelectionAdapterFactory.java @@ -0,0 +1,106 @@ +package org.eclipse.e4.ui.internal.workbench.swt; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import org.eclipse.core.expressions.ICountable; +import org.eclipse.core.expressions.IIterable; +import org.eclipse.core.runtime.IAdapterFactory; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; + +/** + * Adapts ISelection instances to either IIterable or ICountable. For use with + * core expressions. + * + * @since 3.3 + */ +public class SelectionAdapterFactory implements IAdapterFactory { + private static final ICountable ICOUNT_0 = new ICountable() { + /* + * (non-Javadoc) + * + * @see org.eclipse.core.expressions.ICountable#count() + */ + public int count() { + return 0; + } + }; + private static final ICountable ICOUNT_1 = new ICountable() { + /* + * (non-Javadoc) + * + * @see org.eclipse.core.expressions.ICountable#count() + */ + public int count() { + return 1; + } + }; + private static final IIterable ITERATE_EMPTY = new IIterable() { + /* + * (non-Javadoc) + * + * @see org.eclipse.core.expressions.IIterable#iterator() + */ + public Iterator iterator() { + return Collections.EMPTY_LIST.iterator(); + } + }; + + /** + * The classes we can adapt to. + */ + private static final Class[] CLASSES = new Class[] { IIterable.class, + ICountable.class }; + + public Object getAdapter(Object adaptableObject, Class adapterType) { + if (adaptableObject instanceof ISelection) { + if (adapterType == IIterable.class) { + return iterable((ISelection) adaptableObject); + } else if (adapterType == ICountable.class) { + return countable((ISelection) adaptableObject); + } + } + return null; + } + + private Object iterable(final ISelection sel) { + if (sel.isEmpty()) { + return ITERATE_EMPTY; + } + if (sel instanceof IStructuredSelection) { + return new IIterable() { + public Iterator iterator() { + return ((IStructuredSelection) sel).iterator(); + } + }; + } + final List list = Arrays.asList(new Object[] { sel }); + return new IIterable() { + + public Iterator iterator() { + return list.iterator(); + } + }; + } + + private Object countable(final ISelection sel) { + if (sel.isEmpty()) { + return ICOUNT_0; + } + if (sel instanceof IStructuredSelection) { + final IStructuredSelection ss = (IStructuredSelection) sel; + return new ICountable() { + public int count() { + return ss.size(); + } + }; + } + return ICOUNT_1; + } + + public Class[] getAdapterList() { + return CLASSES; + } +} |