1 11 package org.eclipse.ui.internal; 12 13 import java.util.Arrays ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.expressions.ICountable; 19 import org.eclipse.core.expressions.IIterable; 20 import org.eclipse.core.runtime.IAdapterFactory; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 24 30 public class SelectionAdapterFactory implements IAdapterFactory { 31 private static final ICountable ICOUNT_0 = new ICountable() { 32 37 public int count() { 38 return 0; 39 } 40 }; 41 private static final ICountable ICOUNT_1 = new ICountable() { 42 47 public int count() { 48 return 1; 49 } 50 }; 51 private static final IIterable ITERATE_EMPTY = new IIterable() { 52 57 public Iterator iterator() { 58 return Collections.EMPTY_LIST.iterator(); 59 } 60 }; 61 62 65 private static final Class [] CLASSES = new Class [] { IIterable.class, 66 ICountable.class }; 67 68 public Object getAdapter(Object adaptableObject, Class adapterType) { 69 if (adaptableObject instanceof ISelection) { 70 if (adapterType == IIterable.class) { 71 return iterable((ISelection) adaptableObject); 72 } else if (adapterType == ICountable.class) { 73 return countable((ISelection) adaptableObject); 74 } 75 } 76 return null; 77 } 78 79 private Object iterable(final ISelection sel) { 80 if (sel.isEmpty()) { 81 return ITERATE_EMPTY; 82 } 83 if (sel instanceof IStructuredSelection) { 84 return new IIterable() { 85 public Iterator iterator() { 86 return ((IStructuredSelection) sel).iterator(); 87 } 88 }; 89 } 90 final List list = Arrays.asList(new Object [] { sel }); 91 return new IIterable() { 92 93 public Iterator iterator() { 94 return list.iterator(); 95 } 96 }; 97 } 98 99 private Object countable(final ISelection sel) { 100 if (sel.isEmpty()) { 101 return ICOUNT_0; 102 } 103 if (sel instanceof IStructuredSelection) { 104 final IStructuredSelection ss = (IStructuredSelection) sel; 105 return new ICountable() { 106 public int count() { 107 return ss.size(); 108 } 109 }; 110 } 111 return ICOUNT_1; 112 } 113 114 public Class [] getAdapterList() { 115 return CLASSES; 116 } 117 } 118 | Popular Tags |