001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.Arrays;
013: import java.util.Collections;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.eclipse.core.expressions.ICountable;
018: import org.eclipse.core.expressions.IIterable;
019: import org.eclipse.core.runtime.IAdapterFactory;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022:
023: /**
024: * Adapts ISelection instances to either IIterable or ICountable. For use with
025: * core expressions.
026: *
027: * @since 3.3
028: */
029: public class SelectionAdapterFactory implements IAdapterFactory {
030: private static final ICountable ICOUNT_0 = new ICountable() {
031: /*
032: * (non-Javadoc)
033: *
034: * @see org.eclipse.core.expressions.ICountable#count()
035: */
036: public int count() {
037: return 0;
038: }
039: };
040: private static final ICountable ICOUNT_1 = new ICountable() {
041: /*
042: * (non-Javadoc)
043: *
044: * @see org.eclipse.core.expressions.ICountable#count()
045: */
046: public int count() {
047: return 1;
048: }
049: };
050: private static final IIterable ITERATE_EMPTY = new IIterable() {
051: /*
052: * (non-Javadoc)
053: *
054: * @see org.eclipse.core.expressions.IIterable#iterator()
055: */
056: public Iterator iterator() {
057: return Collections.EMPTY_LIST.iterator();
058: }
059: };
060:
061: /**
062: * The classes we can adapt to.
063: */
064: private static final Class[] CLASSES = new Class[] {
065: IIterable.class, ICountable.class };
066:
067: public Object getAdapter(Object adaptableObject, Class adapterType) {
068: if (adaptableObject instanceof ISelection) {
069: if (adapterType == IIterable.class) {
070: return iterable((ISelection) adaptableObject);
071: } else if (adapterType == ICountable.class) {
072: return countable((ISelection) adaptableObject);
073: }
074: }
075: return null;
076: }
077:
078: private Object iterable(final ISelection sel) {
079: if (sel.isEmpty()) {
080: return ITERATE_EMPTY;
081: }
082: if (sel instanceof IStructuredSelection) {
083: return new IIterable() {
084: public Iterator iterator() {
085: return ((IStructuredSelection) sel).iterator();
086: }
087: };
088: }
089: final List list = Arrays.asList(new Object[] { sel });
090: return new IIterable() {
091:
092: public Iterator iterator() {
093: return list.iterator();
094: }
095: };
096: }
097:
098: private Object countable(final ISelection sel) {
099: if (sel.isEmpty()) {
100: return ICOUNT_0;
101: }
102: if (sel instanceof IStructuredSelection) {
103: final IStructuredSelection ss = (IStructuredSelection) sel;
104: return new ICountable() {
105: public int count() {
106: return ss.size();
107: }
108: };
109: }
110: return ICOUNT_1;
111: }
112:
113: public Class[] getAdapterList() {
114: return CLASSES;
115: }
116: }
|