KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > datatransfer > FileSystemStructureProvider


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.wizards.datatransfer;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
21
22 /**
23  * This class provides information regarding the structure and
24  * content of specified file system File objects.
25  */

26 public class FileSystemStructureProvider implements IImportStructureProvider {
27
28     /**
29      * Holds a singleton instance of this class.
30      */

31     public final static FileSystemStructureProvider INSTANCE = new FileSystemStructureProvider();
32
33     /**
34      * Creates an instance of <code>FileSystemStructureProvider</code>.
35      */

36     private FileSystemStructureProvider() {
37         super();
38     }
39
40     /* (non-Javadoc)
41      * Method declared on IImportStructureProvider
42      */

43     public List JavaDoc getChildren(Object JavaDoc element) {
44         File JavaDoc folder = (File JavaDoc) element;
45         String JavaDoc[] children = folder.list();
46         int childrenLength = children == null ? 0 : children.length;
47         List JavaDoc result = new ArrayList JavaDoc(childrenLength);
48
49         for (int i = 0; i < childrenLength; i++) {
50             result.add(new File JavaDoc(folder, children[i]));
51         }
52
53         return result;
54     }
55
56     /* (non-Javadoc)
57      * Method declared on IImportStructureProvider
58      */

59     public InputStream JavaDoc getContents(Object JavaDoc element) {
60         try {
61             return new FileInputStream JavaDoc((File JavaDoc) element);
62         } catch (FileNotFoundException JavaDoc e) {
63             IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
64             return null;
65         }
66     }
67
68     /* (non-Javadoc)
69      * Method declared on IImportStructureProvider
70      */

71     public String JavaDoc getFullPath(Object JavaDoc element) {
72         return ((File JavaDoc) element).getPath();
73     }
74
75     /* (non-Javadoc)
76      * Method declared on IImportStructureProvider
77      */

78     public String JavaDoc getLabel(Object JavaDoc element) {
79
80         //Get the name - if it is empty then return the path as it is a file root
81
File JavaDoc file = (File JavaDoc) element;
82         String JavaDoc name = file.getName();
83         if (name.length() == 0) {
84             return file.getPath();
85         }
86         return name;
87     }
88
89     /* (non-Javadoc)
90      * Method declared on IImportStructureProvider
91      */

92     public boolean isFolder(Object JavaDoc element) {
93         return ((File JavaDoc) element).isDirectory();
94     }
95 }
96
Popular Tags