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.internal.resources.manager.files.IAbstractFile; 23 import com.android.ide.eclipse.adt.internal.resources.manager.files.IAbstractFolder; 24 25 import org.eclipse.core.resources.IFile; 26 import org.eclipse.core.resources.IFolder; 27 28 import java.util.ArrayList; 29 import java.util.Collection; 30 31 /** 32 * Resource Folder class. Contains list of {@link ResourceFile}s, 33 * the {@link FolderConfiguration}, and a link to the workspace {@link IFolder} object. 34 */ 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; 41 42 /** 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 48 */ ResourceFolder(ResourceFolderType type, FolderConfiguration config, IAbstractFolder folder, boolean isFrameworkRepository)49 public ResourceFolder(ResourceFolderType type, FolderConfiguration config, 50 IAbstractFolder folder, boolean isFrameworkRepository) { 51 mType = type; 52 mConfiguration = config; 53 mFolder = folder; 54 mIsFramework = isFrameworkRepository; 55 } 56 57 /** 58 * Adds a {@link ResourceFile} to the folder. 59 * @param file The {@link ResourceFile}. 60 */ addFile(ResourceFile file)61 public void addFile(ResourceFile file) { 62 if (mFiles == null) { 63 mFiles = new ArrayList<ResourceFile>(); 64 } 65 66 mFiles.add(file); 67 } 68 69 /** 70 * Attempts to remove the {@link ResourceFile} associated with a specified {@link IFile}. 71 * @param file the IFile object. 72 */ removeFile(IFile file)73 public void removeFile(IFile file) { 74 if (mFiles != null) { 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)) { 81 mFiles.remove(i); 82 touch(); 83 } 84 } 85 } 86 } 87 } 88 89 /** 90 * Returns the {@link IFolder} associated with this object. 91 */ getFolder()92 public IAbstractFolder getFolder() { 93 return mFolder; 94 } 95 96 /** 97 * Returns the {@link ResourceFolderType} of this object. 98 */ getType()99 public ResourceFolderType getType() { 100 return mType; 101 } 102 103 /** 104 * Returns whether the folder is a framework resource folder. 105 */ isFramework()106 public boolean isFramework() { 107 return mIsFramework; 108 } 109 110 /** 111 * Returns the list of {@link ResourceType}s generated by the files inside this folder. 112 */ getResourceTypes()113 public Collection<ResourceType> getResourceTypes() { 114 ArrayList<ResourceType> list = new ArrayList<ResourceType>(); 115 116 if (mFiles != null) { 117 for (ResourceFile file : mFiles) { 118 ResourceType[] types = file.getResourceTypes(); 119 120 // loop through those and add them to the main list, 121 // if they are not already present 122 if (types != null) { 123 for (ResourceType resType : types) { 124 if (list.indexOf(resType) == -1) { 125 list.add(resType); 126 } 127 } 128 } 129 } 130 } 131 132 return list; 133 } 134 135 /* 136 * (non-Javadoc) 137 * @see com.android.ide.eclipse.editors.resources.manager.Resource#getConfiguration() 138 */ 139 @Override getConfiguration()140 public FolderConfiguration getConfiguration() { 141 return mConfiguration; 142 } 143 144 /** 145 * Returns whether the folder contains a file with the given name. 146 * @param name the name of the file. 147 */ hasFile(String name)148 public boolean hasFile(String name) { 149 return mFolder.hasFile(name); 150 } 151 152 /** 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. 156 */ getFile(IAbstractFile file)157 public ResourceFile getFile(IAbstractFile file) { 158 if (mFiles != null) { 159 for (ResourceFile f : mFiles) { 160 if (f.getFile().equals(file)) { 161 return f; 162 } 163 } 164 } 165 return null; 166 } 167 168 /** 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. 172 */ getFile(IFile file)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)) { 178 return f; 179 } 180 } 181 } 182 return null; 183 } 184 185 186 /** 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. 190 */ getFile(String filename)191 public ResourceFile getFile(String filename) { 192 if (mFiles != null) { 193 for (ResourceFile f : mFiles) { 194 if (f.getFile().getName().equals(filename)) { 195 return f; 196 } 197 } 198 } 199 return null; 200 } 201 202 /** 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. 205 */ hasResources(ResourceType type)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); 210 211 boolean valid = false; 212 for (ResourceFolderType rft : folderTypes) { 213 if (rft == mType) { 214 valid = true; 215 break; 216 } 217 } 218 219 if (valid) { 220 if (mFiles != null) { 221 for (ResourceFile f : mFiles) { 222 if (f.hasResources(type)) { 223 return true; 224 } 225 } 226 } 227 } 228 return false; 229 } 230 231 /** 232 * Get the list of {@link ResourceItem} of a specific type generated by all the files 233 * in the folder. 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) 240 */ getResources(ResourceType type, ProjectResources projectResources)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)); 247 } 248 } 249 return list; 250 } 251 } 252