1 11 package org.eclipse.jdt.internal.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IStatus; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Link; 27 import org.eclipse.swt.widgets.Shell; 28 29 import org.eclipse.jface.action.Action; 30 import org.eclipse.jface.action.ToolBarManager; 31 import org.eclipse.jface.dialogs.IDialogSettings; 32 import org.eclipse.jface.viewers.CheckboxTreeViewer; 33 import org.eclipse.jface.viewers.ITreeContentProvider; 34 import org.eclipse.jface.viewers.Viewer; 35 import org.eclipse.jface.viewers.ViewerComparator; 36 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer; 39 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 40 41 import org.eclipse.jdt.core.IType; 42 import org.eclipse.jdt.core.JavaModelException; 43 import org.eclipse.jdt.core.dom.AST; 44 import org.eclipse.jdt.core.dom.CompilationUnit; 45 import org.eclipse.jdt.core.dom.IBinding; 46 import org.eclipse.jdt.core.dom.IMethodBinding; 47 import org.eclipse.jdt.core.dom.IPackageBinding; 48 import org.eclipse.jdt.core.dom.ITypeBinding; 49 import org.eclipse.jdt.core.dom.Modifier; 50 51 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2; 52 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 53 import org.eclipse.jdt.internal.corext.dom.Bindings; 54 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; 55 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType; 56 import org.eclipse.jdt.internal.corext.util.Messages; 57 58 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 59 import org.eclipse.jdt.internal.ui.JavaPlugin; 60 import org.eclipse.jdt.internal.ui.JavaPluginImages; 61 import org.eclipse.jdt.internal.ui.JavaUIMessages; 62 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 63 import org.eclipse.jdt.internal.ui.util.ViewerPane; 64 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; 65 66 public class OverrideMethodDialog extends SourceActionDialog { 67 68 private class OverrideFlatTreeAction extends Action { 69 70 private boolean fToggle; 71 72 public OverrideFlatTreeAction() { 73 setToolTipText(JavaUIMessages.OverrideMethodDialog_groupMethodsByTypes); 74 75 JavaPluginImages.setLocalImageDescriptors(this, "impl_co.gif"); 77 fToggle= getOverrideContentProvider().isShowTypes(); 78 setChecked(fToggle); 79 } 80 81 private OverrideMethodContentProvider getOverrideContentProvider() { 82 return (OverrideMethodContentProvider) getContentProvider(); 83 } 84 85 public void run() { 86 Object [] elementList= getOverrideContentProvider().getViewer().getCheckedElements(); 88 fToggle= !fToggle; 89 setChecked(fToggle); 90 getOverrideContentProvider().setShowTypes(fToggle); 91 getOverrideContentProvider().getViewer().setCheckedElements(elementList); 92 } 93 94 } 95 96 private static class OverrideMethodContentProvider implements ITreeContentProvider { 97 98 private final Object [] fEmpty= new Object [0]; 99 100 private IMethodBinding[] fMethods; 101 102 private IDialogSettings fSettings; 103 104 private boolean fShowTypes; 105 106 private Object [] fTypes; 107 108 private ContainerCheckedTreeViewer fViewer; 109 110 private final String SETTINGS_SECTION= "OverrideMethodDialog"; 112 private final String SETTINGS_SHOWTYPES= "showtypes"; 114 117 public OverrideMethodContentProvider() { 118 IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings(); 119 fSettings= dialogSettings.getSection(SETTINGS_SECTION); 120 if (fSettings == null) { 121 fSettings= dialogSettings.addNewSection(SETTINGS_SECTION); 122 fSettings.put(SETTINGS_SHOWTYPES, true); 123 } 124 fShowTypes= fSettings.getBoolean(SETTINGS_SHOWTYPES); 125 } 126 127 130 public void dispose() { 131 } 132 133 136 public Object [] getChildren(Object parentElement) { 137 if (parentElement instanceof ITypeBinding) { 138 ArrayList result= new ArrayList (fMethods.length); 139 for (int index= 0; index < fMethods.length; index++) { 140 if (fMethods[index].getDeclaringClass().isEqualTo((IBinding) parentElement)) 141 result.add(fMethods[index]); 142 } 143 return result.toArray(); 144 } 145 return fEmpty; 146 } 147 148 151 public Object [] getElements(Object inputElement) { 152 return fShowTypes ? fTypes : fMethods; 153 } 154 155 158 public Object getParent(Object element) { 159 if (element instanceof IMethodBinding) { 160 return ((IMethodBinding) element).getDeclaringClass(); 161 } 162 return null; 163 } 164 165 public ContainerCheckedTreeViewer getViewer() { 166 return fViewer; 167 } 168 169 172 public boolean hasChildren(Object element) { 173 return getChildren(element).length > 0; 174 } 175 176 public void init(IMethodBinding[] methods, ITypeBinding[] types) { 177 fMethods= methods; 178 fTypes= types; 179 } 180 181 184 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 185 fViewer= (ContainerCheckedTreeViewer) viewer; 186 } 187 188 public boolean isShowTypes() { 189 return fShowTypes; 190 } 191 192 public void setShowTypes(boolean showTypes) { 193 if (fShowTypes != showTypes) { 194 fShowTypes= showTypes; 195 fSettings.put(SETTINGS_SHOWTYPES, showTypes); 196 if (fViewer != null) 197 fViewer.refresh(); 198 } 199 } 200 } 201 202 private static class OverrideMethodComparator extends ViewerComparator { 203 204 private ITypeBinding[] fAllTypes= new ITypeBinding[0]; 205 206 public OverrideMethodComparator(ITypeBinding curr) { 207 if (curr != null) { 208 ITypeBinding[] superTypes= Bindings.getAllSuperTypes(curr); 209 fAllTypes= new ITypeBinding[superTypes.length + 1]; 210 fAllTypes[0]= curr; 211 System.arraycopy(superTypes, 0, fAllTypes, 1, superTypes.length); 212 } 213 } 214 215 218 public int compare(Viewer viewer, Object first, Object second) { 219 if (first instanceof ITypeBinding && second instanceof ITypeBinding) { 220 final ITypeBinding left= (ITypeBinding) first; 221 final ITypeBinding right= (ITypeBinding) second; 222 if (right.getQualifiedName().equals("java.lang.Object")) return -1; 224 if (left.isEqualTo(right)) 225 return 0; 226 if (Bindings.isSuperType(left, right)) 227 return +1; 228 else if (Bindings.isSuperType(right, left)) 229 return -1; 230 return 0; 231 } else 232 return super.compare(viewer, first, second); 233 } 234 } 235 236 private static class OverrideMethodValidator implements ISelectionStatusValidator { 237 238 private static int fNumMethods; 239 240 public OverrideMethodValidator(int entries) { 241 fNumMethods= entries; 242 } 243 244 247 public IStatus validate(Object [] selection) { 248 int count= 0; 249 for (int index= 0; index < selection.length; index++) { 250 if (selection[index] instanceof IMethodBinding) 251 count++; 252 } 253 if (count == 0) 254 return new StatusInfo(IStatus.ERROR, ""); return new StatusInfo(IStatus.INFO, Messages.format(JavaUIMessages.OverrideMethodDialog_selectioninfo_more, new String [] { String.valueOf(count), String.valueOf(fNumMethods)})); 256 } 257 } 258 259 private static ITypeBinding getSuperType(final ITypeBinding binding, final String name) { 260 261 if (binding.isArray() || binding.isPrimitive()) 262 return null; 263 264 if (binding.getQualifiedName().startsWith(name)) 265 return binding; 266 267 final ITypeBinding type= binding.getSuperclass(); 268 if (type != null) { 269 final ITypeBinding result= getSuperType(type, name); 270 if (result != null) 271 return result; 272 } 273 final ITypeBinding[] types= binding.getInterfaces(); 274 for (int index= 0; index < types.length; index++) { 275 final ITypeBinding result= getSuperType(types[index], name); 276 if (result != null) 277 return result; 278 } 279 return null; 280 } 281 282 private CompilationUnit fUnit= null; 283 284 public OverrideMethodDialog(Shell shell, CompilationUnitEditor editor, IType type, boolean isSubType) throws JavaModelException { 285 super(shell, new BindingLabelProvider(), new OverrideMethodContentProvider(), editor, type, false); 286 RefactoringASTParser parser= new RefactoringASTParser(AST.JLS3); 287 fUnit= parser.parse(type.getCompilationUnit(), true); 288 final ITypeBinding binding= ASTNodes.getTypeBinding(fUnit, type); 289 List toImplement= new ArrayList (); 290 IMethodBinding[] overridable= null; 291 if (binding != null) { 292 final IPackageBinding pack= binding.getPackage(); 293 final IMethodBinding[] methods= StubUtility2.getOverridableMethods(fUnit.getAST(), binding, false); 294 List list= new ArrayList (methods.length); 295 for (int index= 0; index < methods.length; index++) { 296 final IMethodBinding cur= methods[index]; 297 if (Bindings.isVisibleInHierarchy(cur, pack)) 298 list.add(cur); 299 } 300 overridable= (IMethodBinding[]) list.toArray(new IMethodBinding[list.size()]); 301 } else 302 overridable= new IMethodBinding[] {}; 303 for (int i= 0; i < overridable.length; i++) { 304 if (Modifier.isAbstract(overridable[i].getModifiers())) { 305 toImplement.add(overridable[i]); 306 } 307 } 308 309 if (binding != null) { 310 ITypeBinding cloneable= getSuperType(binding, "java.lang.Cloneable"); if (cloneable != null) { 312 IMethodBinding[] methods= fUnit.getAST().resolveWellKnownType("java.lang.Object").getDeclaredMethods(); for (int index= 0; index < methods.length; index++) { 314 IMethodBinding method= methods[index]; 315 if (method.getName().equals("clone") && method.getParameterTypes().length == 0) toImplement.add(method); 317 } 318 } 319 } 320 321 IMethodBinding[] toImplementArray= (IMethodBinding[]) toImplement.toArray(new IMethodBinding[toImplement.size()]); 322 setInitialSelections(toImplementArray); 323 324 HashSet expanded= new HashSet (toImplementArray.length); 325 for (int i= 0; i < toImplementArray.length; i++) { 326 expanded.add(toImplementArray[i].getDeclaringClass()); 327 } 328 329 HashSet types= new HashSet (overridable.length); 330 for (int i= 0; i < overridable.length; i++) { 331 types.add(overridable[i].getDeclaringClass()); 332 } 333 334 ITypeBinding[] typesArrays= (ITypeBinding[]) types.toArray(new ITypeBinding[types.size()]); 335 OverrideMethodComparator comparator= new OverrideMethodComparator(binding); 336 if (expanded.isEmpty() && typesArrays.length > 0) { 337 comparator.sort(null, typesArrays); 338 expanded.add(typesArrays[0]); 339 } 340 setExpandedElements(expanded.toArray()); 341 342 ((OverrideMethodContentProvider) getContentProvider()).init(overridable, typesArrays); 343 344 setTitle(JavaUIMessages.OverrideMethodDialog_dialog_title); 345 setMessage(null); 346 setValidator(new OverrideMethodValidator(overridable.length)); 347 setComparator(comparator); 348 setContainerMode(true); 349 setSize(60, 18); 350 setInput(new Object ()); 351 } 352 353 public CompilationUnit getCompilationUnit() { 354 return fUnit; 355 } 356 357 360 protected void configureShell(Shell newShell) { 361 super.configureShell(newShell); 362 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.OVERRIDE_TREE_SELECTION_DIALOG); 363 } 364 365 368 protected Control createLinkControl(Composite composite) { 369 Link link= new Link(composite, SWT.WRAP); 370 link.setText(JavaUIMessages.OverrideMethodDialog_link_message); 371 link.addSelectionListener(new SelectionAdapter() { 372 public void widgetSelected(SelectionEvent e) { 373 openCodeTempatePage(CodeTemplateContextType.OVERRIDECOMMENT_ID); 374 } 375 }); 376 link.setToolTipText(JavaUIMessages.OverrideMethodDialog_link_tooltip); 377 378 GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 379 gridData.widthHint= convertWidthInCharsToPixels(40); link.setLayoutData(gridData); 381 return link; 382 } 383 384 387 protected CheckboxTreeViewer createTreeViewer(Composite composite) { 388 initializeDialogUnits(composite); 389 ViewerPane pane= new ViewerPane(composite, SWT.BORDER | SWT.FLAT); 390 pane.setText(JavaUIMessages.OverrideMethodDialog_dialog_description); 391 CheckboxTreeViewer treeViewer= super.createTreeViewer(pane); 392 pane.setContent(treeViewer.getControl()); 393 GridLayout paneLayout= new GridLayout(); 394 paneLayout.marginHeight= 0; 395 paneLayout.marginWidth= 0; 396 paneLayout.numColumns= 1; 397 pane.setLayout(paneLayout); 398 GridData gd= new GridData(GridData.FILL_BOTH); 399 gd.widthHint= convertWidthInCharsToPixels(55); 400 gd.heightHint= convertHeightInCharsToPixels(15); 401 pane.setLayoutData(gd); 402 ToolBarManager manager= pane.getToolBarManager(); 403 manager.add(new OverrideFlatTreeAction()); manager.update(true); 405 treeViewer.getTree().setFocus(); 406 return treeViewer; 407 } 408 409 public boolean hasMethodsToOverride() { 410 return getContentProvider().getElements(null).length > 0; 411 } 412 413 } 414 | Popular Tags |