Bug 347338

Summary: [quick fix] One-step creation of nested Java listeners
Product: [Eclipse Project] JDT Reporter: Andy Thomas-Cramer <thomascramera>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: ASSIGNED --- QA Contact:
Severity: enhancement    
Priority: P3 CC: amj87.iitr, deepakazad, markus.kell.r, Olivier_Thomann, remy.suen, thomascramera
Version: 3.7   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Andy Thomas-Cramer CLA 2011-05-26 11:56:13 EDT
Build Identifier: 

The observer pattern is common in Java. For example:

    this.addMouseListener( new MouseListener() { ... } );

Frequently the inner class is sufficiently long to prefer a nested class to anonymous. 

Consequently, I find myself repeatedly executing this series of steps:

1. Type this.addXListener( new X() ), where X is an interface or abstract class.
2. Use the Quick Fix to "Import X".
3. Use the Quick Fix to "Add unimplemented methods".
4. Use the refactoring "Convert Anonymous Class to Nested."

I appreciate the assistance provided by the JDT for the last three steps. Still, the repetition grows increasingly annoying over time. 

One or two steps would be preferable to three. For example, the Quick Fix could provide one or both of these additional options:

* "Import X, add methods"
* "Import X, add methods in nested class"


Reproducible: Always

Steps to Reproduce:
1. Add a listener implemented by a new nested class.
2.
3.
Comment 1 Ayushman Jain CLA 2011-05-26 13:31:16 EDT
You can actually do this in one step using content assist (CTRL-SPACE). If you use CTRL-SPACE after having typed X, the first proposal you get is "X - anonymous inner type". If you use that proposal, not only will content assist add the import for you, but also add the overriden method stub.

HTH.
Comment 2 Andy Thomas-Cramer CLA 2011-05-26 13:41:31 EDT
Thanks! That's very helpful; I was hoping someone would say "you can already do that." 

That gets it down to two steps. 

These enhancements still seem valuable:
* Add an option "X() Nested Inner Type", to make it a single step.
* Make both actions also available through QuickFix, for discoverability, for those who don't discover the content-assist approach.
Comment 3 Olivier Thomann CLA 2011-06-15 12:18:01 EDT
Moving to JDT/UI
Comment 4 Markus Keller CLA 2011-06-16 08:44:23 EDT
See bug 97235 for a quick fix to turn something like "new MouseListener()" into an anonymous class.

Keeping this bug for a quick fix to create a nested class instead of an anonymous. I don't think we should add this to content assist, since
- it's not a basic operation,
- it implies non-local changes (far away from the cursor position), and
- it needs 2 inputs: the name of the nested class and the type to extend (e.g. MouseListener or MouseAdapter)