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.HashSet ; 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 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 70 private Point fLocation; 71 72 private Point fSize; 73 74 private IRunnableContext fContext; 75 private IJavaSearchScope fScope; 76 private int fFlags; 77 78 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 105 public int open() { 106 final ArrayList packageList= new ArrayList (); 107 108 IRunnableWithProgress runnable= new IRunnableWithProgress() { 109 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 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 fSet= new HashSet (); 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 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 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("*", 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 (); 154 } 155 156 if (hideEmpty) { 157 removeEmptyPackages(new SubProgressMonitor(monitor, 1)); 158 } 159 } catch (CoreException e) { 160 throw new InvocationTargetException (e); 161 } catch (OperationCanceledException e) { 162 throw new InterruptedException (); 163 } finally { 164 monitor.done(); 165 } 166 } 167 168 private void removeEmptyPackages(IProgressMonitor monitor) throws JavaModelException, InterruptedException { 169 monitor.beginTask(JavaUIMessages.PackageSelectionDialog_progress_findEmpty, packageList.size()); 170 try { 171 ArrayList res= new ArrayList (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 (); 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 e) { 193 ExceptionHandler.handle(e, JavaUIMessages.PackageSelectionDialog_error_title, JavaUIMessages.PackageSelectionDialog_error3Message); 194 return CANCEL; 195 } catch (InterruptedException e) { 196 return CANCEL; 198 } 199 200 if (packageList.isEmpty()) { 201 String title= JavaUIMessages.PackageSelectionDialog_nopackages_title; 202 String 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 216 protected void configureShell(Shell newShell) { 217 super.configureShell(newShell); 218 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OPEN_PACKAGE_DIALOG); 219 } 220 221 224 public boolean close() { 225 writeSettings(); 226 return super.close(); 227 } 228 229 232 protected Control createContents(Composite parent) { 233 Control control= super.createContents(parent); 234 readSettings(); 235 return control; 236 } 237 238 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 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 280 private void readSettings() { 281 IDialogSettings s= getDialogSettings(); 282 try { 283 int x= s.getInt("x"); int y= s.getInt("y"); fLocation= new Point(x, y); 286 int width= s.getInt("width"); int height= s.getInt("height"); fSize= new Point(width, height); 289 290 } catch (NumberFormatException e) { 291 fLocation= null; 292 fSize= null; 293 } 294 } 295 296 299 private void writeSettings() { 300 IDialogSettings s= getDialogSettings(); 301 302 Point location= getShell().getLocation(); 303 s.put("x", location.x); s.put("y", location.y); 306 Point size= getShell().getSize(); 307 s.put("width", size.x); s.put("height", size.y); } 310 311 317 private IDialogSettings getDialogSettings() { 318 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); 319 String 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 |