1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.io.File ; 14 15 import org.eclipse.core.runtime.IStatus; 16 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.swt.widgets.Shell; 19 20 import org.eclipse.jface.viewers.ITreeContentProvider; 21 import org.eclipse.jface.viewers.LabelProvider; 22 import org.eclipse.jface.viewers.Viewer; 23 import org.eclipse.jface.viewers.ViewerComparator; 24 import org.eclipse.jface.viewers.ViewerFilter; 25 26 import org.eclipse.ui.ISharedImages; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 29 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 30 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 import org.eclipse.jdt.internal.ui.JavaPluginImages; 33 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 34 35 39 public class JARFileSelectionDialog extends ElementTreeSelectionDialog { 40 41 47 public JARFileSelectionDialog(Shell parent, boolean multiSelect, boolean acceptFolders) { 48 super(parent, new FileLabelProvider(), new FileContentProvider()); 49 setComparator(new FileViewerComparator()); 50 addFilter(new FileArchiveFileFilter(acceptFolders)); 51 setValidator(new FileSelectionValidator(multiSelect, acceptFolders)); 52 setHelpAvailable(false); 53 } 54 55 private static boolean isArchive(File file) { 56 String name= file.getName(); 57 int detIndex= name.lastIndexOf('.'); 58 return (detIndex != -1 && ArchiveFileFilter.isArchiveFileExtension(name.substring(detIndex + 1))); 59 } 60 61 private static class FileLabelProvider extends LabelProvider { 62 private final Image IMG_FOLDER= PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); 63 private final Image IMG_JAR= JavaPlugin.getDefault().getImageRegistry().get(JavaPluginImages.IMG_OBJS_EXTJAR); 64 65 public Image getImage(Object element) { 66 if (element instanceof File ) { 67 File curr= (File ) element; 68 if (curr.isDirectory()) { 69 return IMG_FOLDER; 70 } else { 71 return IMG_JAR; 72 } 73 } 74 return null; 75 } 76 77 public String getText(Object element) { 78 if (element instanceof File ) { 79 return ((File ) element).getName(); 80 } 81 return super.getText(element); 82 } 83 } 84 85 private static class FileContentProvider implements ITreeContentProvider { 86 87 private final Object [] EMPTY= new Object [0]; 88 89 public Object [] getChildren(Object parentElement) { 90 if (parentElement instanceof File ) { 91 File [] children= ((File ) parentElement).listFiles(); 92 if (children != null) { 93 return children; 94 } 95 } 96 return EMPTY; 97 } 98 99 public Object getParent(Object element) { 100 if (element instanceof File ) { 101 return ((File ) element).getParentFile(); 102 } 103 return null; 104 } 105 106 public boolean hasChildren(Object element) { 107 return getChildren(element).length > 0; 108 } 109 110 public Object [] getElements(Object element) { 111 return getChildren(element); 112 } 113 114 public void dispose() { 115 } 116 117 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 118 } 119 120 } 121 122 private static class FileArchiveFileFilter extends ViewerFilter { 123 private final boolean fAcceptFolders; 124 125 public FileArchiveFileFilter(boolean acceptFolders) { 126 fAcceptFolders= acceptFolders; 127 } 128 129 public boolean select(Viewer viewer, Object parent, Object element) { 130 if (element instanceof File ) { 131 File file= (File ) element; 132 if (file.isFile()) { 133 return isArchive(file); 134 } else if (fAcceptFolders) { 135 return true; 136 } else { 137 File [] listFiles= file.listFiles(); 138 if (listFiles != null) { 139 for (int i= 0; i < listFiles.length; i++) { 140 if (select(viewer, file, listFiles[i])) { 141 return true; 142 } 143 } 144 } 145 } 146 } 147 return false; 148 } 149 } 150 151 private static class FileViewerComparator extends ViewerComparator { 152 public int category(Object element) { 153 if (element instanceof File ) { 154 if (((File ) element).isFile()) { 155 return 1; 156 } 157 } 158 return 0; 159 } 160 } 161 162 private static class FileSelectionValidator implements ISelectionStatusValidator { 163 private boolean fMultiSelect; 164 private boolean fAcceptFolders; 165 166 public FileSelectionValidator(boolean multiSelect, boolean acceptFolders) { 167 fMultiSelect= multiSelect; 168 fAcceptFolders= acceptFolders; 169 } 170 171 public IStatus validate(Object [] selection) { 172 int nSelected= selection.length; 173 if (nSelected == 0 || (nSelected > 1 && !fMultiSelect)) { 174 return new StatusInfo(IStatus.ERROR, ""); } 176 for (int i= 0; i < selection.length; i++) { 177 Object curr= selection[i]; 178 if (curr instanceof File ) { 179 File file= (File ) curr; 180 if (!fAcceptFolders && !file.isFile()) { 181 return new StatusInfo(IStatus.ERROR, ""); } 183 } 184 } 185 return new StatusInfo(); 186 } 187 } 188 189 190 } 191 | Popular Tags |