1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 32 public class SelectFilesOperation implements IRunnableWithProgress { 33 IProgressMonitor monitor; 34 35 Object root; 36 37 IImportStructureProvider provider; 38 39 String desiredExtensions[]; 40 41 FileSystemElement result; 42 43 46 public SelectFilesOperation(Object rootObject, 47 IImportStructureProvider structureProvider) { 48 super(); 49 root = rootObject; 50 provider = structureProvider; 51 } 52 53 61 protected FileSystemElement createElement(FileSystemElement parent, 62 Object fileSystemObject) throws InterruptedException { 63 ModalContext.checkCanceled(monitor); 64 boolean isContainer = provider.isFolder(fileSystemObject); 65 String 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 children = provider.getChildren(fileSystemObject); 80 if (children == null) { 81 children = new ArrayList (1); 82 } 83 Iterator 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 102 protected String getExtensionFor(String filename) { 103 int nIndex = filename.lastIndexOf('.'); 104 105 if (nIndex >= 0) { 106 return filename.substring(nIndex + 1); 107 } 108 109 return ""; 111 } 112 113 116 public FileSystemElement getResult() { 117 return result; 118 } 119 120 124 protected boolean hasDesiredExtension(String 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 143 public void run(IProgressMonitor monitor) throws InterruptedException { 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 162 public void setDesiredExtensions(String [] extensions) { 163 desiredExtensions = extensions; 164 } 165 } 166 | Popular Tags |