Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

NetBeans Platform Student Projects: Batch Refactoring

10.14.2008
| 9495 views |
  • submit to reddit

This is the start of a series on ongoing student projects that relate to the NetBeans Platform. Typically, after a NetBeans Platform Certified Training course is held, the students take on an assignment to prove their understanding of the NetBeans Platform. This series focuses on these projects, picked by the students themselves. If you have any advice to give or feedback to offer, take this opportunity to shape some of the cool ongoing projects out there, some of which might even end up enriching NetBeans IDE itself. -- Geertjan Wielenga, NetBeans Zone Leader

The first project to focus on is outlined below. It basically picks up on this issue:

http://www.netbeans.org/issues/show_bug.cgi?id=25377

And here is the full description of the project, by a student at the Julius-Maximilians-Universität, in Würzburg, Germany:

 

Any feedback or tips for this project? Any code snippets that the students on this project might find helpful? Feel free to leave them here.

AttachmentSize
batch-refactoring.png25.11 KB
Published at DZone with permission of its author, Geertjan Wielenga.
Tags:

Comments

Harris Goldstone replied on Tue, 2008/10/14 - 4:55am

Excellent idea! I miss this from IntelliJ -- source formatting can be done on a higher level, not only on individual files. I wonder how this would be done in NetBeans.

Geertjan Wielenga replied on Tue, 2008/10/14 - 5:03am

I think this project could be tackled in the following phases:

  1. Identify exactly which refactorings you want to work with. I.e., only 'Format Source' and 'Fix Imports' or others too?
  2. Identify exactly which scopes you want the refactoring to be applied to. I.e., package level only? Or also application level? If also application level, then think about module level (i.e., maybe there are differences between a Java application and a module project)? Maybe it is best to only focus on package level.
  3. Then write code that identifies the scope. For example, whenever you select a package, send a 'Hello world' message to the status bar.
  4. Then find the refactoring code in the NetBeans sources. Maybe it would be best to leave it there and to add your code to the same module in the sources. Then, instead of calling the code just for one file, call it for all the files in the current scope (i.e., in the package). So, I would suggest to not create a separate module, but to change the sources. Then create a patch and put the patch in the issue.
  5. Make sure to also include a JUnit test to prove that the code works.

Lorenz Weber replied on Tue, 2008/10/14 - 6:53pm

Copied from the forum:

 

I'm still investigating in the way how NetBeans is registering these options - and depending on my results, I will decide on your point 1: "Identify exactly which refactorings you want to work with. I.e., only 'Format Source' and 'Fix Imports' or others too?". There might be a posibility that this could be solved much more dynamically (all source options aplying to all selected source files, doing a Lookup for every file selected and combining resulting possible actions)

Here are my results so far:

- every Refactoring Action is an extended "RefactoringGlobalAction", which has to be registered per layer file and decides by a Lookup context if it can be applied. (internally registered with a lookup on org.netbeans.modules.refactoring.spi.ui.ActionsImplementationProvider)

If I want to add it to the Refactor menu (would make sense, as in contrast to the "source" menu, this is already registered with a right-click on packages etc.), I have to go this way.

- the existing "Formatting" Action is an Editor Action and not registered this way. Applied on a target Component, I will have to open an editor for every file I want to format if I don't copy and change the Code from ActionFactory$FormatAction.actionPerformed.

- the existing "Fix Imports" Action is a "Java Editor" Action (ex. BaseAction). Can be applied on a FileObject, but this would be Java-Specific.

Vita Stejskal replied on Fri, 2008/10/17 - 2:38am

Re. "Formatting" Action - The action does not do anything fancy. It simply formats a document (or its part). The same can be achieved by using Editor Indentation API and its Reformat class. The part that might not be obvious is how to work with a document that is not opened (visible) in an editor pane. The answer is quite simple - use the EditorCookie. The EditorCookie can be found in the file's Lookup (ie. DataObject.getLookup()) and it provides two useful methods - openDocument() and saveDocument().

Formatting multiple files could then be implemented by iterating through the files, finding EditorCookie for each file, calling its openDocument method to get the Document instance that can then be formatted using the Reformat class. After that the document should be saved by calling the cookie's saveDocument method.

There is the issue #125051 requesting this functionality. If you need more information or help please ask in the issue. Thanks a lot for your help!

Karl Beckers replied on Wed, 2009/04/29 - 6:43am

Thanks a lot for those pointers. I'll finally be able to use NB as a command-line code beautifier :)

hanah rich replied on Wed, 2009/10/07 - 1:31pm

I suggest recording these trainings and publish it on NetBeans TV,I hope this training and the one was in Egypt last month encourage you continue your series of Top 10 NetBeans Platform APIs, it is really good reference for us senior trips">

hanah rich replied on Wed, 2009/10/21 - 11:12am

I suggest recording these trainings and publish it on NetBeans TV,I hope this training and the one was in Egypt last month encourage you continue your series of Top 10 NetBeans Platform APIs, it is really good reference for us senior student tours

Matt Coleman replied on Thu, 2012/11/22 - 12:29am in response to: hanah rich

 this is such a great suggestion hanah..documenting via video and sharing it with us benefits us greatly buffalo freelance website designer

Gigi Han replied on Mon, 2013/07/15 - 3:39pm

These days a growing number of movies are in the public domain. These works are considered part of the public cultural heritage and may be freely used by all. Public domain refers to the body of creative works and knowledge in which no person, government or organization has any proprietary interest such as a copyright.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.