]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.ArrayList; | |
15 | import java.util.HashSet; | |
16 | ||
17 | import org.eclipse.swt.graphics.Point; | |
18 | import org.eclipse.swt.graphics.Rectangle; | |
19 | import org.eclipse.swt.widgets.Composite; | |
20 | import org.eclipse.swt.widgets.Control; | |
21 | import org.eclipse.swt.widgets.Shell; | |
22 | ||
23 | import org.eclipse.core.runtime.CoreException; | |
24 | import org.eclipse.core.runtime.IProgressMonitor; | |
25 | import org.eclipse.core.runtime.NullProgressMonitor; | |
26 | import org.eclipse.core.runtime.OperationCanceledException; | |
27 | import org.eclipse.core.runtime.SubProgressMonitor; | |
28 | ||
29 | import org.eclipse.jface.dialogs.IDialogConstants; | |
30 | import org.eclipse.jface.dialogs.IDialogSettings; | |
31 | import org.eclipse.jface.dialogs.MessageDialog; | |
32 | import org.eclipse.jface.operation.IRunnableContext; | |
33 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
34 | import org.eclipse.jface.viewers.ILabelProvider; | |
35 | ||
36 | import org.eclipse.ui.PlatformUI; | |
37 | import org.eclipse.ui.dialogs.ElementListSelectionDialog; | |
38 | import org.eclipse.ui.dialogs.SelectionDialog; | |
39 | ||
40 | import org.eclipse.jdt.core.IJavaElement; | |
41 | import org.eclipse.jdt.core.IPackageFragment; | |
42 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
43 | import org.eclipse.jdt.core.JavaModelException; | |
44 | import org.eclipse.jdt.core.search.IJavaSearchConstants; | |
45 | import org.eclipse.jdt.core.search.IJavaSearchScope; | |
46 | import org.eclipse.jdt.core.search.SearchEngine; | |
47 | import org.eclipse.jdt.core.search.SearchMatch; | |
48 | import org.eclipse.jdt.core.search.SearchPattern; | |
49 | import org.eclipse.jdt.core.search.SearchRequestor; | |
50 | ||
51 | import org.eclipse.jdt.internal.corext.util.SearchUtils; | |
52 | ||
53 | import org.eclipse.jdt.ui.JavaElementLabelProvider; | |
54 | ||
55 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
56 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
57 | import org.eclipse.jdt.internal.ui.JavaUIMessages; | |
58 | import org.eclipse.jdt.internal.ui.preferences.ImportOrganizeInputDialog; | |
59 | import org.eclipse.jdt.internal.ui.preferences.PreferencesMessages; | |
60 | import org.eclipse.jdt.internal.ui.preferences.TypeFilterInputDialog; | |
61 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
62 | ||
63 | /** | |
64 | * Dialog to browse for package fragments. | |
65 | */ | |
66 | public class PackageSelectionDialog extends ElementListSelectionDialog { | |
67 | ||
68 | public static final int F_REMOVE_DUPLICATES= 1; | |
69 | public static final int F_SHOW_PARENTS= 2; | |
70 | public static final int F_HIDE_DEFAULT_PACKAGE= 4; | |
71 | public static final int F_HIDE_EMPTY_INNER= 8; | |
72 | ||
73 | ||
74 | /** The dialog location. */ | |
75 | private Point fLocation; | |
76 | /** The dialog size. */ | |
77 | private Point fSize; | |
78 | ||
79 | private IRunnableContext fContext; | |
80 | private IJavaSearchScope fScope; | |
81 | private int fFlags; | |
82 | ||
83 | /** | |
84 | * Creates a package selection dialog. | |
85 | * @param parent the parent shell | |
86 | * @param context the runnable context to run the search in | |
87 | * @param flags a combination of <code>F_REMOVE_DUPLICATES</code>, <code>F_SHOW_PARENTS</code>, | |
88 | * <code>F_HIDE_DEFAULT_PACKAGE</code> and <code>F_HIDE_EMPTY_INNER</code> | |
89 | * @param scope the scope defining the available packages. | |
90 | */ | |
91 | public PackageSelectionDialog(Shell parent, IRunnableContext context, int flags, IJavaSearchScope scope) { | |
92 | super(parent, createLabelProvider(flags)); | |
93 | fFlags= flags; | |
94 | fScope= scope; | |
95 | fContext= context; | |
96 | } | |
97 | ||
98 | private static ILabelProvider createLabelProvider(int dialogFlags) { | |
99 | int flags= JavaElementLabelProvider.SHOW_DEFAULT; | |
100 | if ((dialogFlags & F_REMOVE_DUPLICATES) == 0) { | |
101 | flags= flags | JavaElementLabelProvider.SHOW_ROOT; | |
102 | } | |
103 | return new JavaElementLabelProvider(flags); | |
104 | } | |
105 | ||
106 | ||
107 | /* | |
108 | * @see org.eclipse.jface.window.Window#open() | |
109 | */ | |
110 | @Override | |
111 | public int open() { | |
112 | final ArrayList<IJavaElement> packageList= new ArrayList<IJavaElement>(); | |
113 | ||
114 | IRunnableWithProgress runnable= new IRunnableWithProgress() { | |
115 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
116 | if (monitor == null) { | |
117 | monitor= new NullProgressMonitor(); | |
118 | } | |
119 | boolean hideEmpty= (fFlags & F_HIDE_EMPTY_INNER) != 0; | |
120 | monitor.beginTask(JavaUIMessages.PackageSelectionDialog_progress_search, hideEmpty ? 2 : 1); | |
121 | try { | |
122 | SearchRequestor requestor= new SearchRequestor() { | |
123 | private HashSet<String> fSet= new HashSet<String>(); | |
124 | private final boolean fAddDefault= (fFlags & F_HIDE_DEFAULT_PACKAGE) == 0; | |
125 | private final boolean fDuplicates= (fFlags & F_REMOVE_DUPLICATES) == 0; | |
126 | private final boolean fIncludeParents= (fFlags & F_SHOW_PARENTS) != 0; | |
127 | ||
128 | @Override | |
129 | public void acceptSearchMatch(SearchMatch match) throws CoreException { | |
130 | IJavaElement enclosingElement= (IJavaElement) match.getElement(); | |
131 | String name= enclosingElement.getElementName(); | |
132 | if (fAddDefault || name.length() > 0) { | |
133 | if (fDuplicates || fSet.add(name)) { | |
134 | packageList.add(enclosingElement); | |
135 | if (fIncludeParents) { | |
136 | addParentPackages(enclosingElement, name); | |
137 | } | |
138 | } | |
139 | } | |
140 | } | |
141 | ||
142 | private void addParentPackages(IJavaElement enclosingElement, String name) { | |
143 | IPackageFragmentRoot root= (IPackageFragmentRoot) enclosingElement.getParent(); | |
144 | int idx= name.lastIndexOf('.'); | |
145 | while (idx != -1) { | |
146 | name= name.substring(0, idx); | |
147 | if (fDuplicates || fSet.add(name)) { | |
148 | packageList.add(root.getPackageFragment(name)); | |
149 | } | |
150 | idx= name.lastIndexOf('.'); | |
151 | } | |
152 | } | |
153 | }; | |
154 | SearchPattern pattern= SearchPattern.createPattern("*", //$NON-NLS-1$ | |
155 | IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, | |
156 | SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE); | |
157 | new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), fScope, requestor, new SubProgressMonitor(monitor, 1)); | |
158 | ||
159 | if (monitor.isCanceled()) { | |
160 | throw new InterruptedException(); | |
161 | } | |
162 | ||
163 | if (hideEmpty) { | |
164 | removeEmptyPackages(new SubProgressMonitor(monitor, 1)); | |
165 | } | |
166 | } catch (CoreException e) { | |
167 | throw new InvocationTargetException(e); | |
168 | } catch (OperationCanceledException e) { | |
169 | throw new InterruptedException(); | |
170 | } finally { | |
171 | monitor.done(); | |
172 | } | |
173 | } | |
174 | ||
175 | private void removeEmptyPackages(IProgressMonitor monitor) throws JavaModelException, InterruptedException { | |
176 | monitor.beginTask(JavaUIMessages.PackageSelectionDialog_progress_findEmpty, packageList.size()); | |
177 | try { | |
178 | ArrayList<IPackageFragment> res= new ArrayList<IPackageFragment>(packageList.size()); | |
179 | for (int i= 0; i < packageList.size(); i++) { | |
180 | IPackageFragment pkg= (IPackageFragment) packageList.get(i); | |
181 | if (pkg.hasChildren() || !pkg.hasSubpackages()) { | |
182 | res.add(pkg); | |
183 | } | |
184 | monitor.worked(1); | |
185 | if (monitor.isCanceled()) { | |
186 | throw new InterruptedException(); | |
187 | } | |
188 | } | |
189 | packageList.clear(); | |
190 | packageList.addAll(res); | |
191 | } finally{ | |
192 | monitor.done(); | |
193 | } | |
194 | } | |
195 | }; | |
196 | ||
197 | try { | |
198 | fContext.run(true, true, runnable); | |
199 | } catch (InvocationTargetException e) { | |
200 | ExceptionHandler.handle(e, JavaUIMessages.PackageSelectionDialog_error_title, JavaUIMessages.PackageSelectionDialog_error3Message); | |
201 | return CANCEL; | |
202 | } catch (InterruptedException e) { | |
203 | // cancelled by user | |
204 | return CANCEL; | |
205 | } | |
206 | ||
207 | if (packageList.isEmpty()) { | |
208 | String title= JavaUIMessages.PackageSelectionDialog_nopackages_title; | |
209 | String message= JavaUIMessages.PackageSelectionDialog_nopackages_message; | |
210 | MessageDialog.openInformation(getShell(), title, message); | |
211 | return CANCEL; | |
212 | } | |
213 | ||
214 | setElements(packageList.toArray()); | |
215 | ||
216 | return super.open(); | |
217 | } | |
218 | ||
219 | ||
220 | /* | |
221 | * @see org.eclipse.jface.window.Window#configureShell(Shell) | |
222 | */ | |
223 | @Override | |
224 | protected void configureShell(Shell newShell) { | |
225 | super.configureShell(newShell); | |
226 | PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OPEN_PACKAGE_DIALOG); | |
227 | } | |
228 | ||
229 | /* | |
230 | * @see Window#close() | |
231 | */ | |
232 | @Override | |
233 | public boolean close() { | |
234 | writeSettings(); | |
235 | return super.close(); | |
236 | } | |
237 | ||
238 | /* | |
239 | * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite) | |
240 | */ | |
241 | @Override | |
242 | protected Control createContents(Composite parent) { | |
243 | Control control= super.createContents(parent); | |
244 | readSettings(); | |
245 | return control; | |
246 | } | |
247 | ||
248 | /* (non-Javadoc) | |
249 | * @see org.eclipse.jface.window.Window#getInitialSize() | |
250 | */ | |
251 | @Override | |
252 | protected Point getInitialSize() { | |
253 | Point result= super.getInitialSize(); | |
254 | if (fSize != null) { | |
255 | result.x= Math.max(result.x, fSize.x); | |
256 | result.y= Math.max(result.y, fSize.y); | |
257 | Rectangle display= getShell().getDisplay().getClientArea(); | |
258 | result.x= Math.min(result.x, display.width); | |
259 | result.y= Math.min(result.y, display.height); | |
260 | } | |
261 | return result; | |
262 | } | |
263 | ||
264 | /* (non-Javadoc) | |
265 | * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point) | |
266 | */ | |
267 | @Override | |
268 | protected Point getInitialLocation(Point initialSize) { | |
269 | Point result= super.getInitialLocation(initialSize); | |
270 | if (fLocation != null) { | |
271 | result.x= fLocation.x; | |
272 | result.y= fLocation.y; | |
273 | Rectangle display= getShell().getDisplay().getClientArea(); | |
274 | int xe= result.x + initialSize.x; | |
275 | if (xe > display.width) { | |
276 | result.x-= xe - display.width; | |
277 | } | |
278 | int ye= result.y + initialSize.y; | |
279 | if (ye > display.height) { | |
280 | result.y-= ye - display.height; | |
281 | } | |
282 | } | |
283 | return result; | |
284 | } | |
285 | ||
286 | ||
287 | ||
288 | /** | |
289 | * Initializes itself from the dialog settings with the same state | |
290 | * as at the previous invocation. | |
291 | */ | |
292 | private void readSettings() { | |
293 | IDialogSettings s= getDialogSettings(); | |
294 | try { | |
295 | int x= s.getInt("x"); //$NON-NLS-1$ | |
296 | int y= s.getInt("y"); //$NON-NLS-1$ | |
297 | fLocation= new Point(x, y); | |
298 | int width= s.getInt("width"); //$NON-NLS-1$ | |
299 | int height= s.getInt("height"); //$NON-NLS-1$ | |
300 | fSize= new Point(width, height); | |
301 | ||
302 | } catch (NumberFormatException e) { | |
303 | fLocation= null; | |
304 | fSize= null; | |
305 | } | |
306 | } | |
307 | ||
308 | /** | |
309 | * Stores it current configuration in the dialog store. | |
310 | */ | |
311 | private void writeSettings() { | |
312 | IDialogSettings s= getDialogSettings(); | |
313 | ||
314 | Point location= getShell().getLocation(); | |
315 | s.put("x", location.x); //$NON-NLS-1$ | |
316 | s.put("y", location.y); //$NON-NLS-1$ | |
317 | ||
318 | Point size= getShell().getSize(); | |
319 | s.put("width", size.x); //$NON-NLS-1$ | |
320 | s.put("height", size.y); //$NON-NLS-1$ | |
321 | } | |
322 | ||
323 | /** | |
324 | * Returns the dialog settings object used to share state | |
325 | * between several find/replace dialogs. | |
326 | * | |
327 | * @return the dialog settings to be used | |
328 | */ | |
329 | private IDialogSettings getDialogSettings() { | |
330 | IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); | |
331 | String sectionName= getClass().getName(); | |
332 | IDialogSettings subSettings= settings.getSection(sectionName); | |
333 | if (subSettings == null) | |
334 | subSettings= settings.addNewSection(sectionName); | |
335 | return subSettings; | |
336 | } | |
337 | ||
338 | public SelectionDialog generated_217841240717095971() { | |
339 | setFilter(""); //$NON-NLS-1$ | |
340 | setIgnoreCase(false); | |
341 | setMultipleSelection(false); | |
342 | return this; | |
343 | } | |
344 | ||
345 | public void generated_6893906258549158796(ImportOrganizeInputDialog importorganizeinputdialog) { | |
346 | setFilter(importorganizeinputdialog.fNameDialogField.getText()); | |
347 | setIgnoreCase(false); | |
348 | } | |
349 | ||
350 | public String[] generated_4090575371194484342() { | |
351 | setTitle(PreferencesMessages.TypeFilterPreferencePage_choosepackage_label); | |
352 | setMessage(PreferencesMessages.TypeFilterPreferencePage_choosepackage_description); | |
353 | setMultipleSelection(true); | |
354 | if (open() == IDialogConstants.OK_ID) { | |
355 | Object[] fragments= getResult(); | |
356 | String[] res= new String[fragments.length]; | |
357 | for (int i= 0; i < res.length; i++) { | |
358 | res[i]= ((IPackageFragment) fragments[i]).getElementName() + ".*"; //$NON-NLS-1$ | |
359 | } | |
360 | return res; | |
361 | } | |
362 | return null; | |
363 | } | |
364 | ||
365 | public void generated_2277368292760414862(TypeFilterInputDialog typefilterinputdialog) { | |
366 | setMultipleSelection(false); | |
367 | setFilter(typefilterinputdialog.fNameDialogField.getText()); | |
368 | } | |
369 | ||
370 | ||
371 | ||
372 | } |