001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.wizards.datatransfer;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.operation.IRunnableWithProgress;
018: import org.eclipse.jface.operation.ModalContext;
019: import org.eclipse.ui.dialogs.FileSystemElement;
020: import org.eclipse.ui.internal.wizards.datatransfer.DataTransferMessages;
021:
022: /**
023: * Operation responsible for traversing a specified file system position
024: * recursively and building
025: * - a tree that represents the container structure
026: * - a collection containing all files meeting a specified extension criteria
027: *
028: * This is implemented as an Operation in order to provide an escape to the user
029: * (the Cancel button) if the operation drags on for too long
030: */
031: public class SelectFilesOperation implements IRunnableWithProgress {
032: IProgressMonitor monitor;
033:
034: Object root;
035:
036: IImportStructureProvider provider;
037:
038: String desiredExtensions[];
039:
040: FileSystemElement result;
041:
042: /**
043: * Creates a new <code>SelectFilesOperation</code>.
044: */
045: public SelectFilesOperation(Object rootObject,
046: IImportStructureProvider structureProvider) {
047: super ();
048: root = rootObject;
049: provider = structureProvider;
050: }
051:
052: /**
053: * Creates and returns a <code>FileSystemElement</code> if the specified
054: * file system object merits one. The criteria for this are:
055: * - if the file system object is a container then it must have either a
056: * child container or an associated file
057: * - if the file system object is a file then it must have an extension
058: * suitable for selection
059: */
060: protected FileSystemElement createElement(FileSystemElement parent,
061: Object fileSystemObject) throws InterruptedException {
062: ModalContext.checkCanceled(monitor);
063: boolean isContainer = provider.isFolder(fileSystemObject);
064: String elementLabel = parent == null ? provider
065: .getFullPath(fileSystemObject) : provider
066: .getLabel(fileSystemObject);
067:
068: if (!isContainer && !hasDesiredExtension(elementLabel)) {
069: return null;
070: }
071:
072: FileSystemElement result = new FileSystemElement(elementLabel,
073: parent, isContainer);
074: result.setFileSystemObject(fileSystemObject);
075:
076: if (isContainer) {
077: boolean haveChildOrFile = false;
078: List children = provider.getChildren(fileSystemObject);
079: if (children == null) {
080: children = new ArrayList(1);
081: }
082: Iterator childrenEnum = children.iterator();
083: while (childrenEnum.hasNext()) {
084: if (createElement(result, childrenEnum.next()) != null) {
085: haveChildOrFile = true;
086: }
087: }
088:
089: if (!haveChildOrFile && parent != null) {
090: parent.removeFolder(result);
091: result = null;
092: }
093: }
094:
095: return result;
096: }
097:
098: /**
099: * Returns the extension portion of the passed filename string.
100: */
101: protected String getExtensionFor(String filename) {
102: int nIndex = filename.lastIndexOf('.');
103:
104: if (nIndex >= 0) {
105: return filename.substring(nIndex + 1);
106: }
107:
108: return "";//$NON-NLS-1$
109:
110: }
111:
112: /**
113: * Returns the resulting root file system element.
114: */
115: public FileSystemElement getResult() {
116: return result;
117: }
118:
119: /**
120: * Returns a boolean indicating whether the extension of the passed filename
121: * is one of the extensions specified as desired by the filter.
122: */
123: protected boolean hasDesiredExtension(String filename) {
124: if (desiredExtensions == null) {
125: return true;
126: }
127:
128: int extensionsSize = desiredExtensions.length;
129: for (int i = 0; i < extensionsSize; i++) {
130: if (getExtensionFor(filename).equalsIgnoreCase(
131: desiredExtensions[i])) {
132: return true;
133: }
134: }
135:
136: return false;
137: }
138:
139: /**
140: * Runs the operation.
141: */
142: public void run(IProgressMonitor monitor)
143: throws InterruptedException {
144: try {
145: this .monitor = monitor;
146: monitor.beginTask(
147: DataTransferMessages.DataTransfer_scanningMatching,
148: IProgressMonitor.UNKNOWN);
149: result = createElement(null, root);
150: if (result == null) {
151: result = new FileSystemElement(provider.getLabel(root),
152: null, provider.isFolder(root));
153: result.setFileSystemObject(root);
154: }
155: } finally {
156: monitor.done();
157: }
158: }
159:
160: /**
161: * Sets the file extensions which are desired. A value of <code>null</code>
162: * indicates that all files should be kept regardless of extension.
163: */
164: public void setDesiredExtensions(String[] extensions) {
165: desiredExtensions = extensions;
166: }
167: }
|