KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > utils > DialogSelectionHelper


1 /*
2  * Created on 2004-10-13
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package org.hibernate.eclipse.console.utils;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
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 /**
35  * @author max
36  *
37  */

38 public class DialogSelectionHelper {
39
40     /**
41      *
42      * Shows the UI to select new JAR or ZIP archive entries located in the workspace.
43      * The dialog returns the selected entries or <code>null</code> if the dialog has
44      * been cancelled. The dialog does not apply any changes.
45      * @param shell The parent shell for the dialog.
46      * @param initialSelection The path of the element (container or archive) to initially select or <code>null</code> to not select an entry.
47      * @param usedEntries An array of paths that are already on the classpath and therefore should not be
48      * selected again.
49      * @param fileExtensions An array of file extensions.
50      * @param allowMultiple allow multiple selections.
51      * @param allowFiles TODO
52      * @param acceptedTypes TODO
53      *
54      * @return Returns the new classpath container entry paths or <code>null</code> if the dialog has
55      * been cancelled by the user.
56      *
57      * Inspired by BuildPathDialogAccess.chooseJAREntries from jdt.ui.wizards
58      */

59     public static IPath[] chooseFileEntries(Shell shell, IPath initialSelection, IPath[] usedEntries, String JavaDoc title, String JavaDoc description, String JavaDoc[] fileExtensions, boolean allowMultiple, boolean allowDirectories, boolean allowFiles ) {
60         if (usedEntries == null) {
61             throw new IllegalArgumentException JavaDoc("used entries must be not-null");
62         }
63             
64         List JavaDoc clazzes = new ArrayList JavaDoc();
65         if(allowDirectories) {
66             clazzes.add(IFolder.class);
67         }
68         if(allowFiles) {
69             clazzes.add(IFile.class);
70         }
71         Class JavaDoc[] acceptedClasses = (Class JavaDoc[]) clazzes.toArray(new Class JavaDoc[clazzes.size()]);
72                 
73         TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, true);
74         ArrayList JavaDoc usedFiles= new ArrayList JavaDoc(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 JavaDoc[] 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     /**
107      * Realize a Java Project selection dialog and return the first selected project,
108      * or null if there was none.
109      */

110     public static IJavaProject chooseJavaProject(Shell shell, IJavaProject initialSelection, String JavaDoc title, String JavaDoc 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 JavaDoc[] { javaProject });
128         }
129         if (dialog.open() == Window.OK) {
130             return (IJavaProject) dialog.getFirstResult();
131         }
132         return null;
133     }
134     
135 }
136
Popular Tags