KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21 import org.eclipse.core.runtime.jobs.IJobManager;
22 import org.eclipse.core.runtime.jobs.Job;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Shell;
31
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.operation.IRunnableContext;
34 import org.eclipse.jface.operation.IRunnableWithProgress;
35 import org.eclipse.jface.viewers.ISelection;
36
37 import org.eclipse.jface.text.ITextSelection;
38
39 import org.eclipse.ui.IWorkbenchWindow;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
42 import org.eclipse.ui.dialogs.SelectionStatusDialog;
43
44 import org.eclipse.jdt.core.IPackageFragmentRoot;
45 import org.eclipse.jdt.core.IType;
46 import org.eclipse.jdt.core.JavaConventions;
47 import org.eclipse.jdt.core.JavaCore;
48 import org.eclipse.jdt.core.JavaModelException;
49 import org.eclipse.jdt.core.search.IJavaSearchConstants;
50 import org.eclipse.jdt.core.search.IJavaSearchScope;
51 import org.eclipse.jdt.core.search.SearchEngine;
52 import org.eclipse.jdt.core.search.SearchPattern;
53 import org.eclipse.jdt.core.search.TypeNameMatch;
54 import org.eclipse.jdt.core.search.TypeNameRequestor;
55
56 import org.eclipse.jdt.internal.corext.util.Messages;
57 import org.eclipse.jdt.internal.corext.util.OpenTypeHistory;
58
59 import org.eclipse.jdt.ui.JavaElementLabels;
60 import org.eclipse.jdt.ui.JavaUI;
61 import org.eclipse.jdt.ui.dialogs.TypeSelectionExtension;
62
63 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
64 import org.eclipse.jdt.internal.ui.JavaPlugin;
65 import org.eclipse.jdt.internal.ui.JavaUIMessages;
66 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
67
68 /**
69  * @deprecated use {@link FilteredTypesSelectionDialog}
70  */

71 public class TypeSelectionDialog2 extends SelectionStatusDialog {
72
73     private String JavaDoc fTitle;
74     
75     private boolean fMultipleSelection;
76     private IRunnableContext fRunnableContext;
77     private IJavaSearchScope fScope;
78     private int fElementKind;
79     
80     private String JavaDoc fInitialFilter;
81     private int fSelectionMode;
82     private ISelectionStatusValidator fValidator;
83     private TypeSelectionComponent fContent;
84     private TypeSelectionExtension fExtension;
85     
86     public static final int NONE= TypeSelectionComponent.NONE;
87     public static final int CARET_BEGINNING= TypeSelectionComponent.CARET_BEGINNING;
88     public static final int FULL_SELECTION= TypeSelectionComponent.FULL_SELECTION;
89     
90     private static boolean fgFirstTime= true;
91     
92     private class TitleLabel implements TypeSelectionComponent.ITitleLabel {
93         public void setText(String JavaDoc text) {
94             if (text == null || text.length() == 0) {
95                 getShell().setText(fTitle);
96             } else {
97                 getShell().setText(Messages.format(
98                     JavaUIMessages.TypeSelectionDialog2_title_format,
99                     new String JavaDoc[] { fTitle, text}));
100             }
101         }
102     }
103     
104     public TypeSelectionDialog2(Shell parent, boolean multi, IRunnableContext context,
105             IJavaSearchScope scope, int elementKinds) {
106         this(parent, multi, context, scope, elementKinds, null);
107     }
108     
109     public TypeSelectionDialog2(Shell parent, boolean multi, IRunnableContext context,
110             IJavaSearchScope scope, int elementKinds, TypeSelectionExtension extension) {
111         super(parent);
112         setShellStyle(getShellStyle() | SWT.RESIZE);
113         fMultipleSelection= multi;
114         fRunnableContext= context;
115         fScope= scope;
116         fElementKind= elementKinds;
117         fSelectionMode= NONE;
118         fExtension= extension;
119         if (fExtension != null) {
120             fValidator= fExtension.getSelectionValidator();
121         }
122     }
123     
124     public void setFilter(String JavaDoc filter) {
125         setFilter(filter, FULL_SELECTION);
126     }
127     
128     public void setFilter(String JavaDoc filter, int selectionMode) {
129         fInitialFilter= filter;
130         fSelectionMode= selectionMode;
131     }
132     
133     public void setValidator(ISelectionStatusValidator validator) {
134         fValidator= validator;
135     }
136     
137     protected TypeNameMatch[] getSelectedTypes() {
138         if (fContent == null || fContent.isDisposed())
139             return null;
140         return fContent.getSelection();
141     }
142     
143     public void create() {
144         super.create();
145         fContent.populate(fSelectionMode);
146         getOkButton().setEnabled(fContent.getSelection().length > 0);
147     }
148     
149     protected void configureShell(Shell shell) {
150         super.configureShell(shell);
151         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IJavaHelpContextIds.TYPE_SELECTION_DIALOG2);
152     }
153     
154     protected Control createDialogArea(Composite parent) {
155         Composite area= (Composite)super.createDialogArea(parent);
156         fContent= new TypeSelectionComponent(area, SWT.NONE, getMessage(),
157             fMultipleSelection, fScope, fElementKind, fInitialFilter,
158             new TitleLabel(), fExtension);
159         GridData gd= new GridData(GridData.FILL_BOTH);
160         fContent.setLayoutData(gd);
161         fContent.addSelectionListener(new SelectionListener() {
162             public void widgetDefaultSelected(SelectionEvent e) {
163                 handleDefaultSelected(fContent.getSelection());
164             }
165             public void widgetSelected(SelectionEvent e) {
166                 handleWidgetSelected(fContent.getSelection());
167             }
168         });
169         return area;
170     }
171     
172     protected void handleDefaultSelected(TypeNameMatch[] selection) {
173         if (selection.length == 0)
174             return;
175         okPressed();
176     }
177     
178     protected void handleWidgetSelected(TypeNameMatch[] selection) {
179         IStatus status= null;
180         if (selection.length == 0) {
181             status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR, "",null); //$NON-NLS-1$
182
} else {
183             if (fValidator != null) {
184                 List JavaDoc jElements= new ArrayList JavaDoc();
185                 for (int i= 0; i < selection.length; i++) {
186                     IType type= selection[i].getType();
187                     if (type != null) {
188                         jElements.add(type);
189                     } else {
190                         status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR,
191                             Messages.format(JavaUIMessages.TypeSelectionDialog_error_type_doesnot_exist, selection[i].getFullyQualifiedName()),
192                             null);
193                         break;
194                     }
195                 }
196                 if (status == null) {
197                     status= fValidator.validate(jElements.toArray());
198                 }
199             } else {
200                 status= new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "",null); //$NON-NLS-1$
201
}
202         }
203         updateStatus(status);
204     }
205     
206     public int open() {
207         try {
208             ensureConsistency();
209         } catch (InvocationTargetException JavaDoc e) {
210             ExceptionHandler.handle(e, JavaUIMessages.TypeSelectionDialog_error3Title, JavaUIMessages.TypeSelectionDialog_error3Message);
211             return CANCEL;
212         } catch (InterruptedException JavaDoc e) {
213             // cancelled by user
214
return CANCEL;
215         }
216         if (fInitialFilter == null) {
217             IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
218             if (window != null) {
219                 ISelection selection= window.getSelectionService().getSelection();
220                 if (selection instanceof ITextSelection) {
221                     String JavaDoc text= ((ITextSelection)selection).getText();
222                     if (text != null) {
223                         text= text.trim();
224                         if (text.length() > 0 && JavaConventions.validateJavaTypeName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3).isOK()) {
225                             fInitialFilter= text;
226                             fSelectionMode= FULL_SELECTION;
227                         }
228                     }
229                 }
230             }
231         }
232         return super.open();
233     }
234     
235     public boolean close() {
236         boolean result;
237         try {
238             if (getReturnCode() == OK) {
239                 OpenTypeHistory.getInstance().save();
240             }
241         } finally {
242             result= super.close();
243         }
244         return result;
245     }
246     
247     public void setTitle(String JavaDoc title) {
248         super.setTitle(title);
249         fTitle= title;
250     }
251     
252     protected void computeResult() {
253         TypeNameMatch[] selected= fContent.getSelection();
254         if (selected == null || selected.length == 0) {
255             setResult(null);
256             return;
257         }
258         
259         // If the scope is null then it got computed by the type selection component.
260
if (fScope == null) {
261             fScope= fContent.getScope();
262         }
263         
264         OpenTypeHistory history= OpenTypeHistory.getInstance();
265         List JavaDoc result= new ArrayList JavaDoc(selected.length);
266         for (int i= 0; i < selected.length; i++) {
267             TypeNameMatch typeInfo= selected[i];
268             IType type= typeInfo.getType();
269             if (!type.exists()) {
270                 String JavaDoc title= JavaUIMessages.TypeSelectionDialog_errorTitle;
271                 IPackageFragmentRoot root= typeInfo.getPackageFragmentRoot();
272                 String JavaDoc containerName= JavaElementLabels.getElementLabel(root, JavaElementLabels.ROOT_QUALIFIED);
273                 String JavaDoc message= Messages.format(JavaUIMessages.TypeSelectionDialog_dialogMessage, new String JavaDoc[] { typeInfo.getFullyQualifiedName(), containerName });
274                 MessageDialog.openError(getShell(), title, message);
275                 history.remove(typeInfo);
276                 setResult(null);
277             } else {
278                 history.accessed(typeInfo);
279                 result.add(type);
280             }
281         }
282         setResult(result);
283     }
284     
285     private void ensureConsistency() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
286         // we only have to ensure history consistency here since the search engine
287
// takes care of working copies.
288
class ConsistencyRunnable implements IRunnableWithProgress {
289             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
290                 if (fgFirstTime) {
291                     // Join the initialize after load job.
292
IJobManager manager= Job.getJobManager();
293                     manager.join(JavaUI.ID_PLUGIN, monitor);
294                 }
295                 OpenTypeHistory history= OpenTypeHistory.getInstance();
296                 if (fgFirstTime || history.isEmpty()) {
297                     monitor.beginTask(JavaUIMessages.TypeSelectionDialog_progress_consistency, 100);
298                     if (history.needConsistencyCheck()) {
299                         refreshSearchIndices(new SubProgressMonitor(monitor, 90));
300                         history.checkConsistency(new SubProgressMonitor(monitor, 10));
301                     } else {
302                         refreshSearchIndices(monitor);
303                     }
304                     monitor.done();
305                     fgFirstTime= false;
306                 } else {
307                     history.checkConsistency(monitor);
308                 }
309             }
310             public boolean needsExecution() {
311                 OpenTypeHistory history= OpenTypeHistory.getInstance();
312                 return fgFirstTime || history.isEmpty() || history.needConsistencyCheck();
313             }
314             private void refreshSearchIndices(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
315                 try {
316                     new SearchEngine().searchAllTypeNames(
317                         null,
318                         0,
319                         // make sure we search a concrete name. This is faster according to Kent
320
"_______________".toCharArray(), //$NON-NLS-1$
321
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
322                         IJavaSearchConstants.ENUM,
323                         SearchEngine.createWorkspaceScope(),
324                         new TypeNameRequestor() {},
325                         IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
326                         monitor);
327                 } catch (JavaModelException e) {
328                     throw new InvocationTargetException JavaDoc(e);
329                 }
330             }
331         }
332         ConsistencyRunnable runnable= new ConsistencyRunnable();
333         if (!runnable.needsExecution())
334             return;
335         IRunnableContext context= fRunnableContext != null
336             ? fRunnableContext
337             : PlatformUI.getWorkbench().getProgressService();
338         context.run(true, true, runnable);
339     }
340 }
341
Popular Tags