KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > MinimizedFileSystemElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.preferences;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.model.IWorkbenchAdapter;
25 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
26
27 class MinimizedFileSystemElement implements IWorkbenchAdapter, IAdaptable {
28     private boolean populated = false;
29     private List JavaDoc folders = null;
30     private List JavaDoc files = null;
31     private String JavaDoc name;
32     private boolean isDirectory = false;
33     private MinimizedFileSystemElement parent;
34     private Object JavaDoc fileSystemObject;
35     
36     /**
37      * Create a <code>MinimizedFileSystemElement</code> with the supplied name and parent.
38      * @param name the name of the file element this represents
39      * @param parent the containing parent
40      * @param isDirectory indicated if this could have children or not
41      */

42     public MinimizedFileSystemElement(String JavaDoc name, MinimizedFileSystemElement parent, boolean isDirectory) {
43         this.name = name;
44         this.parent = parent;
45         this.isDirectory = isDirectory;
46         if (parent != null) {
47             parent.addChild(this);
48         }
49     }
50     
51     /**
52      * Returns the adapter
53      */

54     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
55         if (adapter == IWorkbenchAdapter.class) {
56             return this;
57         }
58         //defer to the platform
59
return Platform.getAdapterManager().getAdapter(this, adapter);
60     }
61     
62     /**
63      * Returns true if this element represents a directory, and false
64      * otherwise.
65      */

66     public boolean isDirectory() {
67         return isDirectory;
68     }
69     
70     /**
71      * Adds the passed child to this object's collection of children.
72      *
73      * @param child MinimizedFileSystemElement
74      */

75     private void addChild(MinimizedFileSystemElement child) {
76         if (child.isDirectory()) {
77             if (folders == null) {
78                  folders = new ArrayList JavaDoc(1);
79             }
80             folders.add(child);
81         } else {
82             if (files == null) {
83                  files = new ArrayList JavaDoc(1);
84             }
85             files.add(child);
86         }
87     }
88     /**
89      * Returns a list of the files that are immediate children. Use the supplied provider
90      * if it needs to be populated.
91      */

92     protected List JavaDoc getFiles(IImportStructureProvider provider) {
93         if (!populated) {
94             populate(provider);
95         }
96
97         if (files == null) {
98              return Collections.EMPTY_LIST;
99         }
100         return files;
101
102     }
103     /**
104      * Returns a list of the folders that are immediate children. Use the supplied provider
105      * if it needs to be populated.
106      */

107     protected List JavaDoc getFolders(IImportStructureProvider provider) {
108         if (!populated) {
109             populate(provider);
110         }
111
112         return getFolders();
113
114     }
115
116     protected List JavaDoc getFolders() {
117         if (folders == null){
118              return Collections.EMPTY_LIST;
119         }
120         return folders;
121     }
122     /**
123      * Return whether or not population has happened for the receiver.
124      */

125     protected boolean isPopulated() {
126         return this.populated;
127     }
128     /**
129      * Return whether or not population has not happened for the receiver.
130      */

131     protected boolean notPopulated() {
132         return !this.populated;
133     }
134     /**
135      * Populate the files and folders of the receiver using the supplied
136      * structure provider.
137      * @param provider org.eclipse.ui.wizards.datatransfer.IImportStructureProvider
138      */

139     private void populate(IImportStructureProvider provider) {
140
141         List JavaDoc children = provider.getChildren(fileSystemObject);
142         if (children == null) {
143             children = new ArrayList JavaDoc(1);
144         }
145         Iterator JavaDoc childrenEnum = children.iterator();
146         while (childrenEnum.hasNext()) {
147             Object JavaDoc child = childrenEnum.next();
148
149             String JavaDoc elementLabel = provider.getLabel(child);
150             boolean isFolder= provider.isFolder(child);
151             if (!isFolder && !elementLabel.endsWith(".class")) { //$NON-NLS-1$
152
continue;
153             }
154             //Create one level below
155
MinimizedFileSystemElement result = new MinimizedFileSystemElement(elementLabel, this, isFolder);
156             result.setFileSystemObject(child);
157         }
158         setPopulated();
159     }
160     
161     /**
162      * Returns the file system object property of this element
163      *
164      * @return the file system object
165      */

166     protected Object JavaDoc getFileSystemObject() {
167         return fileSystemObject;
168     }
169     
170     /**
171      * Set the file system object property of this element
172      *
173      * @param value the file system object
174      */

175     protected void setFileSystemObject(Object JavaDoc value) {
176         fileSystemObject = value;
177     }
178     /**
179      * Set whether or not population has happened for the receiver to true.
180      */

181     protected void setPopulated() {
182         this.populated = true;
183     }
184     /**
185      * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
186      */

187     public Object JavaDoc[] getChildren(Object JavaDoc o) {
188         return getFolders().toArray();
189     }
190
191     /**
192      * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
193      */

194     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
195         if (isDirectory()) {
196             return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
197         }
198         return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(name);
199     }
200
201     /**
202      * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
203      */

204     public String JavaDoc getLabel(Object JavaDoc o) {
205         return name;
206     }
207
208     /**
209      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
210      */

211     public Object JavaDoc getParent(Object JavaDoc o) {
212         return parent;
213     }
214
215 }
216
217
Popular Tags