KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > ResourceUtil


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.jdt.internal.corext.refactoring.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IOpenable;
22
23
24 public class ResourceUtil {
25     
26     private ResourceUtil(){
27     }
28     
29     public static IFile[] getFiles(ICompilationUnit[] cus) {
30         List JavaDoc files= new ArrayList JavaDoc(cus.length);
31         for (int i= 0; i < cus.length; i++) {
32             IResource resource= cus[i].getResource();
33             if (resource != null && resource.getType() == IResource.FILE)
34                 files.add(resource);
35         }
36         return (IFile[]) files.toArray(new IFile[files.size()]);
37     }
38
39     public static IFile getFile(ICompilationUnit cu) {
40         IResource resource= cu.getResource();
41         if (resource != null && resource.getType() == IResource.FILE)
42             return (IFile)resource;
43         else
44             return null;
45     }
46
47     //----- other ------------------------------
48

49     public static IResource getResource(Object JavaDoc o){
50         if (o instanceof IResource)
51             return (IResource)o;
52         if (o instanceof IJavaElement)
53             return getResource((IJavaElement)o);
54         return null;
55     }
56
57     private static IResource getResource(IJavaElement element){
58         if (element.getElementType() == IJavaElement.COMPILATION_UNIT)
59             return ((ICompilationUnit) element).getResource();
60         else if (element instanceof IOpenable)
61             return element.getResource();
62         else
63             return null;
64     }
65 }
66
Popular Tags