KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > FileSystemElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.dialogs;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.ui.ISharedImages;
17 import org.eclipse.ui.internal.WorkbenchImages;
18 import org.eclipse.ui.internal.WorkbenchPlugin;
19 import org.eclipse.ui.model.AdaptableList;
20 import org.eclipse.ui.model.IWorkbenchAdapter;
21
22 /**
23  * Instances of this class represent files or file-like entities (eg.- zip file
24  * entries) on the local file system. They do not represent resources within the
25  * workbench. This distinction is made because the representation of a file
26  * system resource is significantly different from that of a workbench resource.
27  *
28  * If self represents a collection (eg.- file system directory, zip directory)
29  * then its icon will be the folderIcon static field. Otherwise (ie.- self
30  * represents a file system file) self's icon is stored in field "icon", and is
31  * determined by the extension of the file that self represents.
32  *
33  * This class is adaptable, and implements one adapter itself, namely the
34  * IWorkbenchAdapter adapter used for navigation and display in the workbench.
35  */

36 public class FileSystemElement implements IAdaptable {
37     private String JavaDoc name;
38
39     private Object JavaDoc fileSystemObject;
40
41     /*
42      * Wait until a child is added to initialize the receiver's lists. Doing so
43      * minimizes the amount of memory that is allocated when a large directory
44      * structure is being processed.
45      */

46     private AdaptableList folders = null;
47
48     private AdaptableList files = null;
49
50     private boolean isDirectory = false;
51
52     private FileSystemElement parent;
53
54     private IWorkbenchAdapter workbenchAdapter = new IWorkbenchAdapter() {
55         /**
56          * Answer the children property of this element
57          */

58         public Object JavaDoc[] getChildren(Object JavaDoc o) {
59             return getFolders().getChildren(o);
60         }
61
62         /**
63          * Returns the parent of this element
64          */

65         public Object JavaDoc getParent(Object JavaDoc o) {
66             return parent;
67         }
68
69         /**
70          * Returns an appropriate label for this file system element.
71          */

72         public String JavaDoc getLabel(Object JavaDoc o) {
73             return name;
74         }
75
76         /**
77          * Returns an image descriptor for this file system element
78          */

79         public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
80             if (isDirectory()) {
81                 return WorkbenchImages
82                         .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
83             } else {
84                 return WorkbenchPlugin.getDefault().getEditorRegistry()
85                         .getImageDescriptor(name);
86                 //TODO: what are the implications for content types? Should I guess?
87
}
88         }
89     };
90
91     /**
92      * Creates a new <code>FileSystemElement</code> and initializes it and its
93      * parent if applicable.
94      *
95      * @param name
96      * The name of the element
97      * @param parent
98      * The parent element. May be <code>null</code>
99      * @param isDirectory
100      * if <code>true</code> this is representing a directory,
101      * otherwise it is a file.
102      */

103     public FileSystemElement(String JavaDoc name, FileSystemElement parent,
104             boolean isDirectory) {
105         this.name = name;
106         this.parent = parent;
107         this.isDirectory = isDirectory;
108         if (parent != null) {
109             parent.addChild(this);
110         }
111     }
112
113     /**
114      * Adds the passed child to this object's collection of children.
115      *
116      * @param child
117      * FileSystemElement
118      */

119     public void addChild(FileSystemElement child) {
120         if (child.isDirectory()) {
121             if (folders == null) {
122                 folders = new AdaptableList(1);
123             }
124             folders.add(child);
125         } else {
126             if (files == null) {
127                 files = new AdaptableList(1);
128             }
129             files.add(child);
130         }
131     }
132
133     /**
134      * Returns the adapter
135      */

136     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
137         if (adapter == IWorkbenchAdapter.class) {
138             return workbenchAdapter;
139         }
140         //defer to the platform
141
return Platform.getAdapterManager().getAdapter(this, adapter);
142     }
143
144     /**
145      * Returns the extension of this element's filename.
146      *
147      * @return The extension or an empty string if there is no extension.
148      */

149     public String JavaDoc getFileNameExtension() {
150         int lastDot = name.lastIndexOf('.');
151         return lastDot < 0 ? "" : name.substring(lastDot + 1); //$NON-NLS-1$
152
}
153
154     /**
155      * Answer the files property of this element. Answer an empty list if the
156      * files property is null. This method should not be used to add children to
157      * the receiver. Use addChild(FileSystemElement) instead.
158      *
159      * @return AdaptableList The list of files parented by the receiver.
160      */

161     public AdaptableList getFiles() {
162         if (files == null) {
163             // lazily initialize (can't share result since it's modifiable)
164
files = new AdaptableList(0);
165         }
166         return files;
167     }
168
169     /**
170      * Returns the file system object property of this element
171      *
172      * @return the file system object
173      */

174     public Object JavaDoc getFileSystemObject() {
175         return fileSystemObject;
176     }
177
178     /**
179      * Returns a list of the folders that are immediate children of this folder.
180      * Answer an empty list if the folders property is null. This method should
181      * not be used to add children to the receiver. Use
182      * addChild(FileSystemElement) instead.
183      *
184      * @return AdapatableList The list of folders parented by the receiver.
185      */

186     public AdaptableList getFolders() {
187         if (folders == null) {
188             // lazily initialize (can't share result since it's modifiable)
189
folders = new AdaptableList(0);
190         }
191         return folders;
192     }
193
194     /**
195      * Return the parent of this element.
196      *
197      * @return the parent file system element, or <code>null</code> if this is
198      * the root
199      */

200     public FileSystemElement getParent() {
201         return this.parent;
202     }
203
204     /**
205      * @return boolean <code>true</code> if this element represents a
206      * directory, and <code>false</code> otherwise.
207      */

208     public boolean isDirectory() {
209         return isDirectory;
210     }
211
212     /**
213      * Removes a sub-folder from this file system element.
214      * @param child The child to remove.
215      */

216     public void removeFolder(FileSystemElement child) {
217         if (folders == null) {
218             return;
219         }
220         folders.remove(child);
221         child.setParent(null);
222     }
223
224     /**
225      * Set the file system object property of this element
226      *
227      * @param value
228      * the file system object
229      */

230     public void setFileSystemObject(Object JavaDoc value) {
231         fileSystemObject = value;
232     }
233
234     /**
235      * Sets the parent of this file system element.
236      * @param element The new parent.
237      */

238     public void setParent(FileSystemElement element) {
239         parent = element;
240     }
241
242     /**
243      * For debugging purposes only.
244      */

245     public String JavaDoc toString() {
246         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
247         if (isDirectory()) {
248             buf.append("Folder(");//$NON-NLS-1$
249
} else {
250             buf.append("File(");//$NON-NLS-1$
251
}
252         buf.append(name).append(")");//$NON-NLS-1$
253
if (!isDirectory()) {
254             return buf.toString();
255         }
256         buf.append(" folders: ");//$NON-NLS-1$
257
buf.append(folders);
258         buf.append(" files: ");//$NON-NLS-1$
259
buf.append(files);
260         return buf.toString();
261     }
262 }
263
Popular Tags