KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > SelectionAdapterFactory


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
25  * Adapts ISelection instances to either IIterable or ICountable. For use with
26  * core expressions.
27  *
28  * @since 3.3
29  */

30 public class SelectionAdapterFactory implements IAdapterFactory {
31     private static final ICountable ICOUNT_0 = new ICountable() {
32         /*
33          * (non-Javadoc)
34          *
35          * @see org.eclipse.core.expressions.ICountable#count()
36          */

37         public int count() {
38             return 0;
39         }
40     };
41     private static final ICountable ICOUNT_1 = new ICountable() {
42         /*
43          * (non-Javadoc)
44          *
45          * @see org.eclipse.core.expressions.ICountable#count()
46          */

47         public int count() {
48             return 1;
49         }
50     };
51     private static final IIterable ITERATE_EMPTY = new IIterable() {
52         /*
53          * (non-Javadoc)
54          *
55          * @see org.eclipse.core.expressions.IIterable#iterator()
56          */

57         public Iterator JavaDoc iterator() {
58             return Collections.EMPTY_LIST.iterator();
59         }
60     };
61
62     /**
63      * The classes we can adapt to.
64      */

65     private static final Class JavaDoc[] CLASSES = new Class JavaDoc[] { IIterable.class,
66             ICountable.class };
67
68     public Object JavaDoc getAdapter(Object JavaDoc adaptableObject, Class JavaDoc 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 JavaDoc 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 JavaDoc iterator() {
86                     return ((IStructuredSelection) sel).iterator();
87                 }
88             };
89         }
90         final List JavaDoc list = Arrays.asList(new Object JavaDoc[] { sel });
91         return new IIterable() {
92
93             public Iterator JavaDoc iterator() {
94                 return list.iterator();
95             }
96         };
97     }
98
99     private Object JavaDoc 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 JavaDoc[] getAdapterList() {
115         return CLASSES;
116     }
117 }
118
Popular Tags