1 /*
2  * Copyright (C) 2008 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.io;
18 
19 import com.android.io.IAbstractFile;
20 import com.android.io.IAbstractFolder;
21 import com.android.io.IAbstractResource;
22 
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IFolder;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.NullProgressMonitor;
29 import org.eclipse.core.runtime.Path;
30 
31 import java.util.ArrayList;
32 
33 /**
34  * An implementation of {@link IAbstractFolder} on top of either an {@link IFolder} or an
35  * {@link IContainer} object.
36  */
37 public class IFolderWrapper implements IAbstractFolder {
38 
39     private final IFolder mFolder; // could be null.
40     private final IContainer mContainer; // never null.
41 
IFolderWrapper(IFolder folder)42     public IFolderWrapper(IFolder folder) {
43         mContainer = mFolder = folder;
44     }
45 
IFolderWrapper(IContainer container)46     public IFolderWrapper(IContainer container) {
47         mFolder = container instanceof IFolder ? (IFolder)container : null;
48         mContainer = container;
49     }
50 
51     @Override
getName()52     public String getName() {
53         return mContainer.getName();
54     }
55 
56     @Override
exists()57     public boolean exists() {
58         return mContainer.exists();
59     }
60 
61     @Override
delete()62     public boolean delete() {
63         try {
64             mContainer.delete(true /*force*/, new NullProgressMonitor());
65             return true;
66         } catch (CoreException e) {
67             return false;
68         }
69     }
70 
71 
72     @Override
listMembers()73     public IAbstractResource[] listMembers() {
74         try {
75             IResource[] members = mContainer.members();
76             final int count = members.length;
77             IAbstractResource[] afiles = new IAbstractResource[count];
78 
79             for (int i = 0 ; i < count ; i++) {
80                 IResource f = members[i];
81                 if (f instanceof IFile) {
82                     afiles[i] = new IFileWrapper((IFile) f);
83                 } else {
84                     afiles[i] = new IFolderWrapper((IContainer) f);
85                 }
86             }
87 
88             return afiles;
89         } catch (CoreException e) {
90             // return empty array below
91         }
92 
93         return new IAbstractResource[0];
94     }
95 
96     @Override
hasFile(String name)97     public boolean hasFile(String name) {
98         try {
99             IResource[] files = mContainer.members();
100             for (IResource file : files) {
101                 if (name.equals(file.getName())) {
102                     return true;
103                 }
104             }
105         } catch (CoreException e) {
106             // we'll return false below.
107         }
108 
109         return false;
110     }
111 
112     @Override
getFile(String name)113     public IAbstractFile getFile(String name) {
114         if (mFolder != null) {
115             IFile file = mFolder.getFile(name);
116             return new IFileWrapper(file);
117         }
118 
119         IFile file = mContainer.getFile(new Path(name));
120         return new IFileWrapper(file);
121     }
122 
123     /**
124      * Returns the {@link IFolder} object that the receiver could represent.
125      * Can be <code>null</code>
126      */
getIFolder()127     public IFolder getIFolder() {
128         return mFolder;
129     }
130 
131     @Override
equals(Object obj)132     public boolean equals(Object obj) {
133         if (obj instanceof IFolderWrapper) {
134             return mFolder.equals(((IFolderWrapper)obj).mFolder);
135         }
136 
137         if (obj instanceof IFolder) {
138             return mFolder.equals(obj);
139         }
140 
141         return super.equals(obj);
142     }
143 
144     @Override
hashCode()145     public int hashCode() {
146         return mContainer.hashCode();
147     }
148 
149     @Override
getFolder(String name)150     public IAbstractFolder getFolder(String name) {
151         if (mFolder != null) {
152             IFolder folder = mFolder.getFolder(name);
153             return new IFolderWrapper(folder);
154         }
155 
156         IFolder folder = mContainer.getFolder(new Path(name));
157         return new IFolderWrapper(folder);
158     }
159 
160     @Override
getOsLocation()161     public String getOsLocation() {
162         return mContainer.getLocation().toOSString();
163     }
164 
165     @Override
list(FilenameFilter filter)166     public String[] list(FilenameFilter filter) {
167         try {
168             IResource[] members = mContainer.members();
169             if (members.length > 0) {
170                 ArrayList<String> list = new ArrayList<String>();
171                 for (IResource res : members) {
172                     if (filter.accept(this, res.getName())) {
173                         list.add(res.getName());
174                     }
175                 }
176 
177                 return list.toArray(new String[list.size()]);
178             }
179         } catch (CoreException e) {
180             // can't read the members? return empty list below.
181         }
182 
183         return new String[0];
184     }
185 
186     @Override
getParentFolder()187     public IAbstractFolder getParentFolder() {
188         IContainer p = mContainer.getParent();
189         if (p != null) {
190             return new IFolderWrapper(p);
191         }
192 
193         return null;
194     }
195 
196     @Override
toString()197     public String toString() {
198         return mFolder.toString();
199     }
200 }
201