01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.wizards.datatransfer;
11:
12: import java.io.File;
13: import java.io.FileInputStream;
14: import java.io.FileNotFoundException;
15: import java.io.InputStream;
16: import java.util.ArrayList;
17: import java.util.List;
18:
19: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
20:
21: /**
22: * This class provides information regarding the structure and
23: * content of specified file system File objects.
24: */
25: public class FileSystemStructureProvider implements
26: 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 getChildren(Object element) {
44: File folder = (File) element;
45: String[] children = folder.list();
46: int childrenLength = children == null ? 0 : children.length;
47: List result = new ArrayList(childrenLength);
48:
49: for (int i = 0; i < childrenLength; i++) {
50: result.add(new File(folder, children[i]));
51: }
52:
53: return result;
54: }
55:
56: /* (non-Javadoc)
57: * Method declared on IImportStructureProvider
58: */
59: public InputStream getContents(Object element) {
60: try {
61: return new FileInputStream((File) element);
62: } catch (FileNotFoundException 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 getFullPath(Object element) {
72: return ((File) element).getPath();
73: }
74:
75: /* (non-Javadoc)
76: * Method declared on IImportStructureProvider
77: */
78: public String getLabel(Object element) {
79:
80: //Get the name - if it is empty then return the path as it is a file root
81: File file = (File) element;
82: String 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 element) {
93: return ((File) element).isDirectory();
94: }
95: }
|