KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > ResourceSelectionUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.navigator.resources.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21
22 /**
23  * Provides utilities for checking the validity of selections.
24  * <p>
25  * This class provides static methods only; it is not intended to be instantiated
26  * or subclassed.
27  * @since 2.0
28  * </p>
29  */

30 public class ResourceSelectionUtil {
31     /* (non-Javadoc)
32      * Private constructor to block instantiation.
33      */

34     private ResourceSelectionUtil() {
35     }
36
37     /**
38      * Returns whether the types of the resources in the given selection are among
39      * the specified resource types.
40      *
41      * @param selection the selection
42      * @param resourceMask resource mask formed by bitwise OR of resource type
43      * constants (defined on <code>IResource</code>)
44      * @return <code>true</code> if all selected elements are resources of the right
45      * type, and <code>false</code> if at least one element is either a resource
46      * of some other type or a non-resource
47      * @see IResource#getType()
48      */

49     public static boolean allResourcesAreOfType(IStructuredSelection selection,
50             int resourceMask) {
51         Iterator JavaDoc resources = selection.iterator();
52         while (resources.hasNext()) {
53             Object JavaDoc next = resources.next();
54             if (!(next instanceof IResource)) {
55                 return false;
56             }
57             if (!resourceIsType((IResource) next, resourceMask)) {
58                 return false;
59             }
60         }
61         return true;
62     }
63
64     /**
65      * Returns the selection adapted to IResource. Returns null
66      * if any of the entries are not adaptable.
67      *
68      * @param selection the selection
69      * @param resourceMask resource mask formed by bitwise OR of resource type
70      * constants (defined on <code>IResource</code>)
71      * @return IStructuredSelection or null if any of the entries are not adaptable.
72      * @see IResource#getType()
73      */

74     public static IStructuredSelection allResources(
75             IStructuredSelection selection, int resourceMask) {
76         Iterator JavaDoc adaptables = selection.iterator();
77         List JavaDoc result = new ArrayList JavaDoc();
78         while (adaptables.hasNext()) {
79             Object JavaDoc next = adaptables.next();
80             if (next instanceof IAdaptable) {
81                 Object JavaDoc resource = ((IAdaptable) next)
82                         .getAdapter(IResource.class);
83                 if (resource == null) {
84                     return null;
85                 } else if (resourceIsType((IResource) resource, resourceMask)) {
86                     result.add(resource);
87                 }
88             } else {
89                 return null;
90             }
91         }
92         return new StructuredSelection(result);
93
94     }
95
96     /**
97      * Returns whether the type of the given resource is among the specified
98      * resource types.
99      *
100      * @param resource the resource
101      * @param resourceMask resource mask formed by bitwise OR of resource type
102      * constants (defined on <code>IResource</code>)
103      * @return <code>true</code> if the resources has a matching type, and
104      * <code>false</code> otherwise
105      * @see IResource#getType()
106      */

107     public static boolean resourceIsType(IResource resource, int resourceMask) {
108         return (resource.getType() & resourceMask) != 0;
109     }
110
111 }
112
Popular Tags