* Added browser like links (Ctrl+Mouseclick on identifier; same as F3 shortcut)
[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 org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18
19 import net.sourceforge.phpdt.core.ICompilationUnit;
20 import net.sourceforge.phpdt.core.IJavaElement;
21 import net.sourceforge.phpdt.core.IMember;
22 import net.sourceforge.phpdt.core.IOpenable;
23
24 import net.sourceforge.phpdt.internal.corext.Assert;
25
26 public class ResourceUtil {
27         
28         private ResourceUtil(){
29         }
30         
31         public static IFile[] getFiles(ICompilationUnit[] cus) {
32                 List files= new ArrayList(cus.length);
33                 for (int i= 0; i < cus.length; i++) {
34                         IResource resource= ResourceUtil.getResource(cus[i]);
35                         if (resource != null && resource.getType() == IResource.FILE)
36                                 files.add(resource);
37                 }
38                 return (IFile[]) files.toArray(new IFile[files.size()]);
39         }
40
41         public static IFile getFile(ICompilationUnit cu) {
42                 IResource resource= ResourceUtil.getResource(cu);
43                 if (resource != null && resource.getType() == IResource.FILE)
44                         return (IFile)resource;
45                 else
46                         return null;
47         }
48
49         //----- other ------------------------------
50                         
51         /**
52          * Finds an <code>IResource</code> for a given <code>ICompilationUnit</code>.
53          * If the parameter is a working copy then the <code>IResource</code> for
54          * the original element is returned.
55          */
56         public static IResource getResource(ICompilationUnit cu) {
57                 return cu.getResource();
58         }
59
60
61         /**
62          * Returns the <code>IResource</code> that the given <code>IMember</code> is defined in.
63          * @see #getResource
64          */
65         public static IResource getResource(IMember member) {
66                 Assert.isTrue(!member.isBinary());
67                 return getResource(member.getCompilationUnit());
68         }
69
70         public static IResource getResource(Object o){
71                 if (o instanceof IResource)
72                         return (IResource)o;
73                 if (o instanceof IJavaElement)
74                         return getResource((IJavaElement)o);
75                 return null;    
76         }
77
78         private static IResource getResource(IJavaElement element){
79                 if (element.getElementType() == IJavaElement.COMPILATION_UNIT) 
80                         return getResource((ICompilationUnit) element);
81                 else if (element instanceof IOpenable) 
82                         return element.getResource();
83                 else    
84                         return null;    
85         }
86 }