KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.jface.operation.ModalContext;
20 import org.eclipse.ui.dialogs.FileSystemElement;
21 import org.eclipse.ui.internal.wizards.datatransfer.DataTransferMessages;
22
23 /**
24  * Operation responsible for traversing a specified file system position
25  * recursively and building
26  * - a tree that represents the container structure
27  * - a collection containing all files meeting a specified extension criteria
28  *
29  * This is implemented as an Operation in order to provide an escape to the user
30  * (the Cancel button) if the operation drags on for too long
31  */

32 public class SelectFilesOperation implements IRunnableWithProgress {
33     IProgressMonitor monitor;
34
35     Object JavaDoc root;
36
37     IImportStructureProvider provider;
38
39     String JavaDoc desiredExtensions[];
40
41     FileSystemElement result;
42
43     /**
44      * Creates a new <code>SelectFilesOperation</code>.
45      */

46     public SelectFilesOperation(Object JavaDoc rootObject,
47             IImportStructureProvider structureProvider) {
48         super();
49         root = rootObject;
50         provider = structureProvider;
51     }
52
53     /**
54      * Creates and returns a <code>FileSystemElement</code> if the specified
55      * file system object merits one. The criteria for this are:
56      * - if the file system object is a container then it must have either a
57      * child container or an associated file
58      * - if the file system object is a file then it must have an extension
59      * suitable for selection
60      */

61     protected FileSystemElement createElement(FileSystemElement parent,
62             Object JavaDoc fileSystemObject) throws InterruptedException JavaDoc {
63         ModalContext.checkCanceled(monitor);
64         boolean isContainer = provider.isFolder(fileSystemObject);
65         String JavaDoc elementLabel = parent == null ? provider
66                 .getFullPath(fileSystemObject) : provider
67                 .getLabel(fileSystemObject);
68
69         if (!isContainer && !hasDesiredExtension(elementLabel)) {
70             return null;
71         }
72
73         FileSystemElement result = new FileSystemElement(elementLabel, parent,
74                 isContainer);
75         result.setFileSystemObject(fileSystemObject);
76
77         if (isContainer) {
78             boolean haveChildOrFile = false;
79             List JavaDoc children = provider.getChildren(fileSystemObject);
80             if (children == null) {
81                 children = new ArrayList JavaDoc(1);
82             }
83             Iterator JavaDoc childrenEnum = children.iterator();
84             while (childrenEnum.hasNext()) {
85                 if (createElement(result, childrenEnum.next()) != null) {
86                     haveChildOrFile = true;
87                 }
88             }
89
90             if (!haveChildOrFile && parent != null) {
91                 parent.removeFolder(result);
92                 result = null;
93             }
94         }
95
96         return result;
97     }
98
99     /**
100      * Returns the extension portion of the passed filename string.
101      */

102     protected String JavaDoc getExtensionFor(String JavaDoc filename) {
103         int nIndex = filename.lastIndexOf('.');
104
105         if (nIndex >= 0) {
106             return filename.substring(nIndex + 1);
107         }
108
109         return "";//$NON-NLS-1$
110

111     }
112
113     /**
114      * Returns the resulting root file system element.
115      */

116     public FileSystemElement getResult() {
117         return result;
118     }
119
120     /**
121      * Returns a boolean indicating whether the extension of the passed filename
122      * is one of the extensions specified as desired by the filter.
123      */

124     protected boolean hasDesiredExtension(String JavaDoc filename) {
125         if (desiredExtensions == null) {
126             return true;
127         }
128
129         int extensionsSize = desiredExtensions.length;
130         for (int i = 0; i < extensionsSize; i++) {
131             if (getExtensionFor(filename)
132                     .equalsIgnoreCase(desiredExtensions[i])) {
133                 return true;
134             }
135         }
136
137         return false;
138     }
139
140     /**
141      * Runs the operation.
142      */

143     public void run(IProgressMonitor monitor) throws InterruptedException JavaDoc {
144         try {
145             this.monitor = monitor;
146             monitor.beginTask(DataTransferMessages.DataTransfer_scanningMatching, IProgressMonitor.UNKNOWN);
147             result = createElement(null, root);
148             if (result == null) {
149                 result = new FileSystemElement(provider.getLabel(root), null,
150                         provider.isFolder(root));
151                 result.setFileSystemObject(root);
152             }
153         } finally {
154             monitor.done();
155         }
156     }
157
158     /**
159      * Sets the file extensions which are desired. A value of <code>null</code>
160      * indicates that all files should be kept regardless of extension.
161      */

162     public void setDesiredExtensions(String JavaDoc[] extensions) {
163         desiredExtensions = extensions;
164     }
165 }
166
Popular Tags