1 7 package org.hibernate.eclipse.console.utils; 8 9 import java.util.ArrayList ; 10 import java.util.List ; 11 12 import org.eclipse.core.resources.IFile; 13 import org.eclipse.core.resources.IFolder; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.IWorkspaceRoot; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.jdt.core.IJavaProject; 19 import org.eclipse.jdt.core.JavaCore; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 22 import org.eclipse.jdt.ui.JavaElementLabelProvider; 23 import org.eclipse.jface.viewers.ILabelProvider; 24 import org.eclipse.jface.window.Window; 25 import org.eclipse.swt.widgets.Shell; 26 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 27 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 28 import org.eclipse.ui.model.WorkbenchContentProvider; 29 import org.eclipse.ui.model.WorkbenchLabelProvider; 30 import org.eclipse.ui.views.navigator.ResourceSorter; 31 import org.hibernate.eclipse.console.FileFilter; 32 import org.hibernate.eclipse.console.HibernateConsolePlugin; 33 34 38 public class DialogSelectionHelper { 39 40 59 public static IPath[] chooseFileEntries(Shell shell, IPath initialSelection, IPath[] usedEntries, String title, String description, String [] fileExtensions, boolean allowMultiple, boolean allowDirectories, boolean allowFiles ) { 60 if (usedEntries == null) { 61 throw new IllegalArgumentException ("used entries must be not-null"); 62 } 63 64 List clazzes = new ArrayList (); 65 if(allowDirectories) { 66 clazzes.add(IFolder.class); 67 } 68 if(allowFiles) { 69 clazzes.add(IFile.class); 70 } 71 Class [] acceptedClasses = (Class []) clazzes.toArray(new Class [clazzes.size()]); 72 73 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, true); 74 ArrayList usedFiles= new ArrayList (usedEntries.length); 75 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 76 for (int i= 0; i < usedEntries.length; i++) { 77 IResource resource= root.findMember(usedEntries[i]); 78 if (resource instanceof IFile) { 79 usedFiles.add(resource); 80 } 81 } 82 IResource focus= initialSelection != null ? root.findMember(initialSelection) : null; 83 84 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider()); 85 dialog.setValidator(validator); 86 dialog.setAllowMultiple(allowMultiple); 87 dialog.setTitle(title); 88 dialog.setMessage(description); 89 dialog.addFilter(new FileFilter(fileExtensions, usedFiles, true, allowDirectories)); 90 dialog.setInput(root); 91 dialog.setSorter(new ResourceSorter(ResourceSorter.NAME)); 92 dialog.setInitialSelection(focus); 93 94 if (dialog.open() == Window.OK) { 95 Object [] elements= dialog.getResult(); 96 IPath[] res= new IPath[elements.length]; 97 for (int i= 0; i < res.length; i++) { 98 IResource elem= (IResource)elements[i]; 99 res[i]= elem.getFullPath(); 100 } 101 return res; 102 } 103 return null; 104 } 105 106 110 public static IJavaProject chooseJavaProject(Shell shell, IJavaProject initialSelection, String title, String description) { 111 IJavaProject[] projects; 112 try { 113 projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); 114 } catch (JavaModelException e) { 115 HibernateConsolePlugin.getDefault().log(e.getStatus()); 116 projects= new IJavaProject[0]; 117 } 118 119 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 120 ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, labelProvider); 121 dialog.setTitle(title); 122 dialog.setMessage(description); 123 dialog.setElements(projects); 124 125 IJavaProject javaProject = initialSelection; 126 if (javaProject != null) { 127 dialog.setInitialSelections(new Object [] { javaProject }); 128 } 129 if (dialog.open() == Window.OK) { 130 return (IJavaProject) dialog.getFirstResult(); 131 } 132 return null; 133 } 134 135 } 136 | Popular Tags |