KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > IFileTypeInfo


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.util;
12
13 import java.net.URI JavaDoc;
14
15 import org.eclipse.core.filesystem.EFS;
16 import org.eclipse.core.filesystem.IFileInfo;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21
22 import org.eclipse.core.filebuffers.FileBuffers;
23 import org.eclipse.core.filebuffers.ITextFileBuffer;
24 import org.eclipse.core.filebuffers.ITextFileBufferManager;
25
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.IWorkspaceRoot;
28 import org.eclipse.core.resources.ResourcesPlugin;
29
30 import org.eclipse.jdt.core.IJavaElement;
31 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.core.search.IJavaSearchScope;
33
34 /**
35  * A <tt>IFileTypeInfo</tt> represents a type in a class or java file.
36  */

37 public class IFileTypeInfo extends TypeInfo {
38     
39     private final String JavaDoc fProject;
40     private final String JavaDoc fFolder;
41     private final String JavaDoc fFile;
42     private final String JavaDoc fExtension;
43     
44     public IFileTypeInfo(String JavaDoc pkg, String JavaDoc name, char[][] enclosingTypes, int modifiers, String JavaDoc project, String JavaDoc sourceFolder, String JavaDoc file, String JavaDoc extension) {
45         super(pkg, name, enclosingTypes, modifiers);
46         fProject= project;
47         fFolder= sourceFolder;
48         fFile= file;
49         fExtension= extension;
50     }
51     
52     public boolean equals(Object JavaDoc obj) {
53         if (this == obj)
54             return true;
55         if (!IFileTypeInfo.class.equals(obj.getClass()))
56             return false;
57         IFileTypeInfo other= (IFileTypeInfo)obj;
58         return doEquals(other) && fProject.equals(other.fProject) && equals(fFolder, other.fFolder) &&
59             fFile.equals(other.fFile) && fExtension.equals(other.fExtension);
60     }
61     
62     public int getElementType() {
63         return TypeInfo.IFILE_TYPE_INFO;
64     }
65     
66     protected IJavaElement getContainer(IJavaSearchScope scope) {
67         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
68         IPath path= new Path(getPath());
69         IResource resource= root.findMember(path);
70         if (resource != null) {
71             IJavaElement elem= JavaCore.create(resource);
72             if (elem != null && elem.exists()) {
73                 return elem;
74             }
75         }
76         return null;
77     }
78     
79     public IPath getPackageFragmentRootPath() {
80         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
81         buffer.append(TypeInfo.SEPARATOR);
82         buffer.append(fProject);
83         if (fFolder != null && fFolder.length() > 0) {
84             buffer.append(TypeInfo.SEPARATOR);
85             buffer.append(fFolder);
86         }
87         return new Path(buffer.toString());
88     }
89     
90     public String JavaDoc getPackageFragmentRootName() {
91         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
92         buffer.append(fProject);
93         if (fFolder != null && fFolder.length() > 0) {
94             buffer.append(TypeInfo.SEPARATOR);
95             buffer.append(fFolder);
96         }
97         return buffer.toString();
98     }
99         
100     public String JavaDoc getPath() {
101         StringBuffer JavaDoc result= new StringBuffer JavaDoc();
102         result.append(TypeInfo.SEPARATOR);
103         result.append(fProject);
104         result.append(TypeInfo.SEPARATOR);
105         if (fFolder != null && fFolder.length() > 0) {
106                 result.append(fFolder);
107                 result.append(TypeInfo.SEPARATOR);
108         }
109         if (fPackage != null && fPackage.length() > 0) {
110             result.append(fPackage.replace(TypeInfo.PACKAGE_PART_SEPARATOR, TypeInfo.SEPARATOR));
111             result.append(TypeInfo.SEPARATOR);
112         }
113         result.append(fFile);
114         result.append('.');
115         result.append(fExtension);
116         return result.toString();
117     }
118     
119     public String JavaDoc getProject() {
120         return fProject;
121     }
122     
123     public String JavaDoc getFolder() {
124         return fFolder;
125     }
126     
127     public String JavaDoc getFileName() {
128         return fFile;
129     }
130     
131     public String JavaDoc getExtension() {
132         return fExtension;
133     }
134     
135     public long getContainerTimestamp() {
136         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
137         IPath path= new Path(getPath());
138         IResource resource= root.findMember(path);
139         if (resource != null) {
140             URI JavaDoc location= resource.getLocationURI();
141             if (location != null) {
142                 try {
143                     IFileInfo info= EFS.getStore(location).fetchInfo();
144                     if (info.exists()) {
145                         // The element could be removed from the build path. So check
146
// if the Java element still exists.
147
IJavaElement element= JavaCore.create(resource);
148                         if (element != null && element.exists())
149                             return info.getLastModified();
150                     }
151                 } catch (CoreException e) {
152                     // Fall through
153
}
154             }
155         }
156         return IResource.NULL_STAMP;
157     }
158     
159     public boolean isContainerDirty() {
160         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
161         IPath path= new Path(getPath());
162         IResource resource= root.findMember(path);
163         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
164         ITextFileBuffer textFileBuffer= manager.getTextFileBuffer(resource.getFullPath());
165         if (textFileBuffer != null) {
166             return textFileBuffer.isDirty();
167         }
168         return false;
169     }
170 }
171
Popular Tags