KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IPackageFragment;
25 import org.eclipse.jdt.core.IPackageFragmentRoot;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.core.search.IJavaSearchConstants;
28 import org.eclipse.jdt.core.search.IJavaSearchScope;
29 import org.eclipse.jdt.core.search.SearchEngine;
30 import org.eclipse.jdt.core.search.SearchMatch;
31 import org.eclipse.jdt.core.search.SearchPattern;
32 import org.eclipse.jdt.core.search.SearchRequestor;
33
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.graphics.Rectangle;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Shell;
39
40 import org.eclipse.jface.dialogs.IDialogSettings;
41 import org.eclipse.jface.dialogs.MessageDialog;
42 import org.eclipse.jface.operation.IRunnableContext;
43 import org.eclipse.jface.operation.IRunnableWithProgress;
44 import org.eclipse.jface.viewers.ILabelProvider;
45
46 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
47 import org.eclipse.ui.PlatformUI;
48
49 import org.eclipse.jdt.internal.corext.util.SearchUtils;
50
51 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
52 import org.eclipse.jdt.internal.ui.JavaPlugin;
53 import org.eclipse.jdt.internal.ui.JavaUIMessages;
54 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
55
56 import org.eclipse.jdt.ui.JavaElementLabelProvider;
57
58 /**
59  * Dialog to browse for package fragments.
60  */

61 public class PackageSelectionDialog extends ElementListSelectionDialog {
62     
63     public static final int F_REMOVE_DUPLICATES= 1;
64     public static final int F_SHOW_PARENTS= 2;
65     public static final int F_HIDE_DEFAULT_PACKAGE= 4;
66     public static final int F_HIDE_EMPTY_INNER= 8;
67     
68
69     /** The dialog location. */
70     private Point fLocation;
71     /** The dialog size. */
72     private Point fSize;
73     
74     private IRunnableContext fContext;
75     private IJavaSearchScope fScope;
76     private int fFlags;
77
78     /**
79      * Creates a package selection dialog.
80      * @param parent the parent shell
81      * @param context the runnable context to run the search in
82      * @param flags a combination of <code>F_REMOVE_DUPLICATES</code>, <code>F_SHOW_PARENTS</code>,
83      * <code>F_HIDE_DEFAULT_PACKAGE</code> and <code>F_HIDE_EMPTY_INNER</code>
84      * @param scope the scope defining the available packages.
85      */

86     public PackageSelectionDialog(Shell parent, IRunnableContext context, int flags, IJavaSearchScope scope) {
87         super(parent, createLabelProvider(flags));
88         fFlags= flags;
89         fScope= scope;
90         fContext= context;
91     }
92     
93     private static ILabelProvider createLabelProvider(int dialogFlags) {
94         int flags= JavaElementLabelProvider.SHOW_DEFAULT;
95         if ((dialogFlags & F_REMOVE_DUPLICATES) == 0) {
96             flags= flags | JavaElementLabelProvider.SHOW_ROOT;
97         }
98         return new JavaElementLabelProvider(flags);
99     }
100     
101     
102     /*
103      * @see org.eclipse.jface.window.Window#open()
104      */

105     public int open() {
106         final ArrayList JavaDoc packageList= new ArrayList JavaDoc();
107         
108         IRunnableWithProgress runnable= new IRunnableWithProgress() {
109             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
110                 if (monitor == null) {
111                     monitor= new NullProgressMonitor();
112                 }
113                 boolean hideEmpty= (fFlags & F_HIDE_EMPTY_INNER) != 0;
114                 monitor.beginTask(JavaUIMessages.PackageSelectionDialog_progress_search, hideEmpty ? 2 : 1);
115                 try {
116                     SearchRequestor requestor= new SearchRequestor() {
117                         private HashSet JavaDoc fSet= new HashSet JavaDoc();
118                         private final boolean fAddDefault= (fFlags & F_HIDE_DEFAULT_PACKAGE) == 0;
119                         private final boolean fDuplicates= (fFlags & F_REMOVE_DUPLICATES) == 0;
120                         private final boolean fIncludeParents= (fFlags & F_SHOW_PARENTS) != 0;
121
122                         public void acceptSearchMatch(SearchMatch match) throws CoreException {
123                             IJavaElement enclosingElement= (IJavaElement) match.getElement();
124                             String JavaDoc name= enclosingElement.getElementName();
125                             if (fAddDefault || name.length() > 0) {
126                                 if (fDuplicates || fSet.add(name)) {
127                                     packageList.add(enclosingElement);
128                                     if (fIncludeParents) {
129                                         addParentPackages(enclosingElement, name);
130                                     }
131                                 }
132                             }
133                         }
134                         
135                         private void addParentPackages(IJavaElement enclosingElement, String JavaDoc name) {
136                             IPackageFragmentRoot root= (IPackageFragmentRoot) enclosingElement.getParent();
137                             int idx= name.lastIndexOf('.');
138                             while (idx != -1) {
139                                 name= name.substring(0, idx);
140                                 if (fDuplicates || fSet.add(name)) {
141                                     packageList.add(root.getPackageFragment(name));
142                                 }
143                                 idx= name.lastIndexOf('.');
144                             }
145                         }
146                     };
147                     SearchPattern pattern= SearchPattern.createPattern("*", //$NON-NLS-1$
148
IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS,
149                             SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
150                     new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), fScope, requestor, new SubProgressMonitor(monitor, 1));
151                     
152                     if (monitor.isCanceled()) {
153                         throw new InterruptedException JavaDoc();
154                     }
155
156                     if (hideEmpty) {
157                         removeEmptyPackages(new SubProgressMonitor(monitor, 1));
158                     }
159                 } catch (CoreException e) {
160                     throw new InvocationTargetException JavaDoc(e);
161                 } catch (OperationCanceledException e) {
162                     throw new InterruptedException JavaDoc();
163                 } finally {
164                     monitor.done();
165                 }
166             }
167             
168             private void removeEmptyPackages(IProgressMonitor monitor) throws JavaModelException, InterruptedException JavaDoc {
169                 monitor.beginTask(JavaUIMessages.PackageSelectionDialog_progress_findEmpty, packageList.size());
170                 try {
171                     ArrayList JavaDoc res= new ArrayList JavaDoc(packageList.size());
172                     for (int i= 0; i < packageList.size(); i++) {
173                         IPackageFragment pkg= (IPackageFragment) packageList.get(i);
174                         if (pkg.hasChildren() || !pkg.hasSubpackages()) {
175                             res.add(pkg);
176                         }
177                         monitor.worked(1);
178                         if (monitor.isCanceled()) {
179                             throw new InterruptedException JavaDoc();
180                         }
181                     }
182                     packageList.clear();
183                     packageList.addAll(res);
184                 } finally{
185                     monitor.done();
186                 }
187             }
188         };
189
190         try {
191             fContext.run(true, true, runnable);
192         } catch (InvocationTargetException JavaDoc e) {
193             ExceptionHandler.handle(e, JavaUIMessages.PackageSelectionDialog_error_title, JavaUIMessages.PackageSelectionDialog_error3Message);
194             return CANCEL;
195         } catch (InterruptedException JavaDoc e) {
196             // cancelled by user
197
return CANCEL;
198         }
199         
200         if (packageList.isEmpty()) {
201             String JavaDoc title= JavaUIMessages.PackageSelectionDialog_nopackages_title;
202             String JavaDoc message= JavaUIMessages.PackageSelectionDialog_nopackages_message;
203             MessageDialog.openInformation(getShell(), title, message);
204             return CANCEL;
205         }
206         
207         setElements(packageList.toArray());
208
209         return super.open();
210     }
211     
212     
213     /*
214      * @see org.eclipse.jface.window.Window#configureShell(Shell)
215      */

216     protected void configureShell(Shell newShell) {
217         super.configureShell(newShell);
218         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OPEN_PACKAGE_DIALOG);
219     }
220
221     /*
222      * @see Window#close()
223      */

224     public boolean close() {
225         writeSettings();
226         return super.close();
227     }
228
229     /*
230      * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
231      */

232     protected Control createContents(Composite parent) {
233         Control control= super.createContents(parent);
234         readSettings();
235         return control;
236     }
237     
238     /* (non-Javadoc)
239      * @see org.eclipse.jface.window.Window#getInitialSize()
240      */

241     protected Point getInitialSize() {
242         Point result= super.getInitialSize();
243         if (fSize != null) {
244             result.x= Math.max(result.x, fSize.x);
245             result.y= Math.max(result.y, fSize.y);
246             Rectangle display= getShell().getDisplay().getClientArea();
247             result.x= Math.min(result.x, display.width);
248             result.y= Math.min(result.y, display.height);
249         }
250         return result;
251     }
252     
253     /* (non-Javadoc)
254      * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
255      */

256     protected Point getInitialLocation(Point initialSize) {
257         Point result= super.getInitialLocation(initialSize);
258         if (fLocation != null) {
259             result.x= fLocation.x;
260             result.y= fLocation.y;
261             Rectangle display= getShell().getDisplay().getClientArea();
262             int xe= result.x + initialSize.x;
263             if (xe > display.width) {
264                 result.x-= xe - display.width;
265             }
266             int ye= result.y + initialSize.y;
267             if (ye > display.height) {
268                 result.y-= ye - display.height;
269             }
270         }
271         return result;
272     }
273
274
275
276     /**
277      * Initializes itself from the dialog settings with the same state
278      * as at the previous invocation.
279      */

280     private void readSettings() {
281         IDialogSettings s= getDialogSettings();
282         try {
283             int x= s.getInt("x"); //$NON-NLS-1$
284
int y= s.getInt("y"); //$NON-NLS-1$
285
fLocation= new Point(x, y);
286             int width= s.getInt("width"); //$NON-NLS-1$
287
int height= s.getInt("height"); //$NON-NLS-1$
288
fSize= new Point(width, height);
289
290         } catch (NumberFormatException JavaDoc e) {
291             fLocation= null;
292             fSize= null;
293         }
294     }
295
296     /**
297      * Stores it current configuration in the dialog store.
298      */

299     private void writeSettings() {
300         IDialogSettings s= getDialogSettings();
301
302         Point location= getShell().getLocation();
303         s.put("x", location.x); //$NON-NLS-1$
304
s.put("y", location.y); //$NON-NLS-1$
305

306         Point size= getShell().getSize();
307         s.put("width", size.x); //$NON-NLS-1$
308
s.put("height", size.y); //$NON-NLS-1$
309
}
310
311     /**
312      * Returns the dialog settings object used to share state
313      * between several find/replace dialogs.
314      *
315      * @return the dialog settings to be used
316      */

317     private IDialogSettings getDialogSettings() {
318         IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
319         String JavaDoc sectionName= getClass().getName();
320         IDialogSettings subSettings= settings.getSection(sectionName);
321         if (subSettings == null)
322             subSettings= settings.addNewSection(sectionName);
323         return subSettings;
324     }
325
326
327
328 }
329
Popular Tags