1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import org.eclipse.core.filesystem.IFileStore; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.NullProgressMonitor; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.viewers.ITreeContentProvider; 19 import org.eclipse.jface.viewers.LabelProvider; 20 import org.eclipse.jface.viewers.Viewer; 21 import org.eclipse.jface.viewers.ViewerComparator; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.ui.ISharedImages; 25 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 27 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 28 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 29 30 36 public class FileFolderSelectionDialog extends ElementTreeSelectionDialog { 37 38 41 private static class FileLabelProvider extends LabelProvider { 42 private static final Image IMG_FOLDER = PlatformUI.getWorkbench() 43 .getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); 44 45 private static final Image IMG_FILE = PlatformUI.getWorkbench() 46 .getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE); 47 48 53 public Image getImage(Object element) { 54 if (element instanceof IFileStore) { 55 IFileStore curr = (IFileStore) element; 56 if (curr.fetchInfo().isDirectory()) { 57 return IMG_FOLDER; 58 } 59 return IMG_FILE; 60 } 61 return null; 62 } 63 64 69 public String getText(Object element) { 70 if (element instanceof IFileStore) { 71 return ((IFileStore) element).getName(); 72 } 73 return super.getText(element); 74 } 75 } 76 77 80 private static class FileContentProvider implements ITreeContentProvider { 81 private static final Object [] EMPTY = new Object [0]; 82 83 private IFileStoreFilter fileFilter; 84 85 92 public FileContentProvider(final boolean showFiles) { 93 fileFilter = new IFileStoreFilter() { 94 95 100 public boolean accept(IFileStore file) { 101 if (!file.fetchInfo().isDirectory() && showFiles == false) { 102 return false; 103 } 104 return true; 105 } 106 }; 107 } 108 109 public Object [] getChildren(Object parentElement) { 110 if (parentElement instanceof IFileStore) { 111 IFileStore[] children = IDEResourceInfoUtils.listFileStores( 112 (IFileStore) parentElement, fileFilter, 113 new NullProgressMonitor()); 114 if (children != null) { 115 return children; 116 } 117 } 118 return EMPTY; 119 } 120 121 126 public Object getParent(Object element) { 127 if (element instanceof IFileStore) { 128 return ((IFileStore) element).getParent(); 129 } 130 return null; 131 } 132 133 public boolean hasChildren(Object element) { 134 return getChildren(element).length > 0; 135 } 136 137 public Object [] getElements(Object element) { 138 return getChildren(element); 139 } 140 141 public void dispose() { 142 } 143 144 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 145 } 146 } 147 148 151 private static class FileViewerSorter extends ViewerComparator { 152 157 public int category(Object element) { 158 if (element instanceof IFileStore 159 && !((IFileStore) element).fetchInfo().isDirectory()) { 160 return 1; 161 } 162 return 0; 163 } 164 } 165 166 169 private static class FileSelectionValidator implements 170 ISelectionStatusValidator { 171 private boolean multiSelect; 172 173 private boolean acceptFolders; 174 175 185 public FileSelectionValidator(boolean multiSelect, boolean acceptFolders) { 186 this.multiSelect = multiSelect; 187 this.acceptFolders = acceptFolders; 188 } 189 190 195 public IStatus validate(Object [] selection) { 196 int nSelected = selection.length; 197 String pluginId = IDEWorkbenchPlugin.IDE_WORKBENCH; 198 199 if (nSelected == 0 || (nSelected > 1 && multiSelect == false)) { 200 return new Status(IStatus.ERROR, pluginId, IStatus.ERROR, 201 IDEResourceInfoUtils.EMPTY_STRING, null); 202 } 203 for (int i = 0; i < selection.length; i++) { 204 Object curr = selection[i]; 205 if (curr instanceof IFileStore) { 206 IFileStore file = (IFileStore) curr; 207 if (acceptFolders == false 208 && file.fetchInfo().isDirectory()) { 209 return new Status(IStatus.ERROR, pluginId, 210 IStatus.ERROR, 211 IDEResourceInfoUtils.EMPTY_STRING, null); 212 } 213 214 } 215 } 216 return Status.OK_STATUS; 217 } 218 } 219 220 235 public FileFolderSelectionDialog(Shell parent, boolean multiSelect, int type) { 236 super(parent, new FileLabelProvider(), new FileContentProvider( 237 (type & IResource.FILE) != 0)); 238 setComparator(new FileViewerSorter()); 239 setValidator(new FileSelectionValidator(multiSelect, 240 (type & IResource.FOLDER) != 0)); 241 } 242 } 243 | Popular Tags |