KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
48  * A dialog to select a type from a list of types.
49  */

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         /*
61          * @see FilteredList.FilterMatcher#setFilter(String, boolean)
62          */

63         public void setFilter(String JavaDoc pattern, boolean ignoreCase, boolean igoreWildCards) {
64             int qualifierIndex= pattern.lastIndexOf("."); //$NON-NLS-1$
65

66             // type
67
if (qualifierIndex == -1) {
68                 fQualifierMatcher= null;
69                 fMatcher= new StringMatcher(adjustPattern(pattern), ignoreCase, igoreWildCards);
70                 
71             // qualified type
72
} 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         /*
79          * @see FilteredList.FilterMatcher#match(Object)
80          */

81         public boolean match(Object JavaDoc 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 JavaDoc adjustPattern(String JavaDoc 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     /*
114      * A string comparator which is aware of obfuscated code
115      * (type names starting with lower case characters).
116      */

117     private static class StringComparator implements Comparator JavaDoc {
118         public int compare(Object JavaDoc left, Object JavaDoc right) {
119             String JavaDoc leftString= (String JavaDoc) left;
120             String JavaDoc rightString= (String JavaDoc) 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     /**
143      * Constructs a type selection dialog.
144      * @param parent the parent shell.
145      * @param context the runnable context.
146      * @param elementKinds <code>IJavaSearchConstants.CLASS</code>, <code>IJavaSearchConstants.INTERFACE</code>
147      * or <code>IJavaSearchConstants.TYPE</code>
148      * @param scope the java search scope.
149      */

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")); //$NON-NLS-1$
162
setLowerListLabel(JavaUIMessages.getString("TypeSelectionDialog.lowerLabel")); //$NON-NLS-1$
163
}
164
165     /*
166      * @see AbstractElementListSelectionDialog#createFilteredList(Composite)
167      */

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     /*
178      * @see org.eclipse.jface.window.Window#open()
179      */

180     public int open() {
181         final ArrayList JavaDoc typeList= new ArrayList JavaDoc();
182         try {
183             if (isCacheUpToDate()) {
184                 // run without progress monitor
185
AllTypesCache.getTypes(fScope, fElementKinds, null, typeList);
186             } else {
187                 IRunnableWithProgress runnable= new IRunnableWithProgress() {
188                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
189                         AllTypesCache.getTypes(fScope, fElementKinds, monitor, typeList);
190                         if (monitor.isCanceled()) {
191                             throw new InterruptedException JavaDoc();
192                         }
193                     }
194                 };
195                 fRunnableContext.run(true, true, runnable);
196             }
197         } catch (InvocationTargetException JavaDoc e) {
198             ExceptionHandler.handle(e, JavaUIMessages.getString("TypeSelectionDialog.error3Title"), JavaUIMessages.getString("TypeSelectionDialog.error3Message")); //$NON-NLS-1$ //$NON-NLS-2$
199
return CANCEL;
200         } catch (InterruptedException JavaDoc e) {
201             // cancelled by user
202
return CANCEL;
203         }
204             
205         if (typeList.isEmpty()) {
206             String JavaDoc title= JavaUIMessages.getString("TypeSelectionDialog.notypes.title"); //$NON-NLS-1$
207
String JavaDoc message= JavaUIMessages.getString("TypeSelectionDialog.notypes.message"); //$NON-NLS-1$
208
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     /*
219      * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
220      */

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                 // not a class file or compilation unit
231
String JavaDoc title= JavaUIMessages.getString("TypeSelectionDialog.errorTitle"); //$NON-NLS-1$
232
String JavaDoc message= JavaUIMessages.getFormattedString("TypeSelectionDialog.dialogMessage", ref.getPath()); //$NON-NLS-1$
233
MessageDialog.openError(getShell(), title, message);
234                 setResult(null);
235             } else {
236                 List JavaDoc result= new ArrayList JavaDoc(1);
237                 result.add(type);
238                 setResult(result);
239             }
240
241         } catch (JavaModelException e) {
242             String JavaDoc title= JavaUIMessages.getString("TypeSelectionDialog.errorTitle"); //$NON-NLS-1$
243
String JavaDoc message= JavaUIMessages.getString("TypeSelectionDialog.errorMessage"); //$NON-NLS-1$
244
ErrorDialog.openError(getShell(), title, message, e.getStatus());
245             setResult(null);
246         }
247     }
248     
249     private boolean isCacheUpToDate() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
250         final boolean result[]= new boolean[1];
251         IRunnableWithProgress runnable= new IRunnableWithProgress() {
252             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
253                 try {
254                     result[0]= AllTypesCache.isCacheUpToDate(monitor);
255                 } catch (OperationCanceledException e) {
256                     throw new InterruptedException JavaDoc(e.getMessage());
257                 }
258             }
259         };
260         PlatformUI.getWorkbench().getProgressService().run(true, true, runnable);
261         return result[0];
262     }
263 }
264
Popular Tags