KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > core > sourcelookup > containers > DirectorySourceContainer


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.debug.core.sourcelookup.containers;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
20 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
21
22 /**
23  * A directory in the local file system. Source elements returned
24  * from <code>findSourceElements(...)</code> are instances
25  * of <code>LocalFileStorage</code>.
26  * <p>
27  * Clients may instantiate this class. This class is not intended to
28  * be subclassed.
29  * </p>
30  * @since 3.0
31  */

32
33 public class DirectorySourceContainer extends CompositeSourceContainer {
34     
35     // root directory
36
private File JavaDoc fDirectory;
37     // whether to search sub-folders
38
private boolean fSubfolders = false;
39     /**
40      * Unique identifier for the directory source container type
41      * (value <code>org.eclipse.debug.core.containerType.directory</code>).
42      */

43     public static final String JavaDoc TYPE_ID = DebugPlugin.getUniqueIdentifier() + ".containerType.directory"; //$NON-NLS-1$
44

45     /**
46      * Constructs an external folder container for the
47      * directory identified by the given path.
48      *
49      * @param dirPath path to a directory in the local file system
50      * @param subfolders whether folders within the root directory
51      * should be searched for source elements
52      */

53     public DirectorySourceContainer(IPath dirPath, boolean subfolders) {
54         this(dirPath.toFile(), subfolders);
55     }
56     
57     /**
58      * Constructs an external folder container for the
59      * directory identified by the given file.
60      *
61      * @param dir a directory in the local file system
62      * @param subfolders whether folders within the root directory
63      * should be searched for source elements
64      */

65     public DirectorySourceContainer(File JavaDoc dir, boolean subfolders) {
66         fDirectory = dir;
67         fSubfolders = subfolders;
68     }
69         
70     /* (non-Javadoc)
71      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getName()
72      */

73     public String JavaDoc getName() {
74         return fDirectory.getName();
75     }
76     
77     /**
78      * Returns the root directory in the local file system associated
79      * with this source container.
80      *
81      * @return the root directory in the local file system associated
82      * with this source container
83      */

84     public File JavaDoc getDirectory() {
85         return fDirectory;
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
90      */

91     public ISourceContainerType getType() {
92         return getSourceContainerType(TYPE_ID);
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
97      */

98     public Object JavaDoc[] findSourceElements(String JavaDoc name) throws CoreException {
99         ArrayList JavaDoc sources = new ArrayList JavaDoc();
100         File JavaDoc directory = getDirectory();
101         File JavaDoc file = new File JavaDoc(directory, name);
102         if (file.exists() && file.isFile()) {
103             sources.add(new LocalFileStorage(file));
104         }
105         
106         //check sub-folders
107
if ((isFindDuplicates() && fSubfolders) || (sources.isEmpty() && fSubfolders)) {
108             ISourceContainer[] containers = getSourceContainers();
109             for (int i=0; i < containers.length; i++) {
110                 Object JavaDoc[] objects = containers[i].findSourceElements(name);
111                 if (objects == null || objects.length == 0) {
112                     continue;
113                 }
114                 if (isFindDuplicates()) {
115                     for(int j=0; j < objects.length; j++)
116                         sources.add(objects[j]);
117                 } else {
118                     sources.add(objects[0]);
119                     break;
120                 }
121             }
122         }
123         
124         if(sources.isEmpty())
125             return EMPTY;
126         return sources.toArray();
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#isComposite()
131      */

132     public boolean isComposite() {
133         return fSubfolders;
134     }
135
136     /* (non-Javadoc)
137      * @see java.lang.Object#equals(java.lang.Object)
138      */

139     public boolean equals(Object JavaDoc obj) {
140         if (obj instanceof DirectorySourceContainer) {
141             DirectorySourceContainer container = (DirectorySourceContainer) obj;
142             return container.getDirectory().equals(getDirectory());
143         }
144         return false;
145     }
146     /* (non-Javadoc)
147      * @see java.lang.Object#hashCode()
148      */

149     public int hashCode() {
150         return getDirectory().hashCode();
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
155      */

156     protected ISourceContainer[] createSourceContainers() throws CoreException {
157         if (isComposite()) {
158             String JavaDoc[] files = fDirectory.list();
159             if (files != null) {
160                 List JavaDoc dirs = new ArrayList JavaDoc();
161                 for (int i = 0; i < files.length; i++) {
162                     String JavaDoc name = files[i];
163                     File JavaDoc file = new File JavaDoc(getDirectory(), name);
164                     if (file.exists() && file.isDirectory()) {
165                         dirs.add(new DirectorySourceContainer(file, true));
166                     }
167                 }
168                 ISourceContainer[] containers = (ISourceContainer[]) dirs.toArray(new ISourceContainer[dirs.size()]);
169                 for (int i = 0; i < containers.length; i++) {
170                     ISourceContainer container = containers[i];
171                     container.init(getDirector());
172                 }
173                 return containers;
174             }
175         }
176         return new ISourceContainer[0];
177     }
178
179 }
180
Popular Tags