1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 19 import org.eclipse.core.resources.IWorkspaceRunnable; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.events.SelectionListener; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Link; 32 import org.eclipse.swt.widgets.Shell; 33 34 import org.eclipse.jface.dialogs.IDialogConstants; 35 import org.eclipse.jface.dialogs.IDialogSettings; 36 import org.eclipse.jface.dialogs.MessageDialog; 37 import org.eclipse.jface.operation.IRunnableContext; 38 import org.eclipse.jface.viewers.CheckboxTreeViewer; 39 import org.eclipse.jface.viewers.ILabelProvider; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 import org.eclipse.jface.viewers.ITreeContentProvider; 42 import org.eclipse.jface.viewers.Viewer; 43 import org.eclipse.jface.window.Window; 44 45 import org.eclipse.jface.text.IRewriteTarget; 46 import org.eclipse.jface.text.ITextSelection; 47 48 import org.eclipse.ui.IEditorPart; 49 import org.eclipse.ui.IWorkbenchSite; 50 import org.eclipse.ui.PlatformUI; 51 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 52 53 import org.eclipse.jdt.core.Flags; 54 import org.eclipse.jdt.core.ICompilationUnit; 55 import org.eclipse.jdt.core.IType; 56 import org.eclipse.jdt.core.JavaModelException; 57 import org.eclipse.jdt.core.dom.AST; 58 import org.eclipse.jdt.core.dom.ASTParser; 59 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 60 import org.eclipse.jdt.core.dom.CompilationUnit; 61 import org.eclipse.jdt.core.dom.IMethodBinding; 62 import org.eclipse.jdt.core.dom.ITypeBinding; 63 64 import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedConstructorsOperation; 65 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; 66 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2; 67 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 68 import org.eclipse.jdt.internal.corext.dom.NodeFinder; 69 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; 70 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType; 71 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 72 import org.eclipse.jdt.internal.corext.util.Messages; 73 74 import org.eclipse.jdt.ui.JavaElementComparator; 75 import org.eclipse.jdt.ui.JavaUI; 76 77 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 78 import org.eclipse.jdt.internal.ui.JavaPlugin; 79 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 80 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 81 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 82 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 83 import org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog; 84 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 85 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 86 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 87 import org.eclipse.jdt.internal.ui.refactoring.IVisibilityChangeListener; 88 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; 89 import org.eclipse.jdt.internal.ui.util.ElementValidator; 90 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 91 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; 92 93 110 public class AddUnimplementedConstructorsAction extends SelectionDispatchAction { 111 112 private static class AddUnimplementedConstructorsContentProvider implements ITreeContentProvider { 113 114 private static final Object [] EMPTY= new Object [0]; 115 116 private IMethodBinding[] fMethodsList= new IMethodBinding[0]; 117 118 private final CompilationUnit fUnit; 119 120 public AddUnimplementedConstructorsContentProvider(IType type) throws JavaModelException { 121 RefactoringASTParser parser= new RefactoringASTParser(AST.JLS3); 122 fUnit= parser.parse(type.getCompilationUnit(), true); 123 AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(NodeFinder.perform(fUnit, type.getNameRange()), AbstractTypeDeclaration.class); 124 if (declaration != null) { 125 ITypeBinding binding= declaration.resolveBinding(); 126 if (binding != null) 127 fMethodsList= StubUtility2.getVisibleConstructors(binding, true, false); 128 } 129 } 130 131 public CompilationUnit getCompilationUnit() { 132 return fUnit; 133 } 134 135 138 public void dispose() { 139 } 140 141 144 public Object [] getChildren(Object parentElement) { 145 return EMPTY; 146 } 147 148 151 public Object [] getElements(Object inputElement) { 152 return fMethodsList; 153 } 154 155 158 public Object getParent(Object element) { 159 return null; 160 } 161 162 165 public boolean hasChildren(Object element) { 166 return getChildren(element).length > 0; 167 } 168 169 172 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 173 } 174 175 } 176 177 private static class AddUnimplementedConstructorsDialog extends SourceActionDialog { 178 179 private IDialogSettings fAddConstructorsSettings; 180 181 private int fHeight= 18; 182 183 private boolean fOmitSuper; 184 185 private int fWidth= 60; 186 187 private final String OMIT_SUPER= "OmitCallToSuper"; 189 private final String SETTINGS_SECTION= "AddUnimplementedConstructorsDialog"; 191 public AddUnimplementedConstructorsDialog(Shell parent, ILabelProvider labelProvider, ITreeContentProvider contentProvider, CompilationUnitEditor editor, IType type) throws JavaModelException { 192 super(parent, labelProvider, contentProvider, editor, type, true); 193 194 IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings(); 195 fAddConstructorsSettings= dialogSettings.getSection(SETTINGS_SECTION); 196 if (fAddConstructorsSettings == null) { 197 fAddConstructorsSettings= dialogSettings.addNewSection(SETTINGS_SECTION); 198 fAddConstructorsSettings.put(OMIT_SUPER, false); 199 } 200 201 fOmitSuper= fAddConstructorsSettings.getBoolean(OMIT_SUPER); 202 } 203 204 protected void configureShell(Shell shell) { 205 super.configureShell(shell); 206 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IJavaHelpContextIds.ADD_UNIMPLEMENTED_CONSTRUCTORS_DIALOG); 207 } 208 209 protected Control createDialogArea(Composite parent) { 210 initializeDialogUnits(parent); 211 212 Composite composite= new Composite(parent, SWT.NONE); 213 GridLayout layout= new GridLayout(); 214 GridData gd= null; 215 216 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 217 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 218 layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 219 layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 220 composite.setLayout(layout); 221 222 Label messageLabel= createMessageArea(composite); 223 if (messageLabel != null) { 224 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 225 gd.horizontalSpan= 2; 226 messageLabel.setLayoutData(gd); 227 } 228 229 Composite inner= new Composite(composite, SWT.NONE); 230 GridLayout innerLayout= new GridLayout(); 231 innerLayout.numColumns= 2; 232 innerLayout.marginHeight= 0; 233 innerLayout.marginWidth= 0; 234 inner.setLayout(innerLayout); 235 inner.setFont(parent.getFont()); 236 237 CheckboxTreeViewer treeViewer= createTreeViewer(inner); 238 gd= new GridData(GridData.FILL_BOTH); 239 gd.widthHint= convertWidthInCharsToPixels(fWidth); 240 gd.heightHint= convertHeightInCharsToPixels(fHeight); 241 treeViewer.getControl().setLayoutData(gd); 242 243 Composite buttonComposite= createSelectionButtons(inner); 244 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL); 245 buttonComposite.setLayoutData(gd); 246 247 gd= new GridData(GridData.FILL_BOTH); 248 inner.setLayoutData(gd); 249 250 Composite entryComposite= createInsertPositionCombo(composite); 251 entryComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 252 253 Composite commentComposite= createCommentSelection(composite); 254 commentComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 255 256 Composite overrideSuperComposite= createOmitSuper(composite); 257 overrideSuperComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 258 259 Control linkControl= createLinkControl(composite); 260 if (linkControl != null) 261 linkControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 262 263 gd= new GridData(GridData.FILL_BOTH); 264 composite.setLayoutData(gd); 265 266 applyDialogFont(composite); 267 268 return composite; 269 } 270 271 protected Composite createInsertPositionCombo(Composite composite) { 272 Composite entryComposite= super.createInsertPositionCombo(composite); 273 addVisibilityAndModifiersChoices(entryComposite); 274 275 return entryComposite; 276 } 277 278 281 protected Control createLinkControl(Composite composite) { 282 Link link= new Link(composite, SWT.WRAP); 283 link.setText(ActionMessages.AddUnimplementedConstructorsAction_template_link_message); 284 link.addSelectionListener(new SelectionAdapter() { 285 public void widgetSelected(SelectionEvent e) { 286 openCodeTempatePage(CodeTemplateContextType.CONSTRUCTORCOMMENT_ID); 287 } 288 }); 289 link.setToolTipText(ActionMessages.AddUnimplementedConstructorsAction_template_link_tooltip); 290 291 GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 292 gridData.widthHint= convertWidthInCharsToPixels(40); link.setLayoutData(gridData); 294 return link; 295 } 296 297 private Composite createOmitSuper(Composite composite) { 298 Composite omitSuperComposite= new Composite(composite, SWT.NONE); 299 GridLayout layout= new GridLayout(); 300 layout.marginHeight= 0; 301 layout.marginWidth= 0; 302 omitSuperComposite.setLayout(layout); 303 omitSuperComposite.setFont(composite.getFont()); 304 305 Button omitSuperButton= new Button(omitSuperComposite, SWT.CHECK); 306 omitSuperButton.setText(ActionMessages.AddUnimplementedConstructorsDialog_omit_super); 307 omitSuperButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 308 309 omitSuperButton.addSelectionListener(new SelectionListener() { 310 311 public void widgetDefaultSelected(SelectionEvent e) { 312 widgetSelected(e); 313 } 314 315 public void widgetSelected(SelectionEvent e) { 316 boolean isSelected= (((Button) e.widget).getSelection()); 317 setOmitSuper(isSelected); 318 } 319 }); 320 omitSuperButton.setSelection(isOmitSuper()); 321 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 322 gd.horizontalSpan= 2; 323 omitSuperButton.setLayoutData(gd); 324 325 return omitSuperComposite; 326 } 327 328 protected Composite createVisibilityControlAndModifiers(Composite parent, final IVisibilityChangeListener visibilityChangeListener, int[] availableVisibilities, int correctVisibility) { 329 Composite visibilityComposite= createVisibilityControl(parent, visibilityChangeListener, availableVisibilities, correctVisibility); 330 return visibilityComposite; 331 } 332 333 public boolean isOmitSuper() { 334 return fOmitSuper; 335 } 336 337 public void setOmitSuper(boolean omitSuper) { 338 if (fOmitSuper != omitSuper) { 339 fOmitSuper= omitSuper; 340 fAddConstructorsSettings.put(OMIT_SUPER, omitSuper); 341 } 342 } 343 } 344 345 private static class AddUnimplementedConstructorsValidator implements ISelectionStatusValidator { 346 347 private static int fEntries; 348 349 AddUnimplementedConstructorsValidator(int entries) { 350 super(); 351 fEntries= entries; 352 } 353 354 private int countSelectedMethods(Object [] selection) { 355 int count= 0; 356 for (int i= 0; i < selection.length; i++) { 357 if (selection[i] instanceof IMethodBinding) 358 count++; 359 } 360 return count; 361 } 362 363 public IStatus validate(Object [] selection) { 364 int count= countSelectedMethods(selection); 365 if (count == 0) 366 return new StatusInfo(IStatus.ERROR, ""); String message= Messages.format(ActionMessages.AddUnimplementedConstructorsAction_methods_selected, new Object [] { String.valueOf(count), String.valueOf(fEntries)}); 368 return new StatusInfo(IStatus.INFO, message); 369 } 370 } 371 372 private static final String DIALOG_TITLE= ActionMessages.AddUnimplementedConstructorsAction_error_title; 373 374 private CompilationUnitEditor fEditor; 375 376 382 public AddUnimplementedConstructorsAction(CompilationUnitEditor editor) { 383 this(editor.getEditorSite()); 384 fEditor= editor; 385 setEnabled(checkEnabledEditor()); 386 } 387 388 396 public AddUnimplementedConstructorsAction(IWorkbenchSite site) { 397 super(site); 398 setText(ActionMessages.AddUnimplementedConstructorsAction_label); 399 setDescription(ActionMessages.AddUnimplementedConstructorsAction_description); 400 setToolTipText(ActionMessages.AddUnimplementedConstructorsAction_tooltip); 401 402 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_UNIMPLEMENTED_CONSTRUCTORS_ACTION); 403 } 404 405 private boolean canEnable(IStructuredSelection selection) throws JavaModelException { 406 if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) { 407 IType type= (IType) selection.getFirstElement(); 408 return type.getCompilationUnit() != null && !type.isInterface() && !type.isEnum(); 409 } 410 411 if ((selection.size() == 1) && (selection.getFirstElement() instanceof ICompilationUnit)) 412 return true; 413 414 return false; 415 } 416 417 private boolean checkEnabledEditor() { 418 return fEditor != null && SelectionConverter.canOperateOn(fEditor); 419 } 420 421 private String getDialogTitle() { 422 return DIALOG_TITLE; 423 } 424 425 private IType getSelectedType(IStructuredSelection selection) throws JavaModelException { 426 Object [] elements= selection.toArray(); 427 if (elements.length == 1 && (elements[0] instanceof IType)) { 428 IType type= (IType) elements[0]; 429 if (type.getCompilationUnit() != null && !type.isInterface() && !type.isEnum()) { 430 return type; 431 } 432 } else if (elements[0] instanceof ICompilationUnit) { 433 ICompilationUnit cu= (ICompilationUnit) elements[0]; 434 IType type= cu.findPrimaryType(); 435 if (type != null && !type.isInterface() && !type.isEnum()) 436 return type; 437 } 438 return null; 439 } 440 441 444 public void run(IStructuredSelection selection) { 445 Shell shell= getShell(); 446 try { 447 IType type= getSelectedType(selection); 448 if (type == null) { 449 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_not_applicable); 450 return; 451 } 452 if (type.isAnnotation()) { 453 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_annotation_not_applicable); 454 return; 455 } else if (type.isInterface()) { 456 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_interface_not_applicable); 457 return; 458 } else if (type.isEnum()) { 459 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_enum_not_applicable); 460 return; 461 } 462 run(shell, type, false); 463 } catch (CoreException e) { 464 ExceptionHandler.handle(e, shell, getDialogTitle(), null); 465 } 466 } 467 468 471 public void run(ITextSelection selection) { 472 if (!ActionUtil.isProcessable(fEditor)) 473 return; 474 try { 475 Shell shell= getShell(); 476 IType type= SelectionConverter.getTypeAtOffset(fEditor); 477 if (type != null) 478 run(shell, type, true); 479 else 480 MessageDialog.openInformation(shell, getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_not_applicable); 481 } catch (JavaModelException e) { 482 ExceptionHandler.handle(e, getShell(), getDialogTitle(), null); 483 } catch (CoreException e) { 484 ExceptionHandler.handle(e, getShell(), getDialogTitle(), null); 485 } 486 } 487 488 490 private void run(Shell shell, IType type, boolean activatedFromEditor) throws CoreException { 491 if (!ElementValidator.check(type, getShell(), getDialogTitle(), activatedFromEditor)) { 492 notifyResult(false); 493 return; 494 } 495 if (!ActionUtil.isEditable(fEditor, getShell(), type)) { 496 notifyResult(false); 497 return; 498 } 499 500 AddUnimplementedConstructorsContentProvider provider= new AddUnimplementedConstructorsContentProvider(type); 501 Object [] constructors= provider.getElements(null); 502 if (constructors.length == 0) { 503 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_error_nothing_found); 504 notifyResult(false); 505 return; 506 } 507 508 AddUnimplementedConstructorsDialog dialog= new AddUnimplementedConstructorsDialog(shell, new BindingLabelProvider(), provider, fEditor, type); 509 dialog.setCommentString(ActionMessages.SourceActionDialog_createConstructorComment); 510 dialog.setTitle(ActionMessages.AddUnimplementedConstructorsAction_dialog_title); 511 dialog.setInitialSelections(constructors); 512 dialog.setContainerMode(true); 513 dialog.setComparator(new JavaElementComparator()); 514 dialog.setSize(60, 18); 515 dialog.setInput(new Object ()); 516 dialog.setMessage(ActionMessages.AddUnimplementedConstructorsAction_dialog_label); 517 dialog.setValidator(new AddUnimplementedConstructorsValidator(constructors.length)); 518 519 final int dialogResult= dialog.open(); 520 if (dialogResult == Window.OK) { 521 Object [] elements= dialog.getResult(); 522 if (elements == null) { 523 notifyResult(false); 524 return; 525 } 526 527 ArrayList result= new ArrayList (); 528 for (int i= 0; i < elements.length; i++) { 529 Object elem= elements[i]; 530 if (elem instanceof IMethodBinding) { 531 result.add(elem); 532 } 533 } 534 IMethodBinding[] selected= (IMethodBinding[]) result.toArray(new IMethodBinding[result.size()]); 535 536 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()); 537 settings.createComments= dialog.getGenerateComment(); 538 IEditorPart editor= JavaUI.openInEditor(type, true, false); 539 IRewriteTarget target= editor != null ? (IRewriteTarget) editor.getAdapter(IRewriteTarget.class) : null; 540 if (target != null) 541 target.beginCompoundChange(); 542 try { 543 CompilationUnit astRoot= provider.getCompilationUnit(); 544 final ITypeBinding typeBinding= ASTNodes.getTypeBinding(astRoot, type); 545 int insertPos= dialog.getInsertOffset(); 546 547 AddUnimplementedConstructorsOperation operation= (AddUnimplementedConstructorsOperation) createRunnable(astRoot, typeBinding, selected, insertPos, dialog.getGenerateComment(), dialog.getVisibilityModifier(), dialog.isOmitSuper()); 548 IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow(); 549 if (context == null) 550 context= new BusyIndicatorRunnableContext(); 551 PlatformUI.getWorkbench().getProgressService().runInUI(context, new WorkbenchRunnableAdapter(operation, operation.getSchedulingRule()), operation.getSchedulingRule()); 552 String [] created= operation.getCreatedConstructors(); 553 if (created == null || created.length == 0) 554 MessageDialog.openInformation(shell, getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_error_nothing_found); 555 } catch (InvocationTargetException e) { 556 ExceptionHandler.handle(e, shell, getDialogTitle(), null); 557 } catch (InterruptedException e) { 558 } finally { 560 if (target != null) { 561 target.endCompoundChange(); 562 } 563 } 564 } 565 notifyResult(dialogResult == Window.OK); 566 } 567 568 584 public static IWorkspaceRunnable createRunnable(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] constructorsToOverride, int insertPos, boolean createComments, int visibility, boolean omitSuper) { 585 AddUnimplementedConstructorsOperation operation= new AddUnimplementedConstructorsOperation(astRoot, type, constructorsToOverride, insertPos, true, true, false); 586 operation.setCreateComments(createComments); 587 operation.setOmitSuper(omitSuper); 588 operation.setVisibility(visibility); 589 return operation; 590 } 591 592 594 597 public void selectionChanged(IStructuredSelection selection) { 598 try { 599 setEnabled(canEnable(selection)); 600 } catch (JavaModelException e) { 601 if (JavaModelUtil.isExceptionToBeLogged(e)) 603 JavaPlugin.log(e); 604 setEnabled(false); 605 } 606 } 607 608 610 613 public void selectionChanged(ITextSelection selection) { 614 } 615 } 616 | Popular Tags |