1 12 package org.eclipse.jdt.internal.ui.text.correction; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.resource.JFaceResources; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.jface.window.Window; 26 import org.eclipse.jface.wizard.IWizardPage; 27 import org.eclipse.jface.wizard.WizardDialog; 28 29 import org.eclipse.jface.text.IDocument; 30 31 import org.eclipse.jdt.core.ICompilationUnit; 32 import org.eclipse.jdt.core.IJavaElement; 33 import org.eclipse.jdt.core.IPackageFragment; 34 import org.eclipse.jdt.core.IType; 35 import org.eclipse.jdt.core.dom.AST; 36 import org.eclipse.jdt.core.dom.ASTNode; 37 import org.eclipse.jdt.core.dom.CatchClause; 38 import org.eclipse.jdt.core.dom.ITypeBinding; 39 import org.eclipse.jdt.core.dom.MethodDeclaration; 40 import org.eclipse.jdt.core.dom.Name; 41 import org.eclipse.jdt.core.dom.ParameterizedType; 42 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 43 44 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 45 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 46 import org.eclipse.jdt.internal.corext.dom.Bindings; 47 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 48 import org.eclipse.jdt.internal.corext.util.Messages; 49 50 import org.eclipse.jdt.ui.JavaElementLabels; 51 import org.eclipse.jdt.ui.text.java.IInvocationContext; 52 import org.eclipse.jdt.ui.text.java.IProblemLocation; 53 import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; 54 55 import org.eclipse.jdt.internal.ui.JavaPlugin; 56 import org.eclipse.jdt.internal.ui.JavaPluginImages; 57 import org.eclipse.jdt.internal.ui.util.PixelConverter; 58 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; 59 import org.eclipse.jdt.internal.ui.wizards.NewAnnotationCreationWizard; 60 import org.eclipse.jdt.internal.ui.wizards.NewClassCreationWizard; 61 import org.eclipse.jdt.internal.ui.wizards.NewElementWizard; 62 import org.eclipse.jdt.internal.ui.wizards.NewEnumCreationWizard; 63 import org.eclipse.jdt.internal.ui.wizards.NewInterfaceCreationWizard; 64 65 73 74 public class NewCUCompletionUsingWizardProposal extends ChangeCorrectionProposal { 75 76 public static final int K_CLASS= 1; 77 public static final int K_INTERFACE= 2; 78 public static final int K_ENUM= 3; 79 public static final int K_ANNOTATION= 4; 80 81 private Name fNode; 82 private ICompilationUnit fCompilationUnit; 83 private int fTypeKind; 84 private IJavaElement fTypeContainer; private String fTypeNameWithParameters; 86 private IType fCreatedType; 87 88 private boolean fShowDialog; 89 90 public NewCUCompletionUsingWizardProposal(ICompilationUnit cu, Name node, int typeKind, IJavaElement typeContainer, int severity) { 91 super("", null, severity, null); 93 fCompilationUnit= cu; 94 fNode= node; 95 fTypeKind= typeKind; 96 fTypeContainer= typeContainer; 97 fTypeNameWithParameters= getTypeName(typeKind, node); 98 99 fCreatedType= null; 100 101 String containerName= ASTNodes.getQualifier(node); 102 String typeName= fTypeNameWithParameters; 103 boolean isInnerType= typeContainer instanceof IType; 104 switch (typeKind) { 105 case K_CLASS: 106 setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_CLASS)); 107 if (isInnerType) { 108 if (containerName.length() == 0) { 109 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerclass_description, typeName)); 110 } else { 111 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerclass_intype_description, new String [] { typeName, containerName })); 112 } 113 } else { 114 if (containerName.length() == 0) { 115 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createclass_description, typeName)); 116 } else { 117 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createclass_inpackage_description, new String [] { typeName, containerName })); 118 } 119 } 120 break; 121 case K_INTERFACE: 122 setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_INTERFACE)); 123 if (isInnerType) { 124 if (containerName.length() == 0) { 125 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerinterface_description, typeName)); 126 } else { 127 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerinterface_intype_description, new String [] { typeName, containerName })); 128 } 129 } else { 130 if (containerName.length() == 0) { 131 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinterface_description, typeName)); 132 } else { 133 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinterface_inpackage_description, new String [] { typeName, containerName })); 134 } 135 } 136 break; 137 case K_ENUM: 138 setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ENUM)); 139 if (isInnerType) { 140 if (containerName.length() == 0) { 141 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerenum_description, typeName)); 142 } else { 143 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerenum_intype_description, new String [] { typeName, containerName })); 144 } 145 } else { 146 if (containerName.length() == 0) { 147 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createenum_description, typeName)); 148 } else { 149 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createenum_inpackage_description, new String [] { typeName, containerName })); 150 } 151 } 152 break; 153 case K_ANNOTATION: 154 setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ANNOTATION)); 155 if (isInnerType) { 156 if (containerName.length() == 0) { 157 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerannotation_description, typeName)); 158 } else { 159 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinnerannotation_intype_description, new String [] { typeName, containerName })); 160 } 161 } else { 162 if (containerName.length() == 0) { 163 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createannotation_description, typeName)); 164 } else { 165 setDisplayName(Messages.format(CorrectionMessages.NewCUCompletionUsingWizardProposal_createannotation_inpackage_description, new String [] { typeName, containerName })); 166 } 167 } 168 break; 169 default: 170 throw new IllegalArgumentException ("Unknown type kind"); } 172 fShowDialog= true; 173 } 174 175 private static String getTypeName(int typeKind, Name node) { 176 String name= ASTNodes.getSimpleNameIdentifier(node); 177 178 if (typeKind == K_CLASS || typeKind == K_INTERFACE) { 179 ASTNode parent= node.getParent(); 180 if (parent.getLocationInParent() == ParameterizedType.TYPE_PROPERTY) { 181 String typeArgBaseName= name.startsWith(String.valueOf('T')) ? String.valueOf('S') : String.valueOf('T'); 183 int nTypeArgs= ((ParameterizedType) parent.getParent()).typeArguments().size(); 184 StringBuffer buf= new StringBuffer (name); 185 buf.append('<'); 186 if (nTypeArgs == 1) { 187 buf.append(typeArgBaseName); 188 } else { 189 for (int i= 0; i < nTypeArgs; i++) { 190 if (i != 0) 191 buf.append(", "); buf.append(typeArgBaseName).append(i + 1); 193 } 194 } 195 buf.append('>'); 196 return buf.toString(); 197 } 198 } 199 return name; 200 } 201 202 203 public void apply(IDocument document) { 204 NewElementWizard wizard= createWizard(); 205 wizard.init(JavaPlugin.getDefault().getWorkbench(), new StructuredSelection(fCompilationUnit)); 206 207 IType createdType= null; 208 209 if (fShowDialog) { 210 Shell shell= JavaPlugin.getActiveWorkbenchShell(); 211 WizardDialog dialog= new WizardDialog(shell, wizard); 212 PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont()); 213 dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20)); 214 dialog.create(); 215 dialog.getShell().setText(CorrectionMessages.NewCUCompletionUsingWizardProposal_dialogtitle); 216 217 configureWizardPage(wizard); 218 if (dialog.open() == Window.OK) { 219 createdType= (IType) wizard.getCreatedElement(); 220 } 221 } else { 222 wizard.addPages(); 223 try { 224 NewTypeWizardPage page= configureWizardPage(wizard); 225 page.createType(null); 226 createdType= page.getCreatedType(); 227 } catch (CoreException e) { 228 JavaPlugin.log(e); 229 } catch (InterruptedException e) { 230 } 231 } 232 233 if (createdType != null) { 234 IJavaElement container= createdType.getParent(); 235 if (container instanceof ICompilationUnit) { 236 container= container.getParent(); 237 } 238 if (!container.equals(fTypeContainer)) { 239 try { 241 ImportRewrite rewrite= StubUtility.createImportRewrite(fCompilationUnit, true); 242 rewrite.addImport(createdType.getFullyQualifiedName('.')); 243 JavaModelUtil.applyEdit(fCompilationUnit, rewrite.rewriteImports(null), false, null); 244 } catch (CoreException e) { 245 } 246 } 247 fCreatedType= createdType; 248 } 249 250 } 251 252 private NewElementWizard createWizard() { 253 switch (fTypeKind) { 254 case K_CLASS: 255 return new NewClassCreationWizard(); 256 case K_INTERFACE: 257 return new NewInterfaceCreationWizard(); 258 case K_ENUM: 259 return new NewEnumCreationWizard(); 260 case K_ANNOTATION: 261 return new NewAnnotationCreationWizard(); 262 } 263 throw new IllegalArgumentException (); 264 } 265 266 private NewTypeWizardPage configureWizardPage(NewElementWizard wizard) { 267 IWizardPage[] pages= wizard.getPages(); 268 Assert.isTrue(pages.length > 0 && pages[0] instanceof NewTypeWizardPage); 269 270 NewTypeWizardPage page= (NewTypeWizardPage) pages[0]; 271 fillInWizardPageName(page); 272 fillInWizardPageSuperTypes(page); 273 return page; 274 } 275 276 280 private void fillInWizardPageName(NewTypeWizardPage page) { 281 page.setTypeName(fTypeNameWithParameters, fTypeNameWithParameters.indexOf('<') != -1); 283 284 boolean isInEnclosingType= fTypeContainer instanceof IType; 285 if (isInEnclosingType) { 286 page.setEnclosingType((IType) fTypeContainer, true); 287 } else { 288 page.setPackageFragment((IPackageFragment) fTypeContainer, true); 289 } 290 page.setEnclosingTypeSelection(isInEnclosingType, true); 291 } 292 293 297 private void fillInWizardPageSuperTypes(NewTypeWizardPage page) { 298 ITypeBinding type= getPossibleSuperTypeBinding(fNode); 299 type= Bindings.normalizeTypeBinding(type); 300 if (type != null) { 301 if (type.isArray()) { 302 type= type.getElementType(); 303 } 304 if (type.isTopLevel() || type.isMember()) { 305 if (type.isClass() && (fTypeKind == K_CLASS)) { 306 page.setSuperClass(type.getQualifiedName(), true); 307 } else if (type.isInterface()) { 308 List superInterfaces= new ArrayList (); 309 superInterfaces.add(type.getQualifiedName()); 310 page.setSuperInterfaces(superInterfaces, true); 311 } 312 } 313 } 314 } 315 316 private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) { 317 if (fTypeKind == K_ANNOTATION) { 318 return null; 319 } 320 321 AST ast= node.getAST(); 322 node= ASTNodes.getNormalizedNode(node); 323 ASTNode parent= node.getParent(); 324 switch (parent.getNodeType()) { 325 case ASTNode.METHOD_DECLARATION: 326 if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY) { 327 return ast.resolveWellKnownType("java.lang.Exception"); } 329 break; 330 case ASTNode.THROW_STATEMENT : 331 return ast.resolveWellKnownType("java.lang.Exception"); case ASTNode.SINGLE_VARIABLE_DECLARATION: 333 if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) { 334 return ast.resolveWellKnownType("java.lang.Exception"); } 336 break; 337 case ASTNode.VARIABLE_DECLARATION_STATEMENT: 338 case ASTNode.FIELD_DECLARATION: 339 return null; case ASTNode.PARAMETERIZED_TYPE: 341 return null; } 343 return ASTResolving.guessBindingForTypeReference(node); 344 } 345 346 347 348 351 public String getAdditionalProposalInfo() { 352 StringBuffer buf= new StringBuffer (); 353 switch (fTypeKind) { 354 case K_CLASS: 355 buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createclass_info); 356 break; 357 case K_INTERFACE: 358 buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinterface_info); 359 break; 360 case K_ENUM: 361 buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createenum_info); 362 break; 363 case K_ANNOTATION: 364 buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createannotation_info); 365 break; 366 } 367 buf.append("<br>"); buf.append("<br>"); if (fTypeContainer instanceof IType) { 370 buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_enclosingtype); 371 } else { 372 buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_package); 373 } 374 buf.append(" <b>"); buf.append(JavaElementLabels.getElementLabel(fTypeContainer, JavaElementLabels.T_FULLY_QUALIFIED)); 376 buf.append("</b><br>"); buf.append("public "); 379 380 switch (fTypeKind) { 381 case K_CLASS: 382 buf.append("class <b>"); break; 384 case K_INTERFACE: 385 buf.append("interface <b>"); break; 387 case K_ENUM: 388 buf.append("enum <b>"); break; 390 case K_ANNOTATION: 391 buf.append("@interface <b>"); break; 393 } 394 nameToHTML(fTypeNameWithParameters, buf); 395 396 ITypeBinding superclass= getPossibleSuperTypeBinding(fNode); 397 if (superclass != null) { 398 if (superclass.isClass()) { 399 if (fTypeKind == K_CLASS) { 400 buf.append("</b> extends <b>"); nameToHTML(BindingLabelProvider.getBindingLabel(superclass, BindingLabelProvider.DEFAULT_TEXTFLAGS), buf); 402 } 403 } else { 404 if (fTypeKind == K_INTERFACE) { 405 buf.append("</b> extends <b>"); } else { 407 buf.append("</b> implements <b>"); } 409 nameToHTML(BindingLabelProvider.getBindingLabel(superclass, BindingLabelProvider.DEFAULT_TEXTFLAGS), buf); 410 } 411 } 412 buf.append("</b> {<br>}<br>"); return buf.toString(); 414 } 415 416 private void nameToHTML(String name, StringBuffer buf) { 417 for (int i= 0; i < name.length(); i++) { 418 char ch= name.charAt(i); 419 if (ch == '>') { 420 buf.append(">"); } else if (ch == '<') { 422 buf.append("<"); } else { 424 buf.append(ch); 425 } 426 } 427 } 428 429 433 public boolean isShowDialog() { 434 return fShowDialog; 435 } 436 437 441 public void setShowDialog(boolean showDialog) { 442 fShowDialog= showDialog; 443 } 444 445 public IType getCreatedType() { 446 return fCreatedType; 447 } 448 449 450 public int getTypeKind() { 451 return fTypeKind; 452 } 453 454 } 455 | Popular Tags |