Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / refactoring / util / ResourceUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.refactoring.util;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IMember;
19 import net.sourceforge.phpdt.core.IOpenable;
20 import net.sourceforge.phpdt.internal.corext.Assert;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24
25 public class ResourceUtil {
26         
27         private ResourceUtil(){
28         }
29         
30         public static IFile[] getFiles(ICompilationUnit[] cus) {
31                 List files= new ArrayList(cus.length);
32                 for (int i= 0; i < cus.length; i++) {
33                         IResource resource= ResourceUtil.getResource(cus[i]);
34                         if (resource != null && resource.getType() == IResource.FILE)
35                                 files.add(resource);
36                 }
37                 return (IFile[]) files.toArray(new IFile[files.size()]);
38         }
39
40         public static IFile getFile(ICompilationUnit cu) {
41                 IResource resource= ResourceUtil.getResource(cu);
42                 if (resource != null && resource.getType() == IResource.FILE)
43                         return (IFile)resource;
44                 else
45                         return null;
46         }
47
48         //----- other ------------------------------
49                         
50         /**
51          * Finds an <code>IResource</code> for a given <code>ICompilationUnit</code>.
52          * If the parameter is a working copy then the <code>IResource</code> for
53          * the original element is returned.
54          */
55         public static IResource getResource(ICompilationUnit cu) {
56                 return cu.getResource();
57         }
58
59
60         /**
61          * Returns the <code>IResource</code> that the given <code>IMember</code> is defined in.
62          * @see #getResource
63          */
64         public static IResource getResource(IMember member) {
65                 Assert.isTrue(!member.isBinary());
66                 return getResource(member.getCompilationUnit());
67         }
68
69         public static IResource getResource(Object o){
70                 if (o instanceof IResource)
71                         return (IResource)o;
72                 if (o instanceof IJavaElement)
73                         return getResource((IJavaElement)o);
74                 return null;    
75         }
76
77         private static IResource getResource(IJavaElement element){
78                 if (element.getElementType() == IJavaElement.COMPILATION_UNIT) 
79                         return getResource((ICompilationUnit) element);
80                 else if (element instanceof IOpenable) 
81                         return element.getResource();
82                 else    
83                         return null;    
84         }
85 }