2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.ide.eclipse.adt.internal.resources.manager;
19 import com.android.ide.eclipse.adt.internal.resources.ResourceItem;
20 import com.android.ide.eclipse.adt.internal.resources.ResourceType;
21 import com.android.ide.eclipse.adt.internal.resources.configurations.FolderConfiguration;
22 import com.android.ide.eclipse.adt.internal.resources.manager.files.IAbstractFile;
23 import com.android.ide.eclipse.adt.internal.resources.manager.files.IAbstractFolder;
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IFolder;
28 import java.util.ArrayList;
29 import java.util.Collection;
32 * Resource Folder class. Contains list of {@link ResourceFile}s,
33 * the {@link FolderConfiguration}, and a link to the workspace {@link IFolder} object.
35 public final class ResourceFolder extends Resource {
36 ResourceFolderType mType;
37 FolderConfiguration mConfiguration;
38 IAbstractFolder mFolder;
39 ArrayList<ResourceFile> mFiles = null;
40 private final boolean mIsFramework;
43 * Creates a new {@link ResourceFolder}
44 * @param type The type of the folder
45 * @param config The configuration of the folder
46 * @param folder The associated {@link IAbstractFolder} object.
47 * @param isFrameworkRepository
49 public ResourceFolder(ResourceFolderType type, FolderConfiguration config,
50 IAbstractFolder folder, boolean isFrameworkRepository) {
52 mConfiguration = config;
54 mIsFramework = isFrameworkRepository;
58 * Adds a {@link ResourceFile} to the folder.
59 * @param file The {@link ResourceFile}.
61 public void addFile(ResourceFile file) {
63 mFiles = new ArrayList<ResourceFile>();
70 * Attempts to remove the {@link ResourceFile} associated with a specified {@link IFile}.
71 * @param file the IFile object.
73 public void removeFile(IFile file) {
75 int count = mFiles.size();
76 for (int i = 0 ; i < count ; i++) {
77 ResourceFile resFile = mFiles.get(i);
78 if (resFile != null) {
79 IFile iFile = resFile.getFile().getIFile();
80 if (iFile != null && iFile.equals(file)) {
90 * Returns the {@link IFolder} associated with this object.
92 public IAbstractFolder getFolder() {
97 * Returns the {@link ResourceFolderType} of this object.
99 public ResourceFolderType getType() {
104 * Returns whether the folder is a framework resource folder.
106 public boolean isFramework() {
111 * Returns the list of {@link ResourceType}s generated by the files inside this folder.
113 public Collection<ResourceType> getResourceTypes() {
114 ArrayList<ResourceType> list = new ArrayList<ResourceType>();
116 if (mFiles != null) {
117 for (ResourceFile file : mFiles) {
118 ResourceType[] types = file.getResourceTypes();
120 // loop through those and add them to the main list,
121 // if they are not already present
123 for (ResourceType resType : types) {
124 if (list.indexOf(resType) == -1) {
137 * @see com.android.ide.eclipse.editors.resources.manager.Resource#getConfiguration()
140 public FolderConfiguration getConfiguration() {
141 return mConfiguration;
145 * Returns whether the folder contains a file with the given name.
146 * @param name the name of the file.
148 public boolean hasFile(String name) {
149 return mFolder.hasFile(name);
153 * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object.
154 * @param file The {@link IFile} object.
155 * @return the {@link ResourceFile} or null if no match was found.
157 public ResourceFile getFile(IAbstractFile file) {
158 if (mFiles != null) {
159 for (ResourceFile f : mFiles) {
160 if (f.getFile().equals(file)) {
169 * Returns the {@link ResourceFile} matching a {@link IFile} object.
170 * @param file The {@link IFile} object.
171 * @return the {@link ResourceFile} or null if no match was found.
173 public ResourceFile getFile(IFile file) {
174 if (mFiles != null) {
175 for (ResourceFile f : mFiles) {
176 IFile iFile = f.getFile().getIFile();
177 if (iFile != null && iFile.equals(file)) {
187 * Returns the {@link ResourceFile} matching a given name.
188 * @param filename The name of the file to return.
189 * @return the {@link ResourceFile} or <code>null</code> if no match was found.
191 public ResourceFile getFile(String filename) {
192 if (mFiles != null) {
193 for (ResourceFile f : mFiles) {
194 if (f.getFile().getName().equals(filename)) {
203 * Returns whether a file in the folder is generating a resource of a specified type.
204 * @param type The {@link ResourceType} being looked up.
206 public boolean hasResources(ResourceType type) {
207 // Check if the folder type is able to generate resource of the type that was asked.
208 // this is a first check to avoid going through the files.
209 ResourceFolderType[] folderTypes = FolderTypeRelationship.getRelatedFolders(type);
211 boolean valid = false;
212 for (ResourceFolderType rft : folderTypes) {
220 if (mFiles != null) {
221 for (ResourceFile f : mFiles) {
222 if (f.hasResources(type)) {
232 * Get the list of {@link ResourceItem} of a specific type generated by all the files
234 * This method must make sure not to create duplicates.
235 * @param type The type of {@link ResourceItem} to return.
236 * @param projectResources The global Project Resource object, allowing the implementation to
237 * query for already existing {@link ResourceItem}
238 * @return The list of <b>new</b> {@link ResourceItem}
239 * @see ProjectResources#findResourceItem(ResourceType, String)
241 public Collection<ProjectResourceItem> getResources(ResourceType type,
242 ProjectResources projectResources) {
243 Collection<ProjectResourceItem> list = new ArrayList<ProjectResourceItem>();
244 if (mFiles != null) {
245 for (ResourceFile f : mFiles) {
246 list.addAll(f.getResources(type, projectResources));