1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 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 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 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. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.resources.manager; 18 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.io.IFileWrapper; 23 import com.android.sdklib.io.IAbstractFile; 24 import com.android.sdklib.io.IAbstractFolder; 25 26 import org.eclipse.core.resources.IFile; 27 import org.eclipse.core.resources.IFolder; 28 29 import java.util.ArrayList; 30 import java.util.Collection; 31 32 /** 33 * Resource Folder class. Contains list of {@link ResourceFile}s, 34 * the {@link FolderConfiguration}, and a link to the workspace {@link IFolder} object. 35 */ 36 public final class ResourceFolder extends Resource { 37 ResourceFolderType mType; 38 FolderConfiguration mConfiguration; 39 IAbstractFolder mFolder; 40 ArrayList<ResourceFile> mFiles = null; 41 private final boolean mIsFramework; 42 43 /** 44 * Creates a new {@link ResourceFolder} 45 * @param type The type of the folder 46 * @param config The configuration of the folder 47 * @param folder The associated {@link IAbstractFolder} object. 48 * @param isFrameworkRepository 49 */ ResourceFolder(ResourceFolderType type, FolderConfiguration config, IAbstractFolder folder, boolean isFrameworkRepository)50 public ResourceFolder(ResourceFolderType type, FolderConfiguration config, 51 IAbstractFolder folder, boolean isFrameworkRepository) { 52 mType = type; 53 mConfiguration = config; 54 mFolder = folder; 55 mIsFramework = isFrameworkRepository; 56 } 57 58 /** 59 * Adds a {@link ResourceFile} to the folder. 60 * @param file The {@link ResourceFile}. 61 */ addFile(ResourceFile file)62 public void addFile(ResourceFile file) { 63 if (mFiles == null) { 64 mFiles = new ArrayList<ResourceFile>(); 65 } 66 67 mFiles.add(file); 68 } 69 70 /** 71 * Attempts to remove the {@link ResourceFile} associated with a specified {@link IFile}. 72 * @param file the IFile object. 73 * @return the {@link ResourceFile} that was removed. 74 */ removeFile(IFile file)75 public ResourceFile removeFile(IFile file) { 76 if (mFiles != null) { 77 int count = mFiles.size(); 78 for (int i = 0 ; i < count ; i++) { 79 ResourceFile resFile = mFiles.get(i); 80 if (resFile != null) { 81 IAbstractFile abstractFile = resFile.getFile(); 82 if (abstractFile instanceof IFileWrapper) { 83 IFile iFile = ((IFileWrapper)resFile.getFile()).getIFile(); 84 if (iFile != null && iFile.equals(file)) { 85 mFiles.remove(i); 86 touch(); 87 return resFile; 88 } 89 } 90 } 91 } 92 } 93 94 return null; 95 } 96 97 /** 98 * Returns the {@link IFolder} associated with this object. 99 */ getFolder()100 public IAbstractFolder getFolder() { 101 return mFolder; 102 } 103 104 /** 105 * Returns the {@link ResourceFolderType} of this object. 106 */ getType()107 public ResourceFolderType getType() { 108 return mType; 109 } 110 111 /** 112 * Returns whether the folder is a framework resource folder. 113 */ isFramework()114 public boolean isFramework() { 115 return mIsFramework; 116 } 117 118 /** 119 * Returns the list of {@link ResourceType}s generated by the files inside this folder. 120 */ getResourceTypes()121 public Collection<ResourceType> getResourceTypes() { 122 ArrayList<ResourceType> list = new ArrayList<ResourceType>(); 123 124 if (mFiles != null) { 125 for (ResourceFile file : mFiles) { 126 ResourceType[] types = file.getResourceTypes(); 127 128 // loop through those and add them to the main list, 129 // if they are not already present 130 if (types != null) { 131 for (ResourceType resType : types) { 132 if (list.indexOf(resType) == -1) { 133 list.add(resType); 134 } 135 } 136 } 137 } 138 } 139 140 return list; 141 } 142 143 /* 144 * (non-Javadoc) 145 * @see com.android.ide.eclipse.editors.resources.manager.Resource#getConfiguration() 146 */ 147 @Override getConfiguration()148 public FolderConfiguration getConfiguration() { 149 return mConfiguration; 150 } 151 152 /** 153 * Returns whether the folder contains a file with the given name. 154 * @param name the name of the file. 155 */ hasFile(String name)156 public boolean hasFile(String name) { 157 return mFolder.hasFile(name); 158 } 159 160 /** 161 * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object. 162 * @param file The {@link IFile} object. 163 * @return the {@link ResourceFile} or null if no match was found. 164 */ getFile(IAbstractFile file)165 public ResourceFile getFile(IAbstractFile file) { 166 if (mFiles != null) { 167 for (ResourceFile f : mFiles) { 168 if (f.getFile().equals(file)) { 169 return f; 170 } 171 } 172 } 173 return null; 174 } 175 176 /** 177 * Returns the {@link ResourceFile} matching a {@link IFile} object. 178 * @param file The {@link IFile} object. 179 * @return the {@link ResourceFile} or null if no match was found. 180 */ getFile(IFile file)181 public ResourceFile getFile(IFile file) { 182 if (mFiles != null) { 183 for (ResourceFile f : mFiles) { 184 IAbstractFile abstractFile = f.getFile(); 185 if (abstractFile instanceof IFileWrapper) { 186 IFile iFile = ((IFileWrapper)f.getFile()).getIFile(); 187 if (iFile != null && iFile.equals(file)) { 188 return f; 189 } 190 } 191 } 192 } 193 return null; 194 } 195 196 197 /** 198 * Returns the {@link ResourceFile} matching a given name. 199 * @param filename The name of the file to return. 200 * @return the {@link ResourceFile} or <code>null</code> if no match was found. 201 */ getFile(String filename)202 public ResourceFile getFile(String filename) { 203 if (mFiles != null) { 204 for (ResourceFile f : mFiles) { 205 if (f.getFile().getName().equals(filename)) { 206 return f; 207 } 208 } 209 } 210 return null; 211 } 212 213 /** 214 * Returns whether a file in the folder is generating a resource of a specified type. 215 * @param type The {@link ResourceType} being looked up. 216 */ hasResources(ResourceType type)217 public boolean hasResources(ResourceType type) { 218 // Check if the folder type is able to generate resource of the type that was asked. 219 // this is a first check to avoid going through the files. 220 ResourceFolderType[] folderTypes = FolderTypeRelationship.getRelatedFolders(type); 221 222 boolean valid = false; 223 for (ResourceFolderType rft : folderTypes) { 224 if (rft == mType) { 225 valid = true; 226 break; 227 } 228 } 229 230 if (valid) { 231 if (mFiles != null) { 232 for (ResourceFile f : mFiles) { 233 if (f.hasResources(type)) { 234 return true; 235 } 236 } 237 } 238 } 239 return false; 240 } 241 242 /** 243 * Get the list of {@link ResourceItem} of a specific type generated by all the files 244 * in the folder. 245 * This method must make sure not to create duplicates. 246 * @param type The type of {@link ResourceItem} to return. 247 * @param projectResources The global Project Resource object, allowing the implementation to 248 * query for already existing {@link ResourceItem} 249 * @return The list of <b>new</b> {@link ResourceItem} 250 * @see ProjectResources#findResourceItem(ResourceType, String) 251 */ getResources(ResourceType type, ProjectResources projectResources)252 public Collection<ProjectResourceItem> getResources(ResourceType type, 253 ProjectResources projectResources) { 254 Collection<ProjectResourceItem> list = new ArrayList<ProjectResourceItem>(); 255 if (mFiles != null) { 256 for (ResourceFile f : mFiles) { 257 list.addAll(f.getResources(type, projectResources)); 258 } 259 } 260 return list; 261 } 262 } 263