1 11 package org.eclipse.jdt.internal.ui.dialogs; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 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 47 public class MultiTypeSelectionDialog extends ElementListSelectionDialog { 48 49 private IRunnableContext fRunnableContext; 50 private IJavaSearchScope fScope; 51 private int fElementKinds; 52 53 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 78 public int open() { 79 80 final ArrayList typesFound= new ArrayList (); 81 IRunnableWithProgress runnable= new IRunnableWithProgress() { 82 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 83 AllTypesCache.getTypes(fScope, fElementKinds, monitor, typesFound); 84 if (monitor.isCanceled()) { 85 throw new InterruptedException (); 86 } 87 } 88 }; 89 90 try { 91 fRunnableContext.run(true, true, runnable); 92 } catch (InvocationTargetException e) { 93 ExceptionHandler.handle(e, JavaUIMessages.getString("MultiTypeSelectionDialog.error2Title"), JavaUIMessages.getString("MultiTypeSelectionDialog.error2Message")); } catch (InterruptedException e) { 95 return CANCEL; 97 } 98 99 setElements(typesFound.toArray()); 100 101 return super.open(); 102 } 103 104 107 protected void computeResult() { 108 List selection= Arrays.asList(getSelectedElements()); int size= selection.size(); 110 if (size == 0) { 111 setResult(null); 112 return; 113 } 114 115 List result= new ArrayList (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 title= JavaUIMessages.getString("MultiTypeSelectionDialog.dialogTitle"); String message= JavaUIMessages.getFormattedString("MultiTypeSelectionDialog.dialogMessage", typeInfo.getPath()); MessageDialog.openError(getShell(), title, message); 125 } else { 126 result.add(type); 127 } 128 } catch (JavaModelException e) { 129 String title= JavaUIMessages.getString("MultiTypeSelectionDialog.errorTitle"); String message= JavaUIMessages.getString("MultiTypeSelectionDialog.errorMessage"); ErrorDialog.openError(getShell(), title, message, e.getStatus()); 132 } 133 } 134 } 135 setResult(result); 136 } 137 138 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 |