KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > MultiTypeSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.dialogs;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.operation.IRunnableContext;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.util.Assert;
27
28 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
29 import org.eclipse.ui.help.WorkbenchHelp;
30
31 import org.eclipse.jdt.core.IType;
32 import org.eclipse.jdt.core.JavaModelException;
33 import org.eclipse.jdt.core.search.IJavaSearchScope;
34
35 import org.eclipse.jdt.internal.corext.util.AllTypesCache;
36 import org.eclipse.jdt.internal.corext.util.TypeInfo;
37
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.JavaUIMessages;
40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41 import org.eclipse.jdt.internal.ui.util.TypeInfoLabelProvider;
42
43 /**
44  * A dialog to select a type from a list of types. The dialog allows
45  * multiple selections.
46  */

47 public class MultiTypeSelectionDialog extends ElementListSelectionDialog {
48
49     private IRunnableContext fRunnableContext;
50     private IJavaSearchScope fScope;
51     private int fElementKinds;
52     
53     /**
54      * Constructs an instance of <code>MultiTypeSelectionDialog</code>.
55      * @param parent the parent shell.
56      * @param context the context.
57      * @param elementKinds IJavaSearchConstants.CLASS, IJavaSearchConstants.INTERFACE
58      * or IJavaSearchConstants.TYPE
59      * @param scope the java search scope.
60      */

61     public MultiTypeSelectionDialog(Shell parent, IRunnableContext context, int elementKinds, IJavaSearchScope scope)
62     {
63         super(parent, new TypeInfoLabelProvider(TypeInfoLabelProvider.SHOW_PACKAGE_POSTFIX));
64             
65         setMultipleSelection(true);
66
67         Assert.isNotNull(context);
68         Assert.isNotNull(scope);
69
70         fRunnableContext= context;
71         fScope= scope;
72         fElementKinds= elementKinds;
73     }
74
75     /*
76      * @see Window#open()
77      */

78     public int open() {
79         
80         final ArrayList JavaDoc typesFound= new ArrayList JavaDoc();
81         IRunnableWithProgress runnable= new IRunnableWithProgress() {
82             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
83                 AllTypesCache.getTypes(fScope, fElementKinds, monitor, typesFound);
84                 if (monitor.isCanceled()) {
85                     throw new InterruptedException JavaDoc();
86                 }
87             }
88         };
89         
90         try {
91             fRunnableContext.run(true, true, runnable);
92         } catch (InvocationTargetException JavaDoc e) {
93             ExceptionHandler.handle(e, JavaUIMessages.getString("MultiTypeSelectionDialog.error2Title"), JavaUIMessages.getString("MultiTypeSelectionDialog.error2Message")); //$NON-NLS-1$ //$NON-NLS-2$
94
} catch (InterruptedException JavaDoc e) {
95             // cancelled by user
96
return CANCEL;
97         }
98
99         setElements(typesFound.toArray());
100         
101         return super.open();
102     }
103     
104     /*
105      * @see SelectionStatusDialog#computeResult()
106      */

107     protected void computeResult() {
108         List JavaDoc selection= Arrays.asList(getSelectedElements()); // XXX inefficient
109
int size= selection.size();
110         if (size == 0) {
111             setResult(null);
112             return;
113         }
114         
115         List JavaDoc result= new ArrayList JavaDoc(size);
116         if (result != null) {
117             for (int i= 0; i < size; i++) {
118                 try {
119                     TypeInfo typeInfo= (TypeInfo)selection.get(i);
120                     IType type= typeInfo.resolveType(fScope);
121                     if (type == null) {
122                         String JavaDoc title= JavaUIMessages.getString("MultiTypeSelectionDialog.dialogTitle"); //$NON-NLS-1$
123
String JavaDoc message= JavaUIMessages.getFormattedString("MultiTypeSelectionDialog.dialogMessage", typeInfo.getPath()); //$NON-NLS-1$
124
MessageDialog.openError(getShell(), title, message);
125                     } else {
126                         result.add(type);
127                     }
128                 } catch (JavaModelException e) {
129                     String JavaDoc title= JavaUIMessages.getString("MultiTypeSelectionDialog.errorTitle"); //$NON-NLS-1$
130
String JavaDoc message= JavaUIMessages.getString("MultiTypeSelectionDialog.errorMessage"); //$NON-NLS-1$
131
ErrorDialog.openError(getShell(), title, message, e.getStatus());
132                 }
133             }
134         }
135         setResult(result);
136     }
137     
138     /*
139      * @see org.eclipse.jface.window.Window#configureShell(Shell)
140      */

141     protected void configureShell(Shell newShell) {
142         super.configureShell(newShell);
143         WorkbenchHelp.setHelp(newShell, IJavaHelpContextIds.MULTI_TYPE_SELECTION_DIALOG);
144     }
145
146
147 }
148
Popular Tags