Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cee1933b4505a6c8dd02ebf9c548a502c5e42d9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal;

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;
	}
}

Back to the top