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.Comparator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.jface.operation.IRunnableContext; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.util.Assert; 29 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.dialogs.FilteredList; 32 import org.eclipse.ui.dialogs.TwoPaneElementSelector; 33 34 import org.eclipse.jdt.core.IType; 35 import org.eclipse.jdt.core.JavaModelException; 36 import org.eclipse.jdt.core.search.IJavaSearchScope; 37 38 import org.eclipse.jdt.internal.corext.util.AllTypesCache; 39 import org.eclipse.jdt.internal.corext.util.Strings; 40 import org.eclipse.jdt.internal.corext.util.TypeInfo; 41 42 import org.eclipse.jdt.internal.ui.JavaUIMessages; 43 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 44 import org.eclipse.jdt.internal.ui.util.StringMatcher; 45 import org.eclipse.jdt.internal.ui.util.TypeInfoLabelProvider; 46 47 50 public class TypeSelectionDialog extends TwoPaneElementSelector { 51 52 private static class TypeFilterMatcher implements FilteredList.FilterMatcher { 53 54 private static final char END_SYMBOL= '<'; 55 private static final char ANY_STRING= '*'; 56 57 private StringMatcher fMatcher; 58 private StringMatcher fQualifierMatcher; 59 60 63 public void setFilter(String pattern, boolean ignoreCase, boolean igoreWildCards) { 64 int qualifierIndex= pattern.lastIndexOf("."); 66 if (qualifierIndex == -1) { 68 fQualifierMatcher= null; 69 fMatcher= new StringMatcher(adjustPattern(pattern), ignoreCase, igoreWildCards); 70 71 } else { 73 fQualifierMatcher= new StringMatcher(pattern.substring(0, qualifierIndex), ignoreCase, igoreWildCards); 74 fMatcher= new StringMatcher(adjustPattern(pattern.substring(qualifierIndex + 1)), ignoreCase, igoreWildCards); 75 } 76 } 77 78 81 public boolean match(Object element) { 82 if (!(element instanceof TypeInfo)) 83 return false; 84 85 TypeInfo type= (TypeInfo) element; 86 87 if (!fMatcher.match(type.getTypeName())) 88 return false; 89 90 if (fQualifierMatcher == null) 91 return true; 92 93 return fQualifierMatcher.match(type.getTypeContainerName()); 94 } 95 96 private String adjustPattern(String pattern) { 97 int length= pattern.length(); 98 if (length > 0) { 99 switch (pattern.charAt(length - 1)) { 100 case END_SYMBOL: 101 pattern= pattern.substring(0, length - 1); 102 break; 103 case ANY_STRING: 104 break; 105 default: 106 pattern= pattern + ANY_STRING; 107 } 108 } 109 return pattern; 110 } 111 } 112 113 117 private static class StringComparator implements Comparator { 118 public int compare(Object left, Object right) { 119 String leftString= (String ) left; 120 String rightString= (String ) right; 121 122 if (Strings.isLowerCase(leftString.charAt(0)) && 123 !Strings.isLowerCase(rightString.charAt(0))) 124 return +1; 125 126 if (Strings.isLowerCase(rightString.charAt(0)) && 127 !Strings.isLowerCase(leftString.charAt(0))) 128 return -1; 129 130 int result= leftString.compareToIgnoreCase(rightString); 131 if (result == 0) 132 result= leftString.compareTo(rightString); 133 134 return result; 135 } 136 } 137 138 private IRunnableContext fRunnableContext; 139 private IJavaSearchScope fScope; 140 private int fElementKinds; 141 142 150 public TypeSelectionDialog(Shell parent, IRunnableContext context, int elementKinds, IJavaSearchScope scope) { 151 super(parent, new TypeInfoLabelProvider(TypeInfoLabelProvider.SHOW_TYPE_ONLY), 152 new TypeInfoLabelProvider(TypeInfoLabelProvider.SHOW_TYPE_CONTAINER_ONLY + TypeInfoLabelProvider.SHOW_ROOT_POSTFIX)); 153 154 Assert.isNotNull(context); 155 Assert.isNotNull(scope); 156 157 fRunnableContext= context; 158 fScope= scope; 159 fElementKinds= elementKinds; 160 161 setUpperListLabel(JavaUIMessages.getString("TypeSelectionDialog.upperLabel")); setLowerListLabel(JavaUIMessages.getString("TypeSelectionDialog.lowerLabel")); } 164 165 168 protected FilteredList createFilteredList(Composite parent) { 169 FilteredList list= super.createFilteredList(parent); 170 171 fFilteredList.setFilterMatcher(new TypeFilterMatcher()); 172 fFilteredList.setComparator(new StringComparator()); 173 174 return list; 175 } 176 177 180 public int open() { 181 final ArrayList typeList= new ArrayList (); 182 try { 183 if (isCacheUpToDate()) { 184 AllTypesCache.getTypes(fScope, fElementKinds, null, typeList); 186 } else { 187 IRunnableWithProgress runnable= new IRunnableWithProgress() { 188 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 189 AllTypesCache.getTypes(fScope, fElementKinds, monitor, typeList); 190 if (monitor.isCanceled()) { 191 throw new InterruptedException (); 192 } 193 } 194 }; 195 fRunnableContext.run(true, true, runnable); 196 } 197 } catch (InvocationTargetException e) { 198 ExceptionHandler.handle(e, JavaUIMessages.getString("TypeSelectionDialog.error3Title"), JavaUIMessages.getString("TypeSelectionDialog.error3Message")); return CANCEL; 200 } catch (InterruptedException e) { 201 return CANCEL; 203 } 204 205 if (typeList.isEmpty()) { 206 String title= JavaUIMessages.getString("TypeSelectionDialog.notypes.title"); String message= JavaUIMessages.getString("TypeSelectionDialog.notypes.message"); MessageDialog.openInformation(getShell(), title, message); 209 return CANCEL; 210 } 211 212 TypeInfo[] typeRefs= (TypeInfo[])typeList.toArray(new TypeInfo[typeList.size()]); 213 setElements(typeRefs); 214 215 return super.open(); 216 } 217 218 221 protected void computeResult() { 222 TypeInfo ref= (TypeInfo) getLowerSelectedElement(); 223 224 if (ref == null) 225 return; 226 227 try { 228 IType type= ref.resolveType(fScope); 229 if (type == null) { 230 String title= JavaUIMessages.getString("TypeSelectionDialog.errorTitle"); String message= JavaUIMessages.getFormattedString("TypeSelectionDialog.dialogMessage", ref.getPath()); MessageDialog.openError(getShell(), title, message); 234 setResult(null); 235 } else { 236 List result= new ArrayList (1); 237 result.add(type); 238 setResult(result); 239 } 240 241 } catch (JavaModelException e) { 242 String title= JavaUIMessages.getString("TypeSelectionDialog.errorTitle"); String message= JavaUIMessages.getString("TypeSelectionDialog.errorMessage"); ErrorDialog.openError(getShell(), title, message, e.getStatus()); 245 setResult(null); 246 } 247 } 248 249 private boolean isCacheUpToDate() throws InvocationTargetException , InterruptedException { 250 final boolean result[]= new boolean[1]; 251 IRunnableWithProgress runnable= new IRunnableWithProgress() { 252 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 253 try { 254 result[0]= AllTypesCache.isCacheUpToDate(monitor); 255 } catch (OperationCanceledException e) { 256 throw new InterruptedException (e.getMessage()); 257 } 258 } 259 }; 260 PlatformUI.getWorkbench().getProgressService().run(true, true, runnable); 261 return result[0]; 262 } 263 } 264 | Popular Tags |